From d0e25ac8481a647b70f8e1a3ac58372eae659500 Mon Sep 17 00:00:00 2001 From: conneroisu Date: Wed, 11 Sep 2024 09:03:57 -0400 Subject: [PATCH] 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