diff --git a/pkg/generator/generate.go b/pkg/generator/generate.go index 0593a2bf..45ffe2ad 100644 --- a/pkg/generator/generate.go +++ b/pkg/generator/generate.go @@ -613,6 +613,8 @@ func (g *schemaGenerator) generateDeclaredType(t *schemas.Type, scope nameScope) if t.IsSubSchemaTypeElem() || len(validators) > 0 { g.generateUnmarshaler(decl, validators) } + + return &codegen.NamedType{Decl: &decl}, nil } if _, ok := theType.(*codegen.MapType); ok { diff --git a/pkg/generator/json_formatter.go b/pkg/generator/json_formatter.go index 478c4f79..1e2728d5 100644 --- a/pkg/generator/json_formatter.go +++ b/pkg/generator/json_formatter.go @@ -21,15 +21,15 @@ func (jf *jsonFormatter) generate( ) func(*codegen.Emitter) { return func(out *codegen.Emitter) { out.Commentf("Unmarshal%s implements %s.Unmarshaler.", strings.ToUpper(formatJSON), formatJSON) - out.Printlnf("func (j *%s) Unmarshal%s(b []byte) error {", declType.Name, strings.ToUpper(formatJSON)) + out.Printlnf("func (j *%s) Unmarshal%s(value []byte) error {", declType.Name, strings.ToUpper(formatJSON)) out.Indent(1) out.Printlnf("var %s map[string]interface{}", varNameRawMap) - out.Printlnf("if err := %s.Unmarshal(b, &%s); err != nil { return err }", + out.Printlnf("if err := %s.Unmarshal(value, &%s); err != nil { return err }", formatJSON, varNameRawMap) for _, v := range validators { - if v.desc().beforeJSONUnmarshal { - v.generate(out) + if v.desc().beforeUnmarshal { + v.generate(out, "json") } } @@ -43,12 +43,12 @@ func (jf *jsonFormatter) generate( out.Printlnf("type %s %s", tp, declType.Name) out.Printlnf("var %s %s", varNamePlainStruct, tp) - out.Printlnf("if err := %s.Unmarshal(b, &%s); err != nil { return err }", + out.Printlnf("if err := %s.Unmarshal(value, &%s); err != nil { return err }", formatJSON, varNamePlainStruct) for _, v := range validators { - if !v.desc().beforeJSONUnmarshal { - v.generate(out) + if !v.desc().beforeUnmarshal { + v.generate(out, "json") } } diff --git a/pkg/generator/validator.go b/pkg/generator/validator.go index 03ca73f6..fb90ec77 100644 --- a/pkg/generator/validator.go +++ b/pkg/generator/validator.go @@ -12,13 +12,13 @@ import ( ) type validator interface { - generate(out *codegen.Emitter) + generate(out *codegen.Emitter, format string) desc() *validatorDesc } type validatorDesc struct { - hasError bool - beforeJSONUnmarshal bool + hasError bool + beforeUnmarshal bool } var ( @@ -35,7 +35,7 @@ type requiredValidator struct { declName string } -func (v *requiredValidator) generate(out *codegen.Emitter) { +func (v *requiredValidator) generate(out *codegen.Emitter, format string) { out.Printlnf(`if v, ok := %s["%s"]; !ok || v == nil {`, varNameRawMap, v.jsonName) out.Indent(1) out.Printlnf(`return fmt.Errorf("field %s in %s: required")`, v.jsonName, v.declName) @@ -45,8 +45,8 @@ func (v *requiredValidator) generate(out *codegen.Emitter) { func (v *requiredValidator) desc() *validatorDesc { return &validatorDesc{ - hasError: true, - beforeJSONUnmarshal: true, + hasError: true, + beforeUnmarshal: true, } } @@ -56,7 +56,7 @@ type nullTypeValidator struct { arrayDepth int } -func (v *nullTypeValidator) generate(out *codegen.Emitter) { +func (v *nullTypeValidator) generate(out *codegen.Emitter, format string) { value := fmt.Sprintf("%s.%s", varNamePlainStruct, v.fieldName) fieldName := v.jsonName @@ -91,8 +91,8 @@ func (v *nullTypeValidator) generate(out *codegen.Emitter) { func (v *nullTypeValidator) desc() *validatorDesc { return &validatorDesc{ - hasError: true, - beforeJSONUnmarshal: false, + hasError: true, + beforeUnmarshal: false, } } @@ -103,7 +103,7 @@ type defaultValidator struct { defaultValue interface{} } -func (v *defaultValidator) generate(out *codegen.Emitter) { +func (v *defaultValidator) generate(out *codegen.Emitter, format string) { defaultValue := v.dumpDefaultValue(out) out.Printlnf(`if v, ok := %s["%s"]; !ok || v == nil {`, varNameRawMap, v.jsonName) @@ -165,8 +165,8 @@ func (v *defaultValidator) tryDumpDefaultSlice(maxLineLen uint) (string, error) func (v *defaultValidator) desc() *validatorDesc { return &validatorDesc{ - hasError: false, - beforeJSONUnmarshal: false, + hasError: false, + beforeUnmarshal: false, } } @@ -178,7 +178,7 @@ type arrayValidator struct { maxItems int } -func (v *arrayValidator) generate(out *codegen.Emitter) { +func (v *arrayValidator) generate(out *codegen.Emitter, format string) { if v.minItems == 0 && v.maxItems == 0 { return } @@ -227,8 +227,8 @@ func (v *arrayValidator) generate(out *codegen.Emitter) { func (v *arrayValidator) desc() *validatorDesc { return &validatorDesc{ - hasError: true, - beforeJSONUnmarshal: false, + hasError: true, + beforeUnmarshal: false, } } @@ -240,7 +240,7 @@ type stringValidator struct { isNillable bool } -func (v *stringValidator) generate(out *codegen.Emitter) { +func (v *stringValidator) generate(out *codegen.Emitter, format string) { if v.minLength == 0 && v.maxLength == 0 { return } @@ -275,8 +275,8 @@ func (v *stringValidator) generate(out *codegen.Emitter) { func (v *stringValidator) desc() *validatorDesc { return &validatorDesc{ - hasError: true, - beforeJSONUnmarshal: false, + hasError: true, + beforeUnmarshal: false, } } @@ -285,7 +285,7 @@ type anyOfValidator struct { elemCount int } -func (v *anyOfValidator) generate(out *codegen.Emitter) { +func (v *anyOfValidator) generate(out *codegen.Emitter, format string) { for i := 0; i < v.elemCount; i++ { out.Printlnf(`var %s_%d %s_%d`, lowerFirst(v.fieldName), i, upperFirst(v.fieldName), i) } @@ -293,7 +293,12 @@ func (v *anyOfValidator) generate(out *codegen.Emitter) { out.Printlnf(`var errs []error`) for i := 0; i < v.elemCount; i++ { - out.Printlnf(`if err := %s_%d.UnmarshalJSON(b); err != nil {`, lowerFirst(v.fieldName), i) + out.Printlnf( + `if err := %s_%d.Unmarshal%s(value); err != nil {`, + lowerFirst(v.fieldName), + i, + strings.ToUpper(format), + ) out.Indent(1) out.Printlnf(`errs = append(errs, err)`) out.Indent(-1) @@ -309,8 +314,8 @@ func (v *anyOfValidator) generate(out *codegen.Emitter) { func (v *anyOfValidator) desc() *validatorDesc { return &validatorDesc{ - hasError: true, - beforeJSONUnmarshal: true, + hasError: true, + beforeUnmarshal: true, } } diff --git a/pkg/generator/yaml_formatter.go b/pkg/generator/yaml_formatter.go index cf333c34..7dac4a6e 100644 --- a/pkg/generator/yaml_formatter.go +++ b/pkg/generator/yaml_formatter.go @@ -30,8 +30,8 @@ func (yf *yamlFormatter) generate( out.Printlnf("if err := value.Decode(&%s); err != nil { return err }", varNameRawMap) for _, v := range validators { - if v.desc().beforeJSONUnmarshal { - v.generate(out) + if v.desc().beforeUnmarshal { + v.generate(out, "yaml") } } @@ -48,8 +48,8 @@ func (yf *yamlFormatter) generate( out.Printlnf("if err := value.Decode(&%s); err != nil { return err }", varNamePlainStruct) for _, v := range validators { - if !v.desc().beforeJSONUnmarshal { - v.generate(out) + if !v.desc().beforeUnmarshal { + v.generate(out, "yaml") } } diff --git a/tests/data/core/10.2.1.1_allOf.go b/tests/data/core/10.2.1.1_allOf.go deleted file mode 100644 index 6144517c..00000000 --- a/tests/data/core/10.2.1.1_allOf.go +++ /dev/null @@ -1,61 +0,0 @@ -// Code generated by github.com/atombender/go-jsonschema, DO NOT EDIT. - -package test - -import "encoding/json" -import "fmt" - -type A10211AllOfConfigurationsElem struct { - // Bar corresponds to the JSON schema field "bar". - Bar float64 `json:"bar" yaml:"bar" mapstructure:"bar"` - - // Foo corresponds to the JSON schema field "foo". - Foo string `json:"foo" yaml:"foo" mapstructure:"foo"` -} - -// UnmarshalJSON implements json.Unmarshaler. -func (j *A10211AllOfConfigurationsElem) UnmarshalJSON(b []byte) error { - var raw map[string]interface{} - if err := json.Unmarshal(b, &raw); err != nil { - return err - } - if v, ok := raw["bar"]; !ok || v == nil { - return fmt.Errorf("field bar in A10211AllOfConfigurationsElem: required") - } - if v, ok := raw["foo"]; !ok || v == nil { - return fmt.Errorf("field foo in A10211AllOfConfigurationsElem: required") - } - type Plain A10211AllOfConfigurationsElem - var plain Plain - if err := json.Unmarshal(b, &plain); err != nil { - return err - } - *j = A10211AllOfConfigurationsElem(plain) - return nil -} - -// UnmarshalJSON implements json.Unmarshaler. -func (j *A10211AllOfConfigurationsElem) UnmarshalJSON(b []byte) error { - var raw map[string]interface{} - if err := json.Unmarshal(b, &raw); err != nil { - return err - } - if v, ok := raw["bar"]; !ok || v == nil { - return fmt.Errorf("field bar in A10211AllOfConfigurationsElem: required") - } - if v, ok := raw["foo"]; !ok || v == nil { - return fmt.Errorf("field foo in A10211AllOfConfigurationsElem: required") - } - type Plain A10211AllOfConfigurationsElem - var plain Plain - if err := json.Unmarshal(b, &plain); err != nil { - return err - } - *j = A10211AllOfConfigurationsElem(plain) - return nil -} - -type A10211AllOf struct { - // Configurations corresponds to the JSON schema field "configurations". - Configurations []A10211AllOfConfigurationsElem `json:"configurations,omitempty" yaml:"configurations,omitempty" mapstructure:"configurations,omitempty"` -} diff --git a/tests/data/core/10.2.1.2_anyOf.go b/tests/data/core/10.2.1.2_anyOf.go deleted file mode 100644 index 7c1fece2..00000000 --- a/tests/data/core/10.2.1.2_anyOf.go +++ /dev/null @@ -1,159 +0,0 @@ -// Code generated by github.com/atombender/go-jsonschema, DO NOT EDIT. - -package test - -import "encoding/json" -import "errors" -import "fmt" - -type A10212AnyOfConfigurationsElem_0 struct { - // Foo corresponds to the JSON schema field "foo". - Foo string `json:"foo" yaml:"foo" mapstructure:"foo"` -} - -// UnmarshalJSON implements json.Unmarshaler. -func (j *A10212AnyOfConfigurationsElem_0) UnmarshalJSON(b []byte) error { - var raw map[string]interface{} - if err := json.Unmarshal(b, &raw); err != nil { - return err - } - if v, ok := raw["foo"]; !ok || v == nil { - return fmt.Errorf("field foo in A10212AnyOfConfigurationsElem_0: required") - } - type Plain A10212AnyOfConfigurationsElem_0 - var plain Plain - if err := json.Unmarshal(b, &plain); err != nil { - return err - } - *j = A10212AnyOfConfigurationsElem_0(plain) - return nil -} - -// UnmarshalJSON implements json.Unmarshaler. -func (j *A10212AnyOfConfigurationsElem_0) UnmarshalJSON(b []byte) error { - var raw map[string]interface{} - if err := json.Unmarshal(b, &raw); err != nil { - return err - } - if v, ok := raw["foo"]; !ok || v == nil { - return fmt.Errorf("field foo in A10212AnyOfConfigurationsElem_0: required") - } - type Plain A10212AnyOfConfigurationsElem_0 - var plain Plain - if err := json.Unmarshal(b, &plain); err != nil { - return err - } - *j = A10212AnyOfConfigurationsElem_0(plain) - return nil -} - -type A10212AnyOfConfigurationsElem_1 struct { - // Bar corresponds to the JSON schema field "bar". - Bar float64 `json:"bar" yaml:"bar" mapstructure:"bar"` -} - -// UnmarshalJSON implements json.Unmarshaler. -func (j *A10212AnyOfConfigurationsElem_1) UnmarshalJSON(b []byte) error { - var raw map[string]interface{} - if err := json.Unmarshal(b, &raw); err != nil { - return err - } - if v, ok := raw["bar"]; !ok || v == nil { - return fmt.Errorf("field bar in A10212AnyOfConfigurationsElem_1: required") - } - type Plain A10212AnyOfConfigurationsElem_1 - var plain Plain - if err := json.Unmarshal(b, &plain); err != nil { - return err - } - *j = A10212AnyOfConfigurationsElem_1(plain) - return nil -} - -// UnmarshalJSON implements json.Unmarshaler. -func (j *A10212AnyOfConfigurationsElem_1) UnmarshalJSON(b []byte) error { - var raw map[string]interface{} - if err := json.Unmarshal(b, &raw); err != nil { - return err - } - if v, ok := raw["bar"]; !ok || v == nil { - return fmt.Errorf("field bar in A10212AnyOfConfigurationsElem_1: required") - } - type Plain A10212AnyOfConfigurationsElem_1 - var plain Plain - if err := json.Unmarshal(b, &plain); err != nil { - return err - } - *j = A10212AnyOfConfigurationsElem_1(plain) - return nil -} - -type A10212AnyOfConfigurationsElem_2 struct { - // Baz corresponds to the JSON schema field "baz". - Baz *bool `json:"baz,omitempty" yaml:"baz,omitempty" mapstructure:"baz,omitempty"` -} - -// UnmarshalJSON implements json.Unmarshaler. -func (j *A10212AnyOfConfigurationsElem_2) UnmarshalJSON(b []byte) error { - var raw map[string]interface{} - if err := json.Unmarshal(b, &raw); err != nil { - return err - } - type Plain A10212AnyOfConfigurationsElem_2 - var plain Plain - if err := json.Unmarshal(b, &plain); err != nil { - return err - } - *j = A10212AnyOfConfigurationsElem_2(plain) - return nil -} - -type A10212AnyOfConfigurationsElem struct { - // Bar corresponds to the JSON schema field "bar". - Bar float64 `json:"bar" yaml:"bar" mapstructure:"bar"` - - // Baz corresponds to the JSON schema field "baz". - Baz *bool `json:"baz,omitempty" yaml:"baz,omitempty" mapstructure:"baz,omitempty"` - - // Foo corresponds to the JSON schema field "foo". - Foo string `json:"foo" yaml:"foo" mapstructure:"foo"` -} - -// UnmarshalJSON implements json.Unmarshaler. -func (j *A10212AnyOfConfigurationsElem) UnmarshalJSON(b []byte) error { - var raw map[string]interface{} - if err := json.Unmarshal(b, &raw); err != nil { - return err - } - var a10212AnyOfConfigurationsElem_0 A10212AnyOfConfigurationsElem_0 - var a10212AnyOfConfigurationsElem_1 A10212AnyOfConfigurationsElem_1 - var a10212AnyOfConfigurationsElem_2 A10212AnyOfConfigurationsElem_2 - var errs []error - if err := a10212AnyOfConfigurationsElem_0.UnmarshalJSON(b); err != nil { - errs = append(errs, err) - } - if err := a10212AnyOfConfigurationsElem_1.UnmarshalJSON(b); err != nil { - errs = append(errs, err) - } - if err := a10212AnyOfConfigurationsElem_2.UnmarshalJSON(b); err != nil { - errs = append(errs, err) - } - if len(errs) == 3 { - return fmt.Errorf("all validators failed: %s", errors.Join(errs...)) - } - type Plain A10212AnyOfConfigurationsElem - var plain Plain - if err := json.Unmarshal(b, &plain); err != nil { - return err - } - *j = A10212AnyOfConfigurationsElem(plain) - return nil -} - -type A10212AnyOf struct { - // Configurations corresponds to the JSON schema field "configurations". - Configurations []A10212AnyOfConfigurationsElem `json:"configurations,omitempty" yaml:"configurations,omitempty" mapstructure:"configurations,omitempty"` - - // Flags corresponds to the JSON schema field "flags". - Flags interface{} `json:"flags,omitempty" yaml:"flags,omitempty" mapstructure:"flags,omitempty"` -} diff --git a/tests/data/core/allOf/allOf.go b/tests/data/core/allOf/allOf.go new file mode 100644 index 00000000..fcd447e0 --- /dev/null +++ b/tests/data/core/allOf/allOf.go @@ -0,0 +1,62 @@ +// Code generated by github.com/atombender/go-jsonschema, DO NOT EDIT. + +package test + +import "encoding/json" +import "fmt" +import yaml "gopkg.in/yaml.v3" + +type AllOfConfigurationsElem struct { + // Bar corresponds to the JSON schema field "bar". + Bar float64 `json:"bar" yaml:"bar" mapstructure:"bar"` + + // Foo corresponds to the JSON schema field "foo". + Foo string `json:"foo" yaml:"foo" mapstructure:"foo"` +} + +// UnmarshalJSON implements json.Unmarshaler. +func (j *AllOfConfigurationsElem) UnmarshalJSON(value []byte) error { + var raw map[string]interface{} + if err := json.Unmarshal(value, &raw); err != nil { + return err + } + if v, ok := raw["bar"]; !ok || v == nil { + return fmt.Errorf("field bar in AllOfConfigurationsElem: required") + } + if v, ok := raw["foo"]; !ok || v == nil { + return fmt.Errorf("field foo in AllOfConfigurationsElem: required") + } + type Plain AllOfConfigurationsElem + var plain Plain + if err := json.Unmarshal(value, &plain); err != nil { + return err + } + *j = AllOfConfigurationsElem(plain) + return nil +} + +// UnmarshalYAML implements yaml.Unmarshaler. +func (j *AllOfConfigurationsElem) UnmarshalYAML(value *yaml.Node) error { + var raw map[string]interface{} + if err := value.Decode(&raw); err != nil { + return err + } + if v, ok := raw["bar"]; !ok || v == nil { + return fmt.Errorf("field bar in AllOfConfigurationsElem: required") + } + if v, ok := raw["foo"]; !ok || v == nil { + return fmt.Errorf("field foo in AllOfConfigurationsElem: required") + } + type Plain AllOfConfigurationsElem + var plain Plain + if err := value.Decode(&plain); err != nil { + return err + } + *j = AllOfConfigurationsElem(plain) + return nil +} + +type AllOf struct { + // Configurations corresponds to the JSON schema field "configurations". + Configurations []AllOfConfigurationsElem `json:"configurations,omitempty" yaml:"configurations,omitempty" mapstructure:"configurations,omitempty"` +} diff --git a/tests/data/core/10.2.1.1_allOf.json b/tests/data/core/allOf/allOf.json similarity index 100% rename from tests/data/core/10.2.1.1_allOf.json rename to tests/data/core/allOf/allOf.json diff --git a/tests/data/core/anyOf/anyOf.go b/tests/data/core/anyOf/anyOf.go new file mode 100644 index 00000000..358396a1 --- /dev/null +++ b/tests/data/core/anyOf/anyOf.go @@ -0,0 +1,206 @@ +// Code generated by github.com/atombender/go-jsonschema, DO NOT EDIT. + +package test + +import "encoding/json" +import "errors" +import "fmt" +import yaml "gopkg.in/yaml.v3" + +type AnyOf struct { + // Configurations corresponds to the JSON schema field "configurations". + Configurations []AnyOfConfigurationsElem `json:"configurations,omitempty" yaml:"configurations,omitempty" mapstructure:"configurations,omitempty"` + + // Flags corresponds to the JSON schema field "flags". + Flags interface{} `json:"flags,omitempty" yaml:"flags,omitempty" mapstructure:"flags,omitempty"` +} + +type AnyOfConfigurationsElem struct { + // Bar corresponds to the JSON schema field "bar". + Bar float64 `json:"bar" yaml:"bar" mapstructure:"bar"` + + // Baz corresponds to the JSON schema field "baz". + Baz *bool `json:"baz,omitempty" yaml:"baz,omitempty" mapstructure:"baz,omitempty"` + + // Foo corresponds to the JSON schema field "foo". + Foo string `json:"foo" yaml:"foo" mapstructure:"foo"` +} + +type AnyOfConfigurationsElem_0 struct { + // Foo corresponds to the JSON schema field "foo". + Foo string `json:"foo" yaml:"foo" mapstructure:"foo"` +} + +type AnyOfConfigurationsElem_1 struct { + // Bar corresponds to the JSON schema field "bar". + Bar float64 `json:"bar" yaml:"bar" mapstructure:"bar"` +} + +// UnmarshalJSON implements json.Unmarshaler. +func (j *AnyOfConfigurationsElem_1) UnmarshalJSON(value []byte) error { + var raw map[string]interface{} + if err := json.Unmarshal(value, &raw); err != nil { + return err + } + if v, ok := raw["bar"]; !ok || v == nil { + return fmt.Errorf("field bar in AnyOfConfigurationsElem_1: required") + } + type Plain AnyOfConfigurationsElem_1 + var plain Plain + if err := json.Unmarshal(value, &plain); err != nil { + return err + } + *j = AnyOfConfigurationsElem_1(plain) + return nil +} + +// UnmarshalYAML implements yaml.Unmarshaler. +func (j *AnyOfConfigurationsElem_1) UnmarshalYAML(value *yaml.Node) error { + var raw map[string]interface{} + if err := value.Decode(&raw); err != nil { + return err + } + if v, ok := raw["bar"]; !ok || v == nil { + return fmt.Errorf("field bar in AnyOfConfigurationsElem_1: required") + } + type Plain AnyOfConfigurationsElem_1 + var plain Plain + if err := value.Decode(&plain); err != nil { + return err + } + *j = AnyOfConfigurationsElem_1(plain) + return nil +} + +type AnyOfConfigurationsElem_2 struct { + // Baz corresponds to the JSON schema field "baz". + Baz *bool `json:"baz,omitempty" yaml:"baz,omitempty" mapstructure:"baz,omitempty"` +} + +// UnmarshalJSON implements json.Unmarshaler. +func (j *AnyOfConfigurationsElem_2) UnmarshalJSON(value []byte) error { + var raw map[string]interface{} + if err := json.Unmarshal(value, &raw); err != nil { + return err + } + type Plain AnyOfConfigurationsElem_2 + var plain Plain + if err := json.Unmarshal(value, &plain); err != nil { + return err + } + *j = AnyOfConfigurationsElem_2(plain) + return nil +} + +// UnmarshalYAML implements yaml.Unmarshaler. +func (j *AnyOfConfigurationsElem_2) UnmarshalYAML(value *yaml.Node) error { + var raw map[string]interface{} + if err := value.Decode(&raw); err != nil { + return err + } + type Plain AnyOfConfigurationsElem_2 + var plain Plain + if err := value.Decode(&plain); err != nil { + return err + } + *j = AnyOfConfigurationsElem_2(plain) + return nil +} + +// UnmarshalYAML implements yaml.Unmarshaler. +func (j *AnyOfConfigurationsElem_0) UnmarshalYAML(value *yaml.Node) error { + var raw map[string]interface{} + if err := value.Decode(&raw); err != nil { + return err + } + if v, ok := raw["foo"]; !ok || v == nil { + return fmt.Errorf("field foo in AnyOfConfigurationsElem_0: required") + } + type Plain AnyOfConfigurationsElem_0 + var plain Plain + if err := value.Decode(&plain); err != nil { + return err + } + *j = AnyOfConfigurationsElem_0(plain) + return nil +} + +// UnmarshalJSON implements json.Unmarshaler. +func (j *AnyOfConfigurationsElem) UnmarshalJSON(value []byte) error { + var raw map[string]interface{} + if err := json.Unmarshal(value, &raw); err != nil { + return err + } + var anyOfConfigurationsElem_0 AnyOfConfigurationsElem_0 + var anyOfConfigurationsElem_1 AnyOfConfigurationsElem_1 + var anyOfConfigurationsElem_2 AnyOfConfigurationsElem_2 + var errs []error + if err := anyOfConfigurationsElem_0.UnmarshalJSON(value); err != nil { + errs = append(errs, err) + } + if err := anyOfConfigurationsElem_1.UnmarshalJSON(value); err != nil { + errs = append(errs, err) + } + if err := anyOfConfigurationsElem_2.UnmarshalJSON(value); err != nil { + errs = append(errs, err) + } + if len(errs) == 3 { + return fmt.Errorf("all validators failed: %s", errors.Join(errs...)) + } + type Plain AnyOfConfigurationsElem + var plain Plain + if err := json.Unmarshal(value, &plain); err != nil { + return err + } + *j = AnyOfConfigurationsElem(plain) + return nil +} + +// UnmarshalYAML implements yaml.Unmarshaler. +func (j *AnyOfConfigurationsElem) UnmarshalYAML(value *yaml.Node) error { + var raw map[string]interface{} + if err := value.Decode(&raw); err != nil { + return err + } + var anyOfConfigurationsElem_0 AnyOfConfigurationsElem_0 + var anyOfConfigurationsElem_1 AnyOfConfigurationsElem_1 + var anyOfConfigurationsElem_2 AnyOfConfigurationsElem_2 + var errs []error + if err := anyOfConfigurationsElem_0.UnmarshalYAML(value); err != nil { + errs = append(errs, err) + } + if err := anyOfConfigurationsElem_1.UnmarshalYAML(value); err != nil { + errs = append(errs, err) + } + if err := anyOfConfigurationsElem_2.UnmarshalYAML(value); err != nil { + errs = append(errs, err) + } + if len(errs) == 3 { + return fmt.Errorf("all validators failed: %s", errors.Join(errs...)) + } + type Plain AnyOfConfigurationsElem + var plain Plain + if err := value.Decode(&plain); err != nil { + return err + } + *j = AnyOfConfigurationsElem(plain) + return nil +} + +// UnmarshalJSON implements json.Unmarshaler. +func (j *AnyOfConfigurationsElem_0) UnmarshalJSON(value []byte) error { + var raw map[string]interface{} + if err := json.Unmarshal(value, &raw); err != nil { + return err + } + if v, ok := raw["foo"]; !ok || v == nil { + return fmt.Errorf("field foo in AnyOfConfigurationsElem_0: required") + } + type Plain AnyOfConfigurationsElem_0 + var plain Plain + if err := json.Unmarshal(value, &plain); err != nil { + return err + } + *j = AnyOfConfigurationsElem_0(plain) + return nil +} diff --git a/tests/data/core/10.2.1.2_anyOf.json b/tests/data/core/anyOf/anyOf.json similarity index 100% rename from tests/data/core/10.2.1.2_anyOf.json rename to tests/data/core/anyOf/anyOf.json diff --git a/tests/data/core/array/array.go b/tests/data/core/array/array.go index 9b91d96c..c7a83351 100644 --- a/tests/data/core/array/array.go +++ b/tests/data/core/array/array.go @@ -4,6 +4,7 @@ package test import "encoding/json" import "fmt" +import yaml "gopkg.in/yaml.v3" type Array struct { // MyArray corresponds to the JSON schema field "myArray". @@ -34,14 +35,41 @@ type Array struct { type ArrayMyObjectArrayElem map[string]interface{} // UnmarshalJSON implements json.Unmarshaler. -func (j *Array) UnmarshalJSON(b []byte) error { +func (j *Array) UnmarshalJSON(value []byte) error { var raw map[string]interface{} - if err := json.Unmarshal(b, &raw); err != nil { + if err := json.Unmarshal(value, &raw); err != nil { return err } type Plain Array var plain Plain - if err := json.Unmarshal(b, &plain); err != nil { + if err := json.Unmarshal(value, &plain); err != nil { + return err + } + for i0 := range plain.MyNestedNullArray { + for i1 := range plain.MyNestedNullArray[i0] { + if plain.MyNestedNullArray[i0][i1] != nil { + return fmt.Errorf("field %s: must be null", fmt.Sprintf("myNestedNullArray[%d][%d]", i0, i1)) + } + } + } + for i0 := range plain.MyNullArray { + if plain.MyNullArray[i0] != nil { + return fmt.Errorf("field %s: must be null", fmt.Sprintf("myNullArray[%d]", i0)) + } + } + *j = Array(plain) + return nil +} + +// UnmarshalYAML implements yaml.Unmarshaler. +func (j *Array) UnmarshalYAML(value *yaml.Node) error { + var raw map[string]interface{} + if err := value.Decode(&raw); err != nil { + return err + } + type Plain Array + var plain Plain + if err := value.Decode(&plain); err != nil { return err } for i0 := range plain.MyNestedNullArray { diff --git a/tests/data/core/date/date.go b/tests/data/core/date/date.go index adbf75d1..55cf5763 100644 --- a/tests/data/core/date/date.go +++ b/tests/data/core/date/date.go @@ -5,6 +5,7 @@ package test import "encoding/json" import "fmt" import "github.com/atombender/go-jsonschema/pkg/types" +import yaml "gopkg.in/yaml.v3" type DateMyObject struct { // MyDate corresponds to the JSON schema field "myDate". @@ -12,9 +13,9 @@ type DateMyObject struct { } // UnmarshalJSON implements json.Unmarshaler. -func (j *DateMyObject) UnmarshalJSON(b []byte) error { +func (j *DateMyObject) UnmarshalJSON(value []byte) error { var raw map[string]interface{} - if err := json.Unmarshal(b, &raw); err != nil { + if err := json.Unmarshal(value, &raw); err != nil { return err } if v, ok := raw["myDate"]; !ok || v == nil { @@ -22,7 +23,25 @@ func (j *DateMyObject) UnmarshalJSON(b []byte) error { } type Plain DateMyObject var plain Plain - if err := json.Unmarshal(b, &plain); err != nil { + if err := json.Unmarshal(value, &plain); err != nil { + return err + } + *j = DateMyObject(plain) + return nil +} + +// UnmarshalYAML implements yaml.Unmarshaler. +func (j *DateMyObject) UnmarshalYAML(value *yaml.Node) error { + var raw map[string]interface{} + if err := value.Decode(&raw); err != nil { + return err + } + if v, ok := raw["myDate"]; !ok || v == nil { + return fmt.Errorf("field myDate in DateMyObject: required") + } + type Plain DateMyObject + var plain Plain + if err := value.Decode(&plain); err != nil { return err } *j = DateMyObject(plain) diff --git a/tests/data/core/dateTime/dateTime.go b/tests/data/core/dateTime/dateTime.go index b2e40e0a..6b4a7b22 100644 --- a/tests/data/core/dateTime/dateTime.go +++ b/tests/data/core/dateTime/dateTime.go @@ -4,6 +4,7 @@ package test import "encoding/json" import "fmt" +import yaml "gopkg.in/yaml.v3" import "time" type DateTimeMyObject struct { @@ -12,9 +13,9 @@ type DateTimeMyObject struct { } // UnmarshalJSON implements json.Unmarshaler. -func (j *DateTimeMyObject) UnmarshalJSON(b []byte) error { +func (j *DateTimeMyObject) UnmarshalJSON(value []byte) error { var raw map[string]interface{} - if err := json.Unmarshal(b, &raw); err != nil { + if err := json.Unmarshal(value, &raw); err != nil { return err } if v, ok := raw["myDateTime"]; !ok || v == nil { @@ -22,7 +23,25 @@ func (j *DateTimeMyObject) UnmarshalJSON(b []byte) error { } type Plain DateTimeMyObject var plain Plain - if err := json.Unmarshal(b, &plain); err != nil { + if err := json.Unmarshal(value, &plain); err != nil { + return err + } + *j = DateTimeMyObject(plain) + return nil +} + +// UnmarshalYAML implements yaml.Unmarshaler. +func (j *DateTimeMyObject) UnmarshalYAML(value *yaml.Node) error { + var raw map[string]interface{} + if err := value.Decode(&raw); err != nil { + return err + } + if v, ok := raw["myDateTime"]; !ok || v == nil { + return fmt.Errorf("field myDateTime in DateTimeMyObject: required") + } + type Plain DateTimeMyObject + var plain Plain + if err := value.Decode(&plain); err != nil { return err } *j = DateTimeMyObject(plain) diff --git a/tests/data/core/ip/ip.go b/tests/data/core/ip/ip.go index 5794f272..5f80010d 100644 --- a/tests/data/core/ip/ip.go +++ b/tests/data/core/ip/ip.go @@ -4,6 +4,7 @@ package test import "encoding/json" import "fmt" +import yaml "gopkg.in/yaml.v3" import "net/netip" type IpMyObject struct { @@ -12,9 +13,9 @@ type IpMyObject struct { } // UnmarshalJSON implements json.Unmarshaler. -func (j *IpMyObject) UnmarshalJSON(b []byte) error { +func (j *IpMyObject) UnmarshalJSON(value []byte) error { var raw map[string]interface{} - if err := json.Unmarshal(b, &raw); err != nil { + if err := json.Unmarshal(value, &raw); err != nil { return err } if v, ok := raw["myIp"]; !ok || v == nil { @@ -22,7 +23,25 @@ func (j *IpMyObject) UnmarshalJSON(b []byte) error { } type Plain IpMyObject var plain Plain - if err := json.Unmarshal(b, &plain); err != nil { + if err := json.Unmarshal(value, &plain); err != nil { + return err + } + *j = IpMyObject(plain) + return nil +} + +// UnmarshalYAML implements yaml.Unmarshaler. +func (j *IpMyObject) UnmarshalYAML(value *yaml.Node) error { + var raw map[string]interface{} + if err := value.Decode(&raw); err != nil { + return err + } + if v, ok := raw["myIp"]; !ok || v == nil { + return fmt.Errorf("field myIp in IpMyObject: required") + } + type Plain IpMyObject + var plain Plain + if err := value.Decode(&plain); err != nil { return err } *j = IpMyObject(plain) diff --git a/tests/data/core/object/object.go b/tests/data/core/object/object.go index cb852542..e1fcbf08 100644 --- a/tests/data/core/object/object.go +++ b/tests/data/core/object/object.go @@ -4,6 +4,7 @@ package test import "encoding/json" import "fmt" +import yaml "gopkg.in/yaml.v3" type ObjectMyObject struct { // MyString corresponds to the JSON schema field "myString". @@ -11,9 +12,9 @@ type ObjectMyObject struct { } // UnmarshalJSON implements json.Unmarshaler. -func (j *ObjectMyObject) UnmarshalJSON(b []byte) error { +func (j *ObjectMyObject) UnmarshalJSON(value []byte) error { var raw map[string]interface{} - if err := json.Unmarshal(b, &raw); err != nil { + if err := json.Unmarshal(value, &raw); err != nil { return err } if v, ok := raw["myString"]; !ok || v == nil { @@ -21,7 +22,25 @@ func (j *ObjectMyObject) UnmarshalJSON(b []byte) error { } type Plain ObjectMyObject var plain Plain - if err := json.Unmarshal(b, &plain); err != nil { + if err := json.Unmarshal(value, &plain); err != nil { + return err + } + *j = ObjectMyObject(plain) + return nil +} + +// UnmarshalYAML implements yaml.Unmarshaler. +func (j *ObjectMyObject) UnmarshalYAML(value *yaml.Node) error { + var raw map[string]interface{} + if err := value.Decode(&raw); err != nil { + return err + } + if v, ok := raw["myString"]; !ok || v == nil { + return fmt.Errorf("field myString in ObjectMyObject: required") + } + type Plain ObjectMyObject + var plain Plain + if err := value.Decode(&plain); err != nil { return err } *j = ObjectMyObject(plain) diff --git a/tests/data/core/objectAdditionalProperties/objectAdditionalProperties.go b/tests/data/core/objectAdditionalProperties/objectAdditionalProperties.go index 358312c2..d1041f1a 100644 --- a/tests/data/core/objectAdditionalProperties/objectAdditionalProperties.go +++ b/tests/data/core/objectAdditionalProperties/objectAdditionalProperties.go @@ -3,6 +3,7 @@ package test import "encoding/json" +import yaml "gopkg.in/yaml.v3" type ObjectAdditionalProperties struct { // Foo corresponds to the JSON schema field "foo". @@ -12,14 +13,32 @@ type ObjectAdditionalProperties struct { type ObjectAdditionalPropertiesFoo map[string]string // UnmarshalJSON implements json.Unmarshaler. -func (j *ObjectAdditionalProperties) UnmarshalJSON(b []byte) error { +func (j *ObjectAdditionalProperties) UnmarshalJSON(value []byte) error { var raw map[string]interface{} - if err := json.Unmarshal(b, &raw); err != nil { + if err := json.Unmarshal(value, &raw); err != nil { return err } type Plain ObjectAdditionalProperties var plain Plain - if err := json.Unmarshal(b, &plain); err != nil { + if err := json.Unmarshal(value, &plain); err != nil { + return err + } + if v, ok := raw["foo"]; !ok || v == nil { + plain.Foo = map[string]string{} + } + *j = ObjectAdditionalProperties(plain) + return nil +} + +// UnmarshalYAML implements yaml.Unmarshaler. +func (j *ObjectAdditionalProperties) UnmarshalYAML(value *yaml.Node) error { + var raw map[string]interface{} + if err := value.Decode(&raw); err != nil { + return err + } + type Plain ObjectAdditionalProperties + var plain Plain + if err := value.Decode(&plain); err != nil { return err } if v, ok := raw["foo"]; !ok || v == nil { diff --git a/tests/data/core/objectPropertiesDefault.go b/tests/data/core/objectPropertiesDefault/objectPropertiesDefault.go similarity index 59% rename from tests/data/core/objectPropertiesDefault.go rename to tests/data/core/objectPropertiesDefault/objectPropertiesDefault.go index 1f935bfa..af6be8c3 100644 --- a/tests/data/core/objectPropertiesDefault.go +++ b/tests/data/core/objectPropertiesDefault/objectPropertiesDefault.go @@ -5,6 +5,7 @@ package test import "encoding/json" import "errors" import "fmt" +import yaml "gopkg.in/yaml.v3" import "reflect" type DecoratedPlanner struct { @@ -40,33 +41,56 @@ type EventName string const EventNameBIRTHDAY EventName = "BIRTHDAY" const EventNameGAME EventName = "GAME" -const EventNameHOLIDAY EventName = "HOLIDAY" - -type EventTagsElem string - -const EventTagsElemCITY EventTagsElem = "CITY" -// UnmarshalJSON implements json.Unmarshaler. -func (j *DecoratedPlanner) UnmarshalJSON(b []byte) error { +// UnmarshalYAML implements yaml.Unmarshaler. +func (j *DecoratedPlannerDecorator) UnmarshalYAML(value *yaml.Node) error { var raw map[string]interface{} - if err := json.Unmarshal(b, &raw); err != nil { + if err := value.Decode(&raw); err != nil { return err } - type Plain DecoratedPlanner + type Plain DecoratedPlannerDecorator var plain Plain - if err := json.Unmarshal(b, &plain); err != nil { + if err := value.Decode(&plain); err != nil { return err } - if v, ok := raw["decorator"]; !ok || v == nil { - plain.Decorator = DecoratedPlannerDecorator{ - Color: "#ffffff", - Theme: nil, + if v, ok := raw["color"]; !ok || v == nil { + plain.Color = "#ffffff" + } + *j = DecoratedPlannerDecorator(plain) + return nil +} + +// UnmarshalJSON implements json.Unmarshaler. +func (j *EventName) UnmarshalJSON(b []byte) error { + var v string + if err := json.Unmarshal(b, &v); err != nil { + return err + } + var ok bool + for _, expected := range enumValues_EventName { + if reflect.DeepEqual(v, expected) { + ok = true + break } } - *j = DecoratedPlanner(plain) + if !ok { + return fmt.Errorf("invalid value (expected one of %#v): %#v", enumValues_EventName, v) + } + *j = EventName(v) return nil } +const EventNameHOLIDAY EventName = "HOLIDAY" + +type EventTagsElem string + +var enumValues_EventTagsElem = []interface{}{ + "COUNTRY", + "REGION", + "CITY", + "PERSON", +} + // UnmarshalJSON implements json.Unmarshaler. func (j *EventTagsElem) UnmarshalJSON(b []byte) error { var v string @@ -87,29 +111,29 @@ func (j *EventTagsElem) UnmarshalJSON(b []byte) error { return nil } -const EventTagsElemCOUNTRY EventTagsElem = "COUNTRY" -const EventTagsElemREGION EventTagsElem = "REGION" - -// UnmarshalJSON implements json.Unmarshaler. -func (j *EventName) UnmarshalJSON(b []byte) error { +// UnmarshalYAML implements yaml.Unmarshaler. +func (j *EventTagsElem) UnmarshalYAML(value *yaml.Node) error { var v string - if err := json.Unmarshal(b, &v); err != nil { + if err := value.Decode(&v); err != nil { return err } var ok bool - for _, expected := range enumValues_EventName { + for _, expected := range enumValues_EventTagsElem { if reflect.DeepEqual(v, expected) { ok = true break } } if !ok { - return fmt.Errorf("invalid value (expected one of %#v): %#v", enumValues_EventName, v) + return fmt.Errorf("invalid value (expected one of %#v): %#v", enumValues_EventTagsElem, v) } - *j = EventName(v) + *j = EventTagsElem(v) return nil } +const EventTagsElemCOUNTRY EventTagsElem = "COUNTRY" +const EventTagsElemREGION EventTagsElem = "REGION" +const EventTagsElemCITY EventTagsElem = "CITY" const EventTagsElemPERSON EventTagsElem = "PERSON" var enumValues_EventName = []interface{}{ @@ -119,14 +143,14 @@ var enumValues_EventName = []interface{}{ } // UnmarshalJSON implements json.Unmarshaler. -func (j *Event) UnmarshalJSON(b []byte) error { +func (j *Event) UnmarshalJSON(value []byte) error { var raw map[string]interface{} - if err := json.Unmarshal(b, &raw); err != nil { + if err := json.Unmarshal(value, &raw); err != nil { return err } type Plain Event var plain Plain - if err := json.Unmarshal(b, &plain); err != nil { + if err := json.Unmarshal(value, &plain); err != nil { return err } if v, ok := raw["tags"]; !ok || v == nil { @@ -136,15 +160,15 @@ func (j *Event) UnmarshalJSON(b []byte) error { return nil } -// UnmarshalJSON implements json.Unmarshaler. -func (j *Event) UnmarshalJSON(b []byte) error { +// UnmarshalYAML implements yaml.Unmarshaler. +func (j *Event) UnmarshalYAML(value *yaml.Node) error { var raw map[string]interface{} - if err := json.Unmarshal(b, &raw); err != nil { + if err := value.Decode(&raw); err != nil { return err } type Plain Event var plain Plain - if err := json.Unmarshal(b, &plain); err != nil { + if err := value.Decode(&plain); err != nil { return err } if v, ok := raw["tags"]; !ok || v == nil { @@ -154,40 +178,56 @@ func (j *Event) UnmarshalJSON(b []byte) error { return nil } +// UnmarshalYAML implements yaml.Unmarshaler. +func (j *EventName) UnmarshalYAML(value *yaml.Node) error { + var v string + if err := value.Decode(&v); err != nil { + return err + } + var ok bool + for _, expected := range enumValues_EventName { + if reflect.DeepEqual(v, expected) { + ok = true + break + } + } + if !ok { + return fmt.Errorf("invalid value (expected one of %#v): %#v", enumValues_EventName, v) + } + *j = EventName(v) + return nil +} + // UnmarshalJSON implements json.Unmarshaler. -func (j *DecoratedPlannerDecorator) UnmarshalJSON(b []byte) error { +func (j *DecoratedPlanner) UnmarshalJSON(value []byte) error { var raw map[string]interface{} - if err := json.Unmarshal(b, &raw); err != nil { + if err := json.Unmarshal(value, &raw); err != nil { return err } - type Plain DecoratedPlannerDecorator + type Plain DecoratedPlanner var plain Plain - if err := json.Unmarshal(b, &plain); err != nil { + if err := json.Unmarshal(value, &plain); err != nil { return err } - if v, ok := raw["color"]; !ok || v == nil { - plain.Color = "#ffffff" + if v, ok := raw["decorator"]; !ok || v == nil { + plain.Decorator = DecoratedPlannerDecorator{ + Color: "#ffffff", + Theme: nil, + } } - *j = DecoratedPlannerDecorator(plain) + *j = DecoratedPlanner(plain) return nil } -var enumValues_EventTagsElem = []interface{}{ - "COUNTRY", - "REGION", - "CITY", - "PERSON", -} - -// UnmarshalJSON implements json.Unmarshaler. -func (j *DecoratedPlanner) UnmarshalJSON(b []byte) error { +// UnmarshalYAML implements yaml.Unmarshaler. +func (j *DecoratedPlanner) UnmarshalYAML(value *yaml.Node) error { var raw map[string]interface{} - if err := json.Unmarshal(b, &raw); err != nil { + if err := value.Decode(&raw); err != nil { return err } type Plain DecoratedPlanner var plain Plain - if err := json.Unmarshal(b, &plain); err != nil { + if err := value.Decode(&plain); err != nil { return err } if v, ok := raw["decorator"]; !ok || v == nil { @@ -201,14 +241,14 @@ func (j *DecoratedPlanner) UnmarshalJSON(b []byte) error { } // UnmarshalJSON implements json.Unmarshaler. -func (j *DecoratedPlannerDecorator) UnmarshalJSON(b []byte) error { +func (j *DecoratedPlannerDecorator) UnmarshalJSON(value []byte) error { var raw map[string]interface{} - if err := json.Unmarshal(b, &raw); err != nil { + if err := json.Unmarshal(value, &raw); err != nil { return err } type Plain DecoratedPlannerDecorator var plain Plain - if err := json.Unmarshal(b, &plain); err != nil { + if err := json.Unmarshal(value, &plain); err != nil { return err } if v, ok := raw["color"]; !ok || v == nil { @@ -224,14 +264,29 @@ type ObjectPropertiesDefaultPlannersElem_0 struct { } // UnmarshalJSON implements json.Unmarshaler. -func (j *ObjectPropertiesDefaultPlannersElem_0) UnmarshalJSON(b []byte) error { +func (j *ObjectPropertiesDefaultPlannersElem_0) UnmarshalJSON(value []byte) error { + var raw map[string]interface{} + if err := json.Unmarshal(value, &raw); err != nil { + return err + } + type Plain ObjectPropertiesDefaultPlannersElem_0 + var plain Plain + if err := json.Unmarshal(value, &plain); err != nil { + return err + } + *j = ObjectPropertiesDefaultPlannersElem_0(plain) + return nil +} + +// UnmarshalYAML implements yaml.Unmarshaler. +func (j *ObjectPropertiesDefaultPlannersElem_0) UnmarshalYAML(value *yaml.Node) error { var raw map[string]interface{} - if err := json.Unmarshal(b, &raw); err != nil { + if err := value.Decode(&raw); err != nil { return err } type Plain ObjectPropertiesDefaultPlannersElem_0 var plain Plain - if err := json.Unmarshal(b, &plain); err != nil { + if err := value.Decode(&plain); err != nil { return err } *j = ObjectPropertiesDefaultPlannersElem_0(plain) @@ -244,14 +299,29 @@ type ObjectPropertiesDefaultPlannersElem_1 struct { } // UnmarshalJSON implements json.Unmarshaler. -func (j *ObjectPropertiesDefaultPlannersElem_1) UnmarshalJSON(b []byte) error { +func (j *ObjectPropertiesDefaultPlannersElem_1) UnmarshalJSON(value []byte) error { + var raw map[string]interface{} + if err := json.Unmarshal(value, &raw); err != nil { + return err + } + type Plain ObjectPropertiesDefaultPlannersElem_1 + var plain Plain + if err := json.Unmarshal(value, &plain); err != nil { + return err + } + *j = ObjectPropertiesDefaultPlannersElem_1(plain) + return nil +} + +// UnmarshalYAML implements yaml.Unmarshaler. +func (j *ObjectPropertiesDefaultPlannersElem_1) UnmarshalYAML(value *yaml.Node) error { var raw map[string]interface{} - if err := json.Unmarshal(b, &raw); err != nil { + if err := value.Decode(&raw); err != nil { return err } type Plain ObjectPropertiesDefaultPlannersElem_1 var plain Plain - if err := json.Unmarshal(b, &plain); err != nil { + if err := value.Decode(&plain); err != nil { return err } *j = ObjectPropertiesDefaultPlannersElem_1(plain) @@ -267,18 +337,45 @@ type ObjectPropertiesDefaultPlannersElem struct { } // UnmarshalJSON implements json.Unmarshaler. -func (j *ObjectPropertiesDefaultPlannersElem) UnmarshalJSON(b []byte) error { +func (j *ObjectPropertiesDefaultPlannersElem) UnmarshalJSON(value []byte) error { + var raw map[string]interface{} + if err := json.Unmarshal(value, &raw); err != nil { + return err + } + var objectPropertiesDefaultPlannersElem_0 ObjectPropertiesDefaultPlannersElem_0 + var objectPropertiesDefaultPlannersElem_1 ObjectPropertiesDefaultPlannersElem_1 + var errs []error + if err := objectPropertiesDefaultPlannersElem_0.UnmarshalJSON(value); err != nil { + errs = append(errs, err) + } + if err := objectPropertiesDefaultPlannersElem_1.UnmarshalJSON(value); err != nil { + errs = append(errs, err) + } + if len(errs) == 2 { + return fmt.Errorf("all validators failed: %s", errors.Join(errs...)) + } + type Plain ObjectPropertiesDefaultPlannersElem + var plain Plain + if err := json.Unmarshal(value, &plain); err != nil { + return err + } + *j = ObjectPropertiesDefaultPlannersElem(plain) + return nil +} + +// UnmarshalYAML implements yaml.Unmarshaler. +func (j *ObjectPropertiesDefaultPlannersElem) UnmarshalYAML(value *yaml.Node) error { var raw map[string]interface{} - if err := json.Unmarshal(b, &raw); err != nil { + if err := value.Decode(&raw); err != nil { return err } var objectPropertiesDefaultPlannersElem_0 ObjectPropertiesDefaultPlannersElem_0 var objectPropertiesDefaultPlannersElem_1 ObjectPropertiesDefaultPlannersElem_1 var errs []error - if err := objectPropertiesDefaultPlannersElem_0.UnmarshalJSON(b); err != nil { + if err := objectPropertiesDefaultPlannersElem_0.UnmarshalYAML(value); err != nil { errs = append(errs, err) } - if err := objectPropertiesDefaultPlannersElem_1.UnmarshalJSON(b); err != nil { + if err := objectPropertiesDefaultPlannersElem_1.UnmarshalYAML(value); err != nil { errs = append(errs, err) } if len(errs) == 2 { @@ -286,7 +383,7 @@ func (j *ObjectPropertiesDefaultPlannersElem) UnmarshalJSON(b []byte) error { } type Plain ObjectPropertiesDefaultPlannersElem var plain Plain - if err := json.Unmarshal(b, &plain); err != nil { + if err := value.Decode(&plain); err != nil { return err } *j = ObjectPropertiesDefaultPlannersElem(plain) diff --git a/tests/data/core/objectPropertiesDefault.json b/tests/data/core/objectPropertiesDefault/objectPropertiesDefault.json similarity index 100% rename from tests/data/core/objectPropertiesDefault.json rename to tests/data/core/objectPropertiesDefault/objectPropertiesDefault.json diff --git a/tests/data/core/primitives/primitives.go b/tests/data/core/primitives/primitives.go index 65671635..78662486 100644 --- a/tests/data/core/primitives/primitives.go +++ b/tests/data/core/primitives/primitives.go @@ -4,6 +4,7 @@ package test import "encoding/json" import "fmt" +import yaml "gopkg.in/yaml.v3" type Primitives struct { // MyBoolean corresponds to the JSON schema field "myBoolean". @@ -23,14 +24,32 @@ type Primitives struct { } // UnmarshalJSON implements json.Unmarshaler. -func (j *Primitives) UnmarshalJSON(b []byte) error { +func (j *Primitives) UnmarshalJSON(value []byte) error { var raw map[string]interface{} - if err := json.Unmarshal(b, &raw); err != nil { + if err := json.Unmarshal(value, &raw); err != nil { return err } type Plain Primitives var plain Plain - if err := json.Unmarshal(b, &plain); err != nil { + if err := json.Unmarshal(value, &plain); err != nil { + return err + } + if plain.MyNull != nil { + return fmt.Errorf("field %s: must be null", "myNull") + } + *j = Primitives(plain) + return nil +} + +// UnmarshalYAML implements yaml.Unmarshaler. +func (j *Primitives) UnmarshalYAML(value *yaml.Node) error { + var raw map[string]interface{} + if err := value.Decode(&raw); err != nil { + return err + } + type Plain Primitives + var plain Plain + if err := value.Decode(&plain); err != nil { return err } if plain.MyNull != nil { diff --git a/tests/data/core/refToEnum/refToEnum.go b/tests/data/core/refToEnum/refToEnum.go index aac189ad..e1a8eb7a 100644 --- a/tests/data/core/refToEnum/refToEnum.go +++ b/tests/data/core/refToEnum/refToEnum.go @@ -4,6 +4,7 @@ package test import "encoding/json" import "fmt" +import yaml "gopkg.in/yaml.v3" import "reflect" type Thing string @@ -33,6 +34,26 @@ func (j *Thing) UnmarshalJSON(b []byte) error { return nil } +// UnmarshalYAML implements yaml.Unmarshaler. +func (j *Thing) UnmarshalYAML(value *yaml.Node) error { + var v string + if err := value.Decode(&v); err != nil { + return err + } + var ok bool + for _, expected := range enumValues_Thing { + if reflect.DeepEqual(v, expected) { + ok = true + break + } + } + if !ok { + return fmt.Errorf("invalid value (expected one of %#v): %#v", enumValues_Thing, v) + } + *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"` diff --git a/tests/data/core/time/time.go b/tests/data/core/time/time.go index f79004f7..944b80a1 100644 --- a/tests/data/core/time/time.go +++ b/tests/data/core/time/time.go @@ -5,6 +5,7 @@ package test import "encoding/json" import "fmt" import "github.com/atombender/go-jsonschema/pkg/types" +import yaml "gopkg.in/yaml.v3" type TimeMyObject struct { // MyTime corresponds to the JSON schema field "myTime". @@ -12,9 +13,9 @@ type TimeMyObject struct { } // UnmarshalJSON implements json.Unmarshaler. -func (j *TimeMyObject) UnmarshalJSON(b []byte) error { +func (j *TimeMyObject) UnmarshalJSON(value []byte) error { var raw map[string]interface{} - if err := json.Unmarshal(b, &raw); err != nil { + if err := json.Unmarshal(value, &raw); err != nil { return err } if v, ok := raw["myTime"]; !ok || v == nil { @@ -22,7 +23,25 @@ func (j *TimeMyObject) UnmarshalJSON(b []byte) error { } type Plain TimeMyObject var plain Plain - if err := json.Unmarshal(b, &plain); err != nil { + if err := json.Unmarshal(value, &plain); err != nil { + return err + } + *j = TimeMyObject(plain) + return nil +} + +// UnmarshalYAML implements yaml.Unmarshaler. +func (j *TimeMyObject) UnmarshalYAML(value *yaml.Node) error { + var raw map[string]interface{} + if err := value.Decode(&raw); err != nil { + return err + } + if v, ok := raw["myTime"]; !ok || v == nil { + return fmt.Errorf("field myTime in TimeMyObject: required") + } + type Plain TimeMyObject + var plain Plain + if err := value.Decode(&plain); err != nil { return err } *j = TimeMyObject(plain) diff --git a/tests/data/extraImports/gopkgYAMLv3/gopkgYAMLv3.go b/tests/data/extraImports/gopkgYAMLv3/gopkgYAMLv3.go index 3f366e24..9191a910 100644 --- a/tests/data/extraImports/gopkgYAMLv3/gopkgYAMLv3.go +++ b/tests/data/extraImports/gopkgYAMLv3/gopkgYAMLv3.go @@ -78,14 +78,14 @@ const GopkgYAMLv3MyEnumX GopkgYAMLv3MyEnum = "x" const GopkgYAMLv3MyEnumY GopkgYAMLv3MyEnum = "y" // UnmarshalJSON implements json.Unmarshaler. -func (j *GopkgYAMLv3) UnmarshalJSON(b []byte) error { +func (j *GopkgYAMLv3) UnmarshalJSON(value []byte) error { var raw map[string]interface{} - if err := json.Unmarshal(b, &raw); err != nil { + if err := json.Unmarshal(value, &raw); err != nil { return err } type Plain GopkgYAMLv3 var plain Plain - if err := json.Unmarshal(b, &plain); err != nil { + if err := json.Unmarshal(value, &plain); err != nil { return err } if plain.MyNull != nil { diff --git a/tests/data/misc/specialCharacters/specialCharacters.go b/tests/data/misc/specialCharacters/specialCharacters.go index eb3eb26d..f9300854 100644 --- a/tests/data/misc/specialCharacters/specialCharacters.go +++ b/tests/data/misc/specialCharacters/specialCharacters.go @@ -4,6 +4,7 @@ package test import "encoding/json" import "fmt" +import yaml "gopkg.in/yaml.v3" import "reflect" type License string @@ -33,54 +34,125 @@ type SpecialCharacters struct { 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+" +// UnmarshalJSON implements json.Unmarshaler. +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_License { + if reflect.DeepEqual(v, expected) { + ok = true + break + } + } + if !ok { + return fmt.Errorf("invalid value (expected one of %#v): %#v", enumValues_License, v) + } + *j = License(v) + return nil +} -var enumValues_License = []interface{}{ - "GPL-3.0", - "MIT", +// UnmarshalJSON implements json.Unmarshaler. +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_License_1 { + if reflect.DeepEqual(v, expected) { + ok = true + break + } + } + if !ok { + return fmt.Errorf("invalid value (expected one of %#v): %#v", enumValues_License_1, v) + } + *j = License_1(v) + return nil } -var enumValues_License_1 = []interface{}{ - "GPL-3.0+", - "MIT+", + +// UnmarshalYAML implements yaml.Unmarshaler. +func (j *License) UnmarshalYAML(value *yaml.Node) error { + var v string + if err := value.Decode(&v); err != nil { + return err + } + var ok bool + 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_License, v) + } + *j = License(v) + return nil } + +const SpecialCharactersPlainLicensesMIT SpecialCharactersPlainLicenses = "MIT" + var enumValues_SpecialCharactersPlainLicenses = []interface{}{ "GPL-3.0", "MIT", } -var enumValues_SpecialCharactersPlusLicenses = []interface{}{ + +// UnmarshalJSON implements json.Unmarshaler. +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_SpecialCharactersPlainLicenses { + if reflect.DeepEqual(v, expected) { + ok = true + break + } + } + if !ok { + return fmt.Errorf("invalid value (expected one of %#v): %#v", enumValues_SpecialCharactersPlainLicenses, v) + } + *j = SpecialCharactersPlainLicenses(v) + return nil +} + +type SpecialCharactersPlusLicenses string + +var enumValues_License_1 = []interface{}{ "GPL-3.0+", "MIT+", } -// UnmarshalJSON implements json.Unmarshaler. -func (j *SpecialCharactersPlusLicenses) UnmarshalJSON(b []byte) error { +// UnmarshalYAML implements yaml.Unmarshaler. +func (j *License_1) UnmarshalYAML(value *yaml.Node) error { var v string - if err := json.Unmarshal(b, &v); err != nil { + if err := value.Decode(&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 } -// UnmarshalJSON implements json.Unmarshaler. -func (j *SpecialCharactersPlainLicenses) UnmarshalJSON(b []byte) error { +// UnmarshalYAML implements yaml.Unmarshaler. +func (j *SpecialCharactersPlainLicenses) UnmarshalYAML(value *yaml.Node) error { var v string - if err := json.Unmarshal(b, &v); err != nil { + if err := value.Decode(&v); err != nil { return err } var ok bool @@ -97,42 +169,55 @@ func (j *SpecialCharactersPlainLicenses) UnmarshalJSON(b []byte) error { return nil } +var enumValues_SpecialCharactersPlusLicenses = []interface{}{ + "GPL-3.0+", + "MIT+", +} + // UnmarshalJSON implements json.Unmarshaler. -func (j *License_1) 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_1 { + 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_1, v) + return fmt.Errorf("invalid value (expected one of %#v): %#v", enumValues_SpecialCharactersPlusLicenses, v) } - *j = License_1(v) + *j = SpecialCharactersPlusLicenses(v) return nil } -// UnmarshalJSON implements json.Unmarshaler. -func (j *License) UnmarshalJSON(b []byte) error { +// UnmarshalYAML implements yaml.Unmarshaler. +func (j *SpecialCharactersPlusLicenses) UnmarshalYAML(value *yaml.Node) error { var v string - if err := json.Unmarshal(b, &v); err != nil { + if err := value.Decode(&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 } + +const SpecialCharactersPlusLicensesGPL30 SpecialCharactersPlusLicenses = "GPL-3.0+" +const SpecialCharactersPlusLicensesMIT SpecialCharactersPlusLicenses = "MIT+" + +var enumValues_License = []interface{}{ + "GPL-3.0", + "MIT", +} diff --git a/tests/data/miscWithDefaults/cyclicAndRequired1/cyclicAndRequired1.go b/tests/data/miscWithDefaults/cyclicAndRequired1/cyclicAndRequired1.go index 7534c336..897c4f9c 100644 --- a/tests/data/miscWithDefaults/cyclicAndRequired1/cyclicAndRequired1.go +++ b/tests/data/miscWithDefaults/cyclicAndRequired1/cyclicAndRequired1.go @@ -4,6 +4,7 @@ package test import "encoding/json" import "fmt" +import yaml "gopkg.in/yaml.v3" type Foo struct { // RefToBar corresponds to the JSON schema field "refToBar". @@ -11,9 +12,9 @@ type Foo struct { } // UnmarshalJSON implements json.Unmarshaler. -func (j *Foo) UnmarshalJSON(b []byte) error { +func (j *Foo) UnmarshalJSON(value []byte) error { var raw map[string]interface{} - if err := json.Unmarshal(b, &raw); err != nil { + if err := json.Unmarshal(value, &raw); err != nil { return err } if v, ok := raw["refToBar"]; !ok || v == nil { @@ -21,7 +22,25 @@ func (j *Foo) UnmarshalJSON(b []byte) error { } type Plain Foo var plain Plain - if err := json.Unmarshal(b, &plain); err != nil { + if err := json.Unmarshal(value, &plain); err != nil { + return err + } + *j = Foo(plain) + return nil +} + +// UnmarshalYAML implements yaml.Unmarshaler. +func (j *Foo) UnmarshalYAML(value *yaml.Node) error { + var raw map[string]interface{} + if err := value.Decode(&raw); err != nil { + return err + } + if v, ok := raw["refToBar"]; !ok || v == nil { + return fmt.Errorf("field refToBar in Foo: required") + } + type Plain Foo + var plain Plain + if err := value.Decode(&plain); err != nil { return err } *j = Foo(plain) diff --git a/tests/data/regressions/issue32/issue32.go b/tests/data/regressions/issue32/issue32.go index 6dc056be..1fda8c7e 100644 --- a/tests/data/regressions/issue32/issue32.go +++ b/tests/data/regressions/issue32/issue32.go @@ -4,6 +4,7 @@ package test import "encoding/json" import "fmt" +import yaml "gopkg.in/yaml.v3" type TestObject struct { // Config corresponds to the JSON schema field "config". @@ -19,9 +20,9 @@ type TestObject struct { type TestObjectConfig map[string]interface{} // UnmarshalJSON implements json.Unmarshaler. -func (j *TestObject) UnmarshalJSON(b []byte) error { +func (j *TestObject) UnmarshalJSON(value []byte) error { var raw map[string]interface{} - if err := json.Unmarshal(b, &raw); err != nil { + if err := json.Unmarshal(value, &raw); err != nil { return err } if v, ok := raw["name"]; !ok || v == nil { @@ -32,7 +33,28 @@ func (j *TestObject) UnmarshalJSON(b []byte) error { } type Plain TestObject var plain Plain - if err := json.Unmarshal(b, &plain); err != nil { + if err := json.Unmarshal(value, &plain); err != nil { + return err + } + *j = TestObject(plain) + return nil +} + +// UnmarshalYAML implements yaml.Unmarshaler. +func (j *TestObject) UnmarshalYAML(value *yaml.Node) error { + var raw map[string]interface{} + if err := value.Decode(&raw); err != nil { + return err + } + if v, ok := raw["name"]; !ok || v == nil { + return fmt.Errorf("field name in TestObject: required") + } + if v, ok := raw["owner"]; !ok || v == nil { + return fmt.Errorf("field owner in TestObject: required") + } + type Plain TestObject + var plain Plain + if err := value.Decode(&plain); err != nil { return err } *j = TestObject(plain) diff --git a/tests/data/validation/enum/enum.go b/tests/data/validation/enum/enum.go index 616c7954..cc38f18d 100644 --- a/tests/data/validation/enum/enum.go +++ b/tests/data/validation/enum/enum.go @@ -4,112 +4,354 @@ package test import "encoding/json" import "fmt" +import yaml "gopkg.in/yaml.v3" import "reflect" -type Enum struct { - // MyBooleanTypedEnum corresponds to the JSON schema field "myBooleanTypedEnum". - MyBooleanTypedEnum *EnumMyBooleanTypedEnum `json:"myBooleanTypedEnum,omitempty" yaml:"myBooleanTypedEnum,omitempty" mapstructure:"myBooleanTypedEnum,omitempty"` - - // MyBooleanUntypedEnum corresponds to the JSON schema field - // "myBooleanUntypedEnum". - MyBooleanUntypedEnum *EnumMyBooleanUntypedEnum `json:"myBooleanUntypedEnum,omitempty" yaml:"myBooleanUntypedEnum,omitempty" mapstructure:"myBooleanUntypedEnum,omitempty"` +type EnumMyBooleanTypedEnum bool - // MyIntegerTypedEnum corresponds to the JSON schema field "myIntegerTypedEnum". - MyIntegerTypedEnum *EnumMyIntegerTypedEnum `json:"myIntegerTypedEnum,omitempty" yaml:"myIntegerTypedEnum,omitempty" mapstructure:"myIntegerTypedEnum,omitempty"` +var enumValues_EnumMyBooleanTypedEnum = []interface{}{ + true, + false, +} - // MyMixedTypeEnum corresponds to the JSON schema field "myMixedTypeEnum". - MyMixedTypeEnum *EnumMyMixedTypeEnum `json:"myMixedTypeEnum,omitempty" yaml:"myMixedTypeEnum,omitempty" mapstructure:"myMixedTypeEnum,omitempty"` +// UnmarshalJSON implements json.Unmarshaler. +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_EnumMyBooleanTypedEnum { + if reflect.DeepEqual(v, expected) { + ok = true + break + } + } + if !ok { + return fmt.Errorf("invalid value (expected one of %#v): %#v", enumValues_EnumMyBooleanTypedEnum, v) + } + *j = EnumMyBooleanTypedEnum(v) + return nil +} - // MyMixedUntypedEnum corresponds to the JSON schema field "myMixedUntypedEnum". - MyMixedUntypedEnum *EnumMyMixedUntypedEnum `json:"myMixedUntypedEnum,omitempty" yaml:"myMixedUntypedEnum,omitempty" mapstructure:"myMixedUntypedEnum,omitempty"` +// UnmarshalYAML implements yaml.Unmarshaler. +func (j *EnumMyBooleanTypedEnum) UnmarshalYAML(value *yaml.Node) error { + var v bool + if err := value.Decode(&v); err != nil { + return err + } + var ok bool + 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_EnumMyBooleanTypedEnum, v) + } + *j = EnumMyBooleanTypedEnum(v) + return nil +} - // MyNullTypedEnum corresponds to the JSON schema field "myNullTypedEnum". - MyNullTypedEnum *EnumMyNullTypedEnum `json:"myNullTypedEnum,omitempty" yaml:"myNullTypedEnum,omitempty" mapstructure:"myNullTypedEnum,omitempty"` +type EnumMyBooleanUntypedEnum bool - // MyNullUntypedEnum corresponds to the JSON schema field "myNullUntypedEnum". - MyNullUntypedEnum *EnumMyNullUntypedEnum `json:"myNullUntypedEnum,omitempty" yaml:"myNullUntypedEnum,omitempty" mapstructure:"myNullUntypedEnum,omitempty"` +var enumValues_EnumMyBooleanUntypedEnum = []interface{}{ + true, + false, +} - // MyNumberTypedEnum corresponds to the JSON schema field "myNumberTypedEnum". - MyNumberTypedEnum *EnumMyNumberTypedEnum `json:"myNumberTypedEnum,omitempty" yaml:"myNumberTypedEnum,omitempty" mapstructure:"myNumberTypedEnum,omitempty"` +// UnmarshalJSON implements json.Unmarshaler. +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_EnumMyBooleanUntypedEnum { + if reflect.DeepEqual(v, expected) { + ok = true + break + } + } + if !ok { + return fmt.Errorf("invalid value (expected one of %#v): %#v", enumValues_EnumMyBooleanUntypedEnum, v) + } + *j = EnumMyBooleanUntypedEnum(v) + return nil +} - // MyNumberUntypedEnum corresponds to the JSON schema field "myNumberUntypedEnum". - MyNumberUntypedEnum *EnumMyNumberUntypedEnum `json:"myNumberUntypedEnum,omitempty" yaml:"myNumberUntypedEnum,omitempty" mapstructure:"myNumberUntypedEnum,omitempty"` +// UnmarshalYAML implements yaml.Unmarshaler. +func (j *EnumMyBooleanUntypedEnum) UnmarshalYAML(value *yaml.Node) error { + var v bool + if err := value.Decode(&v); err != nil { + return err + } + var ok bool + 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_EnumMyBooleanUntypedEnum, v) + } + *j = EnumMyBooleanUntypedEnum(v) + return nil +} - // MyStringTypedEnum corresponds to the JSON schema field "myStringTypedEnum". - MyStringTypedEnum *EnumMyStringTypedEnum `json:"myStringTypedEnum,omitempty" yaml:"myStringTypedEnum,omitempty" mapstructure:"myStringTypedEnum,omitempty"` +type EnumMyIntegerTypedEnum int - // MyStringUntypedEnum corresponds to the JSON schema field "myStringUntypedEnum". - MyStringUntypedEnum *EnumMyStringUntypedEnum `json:"myStringUntypedEnum,omitempty" yaml:"myStringUntypedEnum,omitempty" mapstructure:"myStringUntypedEnum,omitempty"` +var enumValues_EnumMyIntegerTypedEnum = []interface{}{ + 1, + 2, + 3, } -type EnumMyBooleanTypedEnum bool - -type EnumMyBooleanUntypedEnum bool +// UnmarshalJSON implements json.Unmarshaler. +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_EnumMyIntegerTypedEnum { + if reflect.DeepEqual(v, expected) { + ok = true + break + } + } + if !ok { + return fmt.Errorf("invalid value (expected one of %#v): %#v", enumValues_EnumMyIntegerTypedEnum, v) + } + *j = EnumMyIntegerTypedEnum(v) + return nil +} -type EnumMyIntegerTypedEnum int +// UnmarshalYAML implements yaml.Unmarshaler. +func (j *EnumMyIntegerTypedEnum) UnmarshalYAML(value *yaml.Node) error { + var v int + if err := value.Decode(&v); err != nil { + return err + } + var ok bool + 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_EnumMyIntegerTypedEnum, v) + } + *j = EnumMyIntegerTypedEnum(v) + return nil +} type EnumMyMixedTypeEnum struct { Value interface{} } +var enumValues_EnumMyMixedTypeEnum = []interface{}{ + 42.0, + "smurf", +} + +// MarshalJSON implements json.Marshaler. +func (j *EnumMyMixedTypeEnum) MarshalJSON() ([]byte, error) { + return json.Marshal(j.Value) +} + +// UnmarshalJSON implements json.Unmarshaler. +func (j *EnumMyMixedTypeEnum) 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_EnumMyMixedTypeEnum { + 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) + } + *j = EnumMyMixedTypeEnum(v) + return nil +} + +// MarshalYAML implements yaml.Marshal. +func (j *EnumMyMixedTypeEnum) MarshalYAML() (interface{}, error) { + return yaml.Marshal(j.Value) +} + +// UnmarshalYAML implements yaml.Unmarshaler. +func (j *EnumMyMixedTypeEnum) UnmarshalYAML(value *yaml.Node) error { + var v struct { + Value interface{} + } + if err := value.Decode(&v.Value); err != nil { + return err + } + var ok bool + 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_EnumMyMixedTypeEnum, v.Value) + } + *j = EnumMyMixedTypeEnum(v) + return nil +} + type EnumMyMixedUntypedEnum struct { Value interface{} } +var enumValues_EnumMyMixedUntypedEnum = []interface{}{ + "red", + 1.0, + true, + nil, +} + +// MarshalJSON implements json.Marshaler. +func (j *EnumMyMixedUntypedEnum) MarshalJSON() ([]byte, error) { + return json.Marshal(j.Value) +} + +// UnmarshalJSON implements json.Unmarshaler. +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_EnumMyMixedUntypedEnum { + 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) + } + *j = EnumMyMixedUntypedEnum(v) + return nil +} + +// MarshalYAML implements yaml.Marshal. +func (j *EnumMyMixedUntypedEnum) MarshalYAML() (interface{}, error) { + return yaml.Marshal(j.Value) +} + +// UnmarshalYAML implements yaml.Unmarshaler. +func (j *EnumMyMixedUntypedEnum) UnmarshalYAML(value *yaml.Node) error { + var v struct { + Value interface{} + } + if err := value.Decode(&v.Value); err != nil { + return err + } + var ok bool + 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_EnumMyMixedUntypedEnum, v.Value) + } + *j = EnumMyMixedUntypedEnum(v) + return nil +} + type EnumMyNullTypedEnum struct { Value interface{} } +var enumValues_EnumMyNullTypedEnum = []interface{}{ + nil, +} + +// MarshalJSON implements json.Marshaler. +func (j *EnumMyNullTypedEnum) MarshalJSON() ([]byte, error) { + return json.Marshal(j.Value) +} + +// UnmarshalJSON implements json.Unmarshaler. +func (j *EnumMyNullTypedEnum) 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_EnumMyNullTypedEnum { + 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) + } + *j = EnumMyNullTypedEnum(v) + return nil +} + +// MarshalYAML implements yaml.Marshal. +func (j *EnumMyNullTypedEnum) MarshalYAML() (interface{}, error) { + return yaml.Marshal(j.Value) +} + +// UnmarshalYAML implements yaml.Unmarshaler. +func (j *EnumMyNullTypedEnum) UnmarshalYAML(value *yaml.Node) error { + var v struct { + Value interface{} + } + if err := value.Decode(&v.Value); err != nil { + return err + } + var ok bool + 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_EnumMyNullTypedEnum, v.Value) + } + *j = EnumMyNullTypedEnum(v) + return nil +} + 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, } +// MarshalJSON implements json.Marshaler. +func (j *EnumMyNullUntypedEnum) MarshalJSON() ([]byte, error) { + return json.Marshal(j.Value) +} + // UnmarshalJSON implements json.Unmarshaler. func (j *EnumMyNullUntypedEnum) UnmarshalJSON(b []byte) error { var v struct { @@ -132,6 +374,35 @@ func (j *EnumMyNullUntypedEnum) UnmarshalJSON(b []byte) error { return nil } +// MarshalYAML implements yaml.Marshal. +func (j *EnumMyNullUntypedEnum) MarshalYAML() (interface{}, error) { + return yaml.Marshal(j.Value) +} + +// UnmarshalYAML implements yaml.Unmarshaler. +func (j *EnumMyNullUntypedEnum) UnmarshalYAML(value *yaml.Node) error { + var v struct { + Value interface{} + } + if err := value.Decode(&v.Value); err != nil { + return err + } + var ok bool + 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_EnumMyNullUntypedEnum, v.Value) + } + *j = EnumMyNullUntypedEnum(v) + return nil +} + +type EnumMyNumberTypedEnum float64 + var enumValues_EnumMyNumberTypedEnum = []interface{}{ 1.0, 2.0, @@ -158,11 +429,28 @@ func (j *EnumMyNumberTypedEnum) UnmarshalJSON(b []byte) error { return nil } -// MarshalJSON implements json.Marshaler. -func (j *EnumMyNullUntypedEnum) MarshalJSON() ([]byte, error) { - return json.Marshal(j.Value) +// UnmarshalYAML implements yaml.Unmarshaler. +func (j *EnumMyNumberTypedEnum) UnmarshalYAML(value *yaml.Node) error { + var v float64 + if err := value.Decode(&v); err != nil { + return err + } + var ok bool + 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_EnumMyNumberTypedEnum, v) + } + *j = EnumMyNumberTypedEnum(v) + return nil } +type EnumMyNumberUntypedEnum float64 + var enumValues_EnumMyNumberUntypedEnum = []interface{}{ 1.0, 2.0, @@ -189,28 +477,28 @@ func (j *EnumMyNumberUntypedEnum) UnmarshalJSON(b []byte) error { return nil } -// UnmarshalJSON implements json.Unmarshaler. -func (j *EnumMyNullTypedEnum) UnmarshalJSON(b []byte) error { - var v struct { - Value interface{} - } - if err := json.Unmarshal(b, &v.Value); err != nil { +// UnmarshalYAML implements yaml.Unmarshaler. +func (j *EnumMyNumberUntypedEnum) UnmarshalYAML(value *yaml.Node) error { + var v float64 + if err := value.Decode(&v); err != nil { return err } var ok bool - for _, expected := range enumValues_EnumMyNullTypedEnum { - if reflect.DeepEqual(v.Value, expected) { + 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_EnumMyNullTypedEnum, v.Value) + return fmt.Errorf("invalid value (expected one of %#v): %#v", enumValues_EnumMyNumberUntypedEnum, v) } - *j = EnumMyNullTypedEnum(v) + *j = EnumMyNumberUntypedEnum(v) return nil } +type EnumMyStringTypedEnum string + var enumValues_EnumMyStringTypedEnum = []interface{}{ "red", "blue", @@ -237,59 +525,31 @@ func (j *EnumMyStringTypedEnum) UnmarshalJSON(b []byte) error { return nil } -// MarshalJSON implements json.Marshaler. -func (j *EnumMyNullTypedEnum) MarshalJSON() ([]byte, error) { - return json.Marshal(j.Value) -} - -// UnmarshalJSON implements json.Unmarshaler. -func (j *EnumMyMixedUntypedEnum) UnmarshalJSON(b []byte) error { - var v struct { - Value interface{} - } - if err := json.Unmarshal(b, &v.Value); err != nil { +// UnmarshalYAML implements yaml.Unmarshaler. +func (j *EnumMyStringTypedEnum) UnmarshalYAML(value *yaml.Node) error { + var v string + if err := value.Decode(&v); err != nil { return err } var ok bool - for _, expected := range enumValues_EnumMyMixedUntypedEnum { - if reflect.DeepEqual(v.Value, expected) { + 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_EnumMyMixedUntypedEnum, v.Value) + return fmt.Errorf("invalid value (expected one of %#v): %#v", enumValues_EnumMyStringTypedEnum, v) } - *j = EnumMyMixedUntypedEnum(v) + *j = EnumMyStringTypedEnum(v) return nil } -// MarshalJSON implements json.Marshaler. -func (j *EnumMyMixedUntypedEnum) MarshalJSON() ([]byte, error) { - return json.Marshal(j.Value) -} +const EnumMyStringTypedEnumBlue EnumMyStringTypedEnum = "blue" +const EnumMyStringTypedEnumGreen EnumMyStringTypedEnum = "green" +const EnumMyStringTypedEnumRed EnumMyStringTypedEnum = "red" -// UnmarshalJSON implements json.Unmarshaler. -func (j *EnumMyMixedTypeEnum) 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_EnumMyMixedTypeEnum { - 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) - } - *j = EnumMyMixedTypeEnum(v) - return nil -} +type EnumMyStringUntypedEnum string var enumValues_EnumMyStringUntypedEnum = []interface{}{ "red", @@ -317,67 +577,62 @@ func (j *EnumMyStringUntypedEnum) UnmarshalJSON(b []byte) error { return nil } -// MarshalJSON implements json.Marshaler. -func (j *EnumMyMixedTypeEnum) MarshalJSON() ([]byte, error) { - return json.Marshal(j.Value) -} - -// UnmarshalJSON implements json.Unmarshaler. -func (j *EnumMyIntegerTypedEnum) UnmarshalJSON(b []byte) error { - var v int - if err := json.Unmarshal(b, &v); err != nil { +// UnmarshalYAML implements yaml.Unmarshaler. +func (j *EnumMyStringUntypedEnum) UnmarshalYAML(value *yaml.Node) error { + var v string + if err := value.Decode(&v); err != nil { return err } var ok bool - for _, expected := range enumValues_EnumMyIntegerTypedEnum { + 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_EnumMyIntegerTypedEnum, v) + return fmt.Errorf("invalid value (expected one of %#v): %#v", enumValues_EnumMyStringUntypedEnum, v) } - *j = EnumMyIntegerTypedEnum(v) + *j = EnumMyStringUntypedEnum(v) return nil } -// UnmarshalJSON implements json.Unmarshaler. -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_EnumMyBooleanUntypedEnum { - if reflect.DeepEqual(v, expected) { - ok = true - break - } - } - if !ok { - return fmt.Errorf("invalid value (expected one of %#v): %#v", enumValues_EnumMyBooleanUntypedEnum, v) - } - *j = EnumMyBooleanUntypedEnum(v) - return nil -} +type Enum struct { + // MyBooleanTypedEnum corresponds to the JSON schema field "myBooleanTypedEnum". + MyBooleanTypedEnum *EnumMyBooleanTypedEnum `json:"myBooleanTypedEnum,omitempty" yaml:"myBooleanTypedEnum,omitempty" mapstructure:"myBooleanTypedEnum,omitempty"` -// UnmarshalJSON implements json.Unmarshaler. -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_EnumMyBooleanTypedEnum { - if reflect.DeepEqual(v, expected) { - ok = true - break - } - } - if !ok { - return fmt.Errorf("invalid value (expected one of %#v): %#v", enumValues_EnumMyBooleanTypedEnum, v) - } - *j = EnumMyBooleanTypedEnum(v) - return nil + // MyBooleanUntypedEnum corresponds to the JSON schema field + // "myBooleanUntypedEnum". + MyBooleanUntypedEnum *EnumMyBooleanUntypedEnum `json:"myBooleanUntypedEnum,omitempty" yaml:"myBooleanUntypedEnum,omitempty" mapstructure:"myBooleanUntypedEnum,omitempty"` + + // MyIntegerTypedEnum corresponds to the JSON schema field "myIntegerTypedEnum". + MyIntegerTypedEnum *EnumMyIntegerTypedEnum `json:"myIntegerTypedEnum,omitempty" yaml:"myIntegerTypedEnum,omitempty" mapstructure:"myIntegerTypedEnum,omitempty"` + + // MyMixedTypeEnum corresponds to the JSON schema field "myMixedTypeEnum". + MyMixedTypeEnum *EnumMyMixedTypeEnum `json:"myMixedTypeEnum,omitempty" yaml:"myMixedTypeEnum,omitempty" mapstructure:"myMixedTypeEnum,omitempty"` + + // MyMixedUntypedEnum corresponds to the JSON schema field "myMixedUntypedEnum". + MyMixedUntypedEnum *EnumMyMixedUntypedEnum `json:"myMixedUntypedEnum,omitempty" yaml:"myMixedUntypedEnum,omitempty" mapstructure:"myMixedUntypedEnum,omitempty"` + + // MyNullTypedEnum corresponds to the JSON schema field "myNullTypedEnum". + MyNullTypedEnum *EnumMyNullTypedEnum `json:"myNullTypedEnum,omitempty" yaml:"myNullTypedEnum,omitempty" mapstructure:"myNullTypedEnum,omitempty"` + + // MyNullUntypedEnum corresponds to the JSON schema field "myNullUntypedEnum". + MyNullUntypedEnum *EnumMyNullUntypedEnum `json:"myNullUntypedEnum,omitempty" yaml:"myNullUntypedEnum,omitempty" mapstructure:"myNullUntypedEnum,omitempty"` + + // MyNumberTypedEnum corresponds to the JSON schema field "myNumberTypedEnum". + MyNumberTypedEnum *EnumMyNumberTypedEnum `json:"myNumberTypedEnum,omitempty" yaml:"myNumberTypedEnum,omitempty" mapstructure:"myNumberTypedEnum,omitempty"` + + // MyNumberUntypedEnum corresponds to the JSON schema field "myNumberUntypedEnum". + MyNumberUntypedEnum *EnumMyNumberUntypedEnum `json:"myNumberUntypedEnum,omitempty" yaml:"myNumberUntypedEnum,omitempty" mapstructure:"myNumberUntypedEnum,omitempty"` + + // MyStringTypedEnum corresponds to the JSON schema field "myStringTypedEnum". + MyStringTypedEnum *EnumMyStringTypedEnum `json:"myStringTypedEnum,omitempty" yaml:"myStringTypedEnum,omitempty" mapstructure:"myStringTypedEnum,omitempty"` + + // MyStringUntypedEnum corresponds to the JSON schema field "myStringUntypedEnum". + MyStringUntypedEnum *EnumMyStringUntypedEnum `json:"myStringUntypedEnum,omitempty" yaml:"myStringUntypedEnum,omitempty" mapstructure:"myStringUntypedEnum,omitempty"` } + +const EnumMyStringUntypedEnumBlue EnumMyStringUntypedEnum = "blue" +const EnumMyStringUntypedEnumGreen EnumMyStringUntypedEnum = "green" +const EnumMyStringUntypedEnumRed EnumMyStringUntypedEnum = "red" diff --git a/tests/data/validation/maxItems/maxItems.go b/tests/data/validation/maxItems/maxItems.go index 562642dc..fbe7a99e 100644 --- a/tests/data/validation/maxItems/maxItems.go +++ b/tests/data/validation/maxItems/maxItems.go @@ -4,6 +4,7 @@ package test import "encoding/json" import "fmt" +import yaml "gopkg.in/yaml.v3" type MaxItems struct { // MyNestedArray corresponds to the JSON schema field "myNestedArray". @@ -14,14 +15,40 @@ type MaxItems struct { } // UnmarshalJSON implements json.Unmarshaler. -func (j *MaxItems) UnmarshalJSON(b []byte) error { +func (j *MaxItems) UnmarshalJSON(value []byte) error { var raw map[string]interface{} - if err := json.Unmarshal(b, &raw); err != nil { + if err := json.Unmarshal(value, &raw); err != nil { return err } type Plain MaxItems var plain Plain - if err := json.Unmarshal(b, &plain); err != nil { + if err := json.Unmarshal(value, &plain); err != nil { + return err + } + if len(plain.MyNestedArray) > 5 { + return fmt.Errorf("field %s length: must be <= %d", "myNestedArray", 5) + } + for i1 := range plain.MyNestedArray { + if len(plain.MyNestedArray[i1]) > 5 { + return fmt.Errorf("field %s length: must be <= %d", fmt.Sprintf("myNestedArray[%d]", i1), 5) + } + } + if len(plain.MyStringArray) > 5 { + return fmt.Errorf("field %s length: must be <= %d", "myStringArray", 5) + } + *j = MaxItems(plain) + return nil +} + +// UnmarshalYAML implements yaml.Unmarshaler. +func (j *MaxItems) UnmarshalYAML(value *yaml.Node) error { + var raw map[string]interface{} + if err := value.Decode(&raw); err != nil { + return err + } + type Plain MaxItems + var plain Plain + if err := value.Decode(&plain); err != nil { return err } if len(plain.MyNestedArray) > 5 { diff --git a/tests/data/validation/maxLength/maxLength.go b/tests/data/validation/maxLength/maxLength.go index 2538e9b0..8ca2452b 100644 --- a/tests/data/validation/maxLength/maxLength.go +++ b/tests/data/validation/maxLength/maxLength.go @@ -4,6 +4,7 @@ package test import "encoding/json" import "fmt" +import yaml "gopkg.in/yaml.v3" type MaxLength struct { // MyNullableString corresponds to the JSON schema field "myNullableString". @@ -14,9 +15,9 @@ type MaxLength struct { } // UnmarshalJSON implements json.Unmarshaler. -func (j *MaxLength) UnmarshalJSON(b []byte) error { +func (j *MaxLength) UnmarshalJSON(value []byte) error { var raw map[string]interface{} - if err := json.Unmarshal(b, &raw); err != nil { + if err := json.Unmarshal(value, &raw); err != nil { return err } if v, ok := raw["myString"]; !ok || v == nil { @@ -24,7 +25,31 @@ func (j *MaxLength) UnmarshalJSON(b []byte) error { } type Plain MaxLength var plain Plain - if err := json.Unmarshal(b, &plain); err != nil { + if err := json.Unmarshal(value, &plain); err != nil { + return err + } + if plain.MyNullableString != nil && len(*plain.MyNullableString) >= 10 { + return fmt.Errorf("field %s length: must be <= %d", "myNullableString", 10) + } + if len(plain.MyString) >= 5 { + return fmt.Errorf("field %s length: must be <= %d", "myString", 5) + } + *j = MaxLength(plain) + return nil +} + +// UnmarshalYAML implements yaml.Unmarshaler. +func (j *MaxLength) UnmarshalYAML(value *yaml.Node) error { + var raw map[string]interface{} + if err := value.Decode(&raw); err != nil { + return err + } + if v, ok := raw["myString"]; !ok || v == nil { + return fmt.Errorf("field myString in MaxLength: required") + } + type Plain MaxLength + var plain Plain + if err := value.Decode(&plain); err != nil { return err } if plain.MyNullableString != nil && len(*plain.MyNullableString) >= 10 { diff --git a/tests/data/validation/minItems/minItems.go b/tests/data/validation/minItems/minItems.go index 17df697c..050b2594 100644 --- a/tests/data/validation/minItems/minItems.go +++ b/tests/data/validation/minItems/minItems.go @@ -4,6 +4,7 @@ package test import "encoding/json" import "fmt" +import yaml "gopkg.in/yaml.v3" type MinItems struct { // MyNestedArray corresponds to the JSON schema field "myNestedArray". @@ -14,14 +15,40 @@ type MinItems struct { } // UnmarshalJSON implements json.Unmarshaler. -func (j *MinItems) UnmarshalJSON(b []byte) error { +func (j *MinItems) UnmarshalJSON(value []byte) error { var raw map[string]interface{} - if err := json.Unmarshal(b, &raw); err != nil { + if err := json.Unmarshal(value, &raw); err != nil { return err } type Plain MinItems var plain Plain - if err := json.Unmarshal(b, &plain); err != nil { + if err := json.Unmarshal(value, &plain); err != nil { + return err + } + if plain.MyNestedArray != nil && len(plain.MyNestedArray) < 5 { + return fmt.Errorf("field %s length: must be >= %d", "myNestedArray", 5) + } + for i1 := range plain.MyNestedArray { + if plain.MyNestedArray[i1] != nil && len(plain.MyNestedArray[i1]) < 5 { + return fmt.Errorf("field %s length: must be >= %d", fmt.Sprintf("myNestedArray[%d]", i1), 5) + } + } + if plain.MyStringArray != nil && len(plain.MyStringArray) < 5 { + return fmt.Errorf("field %s length: must be >= %d", "myStringArray", 5) + } + *j = MinItems(plain) + return nil +} + +// UnmarshalYAML implements yaml.Unmarshaler. +func (j *MinItems) UnmarshalYAML(value *yaml.Node) error { + var raw map[string]interface{} + if err := value.Decode(&raw); err != nil { + return err + } + type Plain MinItems + var plain Plain + if err := value.Decode(&plain); err != nil { return err } if plain.MyNestedArray != nil && len(plain.MyNestedArray) < 5 { diff --git a/tests/data/validation/minLength/minLength.go b/tests/data/validation/minLength/minLength.go index 25020340..ba0ce09c 100644 --- a/tests/data/validation/minLength/minLength.go +++ b/tests/data/validation/minLength/minLength.go @@ -4,6 +4,7 @@ package test import "encoding/json" import "fmt" +import yaml "gopkg.in/yaml.v3" type MinLength struct { // MyNullableString corresponds to the JSON schema field "myNullableString". @@ -14,9 +15,9 @@ type MinLength struct { } // UnmarshalJSON implements json.Unmarshaler. -func (j *MinLength) UnmarshalJSON(b []byte) error { +func (j *MinLength) UnmarshalJSON(value []byte) error { var raw map[string]interface{} - if err := json.Unmarshal(b, &raw); err != nil { + if err := json.Unmarshal(value, &raw); err != nil { return err } if v, ok := raw["myString"]; !ok || v == nil { @@ -24,7 +25,31 @@ func (j *MinLength) UnmarshalJSON(b []byte) error { } type Plain MinLength var plain Plain - if err := json.Unmarshal(b, &plain); err != nil { + if err := json.Unmarshal(value, &plain); err != nil { + return err + } + if plain.MyNullableString != nil && len(*plain.MyNullableString) < 10 { + return fmt.Errorf("field %s length: must be >= %d", "myNullableString", 10) + } + if len(plain.MyString) < 5 { + return fmt.Errorf("field %s length: must be >= %d", "myString", 5) + } + *j = MinLength(plain) + return nil +} + +// UnmarshalYAML implements yaml.Unmarshaler. +func (j *MinLength) UnmarshalYAML(value *yaml.Node) error { + var raw map[string]interface{} + if err := value.Decode(&raw); err != nil { + return err + } + if v, ok := raw["myString"]; !ok || v == nil { + return fmt.Errorf("field myString in MinLength: required") + } + type Plain MinLength + var plain Plain + if err := value.Decode(&plain); err != nil { return err } if plain.MyNullableString != nil && len(*plain.MyNullableString) < 10 { diff --git a/tests/data/validation/minMaxItems/minMaxItems.go b/tests/data/validation/minMaxItems/minMaxItems.go index 5f60d3eb..1d338c8f 100644 --- a/tests/data/validation/minMaxItems/minMaxItems.go +++ b/tests/data/validation/minMaxItems/minMaxItems.go @@ -4,6 +4,7 @@ package test import "encoding/json" import "fmt" +import yaml "gopkg.in/yaml.v3" type MinMaxItems struct { // MyNestedArray corresponds to the JSON schema field "myNestedArray". @@ -14,14 +15,49 @@ type MinMaxItems struct { } // UnmarshalJSON implements json.Unmarshaler. -func (j *MinMaxItems) UnmarshalJSON(b []byte) error { +func (j *MinMaxItems) UnmarshalJSON(value []byte) error { var raw map[string]interface{} - if err := json.Unmarshal(b, &raw); err != nil { + if err := json.Unmarshal(value, &raw); err != nil { return err } type Plain MinMaxItems var plain Plain - if err := json.Unmarshal(b, &plain); err != nil { + if err := json.Unmarshal(value, &plain); err != nil { + return err + } + if plain.MyNestedArray != nil && len(plain.MyNestedArray) < 1 { + return fmt.Errorf("field %s length: must be >= %d", "myNestedArray", 1) + } + if len(plain.MyNestedArray) > 5 { + return fmt.Errorf("field %s length: must be <= %d", "myNestedArray", 5) + } + for i1 := range plain.MyNestedArray { + if plain.MyNestedArray[i1] != nil && len(plain.MyNestedArray[i1]) < 1 { + return fmt.Errorf("field %s length: must be >= %d", fmt.Sprintf("myNestedArray[%d]", i1), 1) + } + if len(plain.MyNestedArray[i1]) > 5 { + return fmt.Errorf("field %s length: must be <= %d", fmt.Sprintf("myNestedArray[%d]", i1), 5) + } + } + if plain.MyStringArray != nil && len(plain.MyStringArray) < 1 { + return fmt.Errorf("field %s length: must be >= %d", "myStringArray", 1) + } + if len(plain.MyStringArray) > 3 { + return fmt.Errorf("field %s length: must be <= %d", "myStringArray", 3) + } + *j = MinMaxItems(plain) + return nil +} + +// UnmarshalYAML implements yaml.Unmarshaler. +func (j *MinMaxItems) UnmarshalYAML(value *yaml.Node) error { + var raw map[string]interface{} + if err := value.Decode(&raw); err != nil { + return err + } + type Plain MinMaxItems + var plain Plain + if err := value.Decode(&plain); err != nil { return err } if plain.MyNestedArray != nil && len(plain.MyNestedArray) < 1 { diff --git a/tests/data/validation/requiredFields/requiredFields.go b/tests/data/validation/requiredFields/requiredFields.go index b28390ca..2b5b119b 100644 --- a/tests/data/validation/requiredFields/requiredFields.go +++ b/tests/data/validation/requiredFields/requiredFields.go @@ -4,6 +4,7 @@ package test import "encoding/json" import "fmt" +import yaml "gopkg.in/yaml.v3" type RequiredFieldsMyObject struct { // MyNestedObjectString corresponds to the JSON schema field @@ -12,9 +13,9 @@ type RequiredFieldsMyObject struct { } // UnmarshalJSON implements json.Unmarshaler. -func (j *RequiredFieldsMyObject) UnmarshalJSON(b []byte) error { +func (j *RequiredFieldsMyObject) UnmarshalJSON(value []byte) error { var raw map[string]interface{} - if err := json.Unmarshal(b, &raw); err != nil { + if err := json.Unmarshal(value, &raw); err != nil { return err } if v, ok := raw["myNestedObjectString"]; !ok || v == nil { @@ -22,7 +23,25 @@ func (j *RequiredFieldsMyObject) UnmarshalJSON(b []byte) error { } type Plain RequiredFieldsMyObject var plain Plain - if err := json.Unmarshal(b, &plain); err != nil { + if err := json.Unmarshal(value, &plain); err != nil { + return err + } + *j = RequiredFieldsMyObject(plain) + return nil +} + +// UnmarshalYAML implements yaml.Unmarshaler. +func (j *RequiredFieldsMyObject) UnmarshalYAML(value *yaml.Node) error { + var raw map[string]interface{} + if err := value.Decode(&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 := value.Decode(&plain); err != nil { return err } *j = RequiredFieldsMyObject(plain) @@ -36,9 +55,9 @@ type RequiredFieldsMyObjectArrayElem struct { } // UnmarshalJSON implements json.Unmarshaler. -func (j *RequiredFieldsMyObjectArrayElem) UnmarshalJSON(b []byte) error { +func (j *RequiredFieldsMyObjectArrayElem) UnmarshalJSON(value []byte) error { var raw map[string]interface{} - if err := json.Unmarshal(b, &raw); err != nil { + if err := json.Unmarshal(value, &raw); err != nil { return err } if v, ok := raw["myNestedObjectString"]; !ok || v == nil { @@ -46,7 +65,25 @@ func (j *RequiredFieldsMyObjectArrayElem) UnmarshalJSON(b []byte) error { } type Plain RequiredFieldsMyObjectArrayElem var plain Plain - if err := json.Unmarshal(b, &plain); err != nil { + if err := json.Unmarshal(value, &plain); err != nil { + return err + } + *j = RequiredFieldsMyObjectArrayElem(plain) + return nil +} + +// UnmarshalYAML implements yaml.Unmarshaler. +func (j *RequiredFieldsMyObjectArrayElem) UnmarshalYAML(value *yaml.Node) error { + var raw map[string]interface{} + if err := value.Decode(&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 := value.Decode(&plain); err != nil { return err } *j = RequiredFieldsMyObjectArrayElem(plain) @@ -92,9 +129,62 @@ type RequiredFields struct { } // UnmarshalJSON implements json.Unmarshaler. -func (j *RequiredFields) UnmarshalJSON(b []byte) error { +func (j *RequiredFields) UnmarshalJSON(value []byte) error { + var raw map[string]interface{} + if err := json.Unmarshal(value, &raw); err != nil { + return err + } + if v, ok := raw["myBoolean"]; !ok || v == nil { + return fmt.Errorf("field myBoolean in RequiredFields: required") + } + if v, ok := raw["myBooleanArray"]; !ok || v == nil { + return fmt.Errorf("field myBooleanArray in RequiredFields: required") + } + if v, ok := raw["myNull"]; !ok || v == nil { + return fmt.Errorf("field myNull in RequiredFields: required") + } + if v, ok := raw["myNullArray"]; !ok || v == nil { + return fmt.Errorf("field myNullArray in RequiredFields: required") + } + if v, ok := raw["myNumber"]; !ok || v == nil { + return fmt.Errorf("field myNumber in RequiredFields: required") + } + if v, ok := raw["myNumberArray"]; !ok || v == nil { + return fmt.Errorf("field myNumberArray in RequiredFields: required") + } + if v, ok := raw["myObject"]; !ok || v == nil { + return fmt.Errorf("field myObject in RequiredFields: required") + } + if v, ok := raw["myObjectArray"]; !ok || v == nil { + return fmt.Errorf("field myObjectArray in RequiredFields: required") + } + if v, ok := raw["myString"]; !ok || v == nil { + return fmt.Errorf("field myString in RequiredFields: required") + } + if v, ok := raw["myStringArray"]; !ok || v == nil { + return fmt.Errorf("field myStringArray in RequiredFields: required") + } + type Plain RequiredFields + var plain Plain + if err := json.Unmarshal(value, &plain); err != nil { + return err + } + if plain.MyNull != nil { + return fmt.Errorf("field %s: must be null", "myNull") + } + for i0 := range plain.MyNullArray { + if plain.MyNullArray[i0] != nil { + return fmt.Errorf("field %s: must be null", fmt.Sprintf("myNullArray[%d]", i0)) + } + } + *j = RequiredFields(plain) + return nil +} + +// UnmarshalYAML implements yaml.Unmarshaler. +func (j *RequiredFields) UnmarshalYAML(value *yaml.Node) error { var raw map[string]interface{} - if err := json.Unmarshal(b, &raw); err != nil { + if err := value.Decode(&raw); err != nil { return err } if v, ok := raw["myBoolean"]; !ok || v == nil { @@ -129,7 +219,7 @@ func (j *RequiredFields) UnmarshalJSON(b []byte) error { } type Plain RequiredFields var plain Plain - if err := json.Unmarshal(b, &plain); err != nil { + if err := value.Decode(&plain); err != nil { return err } if plain.MyNull != nil { diff --git a/tests/data/validation/typed_default/typed_default.go b/tests/data/validation/typed_default/typed_default.go index b30c1bd0..dfd38b8d 100644 --- a/tests/data/validation/typed_default/typed_default.go +++ b/tests/data/validation/typed_default/typed_default.go @@ -3,6 +3,7 @@ package test import "encoding/json" +import yaml "gopkg.in/yaml.v3" type TypedDefault struct { // TopLevelDomains corresponds to the JSON schema field "topLevelDomains". @@ -10,14 +11,37 @@ type TypedDefault struct { } // UnmarshalJSON implements json.Unmarshaler. -func (j *TypedDefault) UnmarshalJSON(b []byte) error { +func (j *TypedDefault) UnmarshalJSON(value []byte) error { var raw map[string]interface{} - if err := json.Unmarshal(b, &raw); err != nil { + if err := json.Unmarshal(value, &raw); err != nil { return err } type Plain TypedDefault var plain Plain - if err := json.Unmarshal(b, &plain); err != nil { + if err := json.Unmarshal(value, &plain); err != nil { + return err + } + if v, ok := raw["topLevelDomains"]; !ok || v == nil { + plain.TopLevelDomains = []string{ + ".com", + ".org", + ".info", + ".gov", + } + } + *j = TypedDefault(plain) + return nil +} + +// UnmarshalYAML implements yaml.Unmarshaler. +func (j *TypedDefault) UnmarshalYAML(value *yaml.Node) error { + var raw map[string]interface{} + if err := value.Decode(&raw); err != nil { + return err + } + type Plain TypedDefault + var plain Plain + if err := value.Decode(&plain); err != nil { return err } if v, ok := raw["topLevelDomains"]; !ok || v == nil { diff --git a/tests/data/validation/typed_default_empty/typed_default_empty.go b/tests/data/validation/typed_default_empty/typed_default_empty.go index 322d2695..9b584f52 100644 --- a/tests/data/validation/typed_default_empty/typed_default_empty.go +++ b/tests/data/validation/typed_default_empty/typed_default_empty.go @@ -3,6 +3,7 @@ package test import "encoding/json" +import yaml "gopkg.in/yaml.v3" type TypedDefaultEmpty struct { // TopLevelDomains corresponds to the JSON schema field "topLevelDomains". @@ -10,14 +11,32 @@ type TypedDefaultEmpty struct { } // UnmarshalJSON implements json.Unmarshaler. -func (j *TypedDefaultEmpty) UnmarshalJSON(b []byte) error { +func (j *TypedDefaultEmpty) UnmarshalJSON(value []byte) error { var raw map[string]interface{} - if err := json.Unmarshal(b, &raw); err != nil { + if err := json.Unmarshal(value, &raw); err != nil { return err } type Plain TypedDefaultEmpty var plain Plain - if err := json.Unmarshal(b, &plain); err != nil { + if err := json.Unmarshal(value, &plain); err != nil { + return err + } + if v, ok := raw["topLevelDomains"]; !ok || v == nil { + plain.TopLevelDomains = []string{} + } + *j = TypedDefaultEmpty(plain) + return nil +} + +// UnmarshalYAML implements yaml.Unmarshaler. +func (j *TypedDefaultEmpty) UnmarshalYAML(value *yaml.Node) error { + var raw map[string]interface{} + if err := value.Decode(&raw); err != nil { + return err + } + type Plain TypedDefaultEmpty + var plain Plain + if err := value.Decode(&plain); err != nil { return err } if v, ok := raw["topLevelDomains"]; !ok || v == nil { 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..f68c80d5 100644 --- a/tests/data/validation/typed_default_enums/typed_default_enums.go +++ b/tests/data/validation/typed_default_enums/typed_default_enums.go @@ -4,6 +4,7 @@ package test import "encoding/json" import "fmt" +import yaml "gopkg.in/yaml.v3" import "reflect" type TypedDefaultEnumsSome string @@ -33,6 +34,26 @@ func (j *TypedDefaultEnumsSome) UnmarshalJSON(b []byte) error { return nil } +// UnmarshalYAML implements yaml.Unmarshaler. +func (j *TypedDefaultEnumsSome) UnmarshalYAML(value *yaml.Node) error { + var v string + if err := value.Decode(&v); err != nil { + return err + } + var ok bool + for _, expected := range enumValues_TypedDefaultEnumsSome { + if reflect.DeepEqual(v, expected) { + ok = true + break + } + } + if !ok { + return fmt.Errorf("invalid value (expected one of %#v): %#v", enumValues_TypedDefaultEnumsSome, v) + } + *j = TypedDefaultEnumsSome(v) + return nil +} + type TypedDefaultEnums struct { // Some corresponds to the JSON schema field "some". Some TypedDefaultEnumsSome `json:"some,omitempty" yaml:"some,omitempty" mapstructure:"some,omitempty"` @@ -42,14 +63,32 @@ const TypedDefaultEnumsSomeOther TypedDefaultEnumsSome = "other" const TypedDefaultEnumsSomeRandom TypedDefaultEnumsSome = "random" // UnmarshalJSON implements json.Unmarshaler. -func (j *TypedDefaultEnums) UnmarshalJSON(b []byte) error { +func (j *TypedDefaultEnums) UnmarshalJSON(value []byte) error { + var raw map[string]interface{} + if err := json.Unmarshal(value, &raw); err != nil { + return err + } + type Plain TypedDefaultEnums + var plain Plain + if err := json.Unmarshal(value, &plain); err != nil { + return err + } + if v, ok := raw["some"]; !ok || v == nil { + plain.Some = "random" + } + *j = TypedDefaultEnums(plain) + return nil +} + +// UnmarshalYAML implements yaml.Unmarshaler. +func (j *TypedDefaultEnums) UnmarshalYAML(value *yaml.Node) error { var raw map[string]interface{} - if err := json.Unmarshal(b, &raw); err != nil { + if err := value.Decode(&raw); err != nil { return err } type Plain TypedDefaultEnums var plain Plain - if err := json.Unmarshal(b, &plain); err != nil { + if err := value.Decode(&plain); err != nil { return err } if v, ok := raw["some"]; !ok || v == nil { diff --git a/tests/generation_test.go b/tests/generation_test.go index ca987fc9..cea26605 100644 --- a/tests/generation_test.go +++ b/tests/generation_test.go @@ -18,7 +18,7 @@ var ( basicConfig = generator.Config{ SchemaMappings: []generator.SchemaMapping{}, - ExtraImports: false, + ExtraImports: true, DefaultPackageName: "github.com/example/test", DefaultOutputName: "-", ResolveExtensions: []string{".json", ".yaml"}, diff --git a/tests/go.mod b/tests/go.mod index b1ff07e1..4987f577 100644 --- a/tests/go.mod +++ b/tests/go.mod @@ -15,14 +15,16 @@ require ( ) require ( + dario.cat/mergo v1.0.0 // indirect github.com/fatih/color v1.13.0 // indirect github.com/goccy/go-yaml v1.11.2 // indirect + github.com/google/go-cmp v0.5.9 // indirect github.com/mattn/go-colorable v0.1.12 // indirect github.com/mattn/go-isatty v0.0.14 // indirect github.com/mitchellh/go-wordwrap v1.0.1 // indirect github.com/pkg/errors v0.9.1 // indirect github.com/sanity-io/litter v1.5.5 // indirect - golang.org/x/exp v0.0.0-20231005195138-3e424a577f31 // indirect - golang.org/x/sys v0.12.0 // indirect + golang.org/x/exp v0.0.0-20231006140011-7918f672742d // indirect + golang.org/x/sys v0.13.0 // indirect golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect ) diff --git a/tests/go.sum b/tests/go.sum index 03cef452..ada76f5d 100644 --- a/tests/go.sum +++ b/tests/go.sum @@ -1,3 +1,5 @@ +dario.cat/mergo v1.0.0 h1:AGCNq9Evsj31mOgNPcLyXc+4PNABt905YmuqPYYpBWk= +dario.cat/mergo v1.0.0/go.mod h1:uNxQE+84aUszobStD9th8a29P2fMDhsBdgRYvZOxGmk= github.com/davecgh/go-spew v0.0.0-20161028175848-04cdfd42973b h1:XxMZvQZtTXpWMNWK82vdjCLCe7uGMFXdTsJH0v3Hkvw= github.com/davecgh/go-spew v0.0.0-20161028175848-04cdfd42973b/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w= @@ -34,14 +36,14 @@ github.com/stretchr/testify v0.0.0-20161117074351-18a02ba4a312 h1:UsFdQ3ZmlzS0Bq github.com/stretchr/testify v0.0.0-20161117074351-18a02ba4a312/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= golang.org/x/crypto v0.7.0 h1:AvwMYaRytfdeVt3u6mLaxYtErKYjxA2OXjJ1HHq6t3A= golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU= -golang.org/x/exp v0.0.0-20231005195138-3e424a577f31 h1:9k5exFQKQglLo+RoP+4zMjOFE14P6+vyR0baDAi0Rcs= -golang.org/x/exp v0.0.0-20231005195138-3e424a577f31/go.mod h1:S2oDrQGGwySpoQPVqRShND87VCbxmc6bL1Yd2oYrm6k= +golang.org/x/exp v0.0.0-20231006140011-7918f672742d h1:jtJma62tbqLibJ5sFQz8bKtEM8rJBtfilJ2qTU199MI= +golang.org/x/exp v0.0.0-20231006140011-7918f672742d/go.mod h1:ldy0pHrwJyGW56pPQzzkH36rKxoZW1tw7ZJpeKx+hdo= golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o= -golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE= +golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=