From 84439be2da6aac20eb4f5a30c733c534ff123684 Mon Sep 17 00:00:00 2001 From: conneroisu Date: Wed, 11 Sep 2024 08:50:20 -0400 Subject: [PATCH 1/5] run go mod tidy and workspace sync --- go.mod | 12 ++++++------ go.sum | 6 ++---- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/go.mod b/go.mod index 2462658..9359bd5 100644 --- a/go.mod +++ b/go.mod @@ -3,36 +3,36 @@ module github.com/conneroisu/groq-go go 1.23.0 require ( - github.com/bahlo/generic-list-go v0.2.0 - github.com/buger/jsonparser v1.1.1 github.com/charmbracelet/bubbles v0.20.0 github.com/charmbracelet/bubbletea v1.1.0 github.com/charmbracelet/lipgloss v0.13.0 - github.com/mailru/easyjson v0.7.7 github.com/rs/zerolog v1.33.0 github.com/stretchr/testify v1.9.0 github.com/wk8/go-ordered-map/v2 v2.1.8 - gopkg.in/yaml.v3 v3.0.1 ) require ( github.com/atotto/clipboard v0.1.4 // indirect github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect + github.com/bahlo/generic-list-go v0.2.0 // indirect + github.com/buger/jsonparser v1.1.1 // indirect github.com/charmbracelet/x/ansi v0.2.3 // indirect github.com/charmbracelet/x/term v0.2.0 // indirect github.com/davecgh/go-spew v1.1.1 // indirect github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f // indirect github.com/lucasb-eyer/go-colorful v1.2.0 // indirect + github.com/mailru/easyjson v0.7.7 // indirect github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-isatty v0.0.20 // indirect github.com/mattn/go-localereader v0.0.1 // indirect github.com/mattn/go-runewidth v0.0.16 // indirect github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 // indirect github.com/muesli/cancelreader v0.2.2 // indirect - github.com/muesli/termenv v0.15.2 // indirect + github.com/muesli/termenv v0.15.3-0.20240618155329-98d742f6907a // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/rivo/uniseg v0.4.7 // indirect golang.org/x/sync v0.8.0 // indirect golang.org/x/sys v0.25.0 // indirect - golang.org/x/text v0.3.8 // indirect + golang.org/x/text v0.18.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 1716f5a..8ef3007 100644 --- a/go.sum +++ b/go.sum @@ -43,8 +43,7 @@ github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 h1:ZK8zHtRHOkbHy6Mmr5D github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6/go.mod h1:CJlz5H+gyd6CUWT45Oy4q24RdLyn7Md9Vj2/ldJBSIo= github.com/muesli/cancelreader v0.2.2 h1:3I4Kt4BQjOR54NavqnDogx/MIoWBFa0StPA8ELUXHmA= github.com/muesli/cancelreader v0.2.2/go.mod h1:3XuTXfFS2VjM+HTLZY9Ak0l6eUKfijIfMUZ4EgX0QYo= -github.com/muesli/termenv v0.15.2 h1:GohcuySI0QmI3wN8Ok9PtKGkgkFIk7y6Vpb5PvrY+Wo= -github.com/muesli/termenv v0.15.2/go.mod h1:Epx+iuz8sNs7mNKhxzH4fWXGNpZwUaJKRS1noLXviQ8= +github.com/muesli/termenv v0.15.3-0.20240618155329-98d742f6907a h1:2MaM6YC3mGu54x+RKAA6JiFFHlHDY1UbkxqppT7wYOg= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= @@ -66,8 +65,7 @@ golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34= golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= -golang.org/x/text v0.3.8 h1:nAL+RVCQ9uMn3vJZbV+MRnydTJFPf8qqY42YiA6MrqY= -golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ= +golang.org/x/text v0.18.0 h1:XvMDiNzPAl0jr17s6W9lcaIhGUfUORdGCNsuLmPG224= 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.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= From a868d6dc5f8de85515b408a5bfbe3f45135d7ea4 Mon Sep 17 00:00:00 2001 From: conneroisu Date: Wed, 11 Sep 2024 08:52:47 -0400 Subject: [PATCH 2/5] even better comments for the schema struct def --- schema.go | 43 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/schema.go b/schema.go index ba81df8..7db8662 100644 --- a/schema.go +++ b/schema.go @@ -1252,8 +1252,47 @@ 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[string, *schema] `json:"properties,omitempty"` // section 10.3.2.1 - PatternProperties map[string]*schema `json:"patternProperties,omitempty"` // section 10.3.2.2 + // Properties are the properties of the schema as specified in section 10.3.2.1 of RFC + // draft-bhutton-json-schema-00. + // + // https://datatracker.ietf.org/doc/html/draft-bhutton-json-schema-00#section-10.3.2.1 + // + // The value of "properties" MUST be an object. Each value of this + // object MUST be a valid JSON Schema. + // + // Validation succeeds if, for each name that appears in both the + // instance and as a name within this field's value, the child + // instance for that name successfully validates against the + // corresponding schema. + // + // The annotation result of this field is the set of instance property + // names matched by this keyword. + // + // Omitting this field has the same assertion behavior as an empty + // object. + Properties *orderedmap.OrderedMap[string, *schema] `json:"properties,omitempty"` + // PatternProperties are the pattern properties of the schema as specified in section 10.3.2.2 of RFC + // draft-bhutton-json-schema-00. + // + // https://datatracker.ietf.org/doc/html/draft-bhutton-json-schema-00#section-10.3.2.2 + // + // The value of "patternProperties" MUST be an object. Each property + // name of this object SHOULD be a valid regular expression, according + // to the ECMA-262 regular expression dialect. Each property value of + // this object MUST be a valid JSON Schema. + // + // Validation succeeds if, for each instance name that matches any + // regular expressions that appear as a property name in this field's + // value, the child instance for that name successfully validates + // against each schema that corresponds to a matching regular + // expression. + // + // The annotation result of this field is the set of instance property + // names matched by this keyword. + // + // Omitting this field has the same assertion behavior as an empty + // object. + PatternProperties map[string]*schema `json:"patternProperties,omitempty"` // section 10.3.2.2 // AdditionalProperties is the additional properties of the schema as // specified in section 10.3.2.3 of RFC // draft-bhutton-json-schema-00. From 234eae960266f89535706b7cbd5ce300a26ba284 Mon Sep 17 00:00:00 2001 From: conneroisu Date: Wed, 11 Sep 2024 08:57:11 -0400 Subject: [PATCH 3/5] better naming --- schema.go | 112 +++++++++++++++++++++++++++++++------------------ schema_test.go | 16 +++---- 2 files changed, 80 insertions(+), 48 deletions(-) diff --git a/schema.go b/schema.go index 7db8662..0eb320a 100644 --- a/schema.go +++ b/schema.go @@ -70,7 +70,7 @@ type reflector struct { // If no `BaseSchemaID` is provided, we'll take the type's complete package path // and use that as a base instead. Set `Anonymous` to try if you do not want to // include a schema ID. - BaseSchemaID id + BaseSchemaID schemaID // Anonymous when true will hide the auto-generated Schema ID and provide what is // known as an "anonymous schema". As a rule, this is not recommended. @@ -117,7 +117,7 @@ type reflector struct { // types to Schema IDs. This allows existing schema documents to be referenced // by their ID instead of being embedded into the current schema definitions. // Reflected types will never be pointers, only underlying elements. - Lookup func(reflect.Type) id + Lookup func(reflect.Type) schemaID // Mapper is a function that can be used to map custom Go types to jsonschema schemas. Mapper func(reflect.Type) *schema @@ -165,7 +165,7 @@ func (r *reflector) ReflectFromType(t reflect.Type) *schema { name := r.typeName(t) s := new(schema) - definitions := definitions{} + definitions := schemaDefinitions{} s.Definitions = definitions bs := r.reflectTypeToSchemaWithID(definitions, t) if r.ExpandedStruct { @@ -179,7 +179,7 @@ func (r *reflector) ReflectFromType(t reflect.Type) *schema { if !r.Anonymous && s.ID == EmptyID { baseSchemaID := r.BaseSchemaID if baseSchemaID == EmptyID { - i := id("https://" + t.PkgPath()) + i := schemaID("https://" + t.PkgPath()) if err := i.Validate(); err == nil { // it's okay to silently ignore URL errors baseSchemaID = i @@ -225,10 +225,13 @@ var protoEnumType = reflect.TypeOf((*protoEnum)(nil)).Elem() // SetBaseSchemaID is a helper use to be able to set the reflectors base // schema ID from a string as opposed to then ID instance. func (r *reflector) SetBaseSchemaID(identifier string) { - r.BaseSchemaID = id(identifier) + r.BaseSchemaID = schemaID(identifier) } -func (r *reflector) refOrReflectTypeToSchema(definitions definitions, t reflect.Type) *schema { +func (r *reflector) refOrReflectTypeToSchema( + definitions schemaDefinitions, + t reflect.Type, +) *schema { id := r.lookupID(t) if id != EmptyID { return &schema{ @@ -244,7 +247,10 @@ func (r *reflector) refOrReflectTypeToSchema(definitions definitions, t reflect. return r.reflectTypeToSchemaWithID(definitions, t) } -func (r *reflector) reflectTypeToSchemaWithID(defs definitions, t reflect.Type) *schema { +func (r *reflector) reflectTypeToSchemaWithID( + defs schemaDefinitions, + t reflect.Type, +) *schema { s := r.reflectTypeToSchema(defs, t) if s != nil { if r.Lookup != nil { @@ -257,7 +263,10 @@ func (r *reflector) reflectTypeToSchemaWithID(defs definitions, t reflect.Type) return s } -func (r *reflector) reflectTypeToSchema(definitions definitions, t reflect.Type) *schema { +func (r *reflector) reflectTypeToSchema( + definitions schemaDefinitions, + t reflect.Type, +) *schema { // only try to reflect non-pointers if t.Kind() == reflect.Ptr { return r.refOrReflectTypeToSchema(definitions, t.Elem()) @@ -345,7 +354,10 @@ func (r *reflector) reflectTypeToSchema(definitions definitions, t reflect.Type) return st } -func (r *reflector) reflectCustomSchema(definitions definitions, t reflect.Type) *schema { +func (r *reflector) reflectCustomSchema( + definitions schemaDefinitions, + t reflect.Type, +) *schema { if t.Kind() == reflect.Ptr { return r.reflectCustomSchema(definitions, t.Elem()) } @@ -364,7 +376,11 @@ func (r *reflector) reflectCustomSchema(definitions definitions, t reflect.Type) return nil } -func (r *reflector) reflectSchemaExtend(definitions definitions, t reflect.Type, s *schema) *schema { +func (r *reflector) reflectSchemaExtend( + definitions schemaDefinitions, + t reflect.Type, + s *schema, +) *schema { if t.Implements(extendType) { v := reflect.New(t) o := v.Interface().(extendSchemaImpl) @@ -377,7 +393,11 @@ func (r *reflector) reflectSchemaExtend(definitions definitions, t reflect.Type, return s } -func (r *reflector) reflectSliceOrArray(definitions definitions, t reflect.Type, st *schema) { +func (r *reflector) reflectSliceOrArray( + definitions schemaDefinitions, + t reflect.Type, + st *schema, +) { if t == rawMessageType { return } @@ -403,7 +423,11 @@ func (r *reflector) reflectSliceOrArray(definitions definitions, t reflect.Type, } } -func (r *reflector) reflectMap(definitions definitions, t reflect.Type, st *schema) { +func (r *reflector) reflectMap( + definitions schemaDefinitions, + t reflect.Type, + st *schema, +) { r.addDefinition(definitions, t, st) st.Type = "object" @@ -425,7 +449,11 @@ func (r *reflector) reflectMap(definitions definitions, t reflect.Type, st *sche } // Reflects a struct to a JSON Schema type. -func (r *reflector) reflectStruct(definitions definitions, t reflect.Type, s *schema) { +func (r *reflector) reflectStruct( + definitions schemaDefinitions, + t reflect.Type, + s *schema, +) { // Handle special types switch t { case timeType: // date-time RFC section 7.3.1 @@ -461,7 +489,11 @@ func (r *reflector) reflectStruct(definitions definitions, t reflect.Type, s *sc } } -func (r *reflector) reflectStructFields(st *schema, definitions definitions, t reflect.Type) { +func (r *reflector) reflectStructFields( + st *schema, + definitions schemaDefinitions, + t reflect.Type, +) { if t.Kind() == reflect.Ptr { t = t.Elem() } @@ -505,7 +537,7 @@ func (r *reflector) reflectStructFields(st *schema, definitions definitions, t r property = r.refOrReflectTypeToSchema(definitions, f.Type) } - property.structKeywordsFromTags(f, st, name) + property.keywordsFromTags(f, st, name) if property.Description == "" { property.Description = r.lookupComment(t, f.Name) } @@ -566,7 +598,7 @@ func (r *reflector) lookupComment(t reflect.Type, name string) string { } // addDefinition will append the provided schema. If needed, an ID and anchor will also be added. -func (r *reflector) addDefinition(definitions definitions, t reflect.Type, s *schema) { +func (r *reflector) addDefinition(definitions schemaDefinitions, t reflect.Type, s *schema) { name := r.typeName(t) if name == "" { return @@ -575,7 +607,7 @@ func (r *reflector) addDefinition(definitions definitions, t reflect.Type, s *sc } // refDefinition will provide a schema with a reference to an existing definition. -func (r *reflector) refDefinition(definitions definitions, t reflect.Type) *schema { +func (r *reflector) refDefinition(definitions schemaDefinitions, t reflect.Type) *schema { if r.DoNotReference { return nil } @@ -591,7 +623,7 @@ func (r *reflector) refDefinition(definitions definitions, t reflect.Type) *sche } } -func (r *reflector) lookupID(t reflect.Type) id { +func (r *reflector) lookupID(t reflect.Type) schemaID { if r.Lookup != nil { if t.Kind() == reflect.Ptr { t = t.Elem() @@ -602,7 +634,7 @@ func (r *reflector) lookupID(t reflect.Type) id { return EmptyID } -func (t *schema) structKeywordsFromTags(f reflect.StructField, parent *schema, propertyName string) { +func (t *schema) keywordsFromTags(f reflect.StructField, parent *schema, propertyName string) { t.Description = f.Tag.Get("jsonschema_description") tags := splitOnUnescapedCommas(f.Tag.Get("jsonschema")) @@ -1149,12 +1181,12 @@ func newProperties() *orderedmap.OrderedMap[string, *schema] { // RFC draft-bhutton-json-schema-00 section 4.3 type schema struct { // RFC draft-bhutton-json-schema-00 - Version string `json:"$schema,omitempty"` // section 8.1.1 - ID id `json:"$id,omitempty"` // section 8.2.1 - Anchor string `json:"$anchor,omitempty"` // section 8.2.2 - Ref string `json:"$ref,omitempty"` // section 8.2.3.1 - DynamicRef string `json:"$dynamicRef,omitempty"` // section 8.2.3.2 - Definitions definitions `json:"$defs,omitempty"` // section 8.2.4 + Version string `json:"$schema,omitempty"` // section 8.1.1 + ID schemaID `json:"$id,omitempty"` // section 8.2.1 + Anchor string `json:"$anchor,omitempty"` // section 8.2.2 + Ref string `json:"$ref,omitempty"` // section 8.2.3.1 + DynamicRef string `json:"$dynamicRef,omitempty"` // section 8.2.3.2 + Definitions schemaDefinitions `json:"$defs,omitempty"` // section 8.2.4 // Comments specifies a comment for the schema as // specified RFC draft-bhutton-json-schema-00 section 8.3 // @@ -1798,24 +1830,24 @@ var ( falseSchema = &schema{boolean: &[]bool{false}[0]} ) -// definitions hold schema definitions. +// schemaDefinitions hold schema schemaDefinitions. // // http://json-schema.org/latest/json-schema-validation.html#rfc.section.5.26 // // RFC draft-wright-json-schema-validation-00, section 5.26 -type definitions map[string]*schema +type schemaDefinitions map[string]*schema -// id represents a Schema id type which should always be a URI. +// schemaID represents a Schema schemaID type which should always be a URI. // See draft-bhutton-json-schema-00 section 8.2.1 -type id string +type schemaID string // EmptyID is used to explicitly define an ID with no value. -const EmptyID id = "" +const EmptyID schemaID = "" // Validate is used to check if the ID looks like a proper schema. // This is done by parsing the ID as a URL and checking it has all the // relevant parts. -func (i id) Validate() error { +func (i schemaID) Validate() error { u, err := url.Parse(i.String()) if err != nil { return fmt.Errorf("invalid URL: %w", err) @@ -1836,39 +1868,39 @@ func (i id) Validate() error { } // Anchor sets the anchor part of the schema URI. -func (i id) Anchor(name string) id { +func (i schemaID) Anchor(name string) schemaID { b := i.Base() - return id(b.String() + "#" + name) + return schemaID(b.String() + "#" + name) } // Def adds or replaces a definition identifier. -func (i id) Def(name string) id { +func (i schemaID) Def(name string) schemaID { b := i.Base() - return id(b.String() + "#/$defs/" + name) + return schemaID(b.String() + "#/$defs/" + name) } // Add appends the provided path to the id, and removes any // anchor data that might be there. -func (i id) Add(path string) id { +func (i schemaID) Add(path string) schemaID { b := i.Base() if !strings.HasPrefix(path, "/") { path = "/" + path } - return id(b.String() + path) + return schemaID(b.String() + path) } // Base removes any anchor information from the schema -func (i id) Base() id { +func (i schemaID) Base() schemaID { s := i.String() li := strings.LastIndex(s, "#") if li != -1 { s = s[0:li] } s = strings.TrimRight(s, "/") - return id(s) + return schemaID(s) } // String provides string version of ID -func (i id) String() string { +func (i schemaID) String() string { return string(i) } diff --git a/schema_test.go b/schema_test.go index 26078db..8da5a0b 100644 --- a/schema_test.go +++ b/schema_test.go @@ -19,7 +19,7 @@ import ( func TestID(t *testing.T) { base := "https://github.com/conneroisu/groq-go/schema" - id := id(base) + id := schemaID(base) assert.Equal(t, base, id.String()) @@ -37,7 +37,7 @@ func TestID(t *testing.T) { } func TestIDValidation(t *testing.T) { - id := id("https://invopop.com/schema/user") + id := schemaID("https://invopop.com/schema/user") assert.NoError(t, id.Validate()) id = "https://encoding/json" @@ -483,12 +483,12 @@ func TestSchemaGeneration(t *testing.T) { }, "testdata/custom_type.json"}, {LookupUser{}, &reflector{BaseSchemaID: "https://example.com/schemas"}, "testdata/base_schema_id.json"}, {LookupUser{}, &reflector{ - Lookup: func(i reflect.Type) id { + Lookup: func(i reflect.Type) schemaID { switch i { case reflect.TypeOf(LookupUser{}): - return id("https://example.com/schemas/lookup-user") + return schemaID("https://example.com/schemas/lookup-user") case reflect.TypeOf(LookupName{}): - return id("https://example.com/schemas/lookup-name") + return schemaID("https://example.com/schemas/lookup-name") } return EmptyID }, @@ -497,12 +497,12 @@ func TestSchemaGeneration(t *testing.T) { BaseSchemaID: "https://example.com/schemas", ExpandedStruct: true, AssignAnchor: true, - Lookup: func(i reflect.Type) id { + Lookup: func(i reflect.Type) schemaID { switch i { case reflect.TypeOf(LookupUser{}): - return id("https://example.com/schemas/lookup-user") + return schemaID("https://example.com/schemas/lookup-user") case reflect.TypeOf(LookupName{}): - return id("https://example.com/schemas/lookup-name") + return schemaID("https://example.com/schemas/lookup-name") } return EmptyID }, From d0e25ac8481a647b70f8e1a3ac58372eae659500 Mon Sep 17 00:00:00 2001 From: conneroisu Date: Wed, 11 Sep 2024 09:03:57 -0400 Subject: [PATCH 4/5] make mroe readable --- schema.go | 109 +++++++++++++++++++++++++----------------------------- 1 file changed, 50 insertions(+), 59 deletions(-) diff --git a/schema.go b/schema.go index 0eb320a..80e77e8 100644 --- a/schema.go +++ b/schema.go @@ -17,7 +17,36 @@ import ( ) // version is the JSON Schema version. -var version = "https://json-schema.org/draft/2020-12/schema" +const version = "https://json-schema.org/draft/2020-12/schema" + +// Available Go defined types for JSON Schema Validation. +// +// https://datatracker.ietf.org/doc/html/draft-wright-json-schema-validation-00#section-7.3 +// +// RFC draft-wright-json-schema-validation-00, section 7.3 +var ( + // trueSchema defines a schema with a true value + trueSchema = &schema{boolean: &[]bool{true}[0]} + // falseSchema defines a schema with a false value + falseSchema = &schema{boolean: &[]bool{false}[0]} + + timeType = reflect.TypeOf(time.Time{}) // date-time RFC section 7.3.1 + ipType = reflect.TypeOf(net.IP{}) // ipv4 and ipv6 RFC section 7.3.4, 7.3.5 + uriType = reflect.TypeOf(url.URL{}) // uri RFC section 7.3.6 + + byteSliceType = reflect.TypeOf([]byte(nil)) + rawMessageType = reflect.TypeOf(json.RawMessage{}) + + customType = reflect.TypeOf((*customSchemaImpl)(nil)).Elem() + extendType = reflect.TypeOf((*extendSchemaImpl)(nil)).Elem() + customStructGetFieldDocString = reflect.TypeOf((*customSchemaGetFieldDocString)(nil)).Elem() + protoEnumType = reflect.TypeOf((*protoEnum)(nil)).Elem() + matchFirstCap = regexp.MustCompile("(.)([A-Z][a-z]+)") + matchAllCap = regexp.MustCompile("([a-z0-9])([A-Z])") + + customAliasSchema = reflect.TypeOf((*aliasSchemaImpl)(nil)).Elem() + customPropertyAliasSchema = reflect.TypeOf((*propertyAliasSchemaImpl)(nil)).Elem() +) // customSchemaImpl is used to detect if the type provides it's own // custom Schema Type definition to use instead. Very useful for situations @@ -45,12 +74,6 @@ type propertyAliasSchemaImpl interface { JSONSchemaProperty(prop string) any } -var customAliasSchema = reflect.TypeOf((*aliasSchemaImpl)(nil)).Elem() -var customPropertyAliasSchema = reflect.TypeOf((*propertyAliasSchemaImpl)(nil)).Elem() - -var customType = reflect.TypeOf((*customSchemaImpl)(nil)).Elem() -var extendType = reflect.TypeOf((*extendSchemaImpl)(nil)).Elem() - // customSchemaGetFieldDocString type customSchemaGetFieldDocString interface { GetFieldDocString(fieldName string) string @@ -58,8 +81,6 @@ type customSchemaGetFieldDocString interface { type customGetFieldDocString func(fieldName string) string -var customStructGetFieldDocString = reflect.TypeOf((*customSchemaGetFieldDocString)(nil)).Elem() - // A reflector reflects values into a Schema. type reflector struct { // BaseSchemaID defines the URI that will be used as a base to determine Schema @@ -198,30 +219,11 @@ func (r *reflector) ReflectFromType(t reflect.Type) *schema { return s } -// Available Go defined types for JSON Schema Validation. -// -// https://datatracker.ietf.org/doc/html/draft-wright-json-schema-validation-00#section-7.3 -// -// RFC draft-wright-json-schema-validation-00, section 7.3 -var ( - timeType = reflect.TypeOf(time.Time{}) // date-time RFC section 7.3.1 - ipType = reflect.TypeOf(net.IP{}) // ipv4 and ipv6 RFC section 7.3.4, 7.3.5 - uriType = reflect.TypeOf(url.URL{}) // uri RFC section 7.3.6 -) - -// Byte slices will be encoded as base64 -var byteSliceType = reflect.TypeOf([]byte(nil)) - -// Except for json.RawMessage -var rawMessageType = reflect.TypeOf(json.RawMessage{}) - // Go code generated from protobuf enum types should fulfil this interface. type protoEnum interface { EnumDescriptor() ([]byte, []int) } -var protoEnumType = reflect.TypeOf((*protoEnum)(nil)).Elem() - // SetBaseSchemaID is a helper use to be able to set the reflectors base // schema ID from a string as opposed to then ID instance. func (r *reflector) SetBaseSchemaID(identifier string) { @@ -417,10 +419,10 @@ func (r *reflector) reflectSliceOrArray( st.Type = "string" // NOTE: ContentMediaType is not set here st.ContentEncoding = "base64" - } else { - st.Type = "array" - st.Items = r.refOrReflectTypeToSchema(definitions, t.Elem()) + return } + st.Type = "array" + st.Items = r.refOrReflectTypeToSchema(definitions, t.Elem()) } func (r *reflector) reflectMap( @@ -882,7 +884,6 @@ func (t *schema) arrayKeywords(tags []string) { } if len(unprocessed) == 0 { - // we don't have anything else to process return } @@ -924,21 +925,21 @@ func (t *schema) setExtra(key, val string) { case bool: t.Extras[key] = (val == "true" || val == "t") } - } else { - switch key { - case "minimum": - t.Extras[key], _ = strconv.Atoi(val) - default: - var x any - if val == "true" { - x = true - } else if val == "false" { - x = false - } else { - x = val - } - t.Extras[key] = x + return + } + switch key { + case "minimum": + t.Extras[key], _ = strconv.Atoi(val) + default: + var x any + if val == "true" { + x = true + } else if val == "false" { + x = false + } else { + x = val } + t.Extras[key] = x } } @@ -1147,10 +1148,10 @@ func splitOnUnescapedCommas(tagString string) []string { } if ret[i][len(ret[i])-1] == '\\' { ret[i] = ret[i][:len(ret[i])-1] + "," + nextTag - } else { - ret = append(ret, nextTag) - i++ + continue } + ret = append(ret, nextTag) + i++ } return ret } @@ -1159,9 +1160,6 @@ func fullyQualifiedTypeName(t reflect.Type) string { return t.PkgPath() + "." + t.Name() } -var matchFirstCap = regexp.MustCompile("(.)([A-Z][a-z]+)") -var matchAllCap = regexp.MustCompile("([a-z0-9])([A-Z])") - // ToSnakeCase converts the provided string into snake case using dashes. // This is useful for Schema IDs and definitions to be coherent with // common JSON Schema examples. @@ -1823,13 +1821,6 @@ type schema struct { boolean *bool } -var ( - // trueSchema defines a schema with a true value - trueSchema = &schema{boolean: &[]bool{true}[0]} - // falseSchema defines a schema with a false value - falseSchema = &schema{boolean: &[]bool{false}[0]} -) - // schemaDefinitions hold schema schemaDefinitions. // // http://json-schema.org/latest/json-schema-validation.html#rfc.section.5.26 From 2700fba3249728068b96d9279d777b561c0eeef1 Mon Sep 17 00:00:00 2001 From: conneroisu Date: Wed, 11 Sep 2024 09:08:56 -0400 Subject: [PATCH 5/5] more modern error creation --- schema.go | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/schema.go b/schema.go index 80e77e8..01dd518 100644 --- a/schema.go +++ b/schema.go @@ -3,7 +3,6 @@ package groq import ( "bytes" "encoding/json" - "errors" "fmt" "net" "net/url" @@ -1844,16 +1843,16 @@ func (i schemaID) Validate() error { return fmt.Errorf("invalid URL: %w", err) } if u.Hostname() == "" { - return errors.New("missing hostname") + return fmt.Errorf("missing hostname: %s", u.Hostname()) } if !strings.Contains(u.Hostname(), ".") { - return errors.New("hostname does not look valid") + return fmt.Errorf("hostname does not look valid: %s", u.Hostname()) } if u.Path == "" { - return errors.New("path is expected") + return fmt.Errorf("path is expected: %s", u.Path) } if u.Scheme != "https" && u.Scheme != "http" { - return errors.New("unexpected schema") + return fmt.Errorf("unexpected schema: %s", u.Scheme) } return nil }