Skip to content

Commit

Permalink
interface{} -> any
Browse files Browse the repository at this point in the history
  • Loading branch information
candiduslynx committed Sep 25, 2023
1 parent b0206aa commit 4e3ccc0
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 61 deletions.
6 changes: 3 additions & 3 deletions examples/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
16 changes: 8 additions & 8 deletions examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
32 changes: 16 additions & 16 deletions reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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))
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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))
}

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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) {
Expand All @@ -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" {
Expand Down
68 changes: 34 additions & 34 deletions reflect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type SomeBaseType struct {
someUnexportedUntaggedBaseProperty bool //nolint:unused
}

type MapType map[string]interface{}
type MapType map[string]any

type ArrayType []string

Expand All @@ -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"`
Expand Down Expand Up @@ -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"`
}

Expand All @@ -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
Expand Down Expand Up @@ -359,7 +359,7 @@ func TestReflectFromType(t *testing.T) {

func TestSchemaGeneration(t *testing.T) {
tests := []struct {
typ interface{}
typ any
reflector *Reflector
fixture string
}{
Expand All @@ -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"},
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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{}
Expand Down

0 comments on commit 4e3ccc0

Please sign in to comment.