Skip to content

Commit

Permalink
fix(state): Lint and MarshalJSON for Type field in the attribute
Browse files Browse the repository at this point in the history
Signed-off-by: Luigi Rende <[email protected]>
  • Loading branch information
luigirende committed Sep 11, 2024
1 parent 335f858 commit 12a7455
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
2 changes: 1 addition & 1 deletion state/redis/redis_query_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
28 changes: 26 additions & 2 deletions state/utils/query_selected_attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package utils
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"strconv"
"strings"
Expand All @@ -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
}

Expand Down
8 changes: 4 additions & 4 deletions tests/conformance/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"}`),
},
},
},
Expand Down Expand Up @@ -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}}`),
},
},
},
Expand Down

0 comments on commit 12a7455

Please sign in to comment.