diff --git a/state/redis/redis_query_schema.go b/state/redis/redis_query_schema.go index aec60f054b..fce97b2d58 100644 --- a/state/redis/redis_query_schema.go +++ b/state/redis/redis_query_schema.go @@ -63,7 +63,7 @@ func parseQuerySchemas(content string) (querySchemas, error) { } alias := fmt.Sprintf("var%d", id) elem.keys[indx.Key] = alias - elem.schema = append(elem.schema, fmt.Sprintf("$.data.%s", indx.Key), "AS", alias, indx.Type, "SORTABLE") + elem.schema = append(elem.schema, "$.data."+indx.Key, "AS", alias, indx.Type, "SORTABLE") } ret[schema.Name] = elem } diff --git a/state/utils/query_selected_attributes.go b/state/utils/query_selected_attributes.go index c40711299f..868a26265c 100644 --- a/state/utils/query_selected_attributes.go +++ b/state/utils/query_selected_attributes.go @@ -3,6 +3,7 @@ package utils import ( "bytes" "encoding/json" + "errors" "fmt" "strconv" "strings" @@ -26,20 +27,43 @@ var stringToType = map[string]Type{ `"Array"`: Array, } -func ParseType(typeString string) (Type, error) { +var typeToString = map[Type]string{ + Text: `"Text"`, + Numeric: `"Numeric"`, + Bool: `"Bool"`, + Object: `"Object"`, + Array: `"Array"`, +} + +func parseType(typeString string) (Type, error) { if s, ok := stringToType[typeString]; ok { return s, nil } return Text, fmt.Errorf("invalid type, default text: %s", typeString) } +func evalType(typeValue Type) (string, error) { + if s, ok := typeToString[typeValue]; ok { + return s, nil + } + return "nil", errors.New("invalid type, default text: nil") +} + +func (t Type) MarshalJSON() ([]byte, error) { + if s, error := evalType(t); error != nil { + return nil, error + } else { + return []byte(s), nil + } +} + func (t *Type) UnmarshalJSON(p []byte) error { elem := string(p) if elem == `null` || elem == `""` { return nil } var err error - *t, err = ParseType(strings.Trim(elem, strconv.Itoa(int('"')))) + *t, err = parseType(strings.Trim(elem, strconv.Itoa(int('"')))) return err } diff --git a/tests/conformance/state/state.go b/tests/conformance/state/state.go index 27cf38fd26..4e46232878 100644 --- a/tests/conformance/state/state.go +++ b/tests/conformance/state/state.go @@ -437,8 +437,8 @@ func ConformanceTests(t *testing.T, props map[string]string, statestore state.St }, results: []state.QueryItem{ { - Key: fmt.Sprintf("%s-struct-operations-inactive", key), - Data: []byte(fmt.Sprintf(`{"value":12, "Status":"INACTIVE"}`)), + Key: key + "-struct-operations-inactive", + Data: []byte(`{"value":12, "Status":"INACTIVE"}`), }, }, }, @@ -483,8 +483,8 @@ func ConformanceTests(t *testing.T, props map[string]string, statestore state.St }, results: []state.QueryItem{ { - Key: fmt.Sprintf("%s-struct-operations-inactive", key), - Data: []byte(fmt.Sprintf(`{ "productItem": {"value":12}}`)), + Key: key + "-struct-operations-inactive", + Data: []byte(`{ "productItem": {"value":12}}`), }, }, },