From e6ae264816faddde18d67acdf910c84898518f3d Mon Sep 17 00:00:00 2001 From: Samip Kothari Date: Thu, 23 Jul 2020 10:31:34 -0400 Subject: [PATCH] fix map2table (#168) I think this should work though bahla never got around to it --- .../restructuring/map2table/operation.go | 5 ++- .../restructuring/map2table/operation_test.go | 38 +++++++++++++------ 2 files changed, 30 insertions(+), 13 deletions(-) diff --git a/operations/restructuring/map2table/operation.go b/operations/restructuring/map2table/operation.go index 95b131b..40f4903 100644 --- a/operations/restructuring/map2table/operation.go +++ b/operations/restructuring/map2table/operation.go @@ -31,8 +31,9 @@ func (a *Operation) Eval(inputs map[string]interface{}) (interface{}, error) { a.logger.Debug("The inputs of Operation Map to Table.", inputs) for _, ord := range in.ColOrder { if val, ok := in.Map[ord.(string)]; ok { - ValLen = len(val.([]interface{})) - givenType, _ = data.GetType(val.([]interface{})[0]) + valArr, _ := coerce.ToArray(val) + ValLen = len(valArr) + givenType, _ = data.GetType(valArr[0]) break } } diff --git a/operations/restructuring/map2table/operation_test.go b/operations/restructuring/map2table/operation_test.go index 8b91268..6b204cc 100644 --- a/operations/restructuring/map2table/operation_test.go +++ b/operations/restructuring/map2table/operation_test.go @@ -1,17 +1,20 @@ package map2table + import ( - "github.com/project-flogo/core/support/log" "testing" + "github.com/project-flogo/core/support/log" + "github.com/stretchr/testify/assert" ) + func TestSample0(t *testing.T) { //params := Params{} - opt := &Operation{logger : log.RootLogger(), params :&Params{Axis: 0}} + opt := &Operation{logger: log.RootLogger(), params: &Params{Axis: 0}} var err error inputs := make(map[string]interface{}) - inputs["colOrder"] = []string{"a","b","c","d"} - inputs["map"] = map[string]interface{}{"a":[]interface{}{1,2,4}, "b":[]interface{}{3,5,6}, "c":[]interface{}{7,8,9}} + inputs["colOrder"] = []string{"a", "b", "c", "d"} + inputs["map"] = map[string]interface{}{"a": []interface{}{1, 2, 4}, "b": []interface{}{3, 5, 6}, "c": []interface{}{7, 8, 9}} _, err = opt.Eval(inputs) @@ -20,11 +23,11 @@ func TestSample0(t *testing.T) { } func TestSample1(t *testing.T) { //params := Params{} - opt := &Operation{logger : log.RootLogger(), params :&Params{Axis: 1}} + opt := &Operation{logger: log.RootLogger(), params: &Params{Axis: 1}} var err error inputs := make(map[string]interface{}) - inputs["colOrder"] = []string{"x","a","b","c","d"} - inputs["map"] = map[string]interface{}{"a":[]interface{}{1,2,4}, "b":[]interface{}{3,5,6}, "c":[]interface{}{7,8,9}} + inputs["colOrder"] = []string{"x", "a", "b", "c", "d"} + inputs["map"] = map[string]interface{}{"a": []interface{}{1, 2, 4}, "b": []interface{}{3, 5, 6}, "c": []interface{}{7, 8, 9}} _, err = opt.Eval(inputs) @@ -34,14 +37,27 @@ func TestSample1(t *testing.T) { func TestSampleString(t *testing.T) { //params := Params{} - opt := &Operation{logger : log.RootLogger(), params :&Params{Axis: 1}} + opt := &Operation{logger: log.RootLogger(), params: &Params{Axis: 1}} var err error inputs := make(map[string]interface{}) - inputs["colOrder"] = []string{"x","a","b","c","d"} - inputs["map"] = map[string]interface{}{"a":[]interface{}{"a","b","c"}, "b":[]interface{}{"d","e","f"}, "c":[]interface{}{"g","h","k"}} + inputs["colOrder"] = []string{"x", "a", "b", "c", "d"} + inputs["map"] = map[string]interface{}{"a": []interface{}{"a", "b", "c"}, "b": []interface{}{"d", "e", "f"}, "c": []interface{}{"g", "h", "k"}} _, err = opt.Eval(inputs) assert.Nil(t, err) -} \ No newline at end of file +} +func TestSingleInt(t *testing.T) { + //params := Params{} + opt := &Operation{logger: log.RootLogger(), params: &Params{Axis: 1}} + var err error + inputs := make(map[string]interface{}) + inputs["colOrder"] = []string{"x", "a", "b", "c", "d"} + inputs["map"] = map[string]interface{}{"a": 1} + + _, err = opt.Eval(inputs) + + assert.Nil(t, err) + +}