From 0b174d5950f46f5c40a287619e52bdad1a3df24a Mon Sep 17 00:00:00 2001 From: RobQuistNL Date: Thu, 16 Nov 2023 17:14:55 +0100 Subject: [PATCH 1/9] Add some more names to anonymous Method classes for stable output --- pkg/codegen/model.go | 10 ++++++++++ pkg/generator/schema_generator.go | 3 +++ 2 files changed, 13 insertions(+) diff --git a/pkg/codegen/model.go b/pkg/codegen/model.go index 0229c7c1..5e85870f 100644 --- a/pkg/codegen/model.go +++ b/pkg/codegen/model.go @@ -1,6 +1,7 @@ package codegen import ( + "log" "sort" "strings" @@ -107,6 +108,10 @@ func (p *Package) Generate(out *Emitter) { return false }) + for i, decl := range sorted { + log.Printf("Sorted: %+v %+v", i, decl) + } + for i, t := range sorted { if i > 0 { out.Newline() @@ -168,6 +173,11 @@ func (f Fragment) Generate(out *Emitter) { // Method defines a method and how to generate it. type Method struct { Impl func(*Emitter) + Name string +} + +func (m *Method) GetName() string { + return m.Name } func (m *Method) Generate(out *Emitter) { diff --git a/pkg/generator/schema_generator.go b/pkg/generator/schema_generator.go index 9a1072ac..b3b66ffb 100644 --- a/pkg/generator/schema_generator.go +++ b/pkg/generator/schema_generator.go @@ -279,6 +279,7 @@ func (g *schemaGenerator) generateDeclaredType( g.output.file.Package.AddDecl(&codegen.Method{ Impl: formatter.generate(decl, validators), + Name: "validator_" + decl.GetName(), }) } } @@ -764,11 +765,13 @@ func (g *schemaGenerator) generateEnumType( if wrapInStruct { g.output.file.Package.AddDecl(&codegen.Method{ Impl: formatter.enumMarshal(enumDecl), + Name: "enum_" + enumDecl.GetName(), }) } g.output.file.Package.AddDecl(&codegen.Method{ Impl: formatter.enumUnmarshal(enumDecl, enumType, valueConstant, wrapInStruct), + Name: "enum_" + enumDecl.GetName() + "_unmarshal", }) } } From 96737c890f506b5f8c098a905b99a8ed7e3f4e6b Mon Sep 17 00:00:00 2001 From: RobQuistNL Date: Thu, 16 Nov 2023 17:18:56 +0100 Subject: [PATCH 2/9] Remove debug output --- pkg/codegen/model.go | 5 ----- 1 file changed, 5 deletions(-) diff --git a/pkg/codegen/model.go b/pkg/codegen/model.go index 5e85870f..293039e8 100644 --- a/pkg/codegen/model.go +++ b/pkg/codegen/model.go @@ -1,7 +1,6 @@ package codegen import ( - "log" "sort" "strings" @@ -108,10 +107,6 @@ func (p *Package) Generate(out *Emitter) { return false }) - for i, decl := range sorted { - log.Printf("Sorted: %+v %+v", i, decl) - } - for i, t := range sorted { if i > 0 { out.Newline() From d46078c94fe1078c6183e77b11e6136468fce29d Mon Sep 17 00:00:00 2001 From: RobQuistNL Date: Thu, 16 Nov 2023 17:25:11 +0100 Subject: [PATCH 3/9] Append names instead of prepend so that they are all grouped together --- pkg/generator/schema_generator.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pkg/generator/schema_generator.go b/pkg/generator/schema_generator.go index b3b66ffb..5663fa1f 100644 --- a/pkg/generator/schema_generator.go +++ b/pkg/generator/schema_generator.go @@ -279,7 +279,7 @@ func (g *schemaGenerator) generateDeclaredType( g.output.file.Package.AddDecl(&codegen.Method{ Impl: formatter.generate(decl, validators), - Name: "validator_" + decl.GetName(), + Name: decl.GetName() + "_validator", }) } } @@ -765,13 +765,13 @@ func (g *schemaGenerator) generateEnumType( if wrapInStruct { g.output.file.Package.AddDecl(&codegen.Method{ Impl: formatter.enumMarshal(enumDecl), - Name: "enum_" + enumDecl.GetName(), + Name: enumDecl.GetName() + "_enum", }) } g.output.file.Package.AddDecl(&codegen.Method{ Impl: formatter.enumUnmarshal(enumDecl, enumType, valueConstant, wrapInStruct), - Name: "enum_" + enumDecl.GetName() + "_unmarshal", + Name: enumDecl.GetName() + "_enum_unmarshal", }) } } From 9c14b36038d0da014e6d5c86db651a4fccfa1744 Mon Sep 17 00:00:00 2001 From: RobQuistNL Date: Thu, 16 Nov 2023 17:56:59 +0100 Subject: [PATCH 4/9] Add prefix remover for proper sorting --- pkg/codegen/model.go | 2 +- pkg/generator/schema_generator.go | 2 +- pkg/schemas/types.go | 13 +++++++++++++ 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/pkg/codegen/model.go b/pkg/codegen/model.go index 293039e8..ee7b6d5e 100644 --- a/pkg/codegen/model.go +++ b/pkg/codegen/model.go @@ -100,7 +100,7 @@ func (p *Package) Generate(out *Emitter) { 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 schemas.RemoveEnumValuesPrefix(a.GetName()) < schemas.RemoveEnumValuesPrefix(b.GetName()) } } diff --git a/pkg/generator/schema_generator.go b/pkg/generator/schema_generator.go index 5663fa1f..e6b45db8 100644 --- a/pkg/generator/schema_generator.go +++ b/pkg/generator/schema_generator.go @@ -749,7 +749,7 @@ func (g *schemaGenerator) generateEnumType( if !g.config.OnlyModels { valueConstant := &codegen.Var{ - Name: "enumValues_" + enumDecl.Name, + Name: schemas.PrefixEnumValue + enumDecl.Name, Value: t.Enum, } g.output.file.Package.AddDecl(valueConstant) diff --git a/pkg/schemas/types.go b/pkg/schemas/types.go index 84de9bd0..ce925b64 100644 --- a/pkg/schemas/types.go +++ b/pkg/schemas/types.go @@ -1,5 +1,7 @@ package schemas +import "strings" + const ( TypeNameString = "string" TypeNameArray = "array" @@ -10,6 +12,10 @@ const ( TypeNameNull = "null" ) +const ( + PrefixEnumValue = "enumValues_" +) + func IsPrimitiveType(t string) bool { switch t { case TypeNameString, TypeNameNumber, TypeNameInteger, TypeNameBoolean, TypeNameNull: @@ -19,3 +25,10 @@ func IsPrimitiveType(t string) bool { return false } } + +func RemoveEnumValuesPrefix(name string) string { + if strings.HasPrefix(name, PrefixEnumValue) { + name = strings.TrimPrefix(name, PrefixEnumValue) + } + return name +} From 9535e23cc80a281f1f7d40191e74f2834cd31bbd Mon Sep 17 00:00:00 2001 From: RobQuistNL Date: Thu, 16 Nov 2023 18:03:53 +0100 Subject: [PATCH 5/9] Do proper sorting on names --- pkg/codegen/model.go | 2 +- pkg/schemas/types.go | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/pkg/codegen/model.go b/pkg/codegen/model.go index ee7b6d5e..3e73b5a5 100644 --- a/pkg/codegen/model.go +++ b/pkg/codegen/model.go @@ -100,7 +100,7 @@ func (p *Package) Generate(out *Emitter) { sort.Slice(sorted, func(i, j int) bool { if a, ok := sorted[i].(Named); ok { if b, ok := sorted[j].(Named); ok { - return schemas.RemoveEnumValuesPrefix(a.GetName()) < schemas.RemoveEnumValuesPrefix(b.GetName()) + return schemas.CleanNameForSorting(a.GetName()) < schemas.CleanNameForSorting(b.GetName()) } } diff --git a/pkg/schemas/types.go b/pkg/schemas/types.go index ce925b64..2a59f739 100644 --- a/pkg/schemas/types.go +++ b/pkg/schemas/types.go @@ -26,9 +26,9 @@ func IsPrimitiveType(t string) bool { } } -func RemoveEnumValuesPrefix(name string) string { +func CleanNameForSorting(name string) string { if strings.HasPrefix(name, PrefixEnumValue) { - name = strings.TrimPrefix(name, PrefixEnumValue) + name = strings.TrimPrefix(name, PrefixEnumValue) + "_enumValues" // Append a string for sorting properly } return name } From 978b064d99cc265a72b8c4f7bfc4b7078a2ed29f Mon Sep 17 00:00:00 2001 From: Rob Quist Date: Thu, 18 Jan 2024 12:19:12 +0100 Subject: [PATCH 6/9] Update pkg/schemas/types.go Co-authored-by: Claudio Beatrice --- pkg/schemas/types.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/pkg/schemas/types.go b/pkg/schemas/types.go index 2a59f739..eb41e54e 100644 --- a/pkg/schemas/types.go +++ b/pkg/schemas/types.go @@ -10,9 +10,6 @@ const ( TypeNameObject = "object" TypeNameBoolean = "boolean" TypeNameNull = "null" -) - -const ( PrefixEnumValue = "enumValues_" ) From 63f39432e77dc1a6df496bc2a82fa0c53c125691 Mon Sep 17 00:00:00 2001 From: Rob Quist Date: Thu, 18 Jan 2024 12:19:17 +0100 Subject: [PATCH 7/9] Update pkg/schemas/types.go Co-authored-by: Claudio Beatrice --- pkg/schemas/types.go | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg/schemas/types.go b/pkg/schemas/types.go index eb41e54e..da3a76cc 100644 --- a/pkg/schemas/types.go +++ b/pkg/schemas/types.go @@ -27,5 +27,6 @@ func CleanNameForSorting(name string) string { if strings.HasPrefix(name, PrefixEnumValue) { name = strings.TrimPrefix(name, PrefixEnumValue) + "_enumValues" // Append a string for sorting properly } + return name } From cbec879f781d2b0f58dc6b7b70d68cad2e2fda71 Mon Sep 17 00:00:00 2001 From: Rob Quist Date: Thu, 18 Jan 2024 12:19:22 +0100 Subject: [PATCH 8/9] Update pkg/schemas/types.go Co-authored-by: Claudio Beatrice --- pkg/schemas/types.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/schemas/types.go b/pkg/schemas/types.go index da3a76cc..ad62d4d4 100644 --- a/pkg/schemas/types.go +++ b/pkg/schemas/types.go @@ -25,7 +25,7 @@ func IsPrimitiveType(t string) bool { func CleanNameForSorting(name string) string { if strings.HasPrefix(name, PrefixEnumValue) { - name = strings.TrimPrefix(name, PrefixEnumValue) + "_enumValues" // Append a string for sorting properly + return strings.TrimPrefix(name, PrefixEnumValue) + "_enumValues" // Append a string for sorting properly. } return name From 728e58643e22d827b993c925124e52b21310a9cc Mon Sep 17 00:00:00 2001 From: RobQuistNL Date: Fri, 19 Jan 2024 11:40:31 +0100 Subject: [PATCH 9/9] Regenerate the testfiles for the new sorting logic --- tests/data/core/date/date.go | 10 +- tests/data/core/dateTime/dateTime.go | 10 +- tests/data/core/ip/ip.go | 10 +- tests/data/core/object/object.go | 10 +- tests/data/core/refToEnum/refToEnum.go | 16 +- tests/data/core/time/time.go | 10 +- .../extraImports/gopkgYAMLv3/gopkgYAMLv3.go | 46 +-- .../specialCharacters/specialCharacters.go | 107 +++---- .../cyclicAndRequired1/cyclicAndRequired1.go | 20 +- tests/data/validation/enum/enum.go | 290 +++++++++--------- .../requiredFields/requiredFields.go | 96 +++--- .../typed_default_enums.go | 16 +- 12 files changed, 325 insertions(+), 316 deletions(-) diff --git a/tests/data/core/date/date.go b/tests/data/core/date/date.go index adbf75d1..ce558d92 100644 --- a/tests/data/core/date/date.go +++ b/tests/data/core/date/date.go @@ -6,6 +6,11 @@ import "encoding/json" import "fmt" import "github.com/atombender/go-jsonschema/pkg/types" +type Date struct { + // MyObject corresponds to the JSON schema field "myObject". + MyObject *DateMyObject `json:"myObject,omitempty" yaml:"myObject,omitempty" mapstructure:"myObject,omitempty"` +} + type DateMyObject struct { // MyDate corresponds to the JSON schema field "myDate". MyDate types.SerializableDate `json:"myDate" yaml:"myDate" mapstructure:"myDate"` @@ -28,8 +33,3 @@ func (j *DateMyObject) UnmarshalJSON(b []byte) error { *j = DateMyObject(plain) return nil } - -type Date struct { - // MyObject corresponds to the JSON schema field "myObject". - MyObject *DateMyObject `json:"myObject,omitempty" yaml:"myObject,omitempty" mapstructure:"myObject,omitempty"` -} diff --git a/tests/data/core/dateTime/dateTime.go b/tests/data/core/dateTime/dateTime.go index b2e40e0a..cd2df821 100644 --- a/tests/data/core/dateTime/dateTime.go +++ b/tests/data/core/dateTime/dateTime.go @@ -6,6 +6,11 @@ import "encoding/json" import "fmt" import "time" +type DateTime struct { + // MyObject corresponds to the JSON schema field "myObject". + MyObject *DateTimeMyObject `json:"myObject,omitempty" yaml:"myObject,omitempty" mapstructure:"myObject,omitempty"` +} + type DateTimeMyObject struct { // MyDateTime corresponds to the JSON schema field "myDateTime". MyDateTime time.Time `json:"myDateTime" yaml:"myDateTime" mapstructure:"myDateTime"` @@ -28,8 +33,3 @@ func (j *DateTimeMyObject) UnmarshalJSON(b []byte) error { *j = DateTimeMyObject(plain) return nil } - -type DateTime struct { - // MyObject corresponds to the JSON schema field "myObject". - MyObject *DateTimeMyObject `json:"myObject,omitempty" yaml:"myObject,omitempty" mapstructure:"myObject,omitempty"` -} diff --git a/tests/data/core/ip/ip.go b/tests/data/core/ip/ip.go index 5794f272..380bbb00 100644 --- a/tests/data/core/ip/ip.go +++ b/tests/data/core/ip/ip.go @@ -6,6 +6,11 @@ import "encoding/json" import "fmt" import "net/netip" +type Ip struct { + // MyObject corresponds to the JSON schema field "myObject". + MyObject *IpMyObject `json:"myObject,omitempty" yaml:"myObject,omitempty" mapstructure:"myObject,omitempty"` +} + type IpMyObject struct { // MyIp corresponds to the JSON schema field "myIp". MyIp netip.Addr `json:"myIp" yaml:"myIp" mapstructure:"myIp"` @@ -28,8 +33,3 @@ func (j *IpMyObject) UnmarshalJSON(b []byte) error { *j = IpMyObject(plain) return nil } - -type Ip struct { - // MyObject corresponds to the JSON schema field "myObject". - MyObject *IpMyObject `json:"myObject,omitempty" yaml:"myObject,omitempty" mapstructure:"myObject,omitempty"` -} diff --git a/tests/data/core/object/object.go b/tests/data/core/object/object.go index cb852542..76652123 100644 --- a/tests/data/core/object/object.go +++ b/tests/data/core/object/object.go @@ -5,6 +5,11 @@ package test import "encoding/json" import "fmt" +type Object struct { + // MyObject corresponds to the JSON schema field "myObject". + MyObject *ObjectMyObject `json:"myObject,omitempty" yaml:"myObject,omitempty" mapstructure:"myObject,omitempty"` +} + type ObjectMyObject struct { // MyString corresponds to the JSON schema field "myString". MyString string `json:"myString" yaml:"myString" mapstructure:"myString"` @@ -27,8 +32,3 @@ func (j *ObjectMyObject) UnmarshalJSON(b []byte) error { *j = ObjectMyObject(plain) return nil } - -type Object struct { - // MyObject corresponds to the JSON schema field "myObject". - MyObject *ObjectMyObject `json:"myObject,omitempty" yaml:"myObject,omitempty" mapstructure:"myObject,omitempty"` -} diff --git a/tests/data/core/refToEnum/refToEnum.go b/tests/data/core/refToEnum/refToEnum.go index aac189ad..6331c642 100644 --- a/tests/data/core/refToEnum/refToEnum.go +++ b/tests/data/core/refToEnum/refToEnum.go @@ -6,8 +6,16 @@ import "encoding/json" import "fmt" import "reflect" +type RefToEnum struct { + // MyThing corresponds to the JSON schema field "myThing". + MyThing *Thing `json:"myThing,omitempty" yaml:"myThing,omitempty" mapstructure:"myThing,omitempty"` +} + type Thing string +const ThingX Thing = "x" +const ThingY Thing = "y" + var enumValues_Thing = []interface{}{ "x", "y", @@ -32,11 +40,3 @@ func (j *Thing) UnmarshalJSON(b []byte) error { *j = Thing(v) return nil } - -type RefToEnum struct { - // MyThing corresponds to the JSON schema field "myThing". - MyThing *Thing `json:"myThing,omitempty" yaml:"myThing,omitempty" mapstructure:"myThing,omitempty"` -} - -const ThingX Thing = "x" -const ThingY Thing = "y" diff --git a/tests/data/core/time/time.go b/tests/data/core/time/time.go index f79004f7..4e4b5457 100644 --- a/tests/data/core/time/time.go +++ b/tests/data/core/time/time.go @@ -6,6 +6,11 @@ import "encoding/json" import "fmt" import "github.com/atombender/go-jsonschema/pkg/types" +type Time struct { + // MyObject corresponds to the JSON schema field "myObject". + MyObject *TimeMyObject `json:"myObject,omitempty" yaml:"myObject,omitempty" mapstructure:"myObject,omitempty"` +} + type TimeMyObject struct { // MyTime corresponds to the JSON schema field "myTime". MyTime types.SerializableTime `json:"myTime" yaml:"myTime" mapstructure:"myTime"` @@ -28,8 +33,3 @@ func (j *TimeMyObject) UnmarshalJSON(b []byte) error { *j = TimeMyObject(plain) return nil } - -type Time struct { - // MyObject corresponds to the JSON schema field "myObject". - MyObject *TimeMyObject `json:"myObject,omitempty" yaml:"myObject,omitempty" mapstructure:"myObject,omitempty"` -} diff --git a/tests/data/extraImports/gopkgYAMLv3/gopkgYAMLv3.go b/tests/data/extraImports/gopkgYAMLv3/gopkgYAMLv3.go index 3f366e24..3d08a51f 100644 --- a/tests/data/extraImports/gopkgYAMLv3/gopkgYAMLv3.go +++ b/tests/data/extraImports/gopkgYAMLv3/gopkgYAMLv3.go @@ -7,8 +7,31 @@ import "fmt" import yaml "gopkg.in/yaml.v3" import "reflect" +type GopkgYAMLv3 struct { + // MyBoolean corresponds to the JSON schema field "myBoolean". + MyBoolean *bool `json:"myBoolean,omitempty" yaml:"myBoolean,omitempty" mapstructure:"myBoolean,omitempty"` + + // MyEnum corresponds to the JSON schema field "myEnum". + MyEnum *GopkgYAMLv3MyEnum `json:"myEnum,omitempty" yaml:"myEnum,omitempty" mapstructure:"myEnum,omitempty"` + + // MyInteger corresponds to the JSON schema field "myInteger". + MyInteger *int `json:"myInteger,omitempty" yaml:"myInteger,omitempty" mapstructure:"myInteger,omitempty"` + + // MyNull corresponds to the JSON schema field "myNull". + MyNull interface{} `json:"myNull,omitempty" yaml:"myNull,omitempty" mapstructure:"myNull,omitempty"` + + // MyNumber corresponds to the JSON schema field "myNumber". + MyNumber *float64 `json:"myNumber,omitempty" yaml:"myNumber,omitempty" mapstructure:"myNumber,omitempty"` + + // MyString corresponds to the JSON schema field "myString". + MyString *string `json:"myString,omitempty" yaml:"myString,omitempty" mapstructure:"myString,omitempty"` +} + type GopkgYAMLv3MyEnum string +const GopkgYAMLv3MyEnumX GopkgYAMLv3MyEnum = "x" +const GopkgYAMLv3MyEnumY GopkgYAMLv3MyEnum = "y" + var enumValues_GopkgYAMLv3MyEnum = []interface{}{ "x", "y", @@ -54,29 +77,6 @@ func (j *GopkgYAMLv3MyEnum) UnmarshalYAML(value *yaml.Node) error { return nil } -type GopkgYAMLv3 struct { - // MyBoolean corresponds to the JSON schema field "myBoolean". - MyBoolean *bool `json:"myBoolean,omitempty" yaml:"myBoolean,omitempty" mapstructure:"myBoolean,omitempty"` - - // MyEnum corresponds to the JSON schema field "myEnum". - MyEnum *GopkgYAMLv3MyEnum `json:"myEnum,omitempty" yaml:"myEnum,omitempty" mapstructure:"myEnum,omitempty"` - - // MyInteger corresponds to the JSON schema field "myInteger". - MyInteger *int `json:"myInteger,omitempty" yaml:"myInteger,omitempty" mapstructure:"myInteger,omitempty"` - - // MyNull corresponds to the JSON schema field "myNull". - MyNull interface{} `json:"myNull,omitempty" yaml:"myNull,omitempty" mapstructure:"myNull,omitempty"` - - // MyNumber corresponds to the JSON schema field "myNumber". - MyNumber *float64 `json:"myNumber,omitempty" yaml:"myNumber,omitempty" mapstructure:"myNumber,omitempty"` - - // MyString corresponds to the JSON schema field "myString". - MyString *string `json:"myString,omitempty" yaml:"myString,omitempty" mapstructure:"myString,omitempty"` -} - -const GopkgYAMLv3MyEnumX GopkgYAMLv3MyEnum = "x" -const GopkgYAMLv3MyEnumY GopkgYAMLv3MyEnum = "y" - // UnmarshalJSON implements json.Unmarshaler. func (j *GopkgYAMLv3) UnmarshalJSON(b []byte) error { var raw map[string]interface{} diff --git a/tests/data/misc/specialCharacters/specialCharacters.go b/tests/data/misc/specialCharacters/specialCharacters.go index eb3eb26d..7895d43a 100644 --- a/tests/data/misc/specialCharacters/specialCharacters.go +++ b/tests/data/misc/specialCharacters/specialCharacters.go @@ -16,123 +16,126 @@ type License_1 string const License_1_GPL30 License_1 = "GPL-3.0+" const License_1_MIT License_1 = "MIT+" -type SpecialCharacters struct { - // PlainLicenses corresponds to the JSON schema field "plainLicenses". - PlainLicenses *SpecialCharactersPlainLicenses `json:"plainLicenses,omitempty" yaml:"plainLicenses,omitempty" mapstructure:"plainLicenses,omitempty"` - - // PlainLicensesRef corresponds to the JSON schema field "plainLicensesRef". - PlainLicensesRef []License `json:"plainLicensesRef,omitempty" yaml:"plainLicensesRef,omitempty" mapstructure:"plainLicensesRef,omitempty"` - - // PlusLicenses corresponds to the JSON schema field "plusLicenses". - PlusLicenses *SpecialCharactersPlusLicenses `json:"plusLicenses,omitempty" yaml:"plusLicenses,omitempty" mapstructure:"plusLicenses,omitempty"` - - // PlusLicensesRef corresponds to the JSON schema field "plusLicensesRef". - PlusLicensesRef []License_1 `json:"plusLicensesRef,omitempty" yaml:"plusLicensesRef,omitempty" mapstructure:"plusLicensesRef,omitempty"` -} - -type SpecialCharactersPlainLicenses string - -const SpecialCharactersPlainLicensesGPL30 SpecialCharactersPlainLicenses = "GPL-3.0" -const SpecialCharactersPlainLicensesMIT SpecialCharactersPlainLicenses = "MIT" - -type SpecialCharactersPlusLicenses string - -const SpecialCharactersPlusLicensesGPL30 SpecialCharactersPlusLicenses = "GPL-3.0+" -const SpecialCharactersPlusLicensesMIT SpecialCharactersPlusLicenses = "MIT+" - -var enumValues_License = []interface{}{ - "GPL-3.0", - "MIT", -} var enumValues_License_1 = []interface{}{ "GPL-3.0+", "MIT+", } -var enumValues_SpecialCharactersPlainLicenses = []interface{}{ - "GPL-3.0", - "MIT", -} -var enumValues_SpecialCharactersPlusLicenses = []interface{}{ - "GPL-3.0+", - "MIT+", -} // UnmarshalJSON implements json.Unmarshaler. -func (j *SpecialCharactersPlusLicenses) UnmarshalJSON(b []byte) error { +func (j *License_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_SpecialCharactersPlusLicenses { + for _, expected := range enumValues_License_1 { if reflect.DeepEqual(v, expected) { ok = true break } } if !ok { - return fmt.Errorf("invalid value (expected one of %#v): %#v", enumValues_SpecialCharactersPlusLicenses, v) + return fmt.Errorf("invalid value (expected one of %#v): %#v", enumValues_License_1, v) } - *j = SpecialCharactersPlusLicenses(v) + *j = License_1(v) return nil } +var enumValues_License = []interface{}{ + "GPL-3.0", + "MIT", +} + // UnmarshalJSON implements json.Unmarshaler. -func (j *SpecialCharactersPlainLicenses) UnmarshalJSON(b []byte) error { +func (j *License) UnmarshalJSON(b []byte) error { var v string if err := json.Unmarshal(b, &v); err != nil { return err } var ok bool - for _, expected := range enumValues_SpecialCharactersPlainLicenses { + for _, expected := range enumValues_License { if reflect.DeepEqual(v, expected) { ok = true break } } if !ok { - return fmt.Errorf("invalid value (expected one of %#v): %#v", enumValues_SpecialCharactersPlainLicenses, v) + return fmt.Errorf("invalid value (expected one of %#v): %#v", enumValues_License, v) } - *j = SpecialCharactersPlainLicenses(v) + *j = License(v) return nil } +type SpecialCharacters struct { + // PlainLicenses corresponds to the JSON schema field "plainLicenses". + PlainLicenses *SpecialCharactersPlainLicenses `json:"plainLicenses,omitempty" yaml:"plainLicenses,omitempty" mapstructure:"plainLicenses,omitempty"` + + // PlainLicensesRef corresponds to the JSON schema field "plainLicensesRef". + PlainLicensesRef []License `json:"plainLicensesRef,omitempty" yaml:"plainLicensesRef,omitempty" mapstructure:"plainLicensesRef,omitempty"` + + // PlusLicenses corresponds to the JSON schema field "plusLicenses". + PlusLicenses *SpecialCharactersPlusLicenses `json:"plusLicenses,omitempty" yaml:"plusLicenses,omitempty" mapstructure:"plusLicenses,omitempty"` + + // PlusLicensesRef corresponds to the JSON schema field "plusLicensesRef". + PlusLicensesRef []License_1 `json:"plusLicensesRef,omitempty" yaml:"plusLicensesRef,omitempty" mapstructure:"plusLicensesRef,omitempty"` +} + +type SpecialCharactersPlainLicenses string + +const SpecialCharactersPlainLicensesGPL30 SpecialCharactersPlainLicenses = "GPL-3.0" +const SpecialCharactersPlainLicensesMIT SpecialCharactersPlainLicenses = "MIT" + +var enumValues_SpecialCharactersPlainLicenses = []interface{}{ + "GPL-3.0", + "MIT", +} + // UnmarshalJSON implements json.Unmarshaler. -func (j *License_1) UnmarshalJSON(b []byte) error { +func (j *SpecialCharactersPlainLicenses) UnmarshalJSON(b []byte) error { var v string if err := json.Unmarshal(b, &v); err != nil { return err } var ok bool - for _, expected := range enumValues_License_1 { + for _, expected := range enumValues_SpecialCharactersPlainLicenses { if reflect.DeepEqual(v, expected) { ok = true break } } if !ok { - return fmt.Errorf("invalid value (expected one of %#v): %#v", enumValues_License_1, v) + return fmt.Errorf("invalid value (expected one of %#v): %#v", enumValues_SpecialCharactersPlainLicenses, v) } - *j = License_1(v) + *j = SpecialCharactersPlainLicenses(v) return nil } +type SpecialCharactersPlusLicenses string + +const SpecialCharactersPlusLicensesGPL30 SpecialCharactersPlusLicenses = "GPL-3.0+" +const SpecialCharactersPlusLicensesMIT SpecialCharactersPlusLicenses = "MIT+" + +var enumValues_SpecialCharactersPlusLicenses = []interface{}{ + "GPL-3.0+", + "MIT+", +} + // UnmarshalJSON implements json.Unmarshaler. -func (j *License) UnmarshalJSON(b []byte) error { +func (j *SpecialCharactersPlusLicenses) UnmarshalJSON(b []byte) error { var v string if err := json.Unmarshal(b, &v); err != nil { return err } var ok bool - for _, expected := range enumValues_License { + for _, expected := range enumValues_SpecialCharactersPlusLicenses { if reflect.DeepEqual(v, expected) { ok = true break } } if !ok { - return fmt.Errorf("invalid value (expected one of %#v): %#v", enumValues_License, v) + return fmt.Errorf("invalid value (expected one of %#v): %#v", enumValues_SpecialCharactersPlusLicenses, v) } - *j = License(v) + *j = SpecialCharactersPlusLicenses(v) return nil } diff --git a/tests/data/miscWithDefaults/cyclicAndRequired1/cyclicAndRequired1.go b/tests/data/miscWithDefaults/cyclicAndRequired1/cyclicAndRequired1.go index 7534c336..2f924da5 100644 --- a/tests/data/miscWithDefaults/cyclicAndRequired1/cyclicAndRequired1.go +++ b/tests/data/miscWithDefaults/cyclicAndRequired1/cyclicAndRequired1.go @@ -5,6 +5,16 @@ package test import "encoding/json" import "fmt" +type Bar struct { + // RefToFoo corresponds to the JSON schema field "refToFoo". + RefToFoo *Foo `json:"refToFoo,omitempty" yaml:"refToFoo,omitempty" mapstructure:"refToFoo,omitempty"` +} + +type CyclicAndRequired1 struct { + // A corresponds to the JSON schema field "a". + A *Foo `json:"a,omitempty" yaml:"a,omitempty" mapstructure:"a,omitempty"` +} + type Foo struct { // RefToBar corresponds to the JSON schema field "refToBar". RefToBar Bar `json:"refToBar" yaml:"refToBar" mapstructure:"refToBar"` @@ -27,13 +37,3 @@ func (j *Foo) UnmarshalJSON(b []byte) error { *j = Foo(plain) return nil } - -type Bar struct { - // RefToFoo corresponds to the JSON schema field "refToFoo". - RefToFoo *Foo `json:"refToFoo,omitempty" yaml:"refToFoo,omitempty" mapstructure:"refToFoo,omitempty"` -} - -type CyclicAndRequired1 struct { - // A corresponds to the JSON schema field "a". - A *Foo `json:"a,omitempty" yaml:"a,omitempty" mapstructure:"a,omitempty"` -} diff --git a/tests/data/validation/enum/enum.go b/tests/data/validation/enum/enum.go index 616c7954..2f6ae569 100644 --- a/tests/data/validation/enum/enum.go +++ b/tests/data/validation/enum/enum.go @@ -44,153 +44,102 @@ type Enum struct { type EnumMyBooleanTypedEnum bool -type EnumMyBooleanUntypedEnum bool - -type EnumMyIntegerTypedEnum int - -type EnumMyMixedTypeEnum struct { - Value interface{} -} - -type EnumMyMixedUntypedEnum struct { - Value interface{} -} - -type EnumMyNullTypedEnum struct { - Value interface{} -} - -type EnumMyNullUntypedEnum struct { - Value interface{} -} - -type EnumMyNumberTypedEnum float64 - -type EnumMyNumberUntypedEnum float64 - -type EnumMyStringTypedEnum string - -const EnumMyStringTypedEnumBlue EnumMyStringTypedEnum = "blue" -const EnumMyStringTypedEnumGreen EnumMyStringTypedEnum = "green" -const EnumMyStringTypedEnumRed EnumMyStringTypedEnum = "red" - -type EnumMyStringUntypedEnum string - -const EnumMyStringUntypedEnumBlue EnumMyStringUntypedEnum = "blue" -const EnumMyStringUntypedEnumGreen EnumMyStringUntypedEnum = "green" -const EnumMyStringUntypedEnumRed EnumMyStringUntypedEnum = "red" - var enumValues_EnumMyBooleanTypedEnum = []interface{}{ true, false, } -var enumValues_EnumMyBooleanUntypedEnum = []interface{}{ - true, - false, -} -var enumValues_EnumMyIntegerTypedEnum = []interface{}{ - 1, - 2, - 3, -} -var enumValues_EnumMyMixedTypeEnum = []interface{}{ - 42.0, - "smurf", -} -var enumValues_EnumMyMixedUntypedEnum = []interface{}{ - "red", - 1.0, - true, - nil, -} -var enumValues_EnumMyNullTypedEnum = []interface{}{ - nil, -} -var enumValues_EnumMyNullUntypedEnum = []interface{}{ - nil, -} // UnmarshalJSON implements json.Unmarshaler. -func (j *EnumMyNullUntypedEnum) UnmarshalJSON(b []byte) error { - var v struct { - Value interface{} - } - if err := json.Unmarshal(b, &v.Value); err != nil { +func (j *EnumMyBooleanTypedEnum) UnmarshalJSON(b []byte) error { + var v bool + if err := json.Unmarshal(b, &v); err != nil { return err } var ok bool - for _, expected := range enumValues_EnumMyNullUntypedEnum { - if reflect.DeepEqual(v.Value, expected) { + for _, expected := range enumValues_EnumMyBooleanTypedEnum { + if reflect.DeepEqual(v, expected) { ok = true break } } if !ok { - return fmt.Errorf("invalid value (expected one of %#v): %#v", enumValues_EnumMyNullUntypedEnum, v.Value) + return fmt.Errorf("invalid value (expected one of %#v): %#v", enumValues_EnumMyBooleanTypedEnum, v) } - *j = EnumMyNullUntypedEnum(v) + *j = EnumMyBooleanTypedEnum(v) return nil } -var enumValues_EnumMyNumberTypedEnum = []interface{}{ - 1.0, - 2.0, - 3.0, +type EnumMyBooleanUntypedEnum bool + +var enumValues_EnumMyBooleanUntypedEnum = []interface{}{ + true, + false, } // UnmarshalJSON implements json.Unmarshaler. -func (j *EnumMyNumberTypedEnum) UnmarshalJSON(b []byte) error { - var v float64 +func (j *EnumMyBooleanUntypedEnum) UnmarshalJSON(b []byte) error { + var v bool if err := json.Unmarshal(b, &v); err != nil { return err } var ok bool - for _, expected := range enumValues_EnumMyNumberTypedEnum { + for _, expected := range enumValues_EnumMyBooleanUntypedEnum { if reflect.DeepEqual(v, expected) { ok = true break } } if !ok { - return fmt.Errorf("invalid value (expected one of %#v): %#v", enumValues_EnumMyNumberTypedEnum, v) + return fmt.Errorf("invalid value (expected one of %#v): %#v", enumValues_EnumMyBooleanUntypedEnum, v) } - *j = EnumMyNumberTypedEnum(v) + *j = EnumMyBooleanUntypedEnum(v) return nil } -// MarshalJSON implements json.Marshaler. -func (j *EnumMyNullUntypedEnum) MarshalJSON() ([]byte, error) { - return json.Marshal(j.Value) -} +type EnumMyIntegerTypedEnum int -var enumValues_EnumMyNumberUntypedEnum = []interface{}{ - 1.0, - 2.0, - 3.0, +var enumValues_EnumMyIntegerTypedEnum = []interface{}{ + 1, + 2, + 3, } // UnmarshalJSON implements json.Unmarshaler. -func (j *EnumMyNumberUntypedEnum) UnmarshalJSON(b []byte) error { - var v float64 +func (j *EnumMyIntegerTypedEnum) UnmarshalJSON(b []byte) error { + var v int if err := json.Unmarshal(b, &v); err != nil { return err } var ok bool - for _, expected := range enumValues_EnumMyNumberUntypedEnum { + for _, expected := range enumValues_EnumMyIntegerTypedEnum { if reflect.DeepEqual(v, expected) { ok = true break } } if !ok { - return fmt.Errorf("invalid value (expected one of %#v): %#v", enumValues_EnumMyNumberUntypedEnum, v) + return fmt.Errorf("invalid value (expected one of %#v): %#v", enumValues_EnumMyIntegerTypedEnum, v) } - *j = EnumMyNumberUntypedEnum(v) + *j = EnumMyIntegerTypedEnum(v) return nil } +type EnumMyMixedTypeEnum struct { + Value interface{} +} + +// MarshalJSON implements json.Marshaler. +func (j *EnumMyMixedTypeEnum) MarshalJSON() ([]byte, error) { + return json.Marshal(j.Value) +} + +var enumValues_EnumMyMixedTypeEnum = []interface{}{ + 42.0, + "smurf", +} + // UnmarshalJSON implements json.Unmarshaler. -func (j *EnumMyNullTypedEnum) UnmarshalJSON(b []byte) error { +func (j *EnumMyMixedTypeEnum) UnmarshalJSON(b []byte) error { var v struct { Value interface{} } @@ -198,52 +147,72 @@ func (j *EnumMyNullTypedEnum) UnmarshalJSON(b []byte) error { return err } var ok bool - for _, expected := range enumValues_EnumMyNullTypedEnum { + for _, expected := range enumValues_EnumMyMixedTypeEnum { if reflect.DeepEqual(v.Value, expected) { ok = true break } } if !ok { - return fmt.Errorf("invalid value (expected one of %#v): %#v", enumValues_EnumMyNullTypedEnum, v.Value) + return fmt.Errorf("invalid value (expected one of %#v): %#v", enumValues_EnumMyMixedTypeEnum, v.Value) } - *j = EnumMyNullTypedEnum(v) + *j = EnumMyMixedTypeEnum(v) return nil } -var enumValues_EnumMyStringTypedEnum = []interface{}{ +type EnumMyMixedUntypedEnum struct { + Value interface{} +} + +// MarshalJSON implements json.Marshaler. +func (j *EnumMyMixedUntypedEnum) MarshalJSON() ([]byte, error) { + return json.Marshal(j.Value) +} + +var enumValues_EnumMyMixedUntypedEnum = []interface{}{ "red", - "blue", - "green", + 1.0, + true, + nil, } // UnmarshalJSON implements json.Unmarshaler. -func (j *EnumMyStringTypedEnum) UnmarshalJSON(b []byte) error { - var v string - if err := json.Unmarshal(b, &v); err != nil { +func (j *EnumMyMixedUntypedEnum) 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_EnumMyStringTypedEnum { - if reflect.DeepEqual(v, expected) { + for _, expected := range enumValues_EnumMyMixedUntypedEnum { + if reflect.DeepEqual(v.Value, expected) { ok = true break } } if !ok { - return fmt.Errorf("invalid value (expected one of %#v): %#v", enumValues_EnumMyStringTypedEnum, v) + return fmt.Errorf("invalid value (expected one of %#v): %#v", enumValues_EnumMyMixedUntypedEnum, v.Value) } - *j = EnumMyStringTypedEnum(v) + *j = EnumMyMixedUntypedEnum(v) return nil } +type EnumMyNullTypedEnum struct { + Value interface{} +} + // MarshalJSON implements json.Marshaler. func (j *EnumMyNullTypedEnum) MarshalJSON() ([]byte, error) { return json.Marshal(j.Value) } +var enumValues_EnumMyNullTypedEnum = []interface{}{ + nil, +} + // UnmarshalJSON implements json.Unmarshaler. -func (j *EnumMyMixedUntypedEnum) UnmarshalJSON(b []byte) error { +func (j *EnumMyNullTypedEnum) UnmarshalJSON(b []byte) error { var v struct { Value interface{} } @@ -251,26 +220,34 @@ func (j *EnumMyMixedUntypedEnum) UnmarshalJSON(b []byte) error { return err } var ok bool - for _, expected := range enumValues_EnumMyMixedUntypedEnum { + for _, expected := range enumValues_EnumMyNullTypedEnum { if reflect.DeepEqual(v.Value, expected) { ok = true break } } if !ok { - return fmt.Errorf("invalid value (expected one of %#v): %#v", enumValues_EnumMyMixedUntypedEnum, v.Value) + return fmt.Errorf("invalid value (expected one of %#v): %#v", enumValues_EnumMyNullTypedEnum, v.Value) } - *j = EnumMyMixedUntypedEnum(v) + *j = EnumMyNullTypedEnum(v) return nil } +type EnumMyNullUntypedEnum struct { + Value interface{} +} + // MarshalJSON implements json.Marshaler. -func (j *EnumMyMixedUntypedEnum) MarshalJSON() ([]byte, error) { +func (j *EnumMyNullUntypedEnum) MarshalJSON() ([]byte, error) { return json.Marshal(j.Value) } +var enumValues_EnumMyNullUntypedEnum = []interface{}{ + nil, +} + // UnmarshalJSON implements json.Unmarshaler. -func (j *EnumMyMixedTypeEnum) UnmarshalJSON(b []byte) error { +func (j *EnumMyNullUntypedEnum) UnmarshalJSON(b []byte) error { var v struct { Value interface{} } @@ -278,106 +255,135 @@ func (j *EnumMyMixedTypeEnum) UnmarshalJSON(b []byte) error { return err } var ok bool - for _, expected := range enumValues_EnumMyMixedTypeEnum { + for _, expected := range enumValues_EnumMyNullUntypedEnum { if reflect.DeepEqual(v.Value, expected) { ok = true break } } if !ok { - return fmt.Errorf("invalid value (expected one of %#v): %#v", enumValues_EnumMyMixedTypeEnum, v.Value) + return fmt.Errorf("invalid value (expected one of %#v): %#v", enumValues_EnumMyNullUntypedEnum, v.Value) } - *j = EnumMyMixedTypeEnum(v) + *j = EnumMyNullUntypedEnum(v) return nil } -var enumValues_EnumMyStringUntypedEnum = []interface{}{ - "red", - "blue", - "green", +type EnumMyNumberTypedEnum float64 + +var enumValues_EnumMyNumberTypedEnum = []interface{}{ + 1.0, + 2.0, + 3.0, } // UnmarshalJSON implements json.Unmarshaler. -func (j *EnumMyStringUntypedEnum) UnmarshalJSON(b []byte) error { - var v string +func (j *EnumMyNumberTypedEnum) UnmarshalJSON(b []byte) error { + var v float64 if err := json.Unmarshal(b, &v); err != nil { return err } var ok bool - for _, expected := range enumValues_EnumMyStringUntypedEnum { + for _, expected := range enumValues_EnumMyNumberTypedEnum { if reflect.DeepEqual(v, expected) { ok = true break } } if !ok { - return fmt.Errorf("invalid value (expected one of %#v): %#v", enumValues_EnumMyStringUntypedEnum, v) + return fmt.Errorf("invalid value (expected one of %#v): %#v", enumValues_EnumMyNumberTypedEnum, v) } - *j = EnumMyStringUntypedEnum(v) + *j = EnumMyNumberTypedEnum(v) return nil } -// MarshalJSON implements json.Marshaler. -func (j *EnumMyMixedTypeEnum) MarshalJSON() ([]byte, error) { - return json.Marshal(j.Value) +type EnumMyNumberUntypedEnum float64 + +var enumValues_EnumMyNumberUntypedEnum = []interface{}{ + 1.0, + 2.0, + 3.0, } // UnmarshalJSON implements json.Unmarshaler. -func (j *EnumMyIntegerTypedEnum) UnmarshalJSON(b []byte) error { - var v int +func (j *EnumMyNumberUntypedEnum) UnmarshalJSON(b []byte) error { + var v float64 if err := json.Unmarshal(b, &v); err != nil { return err } var ok bool - for _, expected := range enumValues_EnumMyIntegerTypedEnum { + for _, expected := range enumValues_EnumMyNumberUntypedEnum { if reflect.DeepEqual(v, expected) { ok = true break } } if !ok { - return fmt.Errorf("invalid value (expected one of %#v): %#v", enumValues_EnumMyIntegerTypedEnum, v) + return fmt.Errorf("invalid value (expected one of %#v): %#v", enumValues_EnumMyNumberUntypedEnum, v) } - *j = EnumMyIntegerTypedEnum(v) + *j = EnumMyNumberUntypedEnum(v) return nil } +type EnumMyStringTypedEnum string + +const EnumMyStringTypedEnumBlue EnumMyStringTypedEnum = "blue" +const EnumMyStringTypedEnumGreen EnumMyStringTypedEnum = "green" +const EnumMyStringTypedEnumRed EnumMyStringTypedEnum = "red" + +var enumValues_EnumMyStringTypedEnum = []interface{}{ + "red", + "blue", + "green", +} + // UnmarshalJSON implements json.Unmarshaler. -func (j *EnumMyBooleanUntypedEnum) UnmarshalJSON(b []byte) error { - var v bool +func (j *EnumMyStringTypedEnum) UnmarshalJSON(b []byte) error { + var v string if err := json.Unmarshal(b, &v); err != nil { return err } var ok bool - for _, expected := range enumValues_EnumMyBooleanUntypedEnum { + for _, expected := range enumValues_EnumMyStringTypedEnum { if reflect.DeepEqual(v, expected) { ok = true break } } if !ok { - return fmt.Errorf("invalid value (expected one of %#v): %#v", enumValues_EnumMyBooleanUntypedEnum, v) + return fmt.Errorf("invalid value (expected one of %#v): %#v", enumValues_EnumMyStringTypedEnum, v) } - *j = EnumMyBooleanUntypedEnum(v) + *j = EnumMyStringTypedEnum(v) return nil } +type EnumMyStringUntypedEnum string + +const EnumMyStringUntypedEnumBlue EnumMyStringUntypedEnum = "blue" +const EnumMyStringUntypedEnumGreen EnumMyStringUntypedEnum = "green" +const EnumMyStringUntypedEnumRed EnumMyStringUntypedEnum = "red" + +var enumValues_EnumMyStringUntypedEnum = []interface{}{ + "red", + "blue", + "green", +} + // UnmarshalJSON implements json.Unmarshaler. -func (j *EnumMyBooleanTypedEnum) UnmarshalJSON(b []byte) error { - var v bool +func (j *EnumMyStringUntypedEnum) UnmarshalJSON(b []byte) error { + var v string if err := json.Unmarshal(b, &v); err != nil { return err } var ok bool - for _, expected := range enumValues_EnumMyBooleanTypedEnum { + for _, expected := range enumValues_EnumMyStringUntypedEnum { if reflect.DeepEqual(v, expected) { ok = true break } } if !ok { - return fmt.Errorf("invalid value (expected one of %#v): %#v", enumValues_EnumMyBooleanTypedEnum, v) + return fmt.Errorf("invalid value (expected one of %#v): %#v", enumValues_EnumMyStringUntypedEnum, v) } - *j = EnumMyBooleanTypedEnum(v) + *j = EnumMyStringUntypedEnum(v) return nil } diff --git a/tests/data/validation/requiredFields/requiredFields.go b/tests/data/validation/requiredFields/requiredFields.go index b28390ca..5d340b0b 100644 --- a/tests/data/validation/requiredFields/requiredFields.go +++ b/tests/data/validation/requiredFields/requiredFields.go @@ -5,54 +5,6 @@ package test import "encoding/json" import "fmt" -type RequiredFieldsMyObject struct { - // MyNestedObjectString corresponds to the JSON schema field - // "myNestedObjectString". - MyNestedObjectString string `json:"myNestedObjectString" yaml:"myNestedObjectString" mapstructure:"myNestedObjectString"` -} - -// UnmarshalJSON implements json.Unmarshaler. -func (j *RequiredFieldsMyObject) UnmarshalJSON(b []byte) error { - var raw map[string]interface{} - if err := json.Unmarshal(b, &raw); err != nil { - return err - } - if v, ok := raw["myNestedObjectString"]; !ok || v == nil { - return fmt.Errorf("field myNestedObjectString in RequiredFieldsMyObject: required") - } - type Plain RequiredFieldsMyObject - var plain Plain - if err := json.Unmarshal(b, &plain); err != nil { - return err - } - *j = RequiredFieldsMyObject(plain) - return nil -} - -type RequiredFieldsMyObjectArrayElem struct { - // MyNestedObjectString corresponds to the JSON schema field - // "myNestedObjectString". - MyNestedObjectString string `json:"myNestedObjectString" yaml:"myNestedObjectString" mapstructure:"myNestedObjectString"` -} - -// UnmarshalJSON implements json.Unmarshaler. -func (j *RequiredFieldsMyObjectArrayElem) UnmarshalJSON(b []byte) error { - var raw map[string]interface{} - if err := json.Unmarshal(b, &raw); err != nil { - return err - } - if v, ok := raw["myNestedObjectString"]; !ok || v == nil { - return fmt.Errorf("field myNestedObjectString in RequiredFieldsMyObjectArrayElem: required") - } - type Plain RequiredFieldsMyObjectArrayElem - var plain Plain - if err := json.Unmarshal(b, &plain); err != nil { - return err - } - *j = RequiredFieldsMyObjectArrayElem(plain) - return nil -} - type RequiredFields struct { // MyBoolean corresponds to the JSON schema field "myBoolean". MyBoolean bool `json:"myBoolean" yaml:"myBoolean" mapstructure:"myBoolean"` @@ -91,6 +43,54 @@ type RequiredFields struct { MyStringArray []string `json:"myStringArray" yaml:"myStringArray" mapstructure:"myStringArray"` } +type RequiredFieldsMyObject struct { + // MyNestedObjectString corresponds to the JSON schema field + // "myNestedObjectString". + MyNestedObjectString string `json:"myNestedObjectString" yaml:"myNestedObjectString" mapstructure:"myNestedObjectString"` +} + +type RequiredFieldsMyObjectArrayElem struct { + // MyNestedObjectString corresponds to the JSON schema field + // "myNestedObjectString". + MyNestedObjectString string `json:"myNestedObjectString" yaml:"myNestedObjectString" mapstructure:"myNestedObjectString"` +} + +// UnmarshalJSON implements json.Unmarshaler. +func (j *RequiredFieldsMyObjectArrayElem) UnmarshalJSON(b []byte) error { + var raw map[string]interface{} + if err := json.Unmarshal(b, &raw); err != nil { + return err + } + if v, ok := raw["myNestedObjectString"]; !ok || v == nil { + return fmt.Errorf("field myNestedObjectString in RequiredFieldsMyObjectArrayElem: required") + } + type Plain RequiredFieldsMyObjectArrayElem + var plain Plain + if err := json.Unmarshal(b, &plain); err != nil { + return err + } + *j = RequiredFieldsMyObjectArrayElem(plain) + return nil +} + +// UnmarshalJSON implements json.Unmarshaler. +func (j *RequiredFieldsMyObject) UnmarshalJSON(b []byte) error { + var raw map[string]interface{} + if err := json.Unmarshal(b, &raw); err != nil { + return err + } + if v, ok := raw["myNestedObjectString"]; !ok || v == nil { + return fmt.Errorf("field myNestedObjectString in RequiredFieldsMyObject: required") + } + type Plain RequiredFieldsMyObject + var plain Plain + if err := json.Unmarshal(b, &plain); err != nil { + return err + } + *j = RequiredFieldsMyObject(plain) + return nil +} + // UnmarshalJSON implements json.Unmarshaler. func (j *RequiredFields) UnmarshalJSON(b []byte) error { var raw map[string]interface{} diff --git a/tests/data/validation/typed_default_enums/typed_default_enums.go b/tests/data/validation/typed_default_enums/typed_default_enums.go index 922d9324..e72e48b3 100644 --- a/tests/data/validation/typed_default_enums/typed_default_enums.go +++ b/tests/data/validation/typed_default_enums/typed_default_enums.go @@ -6,8 +6,16 @@ import "encoding/json" import "fmt" import "reflect" +type TypedDefaultEnums struct { + // Some corresponds to the JSON schema field "some". + Some TypedDefaultEnumsSome `json:"some,omitempty" yaml:"some,omitempty" mapstructure:"some,omitempty"` +} + type TypedDefaultEnumsSome string +const TypedDefaultEnumsSomeOther TypedDefaultEnumsSome = "other" +const TypedDefaultEnumsSomeRandom TypedDefaultEnumsSome = "random" + var enumValues_TypedDefaultEnumsSome = []interface{}{ "random", "other", @@ -33,14 +41,6 @@ func (j *TypedDefaultEnumsSome) UnmarshalJSON(b []byte) error { return nil } -type TypedDefaultEnums struct { - // Some corresponds to the JSON schema field "some". - Some TypedDefaultEnumsSome `json:"some,omitempty" yaml:"some,omitempty" mapstructure:"some,omitempty"` -} - -const TypedDefaultEnumsSomeOther TypedDefaultEnumsSome = "other" -const TypedDefaultEnumsSomeRandom TypedDefaultEnumsSome = "random" - // UnmarshalJSON implements json.Unmarshaler. func (j *TypedDefaultEnums) UnmarshalJSON(b []byte) error { var raw map[string]interface{}