From 4e3ccc03014b9d4e9edcd7f5e5ba50496d6d57ca Mon Sep 17 00:00:00 2001 From: candiduslynx Date: Mon, 25 Sep 2023 09:44:35 +0300 Subject: [PATCH] interface{} -> any --- examples/user.go | 6 ++--- examples_test.go | 16 ++++++------ reflect.go | 32 +++++++++++------------ reflect_test.go | 68 ++++++++++++++++++++++++------------------------ 4 files changed, 61 insertions(+), 61 deletions(-) diff --git a/examples/user.go b/examples/user.go index 710f12e..4c5b484 100644 --- a/examples/user.go +++ b/examples/user.go @@ -10,9 +10,9 @@ type User struct { // Unique sequential identifier. ID int `json:"id" jsonschema:"required"` // This comment will be ignored - Name string `json:"name" jsonschema:"required,minLength=1,maxLength=20,pattern=.*,description=this is a property,title=the name,example=joe,example=lucy,default=alex"` - Friends []int `json:"friends,omitempty" jsonschema_description:"list of IDs, omitted when empty"` - Tags map[string]interface{} `json:"tags,omitempty"` + Name string `json:"name" jsonschema:"required,minLength=1,maxLength=20,pattern=.*,description=this is a property,title=the name,example=joe,example=lucy,default=alex"` + Friends []int `json:"friends,omitempty" jsonschema_description:"list of IDs, omitted when empty"` + Tags map[string]any `json:"tags,omitempty"` // An array of pets the user cares for. Pets nested.Pets `json:"pets"` diff --git a/examples_test.go b/examples_test.go index 82cae05..5363458 100644 --- a/examples_test.go +++ b/examples_test.go @@ -9,14 +9,14 @@ import ( ) type SampleUser struct { - ID int `json:"id"` - Name string `json:"name" jsonschema:"title=the name,description=The name of a friend,example=joe,example=lucy,default=alex"` - Friends []int `json:"friends,omitempty" jsonschema_description:"The list of IDs, omitted when empty"` - Tags map[string]interface{} `json:"tags,omitempty" jsonschema_extras:"a=b,foo=bar,foo=bar1"` - BirthDate time.Time `json:"birth_date,omitempty" jsonschema:"oneof_required=date"` - YearOfBirth string `json:"year_of_birth,omitempty" jsonschema:"oneof_required=year"` - Metadata interface{} `json:"metadata,omitempty" jsonschema:"oneof_type=string;array"` - FavColor string `json:"fav_color,omitempty" jsonschema:"enum=red,enum=green,enum=blue"` + ID int `json:"id"` + Name string `json:"name" jsonschema:"title=the name,description=The name of a friend,example=joe,example=lucy,default=alex"` + Friends []int `json:"friends,omitempty" jsonschema_description:"The list of IDs, omitted when empty"` + Tags map[string]any `json:"tags,omitempty" jsonschema_extras:"a=b,foo=bar,foo=bar1"` + BirthDate time.Time `json:"birth_date,omitempty" jsonschema:"oneof_required=date"` + YearOfBirth string `json:"year_of_birth,omitempty" jsonschema:"oneof_required=year"` + Metadata any `json:"metadata,omitempty" jsonschema:"oneof_type=string;array"` + FavColor string `json:"fav_color,omitempty" jsonschema:"enum=red,enum=green,enum=blue"` } func ExampleReflect() { diff --git a/reflect.go b/reflect.go index af70461..ee0ef81 100644 --- a/reflect.go +++ b/reflect.go @@ -54,8 +54,8 @@ type Schema struct { 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 - Const interface{} `json:"const,omitempty"` // section 6.1.3 + Enum []any `json:"enum,omitempty"` // section 6.1.2 + Const any `json:"const,omitempty"` // section 6.1.3 MultipleOf *json.Number `json:"multipleOf,omitempty"` // section 6.2.1 Maximum *json.Number `json:"maximum,omitempty"` // section 6.2.2 ExclusiveMaximum *json.Number `json:"exclusiveMaximum,omitempty"` // section 6.2.3 @@ -80,15 +80,15 @@ type Schema struct { ContentMediaType string `json:"contentMediaType,omitempty"` // section 8.4 ContentSchema *Schema `json:"contentSchema,omitempty"` // section 8.5 // RFC draft-bhutton-json-schema-validation-00, section 9 - Title string `json:"title,omitempty"` // section 9.1 - Description string `json:"description,omitempty"` // section 9.1 - Default interface{} `json:"default,omitempty"` // section 9.2 - Deprecated bool `json:"deprecated,omitempty"` // section 9.3 - ReadOnly bool `json:"readOnly,omitempty"` // section 9.4 - WriteOnly bool `json:"writeOnly,omitempty"` // section 9.4 - Examples []interface{} `json:"examples,omitempty"` // section 9.5 + Title string `json:"title,omitempty"` // section 9.1 + Description string `json:"description,omitempty"` // section 9.1 + Default any `json:"default,omitempty"` // section 9.2 + Deprecated bool `json:"deprecated,omitempty"` // section 9.3 + ReadOnly bool `json:"readOnly,omitempty"` // section 9.4 + WriteOnly bool `json:"writeOnly,omitempty"` // section 9.4 + Examples []any `json:"examples,omitempty"` // section 9.5 - Extras map[string]interface{} `json:"-"` + Extras map[string]any `json:"-"` // Special boolean representation of the Schema - section 4.3.2 boolean *bool @@ -127,7 +127,7 @@ type customGetFieldDocString func(fieldName string) string var customStructGetFieldDocString = reflect.TypeOf((*customSchemaGetFieldDocString)(nil)).Elem() // Reflect reflects to Schema from a value using the default Reflector -func Reflect(v interface{}) *Schema { +func Reflect(v any) *Schema { return ReflectFromType(reflect.TypeOf(v)) } @@ -188,7 +188,7 @@ type Reflector struct { // IgnoredTypes defines a slice of types that should be ignored in the schema, // switching to just allowing additional properties instead. - IgnoredTypes []interface{} + IgnoredTypes []any // Lookup allows a function to be defined that will provide a custom mapping of // types to Schema IDs. This allows existing schema documents to be referenced @@ -229,7 +229,7 @@ type Reflector struct { } // Reflect reflects to Schema from a value. -func (r *Reflector) Reflect(v interface{}) *Schema { +func (r *Reflector) Reflect(v any) *Schema { return r.ReflectFromType(reflect.TypeOf(v)) } @@ -891,7 +891,7 @@ func (t *Schema) numericalKeywords(tags []string) { // read struct tags for array type keywords func (t *Schema) arrayKeywords(tags []string) { - var defaultValues []interface{} + var defaultValues []any for _, tag := range tags { nameValue := strings.Split(tag, "=") if len(nameValue) == 2 { @@ -941,7 +941,7 @@ func (t *Schema) extraKeywords(tags []string) { func (t *Schema) setExtra(key, val string) { if t.Extras == nil { - t.Extras = map[string]interface{}{} + t.Extras = map[string]any{} } if existingVal, ok := t.Extras[key]; ok { switch existingVal := existingVal.(type) { @@ -959,7 +959,7 @@ func (t *Schema) setExtra(key, val string) { case "minimum": t.Extras[key], _ = strconv.Atoi(val) default: - var x interface{} + var x any if val == "true" { x = true } else if val == "false" { diff --git a/reflect_test.go b/reflect_test.go index 3dd622f..53ea6e8 100644 --- a/reflect_test.go +++ b/reflect_test.go @@ -41,7 +41,7 @@ type SomeBaseType struct { someUnexportedUntaggedBaseProperty bool //nolint:unused } -type MapType map[string]interface{} +type MapType map[string]any type ArrayType []string @@ -64,12 +64,12 @@ type TestUser struct { nonExported MapType - ID int `json:"id" jsonschema:"required,minimum=bad,maximum=bad,exclusiveMinimum=bad,exclusiveMaximum=bad,default=bad"` - Name string `json:"name" jsonschema:"required,minLength=1,maxLength=20,pattern=.*,description=this is a property,title=the name,example=joe,example=lucy,default=alex,readOnly=true"` - Password string `json:"password" jsonschema:"writeOnly=true"` - Friends []int `json:"friends,omitempty" jsonschema_description:"list of IDs, omitted when empty"` - Tags map[string]string `json:"tags,omitempty"` - Options map[string]interface{} `json:"options,omitempty"` + ID int `json:"id" jsonschema:"required,minimum=bad,maximum=bad,exclusiveMinimum=bad,exclusiveMaximum=bad,default=bad"` + Name string `json:"name" jsonschema:"required,minLength=1,maxLength=20,pattern=.*,description=this is a property,title=the name,example=joe,example=lucy,default=alex,readOnly=true"` + Password string `json:"password" jsonschema:"writeOnly=true"` + Friends []int `json:"friends,omitempty" jsonschema_description:"list of IDs, omitted when empty"` + Tags map[string]string `json:"tags,omitempty"` + Options map[string]any `json:"options,omitempty"` TestFlag bool TestFlagFalse bool `json:",omitempty" jsonschema:"default=false"` @@ -107,7 +107,7 @@ type TestUser struct { Offsets []float64 `json:"offsets,omitempty" jsonschema:"enum=1.570796,enum=3.141592,enum=6.283185"` // Test for raw JSON - Anything interface{} `json:"anything,omitempty"` + Anything any `json:"anything,omitempty"` Raw json.RawMessage `json:"raw"` } @@ -131,34 +131,34 @@ func (CustomTimeWithInterface) JSONSchema() *Schema { } type RootOneOf struct { - Field1 string `json:"field1" jsonschema:"oneof_required=group1"` - Field2 string `json:"field2" jsonschema:"oneof_required=group2"` - Field3 interface{} `json:"field3" jsonschema:"oneof_type=string;array"` - Field4 string `json:"field4" jsonschema:"oneof_required=group1"` - Field5 ChildOneOf `json:"child"` - Field6 interface{} `json:"field6" jsonschema:"oneof_ref=Outer;OuterNamed;OuterPtr"` + Field1 string `json:"field1" jsonschema:"oneof_required=group1"` + Field2 string `json:"field2" jsonschema:"oneof_required=group2"` + Field3 any `json:"field3" jsonschema:"oneof_type=string;array"` + Field4 string `json:"field4" jsonschema:"oneof_required=group1"` + Field5 ChildOneOf `json:"child"` + Field6 any `json:"field6" jsonschema:"oneof_ref=Outer;OuterNamed;OuterPtr"` } type ChildOneOf struct { - Child1 string `json:"child1" jsonschema:"oneof_required=group1"` - Child2 string `json:"child2" jsonschema:"oneof_required=group2"` - Child3 interface{} `json:"child3" jsonschema:"oneof_required=group2,oneof_type=string;array"` - Child4 string `json:"child4" jsonschema:"oneof_required=group1"` + Child1 string `json:"child1" jsonschema:"oneof_required=group1"` + Child2 string `json:"child2" jsonschema:"oneof_required=group2"` + Child3 any `json:"child3" jsonschema:"oneof_required=group2,oneof_type=string;array"` + Child4 string `json:"child4" jsonschema:"oneof_required=group1"` } type RootAnyOf struct { - Field1 string `json:"field1" jsonschema:"anyof_required=group1"` - Field2 string `json:"field2" jsonschema:"anyof_required=group2"` - Field3 interface{} `json:"field3" jsonschema:"anyof_type=string;array"` - Field4 string `json:"field4" jsonschema:"anyof_required=group1"` - Field5 ChildAnyOf `json:"child"` + Field1 string `json:"field1" jsonschema:"anyof_required=group1"` + Field2 string `json:"field2" jsonschema:"anyof_required=group2"` + Field3 any `json:"field3" jsonschema:"anyof_type=string;array"` + Field4 string `json:"field4" jsonschema:"anyof_required=group1"` + Field5 ChildAnyOf `json:"child"` } type ChildAnyOf struct { - Child1 string `json:"child1" jsonschema:"anyof_required=group1"` - Child2 string `json:"child2" jsonschema:"anyof_required=group2"` - Child3 interface{} `json:"child3" jsonschema:"anyof_required=group2,oneof_type=string;array"` - Child4 string `json:"child4" jsonschema:"anyof_required=group1"` + Child1 string `json:"child1" jsonschema:"anyof_required=group1"` + Child2 string `json:"child2" jsonschema:"anyof_required=group2"` + Child3 any `json:"child3" jsonschema:"anyof_required=group2,oneof_type=string;array"` + Child4 string `json:"child4" jsonschema:"anyof_required=group1"` } type Text string @@ -359,7 +359,7 @@ func TestReflectFromType(t *testing.T) { func TestSchemaGeneration(t *testing.T) { tests := []struct { - typ interface{} + typ any reflector *Reflector fixture string }{ @@ -369,7 +369,7 @@ func TestSchemaGeneration(t *testing.T) { {&TestUser{}, &Reflector{AllowAdditionalProperties: true}, "fixtures/allow_additional_props.json"}, {&TestUser{}, &Reflector{RequiredFromJSONSchemaTags: true}, "fixtures/required_from_jsontags.json"}, {&TestUser{}, &Reflector{ExpandedStruct: true}, "fixtures/defaults_expanded_toplevel.json"}, - {&TestUser{}, &Reflector{IgnoredTypes: []interface{}{GrandfatherType{}}}, "fixtures/ignore_type.json"}, + {&TestUser{}, &Reflector{IgnoredTypes: []any{GrandfatherType{}}}, "fixtures/ignore_type.json"}, {&TestUser{}, &Reflector{DoNotReference: true}, "fixtures/no_reference.json"}, {&TestUser{}, &Reflector{DoNotReference: true, AssignAnchor: true}, "fixtures/no_reference_anchor.json"}, {&RootOneOf{}, &Reflector{RequiredFromJSONSchemaTags: true}, "fixtures/oneof.json"}, @@ -489,7 +489,7 @@ func TestBaselineUnmarshal(t *testing.T) { compareSchemaOutput(t, "fixtures/test_user.json", r, &TestUser{}) } -func compareSchemaOutput(t *testing.T, f string, r *Reflector, obj interface{}) { +func compareSchemaOutput(t *testing.T, f string, r *Reflector, obj any) { t.Helper() expectedJSON, err := os.ReadFile(f) require.NoError(t, err) @@ -567,10 +567,10 @@ func TestFieldNameTag(t *testing.T) { func TestFieldOneOfRef(t *testing.T) { type Server struct { - IPAddress interface{} `json:"ip_address,omitempty" jsonschema:"oneof_ref=#/$defs/ipv4;#/$defs/ipv6"` - IPAddresses []interface{} `json:"ip_addresses,omitempty" jsonschema:"oneof_ref=#/$defs/ipv4;#/$defs/ipv6"` - IPAddressAny interface{} `json:"ip_address_any,omitempty" jsonschema:"anyof_ref=#/$defs/ipv4;#/$defs/ipv6"` - IPAddressesAny []interface{} `json:"ip_addresses_any,omitempty" jsonschema:"anyof_ref=#/$defs/ipv4;#/$defs/ipv6"` + IPAddress any `json:"ip_address,omitempty" jsonschema:"oneof_ref=#/$defs/ipv4;#/$defs/ipv6"` + IPAddresses []any `json:"ip_addresses,omitempty" jsonschema:"oneof_ref=#/$defs/ipv4;#/$defs/ipv6"` + IPAddressAny any `json:"ip_address_any,omitempty" jsonschema:"anyof_ref=#/$defs/ipv4;#/$defs/ipv6"` + IPAddressesAny []any `json:"ip_addresses_any,omitempty" jsonschema:"anyof_ref=#/$defs/ipv4;#/$defs/ipv6"` } r := &Reflector{}