diff --git a/pkg/codegen/model.go b/pkg/codegen/model.go index 53b7450d..49f75fda 100644 --- a/pkg/codegen/model.go +++ b/pkg/codegen/model.go @@ -1,6 +1,7 @@ package codegen import ( + "sort" "strings" "github.com/sanity-io/litter" @@ -10,6 +11,11 @@ type Decl interface { Generate(out *Emitter) } +type Named interface { + Decl + GetName() string +} + type File struct { FileName string Package Package @@ -68,7 +74,18 @@ func (p *Package) Generate(out *Emitter) { } } out.Newline() - for i, t := range p.Decls { + + sorted := make([]Decl, len(p.Decls)) + copy(sorted, p.Decls) + sort.Slice(sorted, func(i, j int) bool { + if a, ok := sorted[i].(Named); ok { + if b, ok := sorted[j].(Named); ok { + return a.GetName() < b.GetName() + } + } + return false + }) + for i, t := range sorted { if i > 0 { out.Newline() } @@ -83,6 +100,10 @@ type Var struct { Value interface{} } +func (v *Var) GetName() string { + return v.Name +} + func (v *Var) Generate(out *Emitter) { out.Print("var %s ", v.Name) if v.Type != nil { @@ -98,6 +119,10 @@ type Constant struct { Value interface{} } +func (c *Constant) GetName() string { + return c.Name +} + func (c *Constant) Generate(out *Emitter) { out.Print("const %s ", c.Name) if c.Type != nil { @@ -145,6 +170,10 @@ type TypeDecl struct { Comment string } +func (td *TypeDecl) GetName() string { + return td.Name +} + func (td *TypeDecl) Generate(out *Emitter) { out.Comment(td.Comment) out.Print("type %s ", td.Name) @@ -184,8 +213,12 @@ type NamedType struct { Decl *TypeDecl } +func (t NamedType) GetName() string { + return t.Decl.Name +} + func (t NamedType) IsNillable() bool { - return t.Decl.Type.IsNillable() + return t.Decl.Type != nil && t.Decl.Type.IsNillable() } func (t NamedType) Generate(out *Emitter) { @@ -263,6 +296,10 @@ type StructField struct { DefaultValue interface{} } +func (f *StructField) GetName() string { + return f.Name +} + func (f *StructField) Generate(out *Emitter) { out.Comment(f.Comment) out.Print("%s ", f.Name) diff --git a/pkg/generator/generate.go b/pkg/generator/generate.go index 93b39939..acbf83e9 100644 --- a/pkg/generator/generate.go +++ b/pkg/generator/generate.go @@ -255,14 +255,14 @@ func (g *schemaGenerator) generateRootType() error { return errors.New("schema has no root") } - if len(g.schema.Type.Type) == 0 { - for _, name := range sortDefinitionsByName(g.schema.Definitions) { - def := g.schema.Definitions[name] - _, err := g.generateDeclaredType(def, newNameScope(g.identifierize(name))) - if err != nil { - return err - } + for _, name := range sortDefinitionsByName(g.schema.Definitions) { + def := g.schema.Definitions[name] + _, err := g.generateDeclaredType(def, newNameScope(g.identifierize(name))) + if err != nil { + return err } + } + if len(g.schema.Type.Type) == 0 { return nil } diff --git a/tests/data/core/4.2.1_array.go.output b/tests/data/core/4.2.1_array.go.output index 11a3337a..cee1d6fa 100644 --- a/tests/data/core/4.2.1_array.go.output +++ b/tests/data/core/4.2.1_array.go.output @@ -2,8 +2,6 @@ package test -type A421ArrayMyObjectArrayElem map[string]interface{} - type A421Array struct { // MyBooleanArray corresponds to the JSON schema field "myBooleanArray". MyBooleanArray []bool `json:"myBooleanArray,omitempty"` @@ -23,3 +21,5 @@ type A421Array struct { // MyStringArray corresponds to the JSON schema field "myStringArray". MyStringArray []string `json:"myStringArray,omitempty"` } + +type A421ArrayMyObjectArrayElem map[string]interface{} diff --git a/tests/data/core/objectEmpty.go.output b/tests/data/core/objectEmpty.go.output index 3f85e525..df63ae27 100644 --- a/tests/data/core/objectEmpty.go.output +++ b/tests/data/core/objectEmpty.go.output @@ -2,9 +2,9 @@ package test -type ObjectEmptyFoo map[string]interface{} - type ObjectEmpty struct { // Foo corresponds to the JSON schema field "foo". Foo ObjectEmptyFoo `json:"foo,omitempty"` } + +type ObjectEmptyFoo map[string]interface{} diff --git a/tests/data/core/objectNested.go.output b/tests/data/core/objectNested.go.output index b33a19c4..fbf24ac2 100644 --- a/tests/data/core/objectNested.go.output +++ b/tests/data/core/objectNested.go.output @@ -2,9 +2,9 @@ package test -type ObjectNestedMyObjectMyObject struct { - // MyString corresponds to the JSON schema field "myString". - MyString *string `json:"myString,omitempty"` +type ObjectNested struct { + // MyObject corresponds to the JSON schema field "myObject". + MyObject *ObjectNestedMyObject `json:"myObject,omitempty"` } type ObjectNestedMyObject struct { @@ -12,7 +12,7 @@ type ObjectNestedMyObject struct { MyObject *ObjectNestedMyObjectMyObject `json:"myObject,omitempty"` } -type ObjectNested struct { - // MyObject corresponds to the JSON schema field "myObject". - MyObject *ObjectNestedMyObject `json:"myObject,omitempty"` +type ObjectNestedMyObjectMyObject struct { + // MyString corresponds to the JSON schema field "myString". + MyString *string `json:"myString,omitempty"` } diff --git a/tests/data/core/ref.go.output b/tests/data/core/ref.go.output index f65341f0..55856be7 100644 --- a/tests/data/core/ref.go.output +++ b/tests/data/core/ref.go.output @@ -2,11 +2,6 @@ package test -type Thing struct { - // Name corresponds to the JSON schema field "name". - Name *string `json:"name,omitempty"` -} - type Ref struct { // MyThing corresponds to the JSON schema field "myThing". MyThing *Thing `json:"myThing,omitempty"` @@ -14,3 +9,8 @@ type Ref struct { // MyThing2 corresponds to the JSON schema field "myThing2". MyThing2 *Thing `json:"myThing2,omitempty"` } + +type Thing struct { + // Name corresponds to the JSON schema field "name". + Name *string `json:"name,omitempty"` +} diff --git a/tests/data/core/refExternalFile.go.output b/tests/data/core/refExternalFile.go.output index 6d6873ca..86aa3118 100644 --- a/tests/data/core/refExternalFile.go.output +++ b/tests/data/core/refExternalFile.go.output @@ -2,11 +2,6 @@ package test -type Thing struct { - // Name corresponds to the JSON schema field "name". - Name *string `json:"name,omitempty"` -} - type Ref struct { // MyThing corresponds to the JSON schema field "myThing". MyThing *Thing `json:"myThing,omitempty"` @@ -23,3 +18,8 @@ type RefExternalFile struct { // "someOtherExternalThing". SomeOtherExternalThing *Thing `json:"someOtherExternalThing,omitempty"` } + +type Thing struct { + // Name corresponds to the JSON schema field "name". + Name *string `json:"name,omitempty"` +} diff --git a/tests/data/core/refExternalFileWithDupe.go.output b/tests/data/core/refExternalFileWithDupe.go.output index 29896910..07237aad 100644 --- a/tests/data/core/refExternalFileWithDupe.go.output +++ b/tests/data/core/refExternalFileWithDupe.go.output @@ -2,28 +2,28 @@ package test -type Thing struct { - // Name corresponds to the JSON schema field "name". - Name *string `json:"name,omitempty"` -} - type Ref struct { // MyThing corresponds to the JSON schema field "myThing". - MyThing *Thing `json:"myThing,omitempty"` + MyThing *Thing_1 `json:"myThing,omitempty"` // MyThing2 corresponds to the JSON schema field "myThing2". - MyThing2 *Thing `json:"myThing2,omitempty"` -} - -type Thing_1 struct { - // Something corresponds to the JSON schema field "something". - Something *string `json:"something,omitempty"` + MyThing2 *Thing_1 `json:"myThing2,omitempty"` } type RefExternalFileWithDupe struct { // MyExternalThing corresponds to the JSON schema field "myExternalThing". - MyExternalThing *Thing `json:"myExternalThing,omitempty"` + MyExternalThing *Thing_1 `json:"myExternalThing,omitempty"` // MyThing corresponds to the JSON schema field "myThing". - MyThing *Thing_1 `json:"myThing,omitempty"` + MyThing *Thing `json:"myThing,omitempty"` +} + +type Thing struct { + // Something corresponds to the JSON schema field "something". + Something *string `json:"something,omitempty"` +} + +type Thing_1 struct { + // Name corresponds to the JSON schema field "name". + Name *string `json:"name,omitempty"` } diff --git a/tests/data/core/refToEnum.go.output b/tests/data/core/refToEnum.go.output index 2f3867bc..19442c1c 100644 --- a/tests/data/core/refToEnum.go.output +++ b/tests/data/core/refToEnum.go.output @@ -33,10 +33,40 @@ func (j *Thing) UnmarshalJSON(b []byte) error { return nil } -const ThingX Thing = "x" -const ThingY Thing = "y" - type RefToEnum struct { // MyThing corresponds to the JSON schema field "myThing". - MyThing *Thing `json:"myThing,omitempty"` + MyThing *Thing_1 `json:"myThing,omitempty"` +} + +const ThingX Thing = "x" + +type Thing_1 string + +var enumValues_Thing_1 = []interface{}{ + "x", + "y", +} + +// UnmarshalJSON implements json.Unmarshaler. +func (j *Thing_1) UnmarshalJSON(b []byte) error { + var v string + if err := json.Unmarshal(b, &v); err != nil { + return err + } + var ok bool + for _, expected := range enumValues_Thing_1 { + if reflect.DeepEqual(v, expected) { + ok = true + break + } + } + if !ok { + return fmt.Errorf("invalid value (expected one of %#v): %#v", enumValues_Thing_1, v) + } + *j = Thing_1(v) + return nil } + +const ThingY Thing = "y" +const Thing_1_X Thing_1 = "x" +const Thing_1_Y Thing_1 = "y" diff --git a/tests/data/core/refToPrimitiveString.go.output b/tests/data/core/refToPrimitiveString.go.output index 1c385ab2..fce8f182 100644 --- a/tests/data/core/refToPrimitiveString.go.output +++ b/tests/data/core/refToPrimitiveString.go.output @@ -2,9 +2,9 @@ package test -type Thing string - type RefToPrimitiveString struct { // MyThing corresponds to the JSON schema field "myThing". MyThing *Thing `json:"myThing,omitempty"` } + +type Thing string diff --git a/tests/data/crossPackage/schema.go.output b/tests/data/crossPackage/schema.go.output index 72df6755..0221775e 100644 --- a/tests/data/crossPackage/schema.go.output +++ b/tests/data/crossPackage/schema.go.output @@ -4,11 +4,6 @@ package schema import other "github.com/example/other" -type Thing struct { - // S corresponds to the JSON schema field "s". - S *string `json:"s,omitempty"` -} - type Schema struct { // DefInOtherSchema corresponds to the JSON schema field "defInOtherSchema". DefInOtherSchema *other.Thing `json:"defInOtherSchema,omitempty"` @@ -16,3 +11,8 @@ type Schema struct { // DefInSameSchema corresponds to the JSON schema field "defInSameSchema". DefInSameSchema *Thing `json:"defInSameSchema,omitempty"` } + +type Thing struct { + // S corresponds to the JSON schema field "s". + S *string `json:"s,omitempty"` +} diff --git a/tests/data/crossPackageNoOutput/schema.go.output b/tests/data/crossPackageNoOutput/schema.go.output index 72df6755..0221775e 100644 --- a/tests/data/crossPackageNoOutput/schema.go.output +++ b/tests/data/crossPackageNoOutput/schema.go.output @@ -4,11 +4,6 @@ package schema import other "github.com/example/other" -type Thing struct { - // S corresponds to the JSON schema field "s". - S *string `json:"s,omitempty"` -} - type Schema struct { // DefInOtherSchema corresponds to the JSON schema field "defInOtherSchema". DefInOtherSchema *other.Thing `json:"defInOtherSchema,omitempty"` @@ -16,3 +11,8 @@ type Schema struct { // DefInSameSchema corresponds to the JSON schema field "defInSameSchema". DefInSameSchema *Thing `json:"defInSameSchema,omitempty"` } + +type Thing struct { + // S corresponds to the JSON schema field "s". + S *string `json:"s,omitempty"` +} diff --git a/tests/data/miscWithDefaults/cyclic.go.output b/tests/data/miscWithDefaults/cyclic.go.output index 8b2e4d8e..63a383ec 100644 --- a/tests/data/miscWithDefaults/cyclic.go.output +++ b/tests/data/miscWithDefaults/cyclic.go.output @@ -7,12 +7,12 @@ type Bar struct { RefToFoo *Foo `json:"refToFoo,omitempty"` } -type Foo struct { - // RefToBar corresponds to the JSON schema field "refToBar". - RefToBar *Bar `json:"refToBar,omitempty"` -} - type Cyclic struct { // A corresponds to the JSON schema field "a". A *Foo `json:"a,omitempty"` } + +type Foo struct { + // RefToBar corresponds to the JSON schema field "refToBar". + RefToBar *Bar `json:"refToBar,omitempty"` +} diff --git a/tests/data/miscWithDefaults/cyclicAndRequired1.go.output b/tests/data/miscWithDefaults/cyclicAndRequired1.go.output index 437d5408..e0c924dc 100644 --- a/tests/data/miscWithDefaults/cyclicAndRequired1.go.output +++ b/tests/data/miscWithDefaults/cyclicAndRequired1.go.output @@ -5,11 +5,6 @@ package test import "fmt" import "encoding/json" -type Bar struct { - // RefToFoo corresponds to the JSON schema field "refToFoo". - RefToFoo *Foo `json:"refToFoo,omitempty"` -} - type Foo struct { // RefToBar corresponds to the JSON schema field "refToBar". RefToBar Bar `json:"refToBar"` @@ -33,6 +28,11 @@ func (j *Foo) UnmarshalJSON(b []byte) error { return nil } +type Bar struct { + // RefToFoo corresponds to the JSON schema field "refToFoo". + RefToFoo *Foo `json:"refToFoo,omitempty"` +} + type CyclicAndRequired1 struct { // A corresponds to the JSON schema field "a". A *Foo `json:"a,omitempty"` diff --git a/tests/data/miscWithDefaults/cyclicAndRequired2.go.output b/tests/data/miscWithDefaults/cyclicAndRequired2.go.output index c8fe5a4a..261dbf96 100644 --- a/tests/data/miscWithDefaults/cyclicAndRequired2.go.output +++ b/tests/data/miscWithDefaults/cyclicAndRequired2.go.output @@ -5,49 +5,49 @@ package test import "fmt" import "encoding/json" -type Bar struct { - // RefToFoo corresponds to the JSON schema field "refToFoo". - RefToFoo *Foo `json:"refToFoo"` +type Foo struct { + // RefToBar corresponds to the JSON schema field "refToBar". + RefToBar Bar `json:"refToBar"` } // UnmarshalJSON implements json.Unmarshaler. -func (j *Bar) UnmarshalJSON(b []byte) error { +func (j *Foo) UnmarshalJSON(b []byte) error { var raw map[string]interface{} if err := json.Unmarshal(b, &raw); err != nil { return err } - if v, ok := raw["refToFoo"]; !ok || v == nil { - return fmt.Errorf("field refToFoo: required") + if v, ok := raw["refToBar"]; !ok || v == nil { + return fmt.Errorf("field refToBar: required") } - type Plain Bar + type Plain Foo var plain Plain if err := json.Unmarshal(b, &plain); err != nil { return err } - *j = Bar(plain) + *j = Foo(plain) return nil } -type Foo struct { - // RefToBar corresponds to the JSON schema field "refToBar". - RefToBar Bar `json:"refToBar"` +type Bar struct { + // RefToFoo corresponds to the JSON schema field "refToFoo". + RefToFoo Foo `json:"refToFoo"` } // UnmarshalJSON implements json.Unmarshaler. -func (j *Foo) UnmarshalJSON(b []byte) error { +func (j *Bar) UnmarshalJSON(b []byte) error { var raw map[string]interface{} if err := json.Unmarshal(b, &raw); err != nil { return err } - if v, ok := raw["refToBar"]; !ok || v == nil { - return fmt.Errorf("field refToBar: required") + if v, ok := raw["refToFoo"]; !ok || v == nil { + return fmt.Errorf("field refToFoo: required") } - type Plain Foo + type Plain Bar var plain Plain if err := json.Unmarshal(b, &plain); err != nil { return err } - *j = Foo(plain) + *j = Bar(plain) return nil } diff --git a/tests/data/validation/6.1.2_enum.go.output b/tests/data/validation/6.1.2_enum.go.output index c04e5c36..f321cdee 100644 --- a/tests/data/validation/6.1.2_enum.go.output +++ b/tests/data/validation/6.1.2_enum.go.output @@ -6,77 +6,159 @@ import "fmt" import "reflect" import "encoding/json" +type A612Enum struct { + // MyBooleanTypedEnum corresponds to the JSON schema field "myBooleanTypedEnum". + MyBooleanTypedEnum *A612EnumMyBooleanTypedEnum `json:"myBooleanTypedEnum,omitempty"` + + // MyBooleanUntypedEnum corresponds to the JSON schema field + // "myBooleanUntypedEnum". + MyBooleanUntypedEnum *A612EnumMyBooleanUntypedEnum `json:"myBooleanUntypedEnum,omitempty"` + + // MyIntegerTypedEnum corresponds to the JSON schema field "myIntegerTypedEnum". + MyIntegerTypedEnum *A612EnumMyIntegerTypedEnum `json:"myIntegerTypedEnum,omitempty"` + + // MyMixedTypeEnum corresponds to the JSON schema field "myMixedTypeEnum". + MyMixedTypeEnum *A612EnumMyMixedTypeEnum `json:"myMixedTypeEnum,omitempty"` + + // MyMixedUntypedEnum corresponds to the JSON schema field "myMixedUntypedEnum". + MyMixedUntypedEnum *A612EnumMyMixedUntypedEnum `json:"myMixedUntypedEnum,omitempty"` + + // MyNullTypedEnum corresponds to the JSON schema field "myNullTypedEnum". + MyNullTypedEnum *A612EnumMyNullTypedEnum `json:"myNullTypedEnum,omitempty"` + + // MyNullUntypedEnum corresponds to the JSON schema field "myNullUntypedEnum". + MyNullUntypedEnum *A612EnumMyNullUntypedEnum `json:"myNullUntypedEnum,omitempty"` + + // MyNumberTypedEnum corresponds to the JSON schema field "myNumberTypedEnum". + MyNumberTypedEnum *A612EnumMyNumberTypedEnum `json:"myNumberTypedEnum,omitempty"` + + // MyNumberUntypedEnum corresponds to the JSON schema field "myNumberUntypedEnum". + MyNumberUntypedEnum *A612EnumMyNumberUntypedEnum `json:"myNumberUntypedEnum,omitempty"` + + // MyStringTypedEnum corresponds to the JSON schema field "myStringTypedEnum". + MyStringTypedEnum *A612EnumMyStringTypedEnum `json:"myStringTypedEnum,omitempty"` + + // MyStringUntypedEnum corresponds to the JSON schema field "myStringUntypedEnum". + MyStringUntypedEnum *A612EnumMyStringUntypedEnum `json:"myStringUntypedEnum,omitempty"` +} + type A612EnumMyBooleanTypedEnum bool +type A612EnumMyBooleanUntypedEnum bool + +type A612EnumMyIntegerTypedEnum int + +type A612EnumMyMixedTypeEnum struct { + Value interface{} +} + +type A612EnumMyMixedUntypedEnum struct { + Value interface{} +} + +type A612EnumMyNullTypedEnum struct { + Value interface{} +} + +type A612EnumMyNullUntypedEnum struct { + Value interface{} +} + +type A612EnumMyNumberTypedEnum float64 + +type A612EnumMyNumberUntypedEnum float64 + +type A612EnumMyStringTypedEnum string + +const A612EnumMyStringTypedEnumBlue A612EnumMyStringTypedEnum = "blue" +const A612EnumMyStringTypedEnumGreen A612EnumMyStringTypedEnum = "green" +const A612EnumMyStringTypedEnumRed A612EnumMyStringTypedEnum = "red" + +type A612EnumMyStringUntypedEnum string + +const A612EnumMyStringUntypedEnumBlue A612EnumMyStringUntypedEnum = "blue" +const A612EnumMyStringUntypedEnumGreen A612EnumMyStringUntypedEnum = "green" +const A612EnumMyStringUntypedEnumRed A612EnumMyStringUntypedEnum = "red" + var enumValues_A612EnumMyBooleanTypedEnum = []interface{}{ true, false, } +var enumValues_A612EnumMyBooleanUntypedEnum = []interface{}{ + true, + false, +} +var enumValues_A612EnumMyIntegerTypedEnum = []interface{}{ + 1, + 2, + 3, +} +var enumValues_A612EnumMyMixedTypeEnum = []interface{}{ + 42, + "smurf", +} +var enumValues_A612EnumMyMixedUntypedEnum = []interface{}{ + "red", + 1, + true, + nil, +} +var enumValues_A612EnumMyNullTypedEnum = []interface{}{ + nil, +} +var enumValues_A612EnumMyNullUntypedEnum = []interface{}{ + nil, +} // UnmarshalJSON implements json.Unmarshaler. -func (j *A612EnumMyBooleanTypedEnum) UnmarshalJSON(b []byte) error { - var v bool - if err := json.Unmarshal(b, &v); err != nil { +func (j *A612EnumMyNullUntypedEnum) UnmarshalJSON(b []byte) error { + var v struct { + Value interface{} + } + if err := json.Unmarshal(b, &v.Value); err != nil { return err } var ok bool - for _, expected := range enumValues_A612EnumMyBooleanTypedEnum { - if reflect.DeepEqual(v, expected) { + for _, expected := range enumValues_A612EnumMyNullUntypedEnum { + if reflect.DeepEqual(v.Value, expected) { ok = true break } } if !ok { - return fmt.Errorf("invalid value (expected one of %#v): %#v", enumValues_A612EnumMyBooleanTypedEnum, v) + return fmt.Errorf("invalid value (expected one of %#v): %#v", enumValues_A612EnumMyNullUntypedEnum, v.Value) } - *j = A612EnumMyBooleanTypedEnum(v) + *j = A612EnumMyNullUntypedEnum(v) return nil } -type A612EnumMyIntegerTypedEnum int - -var enumValues_A612EnumMyIntegerTypedEnum = []interface{}{ - 1, - 2, - 3, +// MarshalJSON implements json.Marshaler. +func (j *A612EnumMyNullUntypedEnum) MarshalJSON() ([]byte, error) { + return json.Marshal(j.Value) } // UnmarshalJSON implements json.Unmarshaler. -func (j *A612EnumMyIntegerTypedEnum) UnmarshalJSON(b []byte) error { - var v int +func (j *A612EnumMyNumberTypedEnum) UnmarshalJSON(b []byte) error { + var v float64 if err := json.Unmarshal(b, &v); err != nil { return err } var ok bool - for _, expected := range enumValues_A612EnumMyIntegerTypedEnum { + for _, expected := range enumValues_A612EnumMyNumberTypedEnum { if reflect.DeepEqual(v, expected) { ok = true break } } if !ok { - return fmt.Errorf("invalid value (expected one of %#v): %#v", enumValues_A612EnumMyIntegerTypedEnum, v) + return fmt.Errorf("invalid value (expected one of %#v): %#v", enumValues_A612EnumMyNumberTypedEnum, v) } - *j = A612EnumMyIntegerTypedEnum(v) + *j = A612EnumMyNumberTypedEnum(v) return nil } -type A612EnumMyMixedTypeEnum struct { - Value interface{} -} - -var enumValues_A612EnumMyMixedTypeEnum = []interface{}{ - 42, - "smurf", -} - -// MarshalJSON implements json.Marshaler. -func (j *A612EnumMyMixedTypeEnum) MarshalJSON() ([]byte, error) { - return json.Marshal(j.Value) -} - // UnmarshalJSON implements json.Unmarshaler. -func (j *A612EnumMyMixedTypeEnum) UnmarshalJSON(b []byte) error { +func (j *A612EnumMyNullTypedEnum) UnmarshalJSON(b []byte) error { var v struct { Value interface{} } @@ -84,35 +166,44 @@ func (j *A612EnumMyMixedTypeEnum) UnmarshalJSON(b []byte) error { return err } var ok bool - for _, expected := range enumValues_A612EnumMyMixedTypeEnum { + for _, expected := range enumValues_A612EnumMyNullTypedEnum { if reflect.DeepEqual(v.Value, expected) { ok = true break } } if !ok { - return fmt.Errorf("invalid value (expected one of %#v): %#v", enumValues_A612EnumMyMixedTypeEnum, v.Value) + return fmt.Errorf("invalid value (expected one of %#v): %#v", enumValues_A612EnumMyNullTypedEnum, v.Value) } - *j = A612EnumMyMixedTypeEnum(v) + *j = A612EnumMyNullTypedEnum(v) return nil } -type A612EnumMyMixedUntypedEnum struct { - Value interface{} -} - -var enumValues_A612EnumMyMixedUntypedEnum = []interface{}{ - "red", - 1, - true, - nil, -} - // MarshalJSON implements json.Marshaler. -func (j *A612EnumMyMixedUntypedEnum) MarshalJSON() ([]byte, error) { +func (j *A612EnumMyNullTypedEnum) MarshalJSON() ([]byte, error) { return json.Marshal(j.Value) } +// UnmarshalJSON implements json.Unmarshaler. +func (j *A612EnumMyNumberUntypedEnum) UnmarshalJSON(b []byte) error { + var v float64 + if err := json.Unmarshal(b, &v); err != nil { + return err + } + var ok bool + for _, expected := range enumValues_A612EnumMyNumberUntypedEnum { + if reflect.DeepEqual(v, expected) { + ok = true + break + } + } + if !ok { + return fmt.Errorf("invalid value (expected one of %#v): %#v", enumValues_A612EnumMyNumberUntypedEnum, v) + } + *j = A612EnumMyNumberUntypedEnum(v) + return nil +} + // UnmarshalJSON implements json.Unmarshaler. func (j *A612EnumMyMixedUntypedEnum) UnmarshalJSON(b []byte) error { var v struct { @@ -135,21 +226,33 @@ func (j *A612EnumMyMixedUntypedEnum) UnmarshalJSON(b []byte) error { return nil } -type A612EnumMyNullTypedEnum struct { - Value interface{} -} - -var enumValues_A612EnumMyNullTypedEnum = []interface{}{ - nil, -} - // MarshalJSON implements json.Marshaler. -func (j *A612EnumMyNullTypedEnum) MarshalJSON() ([]byte, error) { +func (j *A612EnumMyMixedUntypedEnum) MarshalJSON() ([]byte, error) { return json.Marshal(j.Value) } // UnmarshalJSON implements json.Unmarshaler. -func (j *A612EnumMyNullTypedEnum) UnmarshalJSON(b []byte) error { +func (j *A612EnumMyStringTypedEnum) UnmarshalJSON(b []byte) error { + var v string + if err := json.Unmarshal(b, &v); err != nil { + return err + } + var ok bool + for _, expected := range enumValues_A612EnumMyStringTypedEnum { + if reflect.DeepEqual(v, expected) { + ok = true + break + } + } + if !ok { + return fmt.Errorf("invalid value (expected one of %#v): %#v", enumValues_A612EnumMyStringTypedEnum, v) + } + *j = A612EnumMyStringTypedEnum(v) + return nil +} + +// UnmarshalJSON implements json.Unmarshaler. +func (j *A612EnumMyMixedTypeEnum) UnmarshalJSON(b []byte) error { var v struct { Value interface{} } @@ -157,83 +260,121 @@ func (j *A612EnumMyNullTypedEnum) UnmarshalJSON(b []byte) error { return err } var ok bool - for _, expected := range enumValues_A612EnumMyNullTypedEnum { + for _, expected := range enumValues_A612EnumMyMixedTypeEnum { if reflect.DeepEqual(v.Value, expected) { ok = true break } } if !ok { - return fmt.Errorf("invalid value (expected one of %#v): %#v", enumValues_A612EnumMyNullTypedEnum, v.Value) + return fmt.Errorf("invalid value (expected one of %#v): %#v", enumValues_A612EnumMyMixedTypeEnum, v.Value) } - *j = A612EnumMyNullTypedEnum(v) + *j = A612EnumMyMixedTypeEnum(v) return nil } -type A612EnumMyStringTypedEnum string - -var enumValues_A612EnumMyStringTypedEnum = []interface{}{ - "red", - "blue", - "green", +// MarshalJSON implements json.Marshaler. +func (j *A612EnumMyMixedTypeEnum) MarshalJSON() ([]byte, error) { + return json.Marshal(j.Value) } // UnmarshalJSON implements json.Unmarshaler. -func (j *A612EnumMyStringTypedEnum) UnmarshalJSON(b []byte) error { - var v string +func (j *A612EnumMyIntegerTypedEnum) UnmarshalJSON(b []byte) error { + var v int if err := json.Unmarshal(b, &v); err != nil { return err } var ok bool - for _, expected := range enumValues_A612EnumMyStringTypedEnum { + for _, expected := range enumValues_A612EnumMyIntegerTypedEnum { if reflect.DeepEqual(v, expected) { ok = true break } } if !ok { - return fmt.Errorf("invalid value (expected one of %#v): %#v", enumValues_A612EnumMyStringTypedEnum, v) + return fmt.Errorf("invalid value (expected one of %#v): %#v", enumValues_A612EnumMyIntegerTypedEnum, v) } - *j = A612EnumMyStringTypedEnum(v) + *j = A612EnumMyIntegerTypedEnum(v) return nil } -const A612EnumMyStringTypedEnumRed A612EnumMyStringTypedEnum = "red" -const A612EnumMyStringTypedEnumBlue A612EnumMyStringTypedEnum = "blue" -const A612EnumMyStringTypedEnumGreen A612EnumMyStringTypedEnum = "green" - -type A612Enum struct { - // MyBooleanTypedEnum corresponds to the JSON schema field "myBooleanTypedEnum". - MyBooleanTypedEnum *A612EnumMyBooleanTypedEnum `json:"myBooleanTypedEnum,omitempty"` - - // MyBooleanUntypedEnum corresponds to the JSON schema field - // "myBooleanUntypedEnum". - MyBooleanUntypedEnum *A612EnumMyBooleanTypedEnum `json:"myBooleanUntypedEnum,omitempty"` - - // MyIntegerTypedEnum corresponds to the JSON schema field "myIntegerTypedEnum". - MyIntegerTypedEnum *A612EnumMyIntegerTypedEnum `json:"myIntegerTypedEnum,omitempty"` - - // MyMixedTypeEnum corresponds to the JSON schema field "myMixedTypeEnum". - MyMixedTypeEnum *A612EnumMyMixedTypeEnum `json:"myMixedTypeEnum,omitempty"` - - // MyMixedUntypedEnum corresponds to the JSON schema field "myMixedUntypedEnum". - MyMixedUntypedEnum *A612EnumMyMixedUntypedEnum `json:"myMixedUntypedEnum,omitempty"` - - // MyNullTypedEnum corresponds to the JSON schema field "myNullTypedEnum". - MyNullTypedEnum *A612EnumMyNullTypedEnum `json:"myNullTypedEnum,omitempty"` - - // MyNullUntypedEnum corresponds to the JSON schema field "myNullUntypedEnum". - MyNullUntypedEnum *A612EnumMyNullTypedEnum `json:"myNullUntypedEnum,omitempty"` - - // MyNumberTypedEnum corresponds to the JSON schema field "myNumberTypedEnum". - MyNumberTypedEnum *A612EnumMyIntegerTypedEnum `json:"myNumberTypedEnum,omitempty"` +// UnmarshalJSON implements json.Unmarshaler. +func (j *A612EnumMyBooleanUntypedEnum) UnmarshalJSON(b []byte) error { + var v bool + if err := json.Unmarshal(b, &v); err != nil { + return err + } + var ok bool + for _, expected := range enumValues_A612EnumMyBooleanUntypedEnum { + if reflect.DeepEqual(v, expected) { + ok = true + break + } + } + if !ok { + return fmt.Errorf("invalid value (expected one of %#v): %#v", enumValues_A612EnumMyBooleanUntypedEnum, v) + } + *j = A612EnumMyBooleanUntypedEnum(v) + return nil +} - // MyNumberUntypedEnum corresponds to the JSON schema field "myNumberUntypedEnum". - MyNumberUntypedEnum *A612EnumMyIntegerTypedEnum `json:"myNumberUntypedEnum,omitempty"` +// UnmarshalJSON implements json.Unmarshaler. +func (j *A612EnumMyBooleanTypedEnum) UnmarshalJSON(b []byte) error { + var v bool + if err := json.Unmarshal(b, &v); err != nil { + return err + } + var ok bool + for _, expected := range enumValues_A612EnumMyBooleanTypedEnum { + if reflect.DeepEqual(v, expected) { + ok = true + break + } + } + if !ok { + return fmt.Errorf("invalid value (expected one of %#v): %#v", enumValues_A612EnumMyBooleanTypedEnum, v) + } + *j = A612EnumMyBooleanTypedEnum(v) + return nil +} - // MyStringTypedEnum corresponds to the JSON schema field "myStringTypedEnum". - MyStringTypedEnum *A612EnumMyStringTypedEnum `json:"myStringTypedEnum,omitempty"` +// UnmarshalJSON implements json.Unmarshaler. +func (j *A612EnumMyStringUntypedEnum) UnmarshalJSON(b []byte) error { + var v string + if err := json.Unmarshal(b, &v); err != nil { + return err + } + var ok bool + for _, expected := range enumValues_A612EnumMyStringUntypedEnum { + if reflect.DeepEqual(v, expected) { + ok = true + break + } + } + if !ok { + return fmt.Errorf("invalid value (expected one of %#v): %#v", enumValues_A612EnumMyStringUntypedEnum, v) + } + *j = A612EnumMyStringUntypedEnum(v) + return nil +} - // MyStringUntypedEnum corresponds to the JSON schema field "myStringUntypedEnum". - MyStringUntypedEnum *A612EnumMyStringTypedEnum `json:"myStringUntypedEnum,omitempty"` +var enumValues_A612EnumMyNumberTypedEnum = []interface{}{ + 1, + 2, + 3, +} +var enumValues_A612EnumMyNumberUntypedEnum = []interface{}{ + 1, + 2, + 3, +} +var enumValues_A612EnumMyStringTypedEnum = []interface{}{ + "red", + "blue", + "green", +} +var enumValues_A612EnumMyStringUntypedEnum = []interface{}{ + "red", + "blue", + "green", }