diff --git a/core/mapper/exprmapper/function/string/match/match.go b/core/mapper/exprmapper/function/string/match/match.go new file mode 100644 index 0000000..d91ec61 --- /dev/null +++ b/core/mapper/exprmapper/function/string/match/match.go @@ -0,0 +1,31 @@ +package match + +import ( + "regexp" + + "github.com/TIBCOSoftware/flogo-lib/core/mapper/exprmapper/expression/function" + "github.com/TIBCOSoftware/flogo-lib/logger" +) + +var log = logger.GetLogger("match-function") + +type Match struct { +} + +func init() { + function.Registry(&Match{}) +} + +func (s *Match) GetName() string { + return "match" +} + +func (s *Match) GetCategory() string { + return "string" +} +func (s *Match) Eval(expr string, inp string) bool { + + match, _ := regexp.MatchString(expr, inp) + log.Infof(`Input "%v" matches: "%v" `, inp, match) + return match +} diff --git a/core/mapper/exprmapper/function/string/match/match_test.go b/core/mapper/exprmapper/function/string/match/match_test.go new file mode 100644 index 0000000..40ebc66 --- /dev/null +++ b/core/mapper/exprmapper/function/string/match/match_test.go @@ -0,0 +1,28 @@ +package match + +import ( + "fmt" + "testing" + + "github.com/TIBCOSoftware/flogo-lib/core/mapper/exprmapper/expression" + "github.com/stretchr/testify/assert" +) + +var s = &Match{} + +func TestStaticFunc_Starts_with(t *testing.T) { + final1 := s.Eval("p([a-z]+)ch", "peach") + fmt.Println(final1) + assert.Equal(t, true, final1) + +} + +func TestExpression(t *testing.T) { + fun, err := expression.ParseExpression(`string.match("p([a-z]+)ch", "peach")`) + assert.Nil(t, err) + assert.NotNil(t, fun) + v, err := fun.Eval() + assert.Nil(t, err) + assert.NotNil(t, v) + assert.Equal(t, true, v) +} diff --git a/core/mapper/exprmapper/function/string/tostring/tostring.go b/core/mapper/exprmapper/function/string/tostring/tostring.go new file mode 100644 index 0000000..93f2a7d --- /dev/null +++ b/core/mapper/exprmapper/function/string/tostring/tostring.go @@ -0,0 +1,31 @@ +package tostring + +import ( + "fmt" + + "github.com/TIBCOSoftware/flogo-lib/core/mapper/exprmapper/expression/function" + "github.com/TIBCOSoftware/flogo-lib/logger" +) + +var log = logger.GetLogger("tostring-function") + +type ToString struct { +} + +func init() { + function.Registry(&ToString{}) +} + +func (s *ToString) GetName() string { + return "toString" +} + +func (s *ToString) GetCategory() string { + return "string" +} +func (s *ToString) Eval(inp interface{}) string { + + result := fmt.Sprintf("%v", inp) + log.Debugf(`Converts value to a simple string: "%s" `, result) + return result +} diff --git a/core/mapper/exprmapper/function/string/tostring/tostring_test.go b/core/mapper/exprmapper/function/string/tostring/tostring_test.go new file mode 100644 index 0000000..0ea83ca --- /dev/null +++ b/core/mapper/exprmapper/function/string/tostring/tostring_test.go @@ -0,0 +1,32 @@ +package tostring + +import ( + "fmt" + "testing" + + "github.com/TIBCOSoftware/flogo-lib/core/mapper/exprmapper/expression" + "github.com/stretchr/testify/assert" +) + +var s = &ToString{} + +func TestStaticFunc_Starts_with(t *testing.T) { + final1 := s.Eval(1) + fmt.Println(final1) + assert.Equal(t, "1", final1) + + final2 := s.Eval(1.23) + fmt.Println(final2) + assert.Equal(t, "1.23", final2) + +} + +func TestExpression(t *testing.T) { + fun, err := expression.ParseExpression(`string.tostring(3.456)`) + assert.Nil(t, err) + assert.NotNil(t, fun) + v, err := fun.Eval() + assert.Nil(t, err) + assert.NotNil(t, v) + assert.Equal(t, "3.456", v) +}