diff --git a/go.mod b/go.mod index ce0ad24..495f879 100644 --- a/go.mod +++ b/go.mod @@ -1,8 +1,17 @@ module github.com/go-openapi/jsonpointer +go 1.18 + require ( - github.com/go-openapi/swag v0.22.3 - github.com/stretchr/testify v1.8.1 + github.com/go-openapi/swag v0.22.4 + github.com/stretchr/testify v1.8.4 ) -go 1.13 +require ( + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/josharian/intern v1.0.0 // indirect + github.com/kr/pretty v0.3.1 // indirect + github.com/mailru/easyjson v0.7.7 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/go.sum b/go.sum index 10ca578..5666509 100644 --- a/go.sum +++ b/go.sum @@ -1,31 +1,24 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= -github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/go-openapi/swag v0.22.3 h1:yMBqmnQ0gyZvEb/+KzuWZOXgllrXT4SADYbvDaXHv/g= -github.com/go-openapi/swag v0.22.3/go.mod h1:UzaqsxGiab7freDnrUUra0MwWfN/q7tE4j+VcZ0yl14= +github.com/go-openapi/swag v0.22.4 h1:QLMzNJnMGPRNDCbySlcj1x01tzU8/9LTTL9hZZZogBU= +github.com/go-openapi/swag v0.22.4/go.mod h1:UzaqsxGiab7freDnrUUra0MwWfN/q7tE4j+VcZ0yl14= github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY= github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y= -github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI= -github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= -github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= -github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= +github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0= github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc= +github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= -github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= -github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= -github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= -github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8= +github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs= +github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= -gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/pointer.go b/pointer.go index ba22d4d..de60dc7 100644 --- a/pointer.go +++ b/pointer.go @@ -50,13 +50,13 @@ var jsonSetableType = reflect.TypeOf(new(JSONSetable)).Elem() // JSONPointable is an interface for structs to implement when they need to customize the // json pointer process type JSONPointable interface { - JSONLookup(string) (interface{}, error) + JSONLookup(string) (any, error) } // JSONSetable is an interface for structs to implement when they need to customize the // json pointer process type JSONSetable interface { - JSONSet(string, interface{}) error + JSONSet(string, any) error } // New creates a new json pointer for the given string @@ -83,7 +83,7 @@ func (p *Pointer) parse(jsonPointerString string) error { err = errors.New(invalidStart) } else { referenceTokens := strings.Split(jsonPointerString, pointerSeparator) - p.referenceTokens = append(p.referenceTokens, referenceTokens[1:]...) + p.referenceTokens = append(p.referenceTokens, referenceTokens[1:]...) } } @@ -91,26 +91,26 @@ func (p *Pointer) parse(jsonPointerString string) error { } // Get uses the pointer to retrieve a value from a JSON document -func (p *Pointer) Get(document interface{}) (interface{}, reflect.Kind, error) { +func (p *Pointer) Get(document any) (any, reflect.Kind, error) { return p.get(document, swag.DefaultJSONNameProvider) } // Set uses the pointer to set a value from a JSON document -func (p *Pointer) Set(document interface{}, value interface{}) (interface{}, error) { +func (p *Pointer) Set(document any, value any) (any, error) { return document, p.set(document, value, swag.DefaultJSONNameProvider) } // GetForToken gets a value for a json pointer token 1 level deep -func GetForToken(document interface{}, decodedToken string) (interface{}, reflect.Kind, error) { +func GetForToken(document any, decodedToken string) (any, reflect.Kind, error) { return getSingleImpl(document, decodedToken, swag.DefaultJSONNameProvider) } // SetForToken gets a value for a json pointer token 1 level deep -func SetForToken(document interface{}, decodedToken string, value interface{}) (interface{}, error) { +func SetForToken(document any, decodedToken string, value any) (any, error) { return document, setSingleImpl(document, value, decodedToken, swag.DefaultJSONNameProvider) } -func getSingleImpl(node interface{}, decodedToken string, nameProvider *swag.NameProvider) (interface{}, reflect.Kind, error) { +func getSingleImpl(node any, decodedToken string, nameProvider *swag.NameProvider) (any, reflect.Kind, error) { rValue := reflect.Indirect(reflect.ValueOf(node)) kind := rValue.Kind() @@ -159,7 +159,7 @@ func getSingleImpl(node interface{}, decodedToken string, nameProvider *swag.Nam } -func setSingleImpl(node, data interface{}, decodedToken string, nameProvider *swag.NameProvider) error { +func setSingleImpl(node, data any, decodedToken string, nameProvider *swag.NameProvider) error { rValue := reflect.Indirect(reflect.ValueOf(node)) if ns, ok := node.(JSONSetable); ok { // pointer impl @@ -210,7 +210,7 @@ func setSingleImpl(node, data interface{}, decodedToken string, nameProvider *sw } -func (p *Pointer) get(node interface{}, nameProvider *swag.NameProvider) (interface{}, reflect.Kind, error) { +func (p *Pointer) get(node any, nameProvider *swag.NameProvider) (any, reflect.Kind, error) { if nameProvider == nil { nameProvider = swag.DefaultJSONNameProvider @@ -241,7 +241,7 @@ func (p *Pointer) get(node interface{}, nameProvider *swag.NameProvider) (interf return node, kind, nil } -func (p *Pointer) set(node, data interface{}, nameProvider *swag.NameProvider) error { +func (p *Pointer) set(node, data any, nameProvider *swag.NameProvider) error { knd := reflect.ValueOf(node).Kind() if knd != reflect.Ptr && knd != reflect.Struct && knd != reflect.Map && knd != reflect.Slice && knd != reflect.Array { diff --git a/pointer_test.go b/pointer_test.go index d19f3c6..4ed2365 100644 --- a/pointer_test.go +++ b/pointer_test.go @@ -52,7 +52,7 @@ const ( }` ) -var testDocumentJSON interface{} +var testDocumentJSON any type testStructJSON struct { Foo []string `json:"foo"` @@ -67,7 +67,7 @@ type testStructJSON struct { } `json:"obj"` } -type aliasedMap map[string]interface{} +type aliasedMap map[string]any var testStructJSONDoc testStructJSON var testStructJSONPtr *testStructJSON @@ -109,7 +109,7 @@ func TestFullDocument(t *testing.T) { t.Errorf("Get(%v) error %v", in, err.Error()) } - if len(result.(map[string]interface{})) != TestDocumentNBItems { + if len(result.(map[string]any)) != TestDocumentNBItems { t.Errorf("Get(%v) = %v, expect full document", in, result) } @@ -118,7 +118,7 @@ func TestFullDocument(t *testing.T) { t.Errorf("Get(%v) error %v", in, err.Error()) } - if len(result.(map[string]interface{})) != TestDocumentNBItems { + if len(result.(map[string]any)) != TestDocumentNBItems { t.Errorf("Get(%v) = %v, expect full document", in, result) } } @@ -160,7 +160,7 @@ type pointableImpl struct { a string } -func (p pointableImpl) JSONLookup(token string) (interface{}, error) { +func (p pointableImpl) JSONLookup(token string) (any, error) { if token == "some" { return p.a, nil } @@ -169,7 +169,7 @@ func (p pointableImpl) JSONLookup(token string) (interface{}, error) { type pointableMap map[string]string -func (p pointableMap) JSONLookup(token string) (interface{}, error) { +func (p pointableMap) JSONLookup(token string) (any, error) { if token == "swap" { return p["swapped"], nil } @@ -213,7 +213,7 @@ func TestGetNode(t *testing.T) { assert.NoError(t, err) assert.Len(t, result, TestNodeObjNBItems) - result, _, err = p.Get(aliasedMap(testDocumentJSON.(map[string]interface{}))) + result, _, err = p.Get(aliasedMap(testDocumentJSON.(map[string]any))) assert.NoError(t, err) assert.Len(t, result, TestNodeObjNBItems) @@ -288,8 +288,8 @@ func TestOtherThings(t *testing.T) { p, err = New("/foo/1") assert.NoError(t, err) expected := "hello" - bbb := testDocumentJSON.(map[string]interface{})["foo"] - bbb.([]interface{})[1] = "hello" + bbb := testDocumentJSON.(map[string]any)["foo"] + bbb.([]any)[1] = "hello" v, _, err := p.Get(testDocumentJSON) assert.NoError(t, err) @@ -371,7 +371,7 @@ func (s *settableDoc) UnmarshalJSON(data []byte) error { } // JSONLookup implements an interface to customize json pointer lookup -func (s settableDoc) JSONLookup(token string) (interface{}, error) { +func (s settableDoc) JSONLookup(token string) (any, error) { switch token { case "a": return &s.Coll, nil @@ -383,7 +383,7 @@ func (s settableDoc) JSONLookup(token string) (interface{}, error) { } // JSONLookup implements an interface to customize json pointer lookup -func (s *settableDoc) JSONSet(token string, data interface{}) error { +func (s *settableDoc) JSONSet(token string, data any) error { switch token { case "a": switch dt := data.(type) { @@ -440,7 +440,7 @@ func (s *settableColl) UnmarshalJSON(data []byte) error { } // JSONLookup implements an interface to customize json pointer lookup -func (s settableColl) JSONLookup(token string) (interface{}, error) { +func (s settableColl) JSONLookup(token string) (any, error) { if tok, err := strconv.Atoi(token); err == nil { return &s.Items[tok], nil } @@ -448,7 +448,7 @@ func (s settableColl) JSONLookup(token string) (interface{}, error) { } // JSONLookup implements an interface to customize json pointer lookup -func (s *settableColl) JSONSet(token string, data interface{}) error { +func (s *settableColl) JSONSet(token string, data any) error { if _, err := strconv.Atoi(token); err == nil { _, err := SetForToken(s.Items, token, data) return err @@ -476,7 +476,7 @@ func TestSetNode(t *testing.T) { jsonText := `{"a":[{"b": 1, "c": 2}], "d": 3}` - var jsonDocument interface{} + var jsonDocument any if assert.NoError(t, json.Unmarshal([]byte(jsonText), &jsonDocument)) { in := "/a/0/c" p, err := New(in) @@ -485,13 +485,13 @@ func TestSetNode(t *testing.T) { _, err = p.Set(jsonDocument, 999) assert.NoError(t, err) - firstNode := jsonDocument.(map[string]interface{}) + firstNode := jsonDocument.(map[string]any) assert.Len(t, firstNode, 2) - sliceNode := firstNode["a"].([]interface{}) + sliceNode := firstNode["a"].([]any) assert.Len(t, sliceNode, 1) - changedNode := sliceNode[0].(map[string]interface{}) + changedNode := sliceNode[0].(map[string]any) chNodeVI := changedNode["c"] if assert.IsType(t, 0, chNodeVI) { changedNodeValue := chNodeVI.(int) @@ -503,14 +503,14 @@ func TestSetNode(t *testing.T) { v, err := New("/a/0") if assert.NoError(t, err) { - _, err = v.Set(jsonDocument, map[string]interface{}{"b": 3, "c": 8}) + _, err = v.Set(jsonDocument, map[string]any{"b": 3, "c": 8}) if assert.NoError(t, err) { - firstNode := jsonDocument.(map[string]interface{}) + firstNode := jsonDocument.(map[string]any) assert.Len(t, firstNode, 2) - sliceNode := firstNode["a"].([]interface{}) + sliceNode := firstNode["a"].([]any) assert.Len(t, sliceNode, 1) - changedNode := sliceNode[0].(map[string]interface{}) + changedNode := sliceNode[0].(map[string]any) assert.Equal(t, 3, changedNode["b"]) assert.Equal(t, 8, changedNode["c"]) }