From 05acc9d1623085a4ab554fd457ff524ff8b510ad Mon Sep 17 00:00:00 2001 From: Andy Hampshire Date: Tue, 3 Jul 2018 12:13:42 +0100 Subject: [PATCH 1/4] Add simple string conversion function --- .../function/string/tostring/tostring.go | 30 +++++++++++++++++ .../function/string/tostring/tostring_test.go | 32 +++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 core/mapper/exprmapper/function/string/tostring/tostring.go create mode 100644 core/mapper/exprmapper/function/string/tostring/tostring_test.go 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..9f4786c --- /dev/null +++ b/core/mapper/exprmapper/function/string/tostring/tostring.go @@ -0,0 +1,30 @@ +package tostring + +import ( + "fmt" + "github.com/TIBCOSoftware/flogo-lib/core/mapper/exprmapper/expression/function" + "github.com/TIBCOSoftware/flogo-lib/logger" +) + +var log = logger.GetLogger("equals-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" `, result) + return result +} \ No newline at end of file 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) +} From 9764ec0102b6e715138a2b2c0155ab1bb9832a54 Mon Sep 17 00:00:00 2001 From: Andy Hampshire Date: Tue, 3 Jul 2018 12:18:57 +0100 Subject: [PATCH 2/4] Add simple string conversion function --- core/mapper/exprmapper/function/string/tostring/tostring.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/core/mapper/exprmapper/function/string/tostring/tostring.go b/core/mapper/exprmapper/function/string/tostring/tostring.go index 9f4786c..faaa7ce 100644 --- a/core/mapper/exprmapper/function/string/tostring/tostring.go +++ b/core/mapper/exprmapper/function/string/tostring/tostring.go @@ -2,6 +2,7 @@ package tostring import ( "fmt" + "github.com/TIBCOSoftware/flogo-lib/core/mapper/exprmapper/expression/function" "github.com/TIBCOSoftware/flogo-lib/logger" ) @@ -25,6 +26,6 @@ func (s *ToString) GetCategory() string { func (s *ToString) Eval(inp interface{}) string { result := fmt.Sprintf("%v", inp) - log.Debugf(`Converts value to a simple string" `, result) + log.Debugf(`Converts value to a simple string: "%s" `, result) return result -} \ No newline at end of file +} From 3f2c8e3d5be9768af29a58ca0d4d1cd90f640b16 Mon Sep 17 00:00:00 2001 From: Andy Hampshire Date: Tue, 3 Jul 2018 12:21:06 +0100 Subject: [PATCH 3/4] Add simple string conversion function --- core/mapper/exprmapper/function/string/tostring/tostring.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/mapper/exprmapper/function/string/tostring/tostring.go b/core/mapper/exprmapper/function/string/tostring/tostring.go index faaa7ce..93f2a7d 100644 --- a/core/mapper/exprmapper/function/string/tostring/tostring.go +++ b/core/mapper/exprmapper/function/string/tostring/tostring.go @@ -7,7 +7,7 @@ import ( "github.com/TIBCOSoftware/flogo-lib/logger" ) -var log = logger.GetLogger("equals-function") +var log = logger.GetLogger("tostring-function") type ToString struct { } From c3b0f3c058b8ab8a8d3a1daf9ca6edd57ebdba18 Mon Sep 17 00:00:00 2001 From: Andy Hampshire Date: Tue, 3 Jul 2018 13:51:32 +0100 Subject: [PATCH 4/4] Add simple regex based match function to string functions --- .../exprmapper/function/string/match/match.go | 31 +++++++++++++++++++ .../function/string/match/match_test.go | 28 +++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 core/mapper/exprmapper/function/string/match/match.go create mode 100644 core/mapper/exprmapper/function/string/match/match_test.go 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) +}