From 8a75157118caf2239196504f2f8db86190a77cd6 Mon Sep 17 00:00:00 2001 From: Sam Lown Date: Thu, 21 Sep 2023 14:21:20 +0000 Subject: [PATCH] Using orderedmap with generics support --- go.mod | 14 ++++++++++++-- go.sum | 25 ++++++++++++++++++++++--- reflect.go | 12 ++++++------ reflect_test.go | 9 +++------ utils.go | 8 ++++++++ 5 files changed, 51 insertions(+), 17 deletions(-) diff --git a/go.mod b/go.mod index aefedd7..dcf7130 100644 --- a/go.mod +++ b/go.mod @@ -1,8 +1,18 @@ module github.com/invopop/jsonschema -go 1.16 +go 1.18 require ( github.com/iancoleman/orderedmap v0.0.0-20190318233801-ac98e3ecb4b0 - github.com/stretchr/testify v1.3.1-0.20190311161405-34c6fa2dc709 + github.com/stretchr/testify v1.8.1 + github.com/wk8/go-ordered-map/v2 v2.1.8 +) + +require ( + github.com/bahlo/generic-list-go v0.2.0 // indirect + github.com/buger/jsonparser v1.1.1 // indirect + github.com/davecgh/go-spew v1.1.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 5e8841f..2cfaaf2 100644 --- a/go.sum +++ b/go.sum @@ -1,9 +1,28 @@ -github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= +github.com/bahlo/generic-list-go v0.2.0 h1:5sz/EEAK+ls5wF+NeqDpk5+iNdMDXrh3z3nPnH1Wvgk= +github.com/bahlo/generic-list-go v0.2.0/go.mod h1:2KvAjgMlE5NNynlg/5iLrrCCZ2+5xWbdbCW3pNTGyYg= +github.com/buger/jsonparser v1.1.1 h1:2PnMjfWD7wBILjqQbt530v576A/cAbQvEW9gGIpYMUs= +github.com/buger/jsonparser v1.1.1/go.mod h1:6RYKKt7H4d4+iWqouImQ9R2FZql3VbhNgx27UK13J/0= 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/iancoleman/orderedmap v0.0.0-20190318233801-ac98e3ecb4b0 h1:i462o439ZjprVSFSZLZxcsoAe592sZB1rci2Z8j4wdk= github.com/iancoleman/orderedmap v0.0.0-20190318233801-ac98e3ecb4b0/go.mod h1:N0Wam8K1arqPXNWjMo21EXnBPOPp36vB07FNRdD2geA= +github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y= +github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0= +github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc= 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/testify v1.3.1-0.20190311161405-34c6fa2dc709 h1:Ko2LQMrRU+Oy/+EDBwX7eZ2jp3C47eDBB8EIhKTun+I= -github.com/stretchr/testify v1.3.1-0.20190311161405-34c6fa2dc709/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +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/wk8/go-ordered-map/v2 v2.1.8 h1:5h/BUHu93oj4gIdvHHHGsScSTMijfx5PeYkE/fJgbpc= +github.com/wk8/go-ordered-map/v2 v2.1.8/go.mod h1:5nJHM5DyteebpVlHnWMV0rPz6Zp7+xBAnxjb1X5vnTw= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +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/reflect.go b/reflect.go index 863b3b7..acccd07 100644 --- a/reflect.go +++ b/reflect.go @@ -16,7 +16,7 @@ import ( "strings" "time" - "github.com/iancoleman/orderedmap" + orderedmap "github.com/wk8/go-ordered-map/v2" ) // Version is the JSON Schema version. @@ -48,10 +48,10 @@ type Schema struct { Items *Schema `json:"items,omitempty"` // section 10.3.1.2 (replaces additionalItems) Contains *Schema `json:"contains,omitempty"` // section 10.3.1.3 // RFC draft-bhutton-json-schema-00 section 10.3.2 (sub-schemas) - Properties *orderedmap.OrderedMap `json:"properties,omitempty"` // section 10.3.2.1 - PatternProperties map[string]*Schema `json:"patternProperties,omitempty"` // section 10.3.2.2 - AdditionalProperties *Schema `json:"additionalProperties,omitempty"` // section 10.3.2.3 - PropertyNames *Schema `json:"propertyNames,omitempty"` // section 10.3.2.4 + Properties *orderedmap.OrderedMap[string, *Schema] `json:"properties,omitempty"` // section 10.3.2.1 + PatternProperties map[string]*Schema `json:"patternProperties,omitempty"` // section 10.3.2.2 + AdditionalProperties *Schema `json:"additionalProperties,omitempty"` // section 10.3.2.3 + PropertyNames *Schema `json:"propertyNames,omitempty"` // section 10.3.2.4 // RFC draft-bhutton-json-schema-validation-00, section 6 Type string `json:"type,omitempty"` // section 6.1.1 Enum []interface{} `json:"enum,omitempty"` // section 6.1.2 @@ -509,7 +509,7 @@ func (r *Reflector) reflectStruct(definitions Definitions, t reflect.Type, s *Sc r.addDefinition(definitions, t, s) s.Type = "object" - s.Properties = orderedmap.New() + s.Properties = NewProperties() s.Description = r.lookupComment(t, "") if r.AssignAnchor { s.Anchor = t.Name() diff --git a/reflect_test.go b/reflect_test.go index b60bcfd..47cbddb 100644 --- a/reflect_test.go +++ b/reflect_test.go @@ -13,8 +13,6 @@ import ( "testing" "time" - "github.com/iancoleman/orderedmap" - "github.com/invopop/jsonschema/examples" "github.com/stretchr/testify/assert" @@ -268,7 +266,7 @@ func (CustomSliceType) JSONSchema() *Schema { type CustomMapType map[string]string func (CustomMapType) JSONSchema() *Schema { - properties := orderedmap.New() + properties := NewProperties() properties.Set("key", &Schema{ Type: "string", }) @@ -326,7 +324,7 @@ func (SchemaExtendTest) JSONSchemaExtend(base *Schema) { base.Properties.Delete("FirstName") base.Properties.Delete("age") val, _ := base.Properties.Get("LastName") - (val).(*Schema).Description = "some extra words" + val.Description = "some extra words" base.Required = []string{"LastName"} } @@ -546,10 +544,9 @@ func TestArrayExtraTags(t *testing.T) { require.NotNil(t, d) props := d.Properties require.NotNil(t, props) - i, found := props.Get("TestURIs") + p, found := props.Get("TestURIs") require.True(t, found) - p := i.(*Schema) pt := p.Items.Format require.Equal(t, pt, "uri") pt = p.Items.Pattern diff --git a/utils.go b/utils.go index 9813b11..ed8edf7 100644 --- a/utils.go +++ b/utils.go @@ -3,6 +3,8 @@ package jsonschema import ( "regexp" "strings" + + orderedmap "github.com/wk8/go-ordered-map/v2" ) var matchFirstCap = regexp.MustCompile("(.)([A-Z][a-z]+)") @@ -16,3 +18,9 @@ func ToSnakeCase(str string) string { snake = matchAllCap.ReplaceAllString(snake, "${1}-${2}") return strings.ToLower(snake) } + +// NewProperties is a helper method to instantiate a new properties ordered +// map. +func NewProperties() *orderedmap.OrderedMap[string, *Schema] { + return orderedmap.New[string, *Schema]() +}