Skip to content

Commit

Permalink
Fix lint
Browse files Browse the repository at this point in the history
  • Loading branch information
islamaliev committed Dec 16, 2024
1 parent 4eae9c5 commit 7a49015
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 21 deletions.
53 changes: 34 additions & 19 deletions client/json_traverse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,21 @@ type traverseNode struct {
path string
}

// Helper functions to safely get values
func getObjectValue(j JSON) map[string]JSON {
if val, ok := j.Value().(map[string]JSON); ok {
return val
}
panic("expected object value")
}

func getArrayValue(j JSON) []JSON {
if val, ok := j.Value().([]JSON); ok {
return val
}
panic("expected array value")
}

func TestTraverseJSON_ShouldVisitAccordingToConfig(t *testing.T) {
// Create a complex JSON structure for testing
json := newJSONObject(map[string]JSON{
Expand Down Expand Up @@ -63,11 +78,11 @@ func TestTraverseJSON_ShouldVisitAccordingToConfig(t *testing.T) {
{path: "number", value: newJSONNumber(42, nil)},
{path: "bool", value: newJSONBool(true, nil)},
{path: "null", value: newJSONNull(nil)},
{path: "object", value: json.Value().(map[string]JSON)["object"]},
{path: "object", value: getObjectValue(json)["object"]},
{path: "object/nested", value: newJSONString("inside", nil)},
{path: "object/deep", value: json.Value().(map[string]JSON)["object"].Value().(map[string]JSON)["deep"]},
{path: "object/deep", value: getObjectValue(getObjectValue(json)["object"])["deep"]},
{path: "object/deep/level", value: newJSONNumber(3, nil)},
{path: "array", value: json.Value().(map[string]JSON)["array"]},
{path: "array", value: getObjectValue(json)["array"]},
},
},
{
Expand All @@ -90,9 +105,9 @@ func TestTraverseJSON_ShouldVisitAccordingToConfig(t *testing.T) {
TraverseJSONWithPrefix([]string{"object"}),
},
expected: []traverseNode{
{path: "object", value: json.Value().(map[string]JSON)["object"]},
{path: "object", value: getObjectValue(json)["object"]},
{path: "object/nested", value: newJSONString("inside", nil)},
{path: "object/deep", value: json.Value().(map[string]JSON)["object"].Value().(map[string]JSON)["deep"]},
{path: "object/deep", value: getObjectValue(getObjectValue(json)["object"])["deep"]},
{path: "object/deep/level", value: newJSONNumber(3, nil)},
},
},
Expand All @@ -102,7 +117,7 @@ func TestTraverseJSON_ShouldVisitAccordingToConfig(t *testing.T) {
TraverseJSONWithPrefix([]string{"object", "deep"}),
},
expected: []traverseNode{
{path: "object/deep", value: json.Value().(map[string]JSON)["object"].Value().(map[string]JSON)["deep"]},
{path: "object/deep", value: getObjectValue(getObjectValue(json)["object"])["deep"]},
{path: "object/deep/level", value: newJSONNumber(3, nil)},
},
},
Expand All @@ -117,16 +132,16 @@ func TestTraverseJSON_ShouldVisitAccordingToConfig(t *testing.T) {
{path: "number", value: newJSONNumber(42, nil)},
{path: "bool", value: newJSONBool(true, nil)},
{path: "null", value: newJSONNull(nil)},
{path: "object", value: json.Value().(map[string]JSON)["object"]},
{path: "object", value: getObjectValue(json)["object"]},
{path: "object/nested", value: newJSONString("inside", nil)},
{path: "object/deep", value: json.Value().(map[string]JSON)["object"].Value().(map[string]JSON)["deep"]},
{path: "object/deep", value: getObjectValue(getObjectValue(json)["object"])["deep"]},
{path: "object/deep/level", value: newJSONNumber(3, nil)},
{path: "array", value: json.Value().(map[string]JSON)["array"]},
{path: "array", value: getObjectValue(json)["array"]},
{path: "array", value: newJSONNumber(1, nil)},
{path: "array", value: newJSONString("two", nil)},
{path: "array", value: json.Value().(map[string]JSON)["array"].Value().([]JSON)[2]},
{path: "array", value: getArrayValue(getObjectValue(json)["array"])[2]},
{path: "array/key", value: newJSONString("value", nil)},
{path: "array", value: json.Value().(map[string]JSON)["array"].Value().([]JSON)[3]},
{path: "array", value: getArrayValue(getObjectValue(json)["array"])[3]},
{path: "array", value: newJSONNumber(4, nil)},
{path: "array", value: newJSONNumber(5, nil)},
},
Expand All @@ -142,11 +157,11 @@ func TestTraverseJSON_ShouldVisitAccordingToConfig(t *testing.T) {
{path: "number", value: newJSONNumber(42, nil)},
{path: "bool", value: newJSONBool(true, nil)},
{path: "null", value: newJSONNull(nil)},
{path: "object", value: json.Value().(map[string]JSON)["object"]},
{path: "object", value: getObjectValue(json)["object"]},
{path: "object/nested", value: newJSONString("inside", nil)},
{path: "object/deep", value: json.Value().(map[string]JSON)["object"].Value().(map[string]JSON)["deep"]},
{path: "object/deep", value: getObjectValue(getObjectValue(json)["object"])["deep"]},
{path: "object/deep/level", value: newJSONNumber(3, nil)},
{path: "array", value: json.Value().(map[string]JSON)["array"]},
{path: "array", value: getObjectValue(json)["array"]},
{path: "array", value: newJSONNumber(1, nil)},
{path: "array", value: newJSONString("two", nil)},
},
Expand All @@ -163,16 +178,16 @@ func TestTraverseJSON_ShouldVisitAccordingToConfig(t *testing.T) {
{path: "number", value: newJSONNumber(42, nil)},
{path: "bool", value: newJSONBool(true, nil)},
{path: "null", value: newJSONNull(nil)},
{path: "object", value: json.Value().(map[string]JSON)["object"]},
{path: "object", value: getObjectValue(json)["object"]},
{path: "object/nested", value: newJSONString("inside", nil)},
{path: "object/deep", value: json.Value().(map[string]JSON)["object"].Value().(map[string]JSON)["deep"]},
{path: "object/deep", value: getObjectValue(getObjectValue(json)["object"])["deep"]},
{path: "object/deep/level", value: newJSONNumber(3, nil)},
{path: "array", value: json.Value().(map[string]JSON)["array"]},
{path: "array", value: getObjectValue(json)["array"]},
{path: "array/0", value: newJSONNumber(1, nil)},
{path: "array/1", value: newJSONString("two", nil)},
{path: "array/2", value: json.Value().(map[string]JSON)["array"].Value().([]JSON)[2]},
{path: "array/2", value: getArrayValue(getObjectValue(json)["array"])[2]},
{path: "array/2/key", value: newJSONString("value", nil)},
{path: "array/3", value: json.Value().(map[string]JSON)["array"].Value().([]JSON)[3]},
{path: "array/3", value: getArrayValue(getObjectValue(json)["array"])[3]},
{path: "array/3/0", value: newJSONNumber(4, nil)},
{path: "array/3/1", value: newJSONNumber(5, nil)},
},
Expand Down
1 change: 0 additions & 1 deletion internal/db/fetcher/indexer_iterators.go
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,6 @@ func (f *IndexFetcher) determineFieldFilterConditions() ([]fieldFilterCond, erro

jsonPath := []string{}
if fieldDef.Kind == client.FieldKind_NILLABLE_JSON {

jsonPathLoop:
for {
for key, filterVal := range condMap {
Expand Down
3 changes: 2 additions & 1 deletion internal/encoding/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ import (
"strings"
"testing"

"github.com/sourcenetwork/defradb/client"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/sourcenetwork/defradb/client"
)

func TestJSONEncodingAndDecoding_ShouldEncodeAndDecodeBack(t *testing.T) {
Expand Down

0 comments on commit 7a49015

Please sign in to comment.