Sfoglia il codice sorgente

opt(converter): optimize custom converter mock

Signed-off-by: Jiyong Huang <huangjy@emqx.io>
Jiyong Huang 2 anni fa
parent
commit
6e01bf2e16

+ 10 - 3
internal/converter/custom/converter_test.go

@@ -16,6 +16,7 @@ package custom
 
 
 import (
 import (
 	"fmt"
 	"fmt"
+	"github.com/gdexlab/go-render/render"
 	"github.com/lf-edge/ekuiper/internal/conf"
 	"github.com/lf-edge/ekuiper/internal/conf"
 	"github.com/lf-edge/ekuiper/internal/schema"
 	"github.com/lf-edge/ekuiper/internal/schema"
 	"github.com/lf-edge/ekuiper/internal/testx"
 	"github.com/lf-edge/ekuiper/internal/testx"
@@ -71,7 +72,7 @@ func testEncode(t *testing.T) {
 				"id":   12,
 				"id":   12,
 				"name": "test",
 				"name": "test",
 			},
 			},
-			r: []byte(`{"id":12,"name":"test"}`),
+			r: []byte(`{"id":12,"name":"test","age":0,"hobbies":{"indoor":null,"outdoor":null}}`),
 		}, {
 		}, {
 			m: map[string]interface{}{
 			m: map[string]interface{}{
 				"id":   7,
 				"id":   7,
@@ -86,7 +87,7 @@ func testEncode(t *testing.T) {
 					},
 					},
 				},
 				},
 			},
 			},
-			r: []byte(`{"age":22,"hobbies":{"indoor":["Chess"],"outdoor":["Basketball"]},"id":7,"name":"John Doe"}`),
+			r: []byte(`{"id":7,"name":"John Doe","age":22,"hobbies":{"indoor":["Chess"],"outdoor":["Basketball"]}}`),
 		},
 		},
 	}
 	}
 	fmt.Printf("The test bucket size is %d.\n\n", len(tests))
 	fmt.Printf("The test bucket size is %d.\n\n", len(tests))
@@ -113,6 +114,12 @@ func testDecode(t *testing.T) {
 		{
 		{
 			m: map[string]interface{}{
 			m: map[string]interface{}{
 				"name": "test",
 				"name": "test",
+				"age":  int64(0),
+				"id":   int64(0),
+				"hobbies": map[string]interface{}{
+					"indoor":  []string(nil),
+					"outdoor": []string(nil),
+				},
 			},
 			},
 			r: []byte(`{"name":"test"}`),
 			r: []byte(`{"name":"test"}`),
 		},
 		},
@@ -123,7 +130,7 @@ func testDecode(t *testing.T) {
 		if !reflect.DeepEqual(tt.e, testx.Errstring(err)) {
 		if !reflect.DeepEqual(tt.e, testx.Errstring(err)) {
 			t.Errorf("%d.error mismatch:\n  exp=%s\n  got=%s\n\n", i, tt.e, err)
 			t.Errorf("%d.error mismatch:\n  exp=%s\n  got=%s\n\n", i, tt.e, err)
 		} else if tt.e == "" && !reflect.DeepEqual(tt.m, a) {
 		} else if tt.e == "" && !reflect.DeepEqual(tt.m, a) {
-			t.Errorf("%d. \n\nresult mismatch:\n\nexp=%v\n\ngot=%v\n\n", i, tt.m, a)
+			t.Errorf("%d. \n\nresult mismatch:\n\nexp=%v\n\ngot=%v\n\n", i, render.AsCode(tt.m), render.AsCode(a))
 		}
 		}
 	}
 	}
 }
 }

+ 36 - 3
internal/converter/custom/test/myformat.go

@@ -17,6 +17,7 @@ package main
 import (
 import (
 	"encoding/json"
 	"encoding/json"
 	"fmt"
 	"fmt"
+	"github.com/mitchellh/mapstructure"
 )
 )
 
 
 type Hobbies struct {
 type Hobbies struct {
@@ -66,16 +67,48 @@ func (x *Sample) GetSchemaJson() string {
 func (x *Sample) Encode(d interface{}) ([]byte, error) {
 func (x *Sample) Encode(d interface{}) ([]byte, error) {
 	switch r := d.(type) {
 	switch r := d.(type) {
 	case map[string]interface{}:
 	case map[string]interface{}:
-		return json.Marshal(r)
+		result := &Sample{}
+		err := MapToStructStrict(r, result)
+		if err != nil {
+			return nil, err
+		}
+		return json.Marshal(result)
 	default:
 	default:
 		return nil, fmt.Errorf("unsupported type %v, must be a map", d)
 		return nil, fmt.Errorf("unsupported type %v, must be a map", d)
 	}
 	}
 }
 }
 
 
 func (x *Sample) Decode(b []byte) (interface{}, error) {
 func (x *Sample) Decode(b []byte) (interface{}, error) {
-	result := make(map[string]interface{})
+	result := &Sample{}
+	// check error
 	err := json.Unmarshal(b, &result)
 	err := json.Unmarshal(b, &result)
-	return result, err
+	if err != nil {
+		return nil, err
+	}
+	// convert struct to map
+	hobbyMap := make(map[string]interface{}, 2)
+	hobbyMap["indoor"] = result.Hobbies.Indoor
+	hobbyMap["outdoor"] = result.Hobbies.Outdoor
+	resultMap := make(map[string]interface{}, 4)
+	resultMap["id"] = result.Id
+	resultMap["name"] = result.Name
+	resultMap["age"] = result.Age
+	resultMap["hobbies"] = hobbyMap
+	return resultMap, err
+}
+
+func MapToStructStrict(input, output interface{}) error {
+	config := &mapstructure.DecoderConfig{
+		ErrorUnused: true,
+		TagName:     "json",
+		Result:      output,
+	}
+	decoder, err := mapstructure.NewDecoder(config)
+	if err != nil {
+		return err
+	}
+
+	return decoder.Decode(input)
 }
 }
 
 
 func GetSample() interface{} {
 func GetSample() interface{} {