From c7be101b983c8170a60c97e44c2efdb5da31df82 Mon Sep 17 00:00:00 2001 From: Alexander Staubo Date: Tue, 2 Oct 2018 13:23:28 -0400 Subject: [PATCH] Support for 'default'. Refactors and simplifies validation a bit to be more flexible. --- README.md | 2 +- pkg/codegen/model.go | 40 +-- pkg/codegen/validation.go | 25 -- pkg/generator/generate.go | 118 +++---- tests/data/core/4.2.1_array.go.output | 26 +- tests/data/core/object.go.output | 66 +--- tests/data/core/objectEmpty.go.output | 90 +----- tests/data/core/objectNested.go.output | 135 +------- tests/data/core/primitives.go.output | 163 +--------- tests/data/core/ref.go.output | 202 +----------- tests/data/core/refExternalFile.go.output | 238 +------------- .../core/refExternalFileWithDupe.go.output | 275 +--------------- tests/data/crossPackage/other.go.output | 16 +- tests/data/crossPackage/schema.go.output | 30 +- tests/data/misc/capitalization.go.output | 16 +- tests/data/miscWithDefaults/case.go.output | 16 +- .../data/miscWithDefaults/caseDupes.go.output | 49 +-- .../rootEmptyJustDefinitions.go.output | 79 +---- .../rootIsArrayOfString.go.output | 82 ----- .../validation/10.1_description.go.output | 16 +- tests/data/validation/6.1.2_enum.go.output | 40 +-- .../validation/6.5.3_requiredFields.go.output | 301 +++--------------- 22 files changed, 157 insertions(+), 1868 deletions(-) delete mode 100644 pkg/codegen/validation.go diff --git a/README.md b/README.md index a5ba74df..8b52c8cb 100644 --- a/README.md +++ b/README.md @@ -67,7 +67,7 @@ While not finished, go-jsonschema can be used today. Aside from some minor featu - Validation ([RFC draft](http://json-schema.org/latest/json-schema-validation.html)) - [ ] Schema annotations (ยง10) - [x] `description` - - [ ] `default` + - [x] `default` (only for struct fields) - [ ] `readOnly` - [ ] `writeOnly` - [ ] ~~`title`~~ (N/A) diff --git a/pkg/codegen/model.go b/pkg/codegen/model.go index 9c9a8c82..f104c585 100644 --- a/pkg/codegen/model.go +++ b/pkg/codegen/model.go @@ -156,10 +156,6 @@ type Type interface { IsNillable() bool } -type RuledType interface { - GetRequiredRules() []Rule -} - type PointerType struct { Type Type } @@ -182,12 +178,6 @@ func (a ArrayType) Generate(out *Emitter) { a.Type.Generate(out) } -func (ArrayType) GetRequiredRules() []Rule { - return []Rule{ - ArrayNotEmpty{}, - } -} - type NamedType struct { Package *Package Decl *TypeDecl @@ -237,7 +227,8 @@ func (EmptyInterfaceType) Generate(out *Emitter) { } type StructType struct { - Fields []StructField + Fields []StructField + RequiredJSONFields []string } func (StructType) IsNillable() bool { return false } @@ -251,31 +242,24 @@ func (s *StructType) Generate(out *Emitter) { out.Indent(1) i := 0 for _, f := range s.Fields { - if !f.Synthetic { - if i > 0 { - out.Newline() - } - f.Generate(out) + if i > 0 { out.Newline() - i++ } + f.Generate(out) + out.Newline() + i++ } out.Indent(-1) out.Print("}") } type StructField struct { - Name string - Type Type - Comment string - Tags string - JSONName string - Synthetic bool - Rules []Rule -} - -func (f *StructField) AddRule(rule Rule) { - f.Rules = append(f.Rules, rule) + Name string + Type Type + Comment string + Tags string + JSONName string + DefaultValue interface{} } func (f *StructField) Generate(out *Emitter) { diff --git a/pkg/codegen/validation.go b/pkg/codegen/validation.go deleted file mode 100644 index e0f7dc7c..00000000 --- a/pkg/codegen/validation.go +++ /dev/null @@ -1,25 +0,0 @@ -package codegen - -type Rule interface { - GenerateValidation(out *Emitter, varName, errorContext string) -} - -type ArrayNotEmpty struct{} - -func (ArrayNotEmpty) GenerateValidation(out *Emitter, varName, errorContext string) { - out.Println(`if len(%s) == 0 {`, varName) - out.Indent(1) - out.Println(`return fmt.Errorf("%s: array cannot be empty")`, errorContext) - out.Indent(-1) - out.Println("}") -} - -type NilStructFieldRequired struct{} - -func (NilStructFieldRequired) GenerateValidation(out *Emitter, varName, errorContext string) { - out.Println(`if %s == nil {`, varName) - out.Indent(1) - out.Println(`return fmt.Errorf("%s: must be set")`, errorContext) - out.Indent(-1) - out.Println("}") -} diff --git a/pkg/generator/generate.go b/pkg/generator/generate.go index 044e99c2..f0a050da 100644 --- a/pkg/generator/generate.go +++ b/pkg/generator/generate.go @@ -9,6 +9,8 @@ import ( "sort" "strings" + "github.com/sanity-io/litter" + "github.com/atombender/go-jsonschema/pkg/codegen" "github.com/atombender/go-jsonschema/pkg/schemas" ) @@ -354,49 +356,59 @@ func (g *schemaGenerator) generateDeclaredType( g.output.file.Package.AddDecl(&decl) if structType, ok := theType.(*codegen.StructType); ok { - g.output.file.Package.AddImport("encoding/json", "") - g.output.file.Package.AddDecl(&codegen.Method{ - Impl: func(out *codegen.Emitter) { - out.Comment("UnmarshalJSON implements json.Unmarshaler.") - out.Println("func (j *%s) UnmarshalJSON(b []byte) error {", decl.Name) - out.Indent(1) - out.Println("var v struct {") - out.Indent(1) - fields := append([]codegen.StructField{}, structType.Fields...) - for _, f := range structType.Fields { - if f.Synthetic { - f.Generate(out) - out.Newline() - } + needUnmarshal := false + if len(structType.RequiredJSONFields) > 0 { + needUnmarshal = true + } else { + for _, f := range structType.Fields { + if f.DefaultValue != nil { + needUnmarshal = true + break } - out.Indent(-1) - out.Println("}") - out.Println("if err := json.Unmarshal(b, &v); err != nil { return err }") - for _, f := range fields { - if f.Synthetic { - for _, r := range f.Rules { - r.GenerateValidation(out, fmt.Sprintf("v.%s", f.Name), - fmt.Sprintf("field %s", f.JSONName)) - } + } + } + if needUnmarshal { + if len(structType.RequiredJSONFields) > 0 { + g.output.file.Package.AddImport("fmt", "") + } + g.output.file.Package.AddImport("encoding/json", "") + g.output.file.Package.AddDecl(&codegen.Method{ + Impl: func(out *codegen.Emitter) { + out.Comment("UnmarshalJSON implements json.Unmarshaler.") + out.Println("func (j *%s) UnmarshalJSON(b []byte) error {", decl.Name) + out.Indent(1) + out.Println("var %s map[string]interface{}", varNameRawMap) + out.Println("if err := json.Unmarshal(b, &%s); err != nil { return err }", + varNameRawMap) + for _, f := range structType.RequiredJSONFields { + out.Println(`if v, ok := %s["%s"]; !ok || v == nil {`, varNameRawMap, f) + out.Indent(1) + out.Println(`return fmt.Errorf("field %s: required")`, f) + out.Indent(-1) + out.Println("}") } - } - out.Println("type plain %s", decl.Name) - out.Println("var p plain") - out.Println("if err := json.Unmarshal(b, &p); err != nil { return err }") - for _, f := range fields { - if !f.Synthetic { - for _, r := range f.Rules { - r.GenerateValidation(out, fmt.Sprintf("p.%s", f.Name), - fmt.Sprintf("field %s", f.JSONName)) + + out.Println("type Plain %s", decl.Name) + out.Println("var %s Plain", varNamePlainStruct) + out.Println("if err := json.Unmarshal(b, &%s); err != nil { return err }", + varNamePlainStruct) + for _, f := range structType.Fields { + if f.DefaultValue != nil { + out.Println(`if v, ok := %s["%s"]; !ok || v == nil {`, varNameRawMap, f.JSONName) + out.Indent(1) + out.Println(`%s.%s = %s`, varNamePlainStruct, f.Name, litter.Sdump(f.DefaultValue)) + out.Indent(-1) + out.Println("}") } } - } - out.Println("*j = %s(p)", decl.Name) - out.Println("return nil") - out.Indent(-1) - out.Println("}") - }, - }) + + out.Println("*j = %s(%s)", decl.Name, varNamePlainStruct) + out.Println("return nil") + out.Indent(-1) + out.Println("}") + }, + }) + } } return &codegen.NamedType{Decl: &decl}, nil @@ -514,23 +526,12 @@ func (g *schemaGenerator) generateStructType( return nil, fmt.Errorf("could not generate type for field %q: %s", name, err) } - if isRequired { - if rt, ok := structField.Type.(codegen.RuledType); ok { - g.output.file.Package.AddImport("fmt", "") // All rules need fmt - for _, r := range rt.GetRequiredRules() { - structField.AddRule(r) - } - } - if !structField.Type.IsNillable() { - g.output.file.Package.AddImport("fmt", "") // All rules need fmt - syntheticField := structField - syntheticField.Comment = "" - syntheticField.Synthetic = true - syntheticField.Type = codegen.PointerType{Type: syntheticField.Type} - syntheticField.AddRule(codegen.NilStructFieldRequired{}) - structType.AddField(syntheticField) - } - } else if !structField.Type.IsNillable() { + if prop.Default != nil { + structField.DefaultValue = prop.Default + } else if isRequired { + structType.RequiredJSONFields = append(structType.RequiredJSONFields, structField.JSONName) + } else { + // Optional, so must be pointer structField.Type = codegen.PointerType{Type: structField.Type} } @@ -754,3 +755,8 @@ func (ns nameScope) add(s string) nameScope { result[len(result)-1] = s return result } + +var ( + varNamePlainStruct = "plain" + varNameRawMap = "raw" +) diff --git a/tests/data/core/4.2.1_array.go.output b/tests/data/core/4.2.1_array.go.output index 2fb4c83d..6edee970 100644 --- a/tests/data/core/4.2.1_array.go.output +++ b/tests/data/core/4.2.1_array.go.output @@ -1,35 +1,21 @@ // THIS FILE IS AUTOMATICALLY GENERATED. DO NOT EDIT. package test -import "encoding/json" type 421ArrayMyObjectArrayElem map[string]interface{} type 421Array struct { // MyBooleanArray corresponds to the JSON schema field "myBooleanArray". - MyBooleanArray []bool `json:"myBooleanArray,omitempty"` + MyBooleanArray *[]bool `json:"myBooleanArray,omitempty"` // MyNullArray corresponds to the JSON schema field "myNullArray". - MyNullArray []interface{} `json:"myNullArray,omitempty"` + MyNullArray *[]interface{} `json:"myNullArray,omitempty"` // MyNumberArray corresponds to the JSON schema field "myNumberArray". - MyNumberArray []float64 `json:"myNumberArray,omitempty"` + MyNumberArray *[]float64 `json:"myNumberArray,omitempty"` // MyObjectArray corresponds to the JSON schema field "myObjectArray". - MyObjectArray []421ArrayMyObjectArrayElem `json:"myObjectArray,omitempty"` + MyObjectArray *[]421ArrayMyObjectArrayElem `json:"myObjectArray,omitempty"` // MyStringArray corresponds to the JSON schema field "myStringArray". - MyStringArray []string `json:"myStringArray,omitempty"` -} - -// UnmarshalJSON implements json.Unmarshaler. -func (j *421Array) UnmarshalJSON(b []byte) error { - var v struct { - } - if err := json.Unmarshal(b, &v); err != nil { return err } - type plain 421Array - var p plain - if err := json.Unmarshal(b, &p); err != nil { return err } - *j = 421Array(p) - return nil -} - + MyStringArray *[]string `json:"myStringArray,omitempty"` +} \ No newline at end of file diff --git a/tests/data/core/object.go.output b/tests/data/core/object.go.output index fd7778fa..0d245020 100644 --- a/tests/data/core/object.go.output +++ b/tests/data/core/object.go.output @@ -1,39 +1,8 @@ // THIS FILE IS AUTOMATICALLY GENERATED. DO NOT EDIT. package test -import "encoding/json" import "fmt" - -type 421ArrayMyObjectArrayElem map[string]interface{} -type 421Array struct { - // MyBooleanArray corresponds to the JSON schema field "myBooleanArray". - MyBooleanArray []bool `json:"myBooleanArray,omitempty"` - - // MyNullArray corresponds to the JSON schema field "myNullArray". - MyNullArray []interface{} `json:"myNullArray,omitempty"` - - // MyNumberArray corresponds to the JSON schema field "myNumberArray". - MyNumberArray []float64 `json:"myNumberArray,omitempty"` - - // MyObjectArray corresponds to the JSON schema field "myObjectArray". - MyObjectArray []421ArrayMyObjectArrayElem `json:"myObjectArray,omitempty"` - - // MyStringArray corresponds to the JSON schema field "myStringArray". - MyStringArray []string `json:"myStringArray,omitempty"` -} - -// UnmarshalJSON implements json.Unmarshaler. -func (j *421Array) UnmarshalJSON(b []byte) error { - var v struct { - } - if err := json.Unmarshal(b, &v); err != nil { return err } - type plain 421Array - var p plain - if err := json.Unmarshal(b, &p); err != nil { return err } - *j = 421Array(p) - return nil -} - +import "encoding/json" type ObjectMyObject struct { // MyString corresponds to the JSON schema field "myString". @@ -42,17 +11,15 @@ type ObjectMyObject struct { // UnmarshalJSON implements json.Unmarshaler. func (j *ObjectMyObject) UnmarshalJSON(b []byte) error { - var v struct { - MyString *string `json:"myString"` + var raw map[string]interface{} + if err := json.Unmarshal(b, &raw); err != nil { return err } + if v, ok := raw["myString"]; !ok || v == nil { + return fmt.Errorf("field myString: required") } - if err := json.Unmarshal(b, &v); err != nil { return err } - if v.MyString == nil { - return fmt.Errorf("field myString: must be set") - } - type plain ObjectMyObject - var p plain - if err := json.Unmarshal(b, &p); err != nil { return err } - *j = ObjectMyObject(p) + type Plain ObjectMyObject + var plain Plain + if err := json.Unmarshal(b, &plain); err != nil { return err } + *j = ObjectMyObject(plain) return nil } @@ -60,17 +27,4 @@ func (j *ObjectMyObject) UnmarshalJSON(b []byte) error { type Object struct { // MyObject corresponds to the JSON schema field "myObject". MyObject *ObjectMyObject `json:"myObject,omitempty"` -} - -// UnmarshalJSON implements json.Unmarshaler. -func (j *Object) UnmarshalJSON(b []byte) error { - var v struct { - } - if err := json.Unmarshal(b, &v); err != nil { return err } - type plain Object - var p plain - if err := json.Unmarshal(b, &p); err != nil { return err } - *j = Object(p) - return nil -} - +} \ No newline at end of file diff --git a/tests/data/core/objectEmpty.go.output b/tests/data/core/objectEmpty.go.output index 11cee9a7..076eca34 100644 --- a/tests/data/core/objectEmpty.go.output +++ b/tests/data/core/objectEmpty.go.output @@ -1,95 +1,9 @@ // THIS FILE IS AUTOMATICALLY GENERATED. DO NOT EDIT. package test -import "encoding/json" -import "fmt" - -type 421ArrayMyObjectArrayElem map[string]interface{} -type 421Array struct { - // MyBooleanArray corresponds to the JSON schema field "myBooleanArray". - MyBooleanArray []bool `json:"myBooleanArray,omitempty"` - - // MyNullArray corresponds to the JSON schema field "myNullArray". - MyNullArray []interface{} `json:"myNullArray,omitempty"` - - // MyNumberArray corresponds to the JSON schema field "myNumberArray". - MyNumberArray []float64 `json:"myNumberArray,omitempty"` - - // MyObjectArray corresponds to the JSON schema field "myObjectArray". - MyObjectArray []421ArrayMyObjectArrayElem `json:"myObjectArray,omitempty"` - - // MyStringArray corresponds to the JSON schema field "myStringArray". - MyStringArray []string `json:"myStringArray,omitempty"` -} - -// UnmarshalJSON implements json.Unmarshaler. -func (j *421Array) UnmarshalJSON(b []byte) error { - var v struct { - } - if err := json.Unmarshal(b, &v); err != nil { return err } - type plain 421Array - var p plain - if err := json.Unmarshal(b, &p); err != nil { return err } - *j = 421Array(p) - return nil -} - - -type ObjectMyObject struct { - // MyString corresponds to the JSON schema field "myString". - MyString string `json:"myString"` -} - -// UnmarshalJSON implements json.Unmarshaler. -func (j *ObjectMyObject) UnmarshalJSON(b []byte) error { - var v struct { - MyString *string `json:"myString"` - } - if err := json.Unmarshal(b, &v); err != nil { return err } - if v.MyString == nil { - return fmt.Errorf("field myString: must be set") - } - type plain ObjectMyObject - var p plain - if err := json.Unmarshal(b, &p); err != nil { return err } - *j = ObjectMyObject(p) - return nil -} - - -type Object struct { - // MyObject corresponds to the JSON schema field "myObject". - MyObject *ObjectMyObject `json:"myObject,omitempty"` -} - -// UnmarshalJSON implements json.Unmarshaler. -func (j *Object) UnmarshalJSON(b []byte) error { - var v struct { - } - if err := json.Unmarshal(b, &v); err != nil { return err } - type plain Object - var p plain - if err := json.Unmarshal(b, &p); err != nil { return err } - *j = Object(p) - return nil -} - type ObjectEmptyFoo map[string]interface{} type ObjectEmpty struct { // Foo corresponds to the JSON schema field "foo". - Foo ObjectEmptyFoo `json:"foo,omitempty"` -} - -// UnmarshalJSON implements json.Unmarshaler. -func (j *ObjectEmpty) UnmarshalJSON(b []byte) error { - var v struct { - } - if err := json.Unmarshal(b, &v); err != nil { return err } - type plain ObjectEmpty - var p plain - if err := json.Unmarshal(b, &p); err != nil { return err } - *j = ObjectEmpty(p) - return nil -} - + Foo *ObjectEmptyFoo `json:"foo,omitempty"` +} \ No newline at end of file diff --git a/tests/data/core/objectNested.go.output b/tests/data/core/objectNested.go.output index 9ccaec54..85a7780d 100644 --- a/tests/data/core/objectNested.go.output +++ b/tests/data/core/objectNested.go.output @@ -1,149 +1,16 @@ // THIS FILE IS AUTOMATICALLY GENERATED. DO NOT EDIT. package test -import "encoding/json" -import "fmt" - -type 421ArrayMyObjectArrayElem map[string]interface{} -type 421Array struct { - // MyBooleanArray corresponds to the JSON schema field "myBooleanArray". - MyBooleanArray []bool `json:"myBooleanArray,omitempty"` - - // MyNullArray corresponds to the JSON schema field "myNullArray". - MyNullArray []interface{} `json:"myNullArray,omitempty"` - - // MyNumberArray corresponds to the JSON schema field "myNumberArray". - MyNumberArray []float64 `json:"myNumberArray,omitempty"` - - // MyObjectArray corresponds to the JSON schema field "myObjectArray". - MyObjectArray []421ArrayMyObjectArrayElem `json:"myObjectArray,omitempty"` - - // MyStringArray corresponds to the JSON schema field "myStringArray". - MyStringArray []string `json:"myStringArray,omitempty"` -} - -// UnmarshalJSON implements json.Unmarshaler. -func (j *421Array) UnmarshalJSON(b []byte) error { - var v struct { - } - if err := json.Unmarshal(b, &v); err != nil { return err } - type plain 421Array - var p plain - if err := json.Unmarshal(b, &p); err != nil { return err } - *j = 421Array(p) - return nil -} - - -type ObjectMyObject struct { - // MyString corresponds to the JSON schema field "myString". - MyString string `json:"myString"` -} - -// UnmarshalJSON implements json.Unmarshaler. -func (j *ObjectMyObject) UnmarshalJSON(b []byte) error { - var v struct { - MyString *string `json:"myString"` - } - if err := json.Unmarshal(b, &v); err != nil { return err } - if v.MyString == nil { - return fmt.Errorf("field myString: must be set") - } - type plain ObjectMyObject - var p plain - if err := json.Unmarshal(b, &p); err != nil { return err } - *j = ObjectMyObject(p) - return nil -} - - -type Object struct { - // MyObject corresponds to the JSON schema field "myObject". - MyObject *ObjectMyObject `json:"myObject,omitempty"` -} - -// UnmarshalJSON implements json.Unmarshaler. -func (j *Object) UnmarshalJSON(b []byte) error { - var v struct { - } - if err := json.Unmarshal(b, &v); err != nil { return err } - type plain Object - var p plain - if err := json.Unmarshal(b, &p); err != nil { return err } - *j = Object(p) - return nil -} - - -type ObjectEmptyFoo map[string]interface{} -type ObjectEmpty struct { - // Foo corresponds to the JSON schema field "foo". - Foo ObjectEmptyFoo `json:"foo,omitempty"` -} - -// UnmarshalJSON implements json.Unmarshaler. -func (j *ObjectEmpty) UnmarshalJSON(b []byte) error { - var v struct { - } - if err := json.Unmarshal(b, &v); err != nil { return err } - type plain ObjectEmpty - var p plain - if err := json.Unmarshal(b, &p); err != nil { return err } - *j = ObjectEmpty(p) - return nil -} - type ObjectNestedMyObjectMyObject struct { // MyString corresponds to the JSON schema field "myString". MyString *string `json:"myString,omitempty"` } - -// UnmarshalJSON implements json.Unmarshaler. -func (j *ObjectNestedMyObjectMyObject) UnmarshalJSON(b []byte) error { - var v struct { - } - if err := json.Unmarshal(b, &v); err != nil { return err } - type plain ObjectNestedMyObjectMyObject - var p plain - if err := json.Unmarshal(b, &p); err != nil { return err } - *j = ObjectNestedMyObjectMyObject(p) - return nil -} - - type ObjectNestedMyObject struct { // MyObject corresponds to the JSON schema field "myObject". MyObject *ObjectNestedMyObjectMyObject `json:"myObject,omitempty"` } - -// UnmarshalJSON implements json.Unmarshaler. -func (j *ObjectNestedMyObject) UnmarshalJSON(b []byte) error { - var v struct { - } - if err := json.Unmarshal(b, &v); err != nil { return err } - type plain ObjectNestedMyObject - var p plain - if err := json.Unmarshal(b, &p); err != nil { return err } - *j = ObjectNestedMyObject(p) - return nil -} - - type ObjectNested struct { // MyObject corresponds to the JSON schema field "myObject". MyObject *ObjectNestedMyObject `json:"myObject,omitempty"` -} - -// UnmarshalJSON implements json.Unmarshaler. -func (j *ObjectNested) UnmarshalJSON(b []byte) error { - var v struct { - } - if err := json.Unmarshal(b, &v); err != nil { return err } - type plain ObjectNested - var p plain - if err := json.Unmarshal(b, &p); err != nil { return err } - *j = ObjectNested(p) - return nil -} - +} \ No newline at end of file diff --git a/tests/data/core/primitives.go.output b/tests/data/core/primitives.go.output index b5fe3a4a..c3e70a12 100644 --- a/tests/data/core/primitives.go.output +++ b/tests/data/core/primitives.go.output @@ -1,176 +1,17 @@ // THIS FILE IS AUTOMATICALLY GENERATED. DO NOT EDIT. package test -import "encoding/json" -import "fmt" - -type 421ArrayMyObjectArrayElem map[string]interface{} -type 421Array struct { - // MyBooleanArray corresponds to the JSON schema field "myBooleanArray". - MyBooleanArray []bool `json:"myBooleanArray,omitempty"` - - // MyNullArray corresponds to the JSON schema field "myNullArray". - MyNullArray []interface{} `json:"myNullArray,omitempty"` - - // MyNumberArray corresponds to the JSON schema field "myNumberArray". - MyNumberArray []float64 `json:"myNumberArray,omitempty"` - - // MyObjectArray corresponds to the JSON schema field "myObjectArray". - MyObjectArray []421ArrayMyObjectArrayElem `json:"myObjectArray,omitempty"` - - // MyStringArray corresponds to the JSON schema field "myStringArray". - MyStringArray []string `json:"myStringArray,omitempty"` -} - -// UnmarshalJSON implements json.Unmarshaler. -func (j *421Array) UnmarshalJSON(b []byte) error { - var v struct { - } - if err := json.Unmarshal(b, &v); err != nil { return err } - type plain 421Array - var p plain - if err := json.Unmarshal(b, &p); err != nil { return err } - *j = 421Array(p) - return nil -} - - -type ObjectMyObject struct { - // MyString corresponds to the JSON schema field "myString". - MyString string `json:"myString"` -} - -// UnmarshalJSON implements json.Unmarshaler. -func (j *ObjectMyObject) UnmarshalJSON(b []byte) error { - var v struct { - MyString *string `json:"myString"` - } - if err := json.Unmarshal(b, &v); err != nil { return err } - if v.MyString == nil { - return fmt.Errorf("field myString: must be set") - } - type plain ObjectMyObject - var p plain - if err := json.Unmarshal(b, &p); err != nil { return err } - *j = ObjectMyObject(p) - return nil -} - - -type Object struct { - // MyObject corresponds to the JSON schema field "myObject". - MyObject *ObjectMyObject `json:"myObject,omitempty"` -} - -// UnmarshalJSON implements json.Unmarshaler. -func (j *Object) UnmarshalJSON(b []byte) error { - var v struct { - } - if err := json.Unmarshal(b, &v); err != nil { return err } - type plain Object - var p plain - if err := json.Unmarshal(b, &p); err != nil { return err } - *j = Object(p) - return nil -} - - -type ObjectEmptyFoo map[string]interface{} -type ObjectEmpty struct { - // Foo corresponds to the JSON schema field "foo". - Foo ObjectEmptyFoo `json:"foo,omitempty"` -} - -// UnmarshalJSON implements json.Unmarshaler. -func (j *ObjectEmpty) UnmarshalJSON(b []byte) error { - var v struct { - } - if err := json.Unmarshal(b, &v); err != nil { return err } - type plain ObjectEmpty - var p plain - if err := json.Unmarshal(b, &p); err != nil { return err } - *j = ObjectEmpty(p) - return nil -} - - -type ObjectNestedMyObjectMyObject struct { - // MyString corresponds to the JSON schema field "myString". - MyString *string `json:"myString,omitempty"` -} - -// UnmarshalJSON implements json.Unmarshaler. -func (j *ObjectNestedMyObjectMyObject) UnmarshalJSON(b []byte) error { - var v struct { - } - if err := json.Unmarshal(b, &v); err != nil { return err } - type plain ObjectNestedMyObjectMyObject - var p plain - if err := json.Unmarshal(b, &p); err != nil { return err } - *j = ObjectNestedMyObjectMyObject(p) - return nil -} - - -type ObjectNestedMyObject struct { - // MyObject corresponds to the JSON schema field "myObject". - MyObject *ObjectNestedMyObjectMyObject `json:"myObject,omitempty"` -} - -// UnmarshalJSON implements json.Unmarshaler. -func (j *ObjectNestedMyObject) UnmarshalJSON(b []byte) error { - var v struct { - } - if err := json.Unmarshal(b, &v); err != nil { return err } - type plain ObjectNestedMyObject - var p plain - if err := json.Unmarshal(b, &p); err != nil { return err } - *j = ObjectNestedMyObject(p) - return nil -} - - -type ObjectNested struct { - // MyObject corresponds to the JSON schema field "myObject". - MyObject *ObjectNestedMyObject `json:"myObject,omitempty"` -} - -// UnmarshalJSON implements json.Unmarshaler. -func (j *ObjectNested) UnmarshalJSON(b []byte) error { - var v struct { - } - if err := json.Unmarshal(b, &v); err != nil { return err } - type plain ObjectNested - var p plain - if err := json.Unmarshal(b, &p); err != nil { return err } - *j = ObjectNested(p) - return nil -} - type Primitives struct { // MyBoolean corresponds to the JSON schema field "myBoolean". MyBoolean *bool `json:"myBoolean,omitempty"` // MyNull corresponds to the JSON schema field "myNull". - MyNull interface{} `json:"myNull,omitempty"` + MyNull *interface{} `json:"myNull,omitempty"` // MyNumber corresponds to the JSON schema field "myNumber". MyNumber *float64 `json:"myNumber,omitempty"` // MyString corresponds to the JSON schema field "myString". MyString *string `json:"myString,omitempty"` -} - -// UnmarshalJSON implements json.Unmarshaler. -func (j *Primitives) UnmarshalJSON(b []byte) error { - var v struct { - } - if err := json.Unmarshal(b, &v); err != nil { return err } - type plain Primitives - var p plain - if err := json.Unmarshal(b, &p); err != nil { return err } - *j = Primitives(p) - return nil -} - +} \ No newline at end of file diff --git a/tests/data/core/ref.go.output b/tests/data/core/ref.go.output index e8d94baa..e3c30dcd 100644 --- a/tests/data/core/ref.go.output +++ b/tests/data/core/ref.go.output @@ -1,198 +1,11 @@ // THIS FILE IS AUTOMATICALLY GENERATED. DO NOT EDIT. package test -import "encoding/json" -import "fmt" - -type 421ArrayMyObjectArrayElem map[string]interface{} -type 421Array struct { - // MyBooleanArray corresponds to the JSON schema field "myBooleanArray". - MyBooleanArray []bool `json:"myBooleanArray,omitempty"` - - // MyNullArray corresponds to the JSON schema field "myNullArray". - MyNullArray []interface{} `json:"myNullArray,omitempty"` - - // MyNumberArray corresponds to the JSON schema field "myNumberArray". - MyNumberArray []float64 `json:"myNumberArray,omitempty"` - - // MyObjectArray corresponds to the JSON schema field "myObjectArray". - MyObjectArray []421ArrayMyObjectArrayElem `json:"myObjectArray,omitempty"` - - // MyStringArray corresponds to the JSON schema field "myStringArray". - MyStringArray []string `json:"myStringArray,omitempty"` -} - -// UnmarshalJSON implements json.Unmarshaler. -func (j *421Array) UnmarshalJSON(b []byte) error { - var v struct { - } - if err := json.Unmarshal(b, &v); err != nil { return err } - type plain 421Array - var p plain - if err := json.Unmarshal(b, &p); err != nil { return err } - *j = 421Array(p) - return nil -} - - -type ObjectMyObject struct { - // MyString corresponds to the JSON schema field "myString". - MyString string `json:"myString"` -} - -// UnmarshalJSON implements json.Unmarshaler. -func (j *ObjectMyObject) UnmarshalJSON(b []byte) error { - var v struct { - MyString *string `json:"myString"` - } - if err := json.Unmarshal(b, &v); err != nil { return err } - if v.MyString == nil { - return fmt.Errorf("field myString: must be set") - } - type plain ObjectMyObject - var p plain - if err := json.Unmarshal(b, &p); err != nil { return err } - *j = ObjectMyObject(p) - return nil -} - - -type Object struct { - // MyObject corresponds to the JSON schema field "myObject". - MyObject *ObjectMyObject `json:"myObject,omitempty"` -} - -// UnmarshalJSON implements json.Unmarshaler. -func (j *Object) UnmarshalJSON(b []byte) error { - var v struct { - } - if err := json.Unmarshal(b, &v); err != nil { return err } - type plain Object - var p plain - if err := json.Unmarshal(b, &p); err != nil { return err } - *j = Object(p) - return nil -} - - -type ObjectEmptyFoo map[string]interface{} -type ObjectEmpty struct { - // Foo corresponds to the JSON schema field "foo". - Foo ObjectEmptyFoo `json:"foo,omitempty"` -} - -// UnmarshalJSON implements json.Unmarshaler. -func (j *ObjectEmpty) UnmarshalJSON(b []byte) error { - var v struct { - } - if err := json.Unmarshal(b, &v); err != nil { return err } - type plain ObjectEmpty - var p plain - if err := json.Unmarshal(b, &p); err != nil { return err } - *j = ObjectEmpty(p) - return nil -} - - -type ObjectNestedMyObjectMyObject struct { - // MyString corresponds to the JSON schema field "myString". - MyString *string `json:"myString,omitempty"` -} - -// UnmarshalJSON implements json.Unmarshaler. -func (j *ObjectNestedMyObjectMyObject) UnmarshalJSON(b []byte) error { - var v struct { - } - if err := json.Unmarshal(b, &v); err != nil { return err } - type plain ObjectNestedMyObjectMyObject - var p plain - if err := json.Unmarshal(b, &p); err != nil { return err } - *j = ObjectNestedMyObjectMyObject(p) - return nil -} - - -type ObjectNestedMyObject struct { - // MyObject corresponds to the JSON schema field "myObject". - MyObject *ObjectNestedMyObjectMyObject `json:"myObject,omitempty"` -} - -// UnmarshalJSON implements json.Unmarshaler. -func (j *ObjectNestedMyObject) UnmarshalJSON(b []byte) error { - var v struct { - } - if err := json.Unmarshal(b, &v); err != nil { return err } - type plain ObjectNestedMyObject - var p plain - if err := json.Unmarshal(b, &p); err != nil { return err } - *j = ObjectNestedMyObject(p) - return nil -} - - -type ObjectNested struct { - // MyObject corresponds to the JSON schema field "myObject". - MyObject *ObjectNestedMyObject `json:"myObject,omitempty"` -} - -// UnmarshalJSON implements json.Unmarshaler. -func (j *ObjectNested) UnmarshalJSON(b []byte) error { - var v struct { - } - if err := json.Unmarshal(b, &v); err != nil { return err } - type plain ObjectNested - var p plain - if err := json.Unmarshal(b, &p); err != nil { return err } - *j = ObjectNested(p) - return nil -} - - -type Primitives struct { - // MyBoolean corresponds to the JSON schema field "myBoolean". - MyBoolean *bool `json:"myBoolean,omitempty"` - - // MyNull corresponds to the JSON schema field "myNull". - MyNull interface{} `json:"myNull,omitempty"` - - // MyNumber corresponds to the JSON schema field "myNumber". - MyNumber *float64 `json:"myNumber,omitempty"` - - // MyString corresponds to the JSON schema field "myString". - MyString *string `json:"myString,omitempty"` -} - -// UnmarshalJSON implements json.Unmarshaler. -func (j *Primitives) UnmarshalJSON(b []byte) error { - var v struct { - } - if err := json.Unmarshal(b, &v); err != nil { return err } - type plain Primitives - var p plain - if err := json.Unmarshal(b, &p); err != nil { return err } - *j = Primitives(p) - return nil -} - type Thing struct { // A field. Name *string `json:"name,omitempty"` } - -// UnmarshalJSON implements json.Unmarshaler. -func (j *Thing) UnmarshalJSON(b []byte) error { - var v struct { - } - if err := json.Unmarshal(b, &v); err != nil { return err } - type plain Thing - var p plain - if err := json.Unmarshal(b, &p); err != nil { return err } - *j = Thing(p) - return nil -} - - // A simple schema. type Ref struct { // MyThing corresponds to the JSON schema field "myThing". @@ -200,17 +13,4 @@ type Ref struct { // MyThing2 corresponds to the JSON schema field "myThing2". MyThing2 *Thing `json:"myThing2,omitempty"` -} - -// UnmarshalJSON implements json.Unmarshaler. -func (j *Ref) UnmarshalJSON(b []byte) error { - var v struct { - } - if err := json.Unmarshal(b, &v); err != nil { return err } - type plain Ref - var p plain - if err := json.Unmarshal(b, &p); err != nil { return err } - *j = Ref(p) - return nil -} - +} \ No newline at end of file diff --git a/tests/data/core/refExternalFile.go.output b/tests/data/core/refExternalFile.go.output index e4fb371e..b47997e2 100644 --- a/tests/data/core/refExternalFile.go.output +++ b/tests/data/core/refExternalFile.go.output @@ -1,198 +1,11 @@ // THIS FILE IS AUTOMATICALLY GENERATED. DO NOT EDIT. package test -import "encoding/json" -import "fmt" - -type 421ArrayMyObjectArrayElem map[string]interface{} -type 421Array struct { - // MyBooleanArray corresponds to the JSON schema field "myBooleanArray". - MyBooleanArray []bool `json:"myBooleanArray,omitempty"` - - // MyNullArray corresponds to the JSON schema field "myNullArray". - MyNullArray []interface{} `json:"myNullArray,omitempty"` - - // MyNumberArray corresponds to the JSON schema field "myNumberArray". - MyNumberArray []float64 `json:"myNumberArray,omitempty"` - - // MyObjectArray corresponds to the JSON schema field "myObjectArray". - MyObjectArray []421ArrayMyObjectArrayElem `json:"myObjectArray,omitempty"` - - // MyStringArray corresponds to the JSON schema field "myStringArray". - MyStringArray []string `json:"myStringArray,omitempty"` -} - -// UnmarshalJSON implements json.Unmarshaler. -func (j *421Array) UnmarshalJSON(b []byte) error { - var v struct { - } - if err := json.Unmarshal(b, &v); err != nil { return err } - type plain 421Array - var p plain - if err := json.Unmarshal(b, &p); err != nil { return err } - *j = 421Array(p) - return nil -} - - -type ObjectMyObject struct { - // MyString corresponds to the JSON schema field "myString". - MyString string `json:"myString"` -} - -// UnmarshalJSON implements json.Unmarshaler. -func (j *ObjectMyObject) UnmarshalJSON(b []byte) error { - var v struct { - MyString *string `json:"myString"` - } - if err := json.Unmarshal(b, &v); err != nil { return err } - if v.MyString == nil { - return fmt.Errorf("field myString: must be set") - } - type plain ObjectMyObject - var p plain - if err := json.Unmarshal(b, &p); err != nil { return err } - *j = ObjectMyObject(p) - return nil -} - - -type Object struct { - // MyObject corresponds to the JSON schema field "myObject". - MyObject *ObjectMyObject `json:"myObject,omitempty"` -} - -// UnmarshalJSON implements json.Unmarshaler. -func (j *Object) UnmarshalJSON(b []byte) error { - var v struct { - } - if err := json.Unmarshal(b, &v); err != nil { return err } - type plain Object - var p plain - if err := json.Unmarshal(b, &p); err != nil { return err } - *j = Object(p) - return nil -} - - -type ObjectEmptyFoo map[string]interface{} -type ObjectEmpty struct { - // Foo corresponds to the JSON schema field "foo". - Foo ObjectEmptyFoo `json:"foo,omitempty"` -} - -// UnmarshalJSON implements json.Unmarshaler. -func (j *ObjectEmpty) UnmarshalJSON(b []byte) error { - var v struct { - } - if err := json.Unmarshal(b, &v); err != nil { return err } - type plain ObjectEmpty - var p plain - if err := json.Unmarshal(b, &p); err != nil { return err } - *j = ObjectEmpty(p) - return nil -} - - -type ObjectNestedMyObjectMyObject struct { - // MyString corresponds to the JSON schema field "myString". - MyString *string `json:"myString,omitempty"` -} - -// UnmarshalJSON implements json.Unmarshaler. -func (j *ObjectNestedMyObjectMyObject) UnmarshalJSON(b []byte) error { - var v struct { - } - if err := json.Unmarshal(b, &v); err != nil { return err } - type plain ObjectNestedMyObjectMyObject - var p plain - if err := json.Unmarshal(b, &p); err != nil { return err } - *j = ObjectNestedMyObjectMyObject(p) - return nil -} - - -type ObjectNestedMyObject struct { - // MyObject corresponds to the JSON schema field "myObject". - MyObject *ObjectNestedMyObjectMyObject `json:"myObject,omitempty"` -} - -// UnmarshalJSON implements json.Unmarshaler. -func (j *ObjectNestedMyObject) UnmarshalJSON(b []byte) error { - var v struct { - } - if err := json.Unmarshal(b, &v); err != nil { return err } - type plain ObjectNestedMyObject - var p plain - if err := json.Unmarshal(b, &p); err != nil { return err } - *j = ObjectNestedMyObject(p) - return nil -} - - -type ObjectNested struct { - // MyObject corresponds to the JSON schema field "myObject". - MyObject *ObjectNestedMyObject `json:"myObject,omitempty"` -} - -// UnmarshalJSON implements json.Unmarshaler. -func (j *ObjectNested) UnmarshalJSON(b []byte) error { - var v struct { - } - if err := json.Unmarshal(b, &v); err != nil { return err } - type plain ObjectNested - var p plain - if err := json.Unmarshal(b, &p); err != nil { return err } - *j = ObjectNested(p) - return nil -} - - -type Primitives struct { - // MyBoolean corresponds to the JSON schema field "myBoolean". - MyBoolean *bool `json:"myBoolean,omitempty"` - - // MyNull corresponds to the JSON schema field "myNull". - MyNull interface{} `json:"myNull,omitempty"` - - // MyNumber corresponds to the JSON schema field "myNumber". - MyNumber *float64 `json:"myNumber,omitempty"` - - // MyString corresponds to the JSON schema field "myString". - MyString *string `json:"myString,omitempty"` -} - -// UnmarshalJSON implements json.Unmarshaler. -func (j *Primitives) UnmarshalJSON(b []byte) error { - var v struct { - } - if err := json.Unmarshal(b, &v); err != nil { return err } - type plain Primitives - var p plain - if err := json.Unmarshal(b, &p); err != nil { return err } - *j = Primitives(p) - return nil -} - type Thing struct { // A field. Name *string `json:"name,omitempty"` } - -// UnmarshalJSON implements json.Unmarshaler. -func (j *Thing) UnmarshalJSON(b []byte) error { - var v struct { - } - if err := json.Unmarshal(b, &v); err != nil { return err } - type plain Thing - var p plain - if err := json.Unmarshal(b, &p); err != nil { return err } - *j = Thing(p) - return nil -} - - // A simple schema. type Ref struct { // MyThing corresponds to the JSON schema field "myThing". @@ -201,57 +14,12 @@ type Ref struct { // MyThing2 corresponds to the JSON schema field "myThing2". MyThing2 *Thing `json:"myThing2,omitempty"` } - -// UnmarshalJSON implements json.Unmarshaler. -func (j *Ref) UnmarshalJSON(b []byte) error { - var v struct { - } - if err := json.Unmarshal(b, &v); err != nil { return err } - type plain Ref - var p plain - if err := json.Unmarshal(b, &p); err != nil { return err } - *j = Ref(p) - return nil -} - - -type Thing_1 struct { - // A field. - Name *string `json:"name,omitempty"` -} - -// UnmarshalJSON implements json.Unmarshaler. -func (j *Thing_1) UnmarshalJSON(b []byte) error { - var v struct { - } - if err := json.Unmarshal(b, &v); err != nil { return err } - type plain Thing_1 - var p plain - if err := json.Unmarshal(b, &p); err != nil { return err } - *j = Thing_1(p) - return nil -} - - // A simple schema. type RefExternalFile struct { // MyExternalThing corresponds to the JSON schema field "myExternalThing". - MyExternalThing *Thing_1 `json:"myExternalThing,omitempty"` + MyExternalThing *Thing `json:"myExternalThing,omitempty"` // SomeOtherExternalThing corresponds to the JSON schema field // "someOtherExternalThing". - SomeOtherExternalThing *Thing_1 `json:"someOtherExternalThing,omitempty"` -} - -// UnmarshalJSON implements json.Unmarshaler. -func (j *RefExternalFile) UnmarshalJSON(b []byte) error { - var v struct { - } - if err := json.Unmarshal(b, &v); err != nil { return err } - type plain RefExternalFile - var p plain - if err := json.Unmarshal(b, &p); err != nil { return err } - *j = RefExternalFile(p) - return nil -} - + SomeOtherExternalThing *Thing `json:"someOtherExternalThing,omitempty"` +} \ No newline at end of file diff --git a/tests/data/core/refExternalFileWithDupe.go.output b/tests/data/core/refExternalFileWithDupe.go.output index daf34867..16f17d6a 100644 --- a/tests/data/core/refExternalFileWithDupe.go.output +++ b/tests/data/core/refExternalFileWithDupe.go.output @@ -1,198 +1,11 @@ // THIS FILE IS AUTOMATICALLY GENERATED. DO NOT EDIT. package test -import "encoding/json" -import "fmt" - -type 421ArrayMyObjectArrayElem map[string]interface{} -type 421Array struct { - // MyBooleanArray corresponds to the JSON schema field "myBooleanArray". - MyBooleanArray []bool `json:"myBooleanArray,omitempty"` - - // MyNullArray corresponds to the JSON schema field "myNullArray". - MyNullArray []interface{} `json:"myNullArray,omitempty"` - - // MyNumberArray corresponds to the JSON schema field "myNumberArray". - MyNumberArray []float64 `json:"myNumberArray,omitempty"` - - // MyObjectArray corresponds to the JSON schema field "myObjectArray". - MyObjectArray []421ArrayMyObjectArrayElem `json:"myObjectArray,omitempty"` - - // MyStringArray corresponds to the JSON schema field "myStringArray". - MyStringArray []string `json:"myStringArray,omitempty"` -} - -// UnmarshalJSON implements json.Unmarshaler. -func (j *421Array) UnmarshalJSON(b []byte) error { - var v struct { - } - if err := json.Unmarshal(b, &v); err != nil { return err } - type plain 421Array - var p plain - if err := json.Unmarshal(b, &p); err != nil { return err } - *j = 421Array(p) - return nil -} - - -type ObjectMyObject struct { - // MyString corresponds to the JSON schema field "myString". - MyString string `json:"myString"` -} - -// UnmarshalJSON implements json.Unmarshaler. -func (j *ObjectMyObject) UnmarshalJSON(b []byte) error { - var v struct { - MyString *string `json:"myString"` - } - if err := json.Unmarshal(b, &v); err != nil { return err } - if v.MyString == nil { - return fmt.Errorf("field myString: must be set") - } - type plain ObjectMyObject - var p plain - if err := json.Unmarshal(b, &p); err != nil { return err } - *j = ObjectMyObject(p) - return nil -} - - -type Object struct { - // MyObject corresponds to the JSON schema field "myObject". - MyObject *ObjectMyObject `json:"myObject,omitempty"` -} - -// UnmarshalJSON implements json.Unmarshaler. -func (j *Object) UnmarshalJSON(b []byte) error { - var v struct { - } - if err := json.Unmarshal(b, &v); err != nil { return err } - type plain Object - var p plain - if err := json.Unmarshal(b, &p); err != nil { return err } - *j = Object(p) - return nil -} - - -type ObjectEmptyFoo map[string]interface{} -type ObjectEmpty struct { - // Foo corresponds to the JSON schema field "foo". - Foo ObjectEmptyFoo `json:"foo,omitempty"` -} - -// UnmarshalJSON implements json.Unmarshaler. -func (j *ObjectEmpty) UnmarshalJSON(b []byte) error { - var v struct { - } - if err := json.Unmarshal(b, &v); err != nil { return err } - type plain ObjectEmpty - var p plain - if err := json.Unmarshal(b, &p); err != nil { return err } - *j = ObjectEmpty(p) - return nil -} - - -type ObjectNestedMyObjectMyObject struct { - // MyString corresponds to the JSON schema field "myString". - MyString *string `json:"myString,omitempty"` -} - -// UnmarshalJSON implements json.Unmarshaler. -func (j *ObjectNestedMyObjectMyObject) UnmarshalJSON(b []byte) error { - var v struct { - } - if err := json.Unmarshal(b, &v); err != nil { return err } - type plain ObjectNestedMyObjectMyObject - var p plain - if err := json.Unmarshal(b, &p); err != nil { return err } - *j = ObjectNestedMyObjectMyObject(p) - return nil -} - - -type ObjectNestedMyObject struct { - // MyObject corresponds to the JSON schema field "myObject". - MyObject *ObjectNestedMyObjectMyObject `json:"myObject,omitempty"` -} - -// UnmarshalJSON implements json.Unmarshaler. -func (j *ObjectNestedMyObject) UnmarshalJSON(b []byte) error { - var v struct { - } - if err := json.Unmarshal(b, &v); err != nil { return err } - type plain ObjectNestedMyObject - var p plain - if err := json.Unmarshal(b, &p); err != nil { return err } - *j = ObjectNestedMyObject(p) - return nil -} - - -type ObjectNested struct { - // MyObject corresponds to the JSON schema field "myObject". - MyObject *ObjectNestedMyObject `json:"myObject,omitempty"` -} - -// UnmarshalJSON implements json.Unmarshaler. -func (j *ObjectNested) UnmarshalJSON(b []byte) error { - var v struct { - } - if err := json.Unmarshal(b, &v); err != nil { return err } - type plain ObjectNested - var p plain - if err := json.Unmarshal(b, &p); err != nil { return err } - *j = ObjectNested(p) - return nil -} - - -type Primitives struct { - // MyBoolean corresponds to the JSON schema field "myBoolean". - MyBoolean *bool `json:"myBoolean,omitempty"` - - // MyNull corresponds to the JSON schema field "myNull". - MyNull interface{} `json:"myNull,omitempty"` - - // MyNumber corresponds to the JSON schema field "myNumber". - MyNumber *float64 `json:"myNumber,omitempty"` - - // MyString corresponds to the JSON schema field "myString". - MyString *string `json:"myString,omitempty"` -} - -// UnmarshalJSON implements json.Unmarshaler. -func (j *Primitives) UnmarshalJSON(b []byte) error { - var v struct { - } - if err := json.Unmarshal(b, &v); err != nil { return err } - type plain Primitives - var p plain - if err := json.Unmarshal(b, &p); err != nil { return err } - *j = Primitives(p) - return nil -} - type Thing struct { // A field. Name *string `json:"name,omitempty"` } - -// UnmarshalJSON implements json.Unmarshaler. -func (j *Thing) UnmarshalJSON(b []byte) error { - var v struct { - } - if err := json.Unmarshal(b, &v); err != nil { return err } - type plain Thing - var p plain - if err := json.Unmarshal(b, &p); err != nil { return err } - *j = Thing(p) - return nil -} - - // A simple schema. type Ref struct { // MyThing corresponds to the JSON schema field "myThing". @@ -201,97 +14,15 @@ type Ref struct { // MyThing2 corresponds to the JSON schema field "myThing2". MyThing2 *Thing `json:"myThing2,omitempty"` } - -// UnmarshalJSON implements json.Unmarshaler. -func (j *Ref) UnmarshalJSON(b []byte) error { - var v struct { - } - if err := json.Unmarshal(b, &v); err != nil { return err } - type plain Ref - var p plain - if err := json.Unmarshal(b, &p); err != nil { return err } - *j = Ref(p) - return nil -} - - type Thing_1 struct { - // A field. - Name *string `json:"name,omitempty"` -} - -// UnmarshalJSON implements json.Unmarshaler. -func (j *Thing_1) UnmarshalJSON(b []byte) error { - var v struct { - } - if err := json.Unmarshal(b, &v); err != nil { return err } - type plain Thing_1 - var p plain - if err := json.Unmarshal(b, &p); err != nil { return err } - *j = Thing_1(p) - return nil -} - - -// A simple schema. -type RefExternalFile struct { - // MyExternalThing corresponds to the JSON schema field "myExternalThing". - MyExternalThing *Thing_1 `json:"myExternalThing,omitempty"` - - // SomeOtherExternalThing corresponds to the JSON schema field - // "someOtherExternalThing". - SomeOtherExternalThing *Thing_1 `json:"someOtherExternalThing,omitempty"` -} - -// UnmarshalJSON implements json.Unmarshaler. -func (j *RefExternalFile) UnmarshalJSON(b []byte) error { - var v struct { - } - if err := json.Unmarshal(b, &v); err != nil { return err } - type plain RefExternalFile - var p plain - if err := json.Unmarshal(b, &p); err != nil { return err } - *j = RefExternalFile(p) - return nil -} - - -type Thing_2 struct { // Something corresponds to the JSON schema field "something". Something *string `json:"something,omitempty"` } - -// UnmarshalJSON implements json.Unmarshaler. -func (j *Thing_2) UnmarshalJSON(b []byte) error { - var v struct { - } - if err := json.Unmarshal(b, &v); err != nil { return err } - type plain Thing_2 - var p plain - if err := json.Unmarshal(b, &p); err != nil { return err } - *j = Thing_2(p) - return nil -} - - // A simple schema. type RefExternalFileWithDupe struct { // MyExternalThing corresponds to the JSON schema field "myExternalThing". - MyExternalThing *Thing_1 `json:"myExternalThing,omitempty"` + MyExternalThing *Thing `json:"myExternalThing,omitempty"` // MyThing corresponds to the JSON schema field "myThing". - MyThing *Thing_2 `json:"myThing,omitempty"` -} - -// UnmarshalJSON implements json.Unmarshaler. -func (j *RefExternalFileWithDupe) UnmarshalJSON(b []byte) error { - var v struct { - } - if err := json.Unmarshal(b, &v); err != nil { return err } - type plain RefExternalFileWithDupe - var p plain - if err := json.Unmarshal(b, &p); err != nil { return err } - *j = RefExternalFileWithDupe(p) - return nil -} - + MyThing *Thing_1 `json:"myThing,omitempty"` +} \ No newline at end of file diff --git a/tests/data/crossPackage/other.go.output b/tests/data/crossPackage/other.go.output index 181cf848..a369b517 100644 --- a/tests/data/crossPackage/other.go.output +++ b/tests/data/crossPackage/other.go.output @@ -1,22 +1,8 @@ // THIS FILE IS AUTOMATICALLY GENERATED. DO NOT EDIT. package other -import "encoding/json" type Thing struct { // A field. S *string `json:"s,omitempty"` -} - -// UnmarshalJSON implements json.Unmarshaler. -func (j *Thing) UnmarshalJSON(b []byte) error { - var v struct { - } - if err := json.Unmarshal(b, &v); err != nil { return err } - type plain Thing - var p plain - if err := json.Unmarshal(b, &p); err != nil { return err } - *j = Thing(p) - return nil -} - +} \ No newline at end of file diff --git a/tests/data/crossPackage/schema.go.output b/tests/data/crossPackage/schema.go.output index 46660e35..24391870 100644 --- a/tests/data/crossPackage/schema.go.output +++ b/tests/data/crossPackage/schema.go.output @@ -2,26 +2,11 @@ package schema import other "github.com/example/other" -import "encoding/json" type Thing struct { // A field. S *string `json:"s,omitempty"` } - -// UnmarshalJSON implements json.Unmarshaler. -func (j *Thing) UnmarshalJSON(b []byte) error { - var v struct { - } - if err := json.Unmarshal(b, &v); err != nil { return err } - type plain Thing - var p plain - if err := json.Unmarshal(b, &p); err != nil { return err } - *j = Thing(p) - return nil -} - - // A simple schema. type Schema struct { // DefInOtherSchema corresponds to the JSON schema field "defInOtherSchema". @@ -29,17 +14,4 @@ type Schema struct { // DefInSameSchema corresponds to the JSON schema field "defInSameSchema". DefInSameSchema *Thing `json:"defInSameSchema,omitempty"` -} - -// UnmarshalJSON implements json.Unmarshaler. -func (j *Schema) UnmarshalJSON(b []byte) error { - var v struct { - } - if err := json.Unmarshal(b, &v); err != nil { return err } - type plain Schema - var p plain - if err := json.Unmarshal(b, &p); err != nil { return err } - *j = Schema(p) - return nil -} - +} \ No newline at end of file diff --git a/tests/data/misc/capitalization.go.output b/tests/data/misc/capitalization.go.output index db945df3..a1b14a52 100644 --- a/tests/data/misc/capitalization.go.output +++ b/tests/data/misc/capitalization.go.output @@ -1,7 +1,6 @@ // THIS FILE IS AUTOMATICALLY GENERATED. DO NOT EDIT. package test -import "encoding/json" type Capitalization struct { // HtMl corresponds to the JSON schema field "html". @@ -39,17 +38,4 @@ type Capitalization struct { // URLSomething corresponds to the JSON schema field "url_something". URLSomething *string `json:"url_something,omitempty"` -} - -// UnmarshalJSON implements json.Unmarshaler. -func (j *Capitalization) UnmarshalJSON(b []byte) error { - var v struct { - } - if err := json.Unmarshal(b, &v); err != nil { return err } - type plain Capitalization - var p plain - if err := json.Unmarshal(b, &p); err != nil { return err } - *j = Capitalization(p) - return nil -} - +} \ No newline at end of file diff --git a/tests/data/miscWithDefaults/case.go.output b/tests/data/miscWithDefaults/case.go.output index d7c53eb8..ce79dad0 100644 --- a/tests/data/miscWithDefaults/case.go.output +++ b/tests/data/miscWithDefaults/case.go.output @@ -1,7 +1,6 @@ // THIS FILE IS AUTOMATICALLY GENERATED. DO NOT EDIT. package test -import "encoding/json" type Case struct { // CapitalCamelField corresponds to the JSON schema field "CapitalCamelField". @@ -21,17 +20,4 @@ type Case struct { // SnakeCase corresponds to the JSON schema field "snake_case". SnakeCase *string `json:"snake_case,omitempty"` -} - -// UnmarshalJSON implements json.Unmarshaler. -func (j *Case) UnmarshalJSON(b []byte) error { - var v struct { - } - if err := json.Unmarshal(b, &v); err != nil { return err } - type plain Case - var p plain - if err := json.Unmarshal(b, &p); err != nil { return err } - *j = Case(p) - return nil -} - +} \ No newline at end of file diff --git a/tests/data/miscWithDefaults/caseDupes.go.output b/tests/data/miscWithDefaults/caseDupes.go.output index bf974c73..36c9b213 100644 --- a/tests/data/miscWithDefaults/caseDupes.go.output +++ b/tests/data/miscWithDefaults/caseDupes.go.output @@ -1,40 +1,6 @@ // THIS FILE IS AUTOMATICALLY GENERATED. DO NOT EDIT. package test -import "encoding/json" - -type Case struct { - // CapitalCamelField corresponds to the JSON schema field "CapitalCamelField". - CapitalCamelField *string `json:"CapitalCamelField,omitempty"` - - // UPPERCASEFIELD corresponds to the JSON schema field "UPPERCASEFIELD". - UPPERCASEFIELD *string `json:"UPPERCASEFIELD,omitempty"` - - // CamelCase corresponds to the JSON schema field "camelCase". - CamelCase *string `json:"camelCase,omitempty"` - - // Lowercase corresponds to the JSON schema field "lowercase". - Lowercase *string `json:"lowercase,omitempty"` - - // SnakeMixedCase corresponds to the JSON schema field "snake_Mixed_Case". - SnakeMixedCase *string `json:"snake_Mixed_Case,omitempty"` - - // SnakeCase corresponds to the JSON schema field "snake_case". - SnakeCase *string `json:"snake_case,omitempty"` -} - -// UnmarshalJSON implements json.Unmarshaler. -func (j *Case) UnmarshalJSON(b []byte) error { - var v struct { - } - if err := json.Unmarshal(b, &v); err != nil { return err } - type plain Case - var p plain - if err := json.Unmarshal(b, &p); err != nil { return err } - *j = Case(p) - return nil -} - type CaseDupes struct { // SomeField corresponds to the JSON schema field "SomeField". @@ -51,17 +17,4 @@ type CaseDupes struct { // Somefield corresponds to the JSON schema field "somefield". Somefield *string `json:"somefield,omitempty"` -} - -// UnmarshalJSON implements json.Unmarshaler. -func (j *CaseDupes) UnmarshalJSON(b []byte) error { - var v struct { - } - if err := json.Unmarshal(b, &v); err != nil { return err } - type plain CaseDupes - var p plain - if err := json.Unmarshal(b, &p); err != nil { return err } - *j = CaseDupes(p) - return nil -} - +} \ No newline at end of file diff --git a/tests/data/miscWithDefaults/rootEmptyJustDefinitions.go.output b/tests/data/miscWithDefaults/rootEmptyJustDefinitions.go.output index c3541379..b7f3570a 100644 --- a/tests/data/miscWithDefaults/rootEmptyJustDefinitions.go.output +++ b/tests/data/miscWithDefaults/rootEmptyJustDefinitions.go.output @@ -1,85 +1,8 @@ // THIS FILE IS AUTOMATICALLY GENERATED. DO NOT EDIT. package test -import "encoding/json" - -type Case struct { - // CapitalCamelField corresponds to the JSON schema field "CapitalCamelField". - CapitalCamelField *string `json:"CapitalCamelField,omitempty"` - - // UPPERCASEFIELD corresponds to the JSON schema field "UPPERCASEFIELD". - UPPERCASEFIELD *string `json:"UPPERCASEFIELD,omitempty"` - - // CamelCase corresponds to the JSON schema field "camelCase". - CamelCase *string `json:"camelCase,omitempty"` - - // Lowercase corresponds to the JSON schema field "lowercase". - Lowercase *string `json:"lowercase,omitempty"` - - // SnakeMixedCase corresponds to the JSON schema field "snake_Mixed_Case". - SnakeMixedCase *string `json:"snake_Mixed_Case,omitempty"` - - // SnakeCase corresponds to the JSON schema field "snake_case". - SnakeCase *string `json:"snake_case,omitempty"` -} - -// UnmarshalJSON implements json.Unmarshaler. -func (j *Case) UnmarshalJSON(b []byte) error { - var v struct { - } - if err := json.Unmarshal(b, &v); err != nil { return err } - type plain Case - var p plain - if err := json.Unmarshal(b, &p); err != nil { return err } - *j = Case(p) - return nil -} - - -type CaseDupes struct { - // SomeField corresponds to the JSON schema field "SomeField". - SomeField *string `json:"SomeField,omitempty"` - - // SomeField_2 corresponds to the JSON schema field "someField". - SomeField_2 *string `json:"someField,omitempty"` - - // SomeField_3 corresponds to the JSON schema field "some_Field". - SomeField_3 *string `json:"some_Field,omitempty"` - - // SomeField_4 corresponds to the JSON schema field "some_field". - SomeField_4 *string `json:"some_field,omitempty"` - - // Somefield corresponds to the JSON schema field "somefield". - Somefield *string `json:"somefield,omitempty"` -} - -// UnmarshalJSON implements json.Unmarshaler. -func (j *CaseDupes) UnmarshalJSON(b []byte) error { - var v struct { - } - if err := json.Unmarshal(b, &v); err != nil { return err } - type plain CaseDupes - var p plain - if err := json.Unmarshal(b, &p); err != nil { return err } - *j = CaseDupes(p) - return nil -} - type Thing struct { // A field. Name *string `json:"name,omitempty"` -} - -// UnmarshalJSON implements json.Unmarshaler. -func (j *Thing) UnmarshalJSON(b []byte) error { - var v struct { - } - if err := json.Unmarshal(b, &v); err != nil { return err } - type plain Thing - var p plain - if err := json.Unmarshal(b, &p); err != nil { return err } - *j = Thing(p) - return nil -} - +} \ No newline at end of file diff --git a/tests/data/miscWithDefaults/rootIsArrayOfString.go.output b/tests/data/miscWithDefaults/rootIsArrayOfString.go.output index 431ab1a4..ec0d2975 100644 --- a/tests/data/miscWithDefaults/rootIsArrayOfString.go.output +++ b/tests/data/miscWithDefaults/rootIsArrayOfString.go.output @@ -1,87 +1,5 @@ // THIS FILE IS AUTOMATICALLY GENERATED. DO NOT EDIT. package test -import "encoding/json" - -type Case struct { - // CapitalCamelField corresponds to the JSON schema field "CapitalCamelField". - CapitalCamelField *string `json:"CapitalCamelField,omitempty"` - - // UPPERCASEFIELD corresponds to the JSON schema field "UPPERCASEFIELD". - UPPERCASEFIELD *string `json:"UPPERCASEFIELD,omitempty"` - - // CamelCase corresponds to the JSON schema field "camelCase". - CamelCase *string `json:"camelCase,omitempty"` - - // Lowercase corresponds to the JSON schema field "lowercase". - Lowercase *string `json:"lowercase,omitempty"` - - // SnakeMixedCase corresponds to the JSON schema field "snake_Mixed_Case". - SnakeMixedCase *string `json:"snake_Mixed_Case,omitempty"` - - // SnakeCase corresponds to the JSON schema field "snake_case". - SnakeCase *string `json:"snake_case,omitempty"` -} - -// UnmarshalJSON implements json.Unmarshaler. -func (j *Case) UnmarshalJSON(b []byte) error { - var v struct { - } - if err := json.Unmarshal(b, &v); err != nil { return err } - type plain Case - var p plain - if err := json.Unmarshal(b, &p); err != nil { return err } - *j = Case(p) - return nil -} - - -type CaseDupes struct { - // SomeField corresponds to the JSON schema field "SomeField". - SomeField *string `json:"SomeField,omitempty"` - - // SomeField_2 corresponds to the JSON schema field "someField". - SomeField_2 *string `json:"someField,omitempty"` - - // SomeField_3 corresponds to the JSON schema field "some_Field". - SomeField_3 *string `json:"some_Field,omitempty"` - - // SomeField_4 corresponds to the JSON schema field "some_field". - SomeField_4 *string `json:"some_field,omitempty"` - - // Somefield corresponds to the JSON schema field "somefield". - Somefield *string `json:"somefield,omitempty"` -} - -// UnmarshalJSON implements json.Unmarshaler. -func (j *CaseDupes) UnmarshalJSON(b []byte) error { - var v struct { - } - if err := json.Unmarshal(b, &v); err != nil { return err } - type plain CaseDupes - var p plain - if err := json.Unmarshal(b, &p); err != nil { return err } - *j = CaseDupes(p) - return nil -} - - -type Thing struct { - // A field. - Name *string `json:"name,omitempty"` -} - -// UnmarshalJSON implements json.Unmarshaler. -func (j *Thing) UnmarshalJSON(b []byte) error { - var v struct { - } - if err := json.Unmarshal(b, &v); err != nil { return err } - type plain Thing - var p plain - if err := json.Unmarshal(b, &p); err != nil { return err } - *j = Thing(p) - return nil -} - type RootIsArrayOfString []string \ No newline at end of file diff --git a/tests/data/validation/10.1_description.go.output b/tests/data/validation/10.1_description.go.output index 9a66f470..2532956d 100644 --- a/tests/data/validation/10.1_description.go.output +++ b/tests/data/validation/10.1_description.go.output @@ -1,7 +1,6 @@ // THIS FILE IS AUTOMATICALLY GENERATED. DO NOT EDIT. package test -import "encoding/json" // A simple schema. type 101Description struct { @@ -11,17 +10,4 @@ type 101Description struct { // A string field. MyField *string `json:"myField,omitempty"` -} - -// UnmarshalJSON implements json.Unmarshaler. -func (j *101Description) UnmarshalJSON(b []byte) error { - var v struct { - } - if err := json.Unmarshal(b, &v); err != nil { return err } - type plain 101Description - var p plain - if err := json.Unmarshal(b, &p); err != nil { return err } - *j = 101Description(p) - return nil -} - +} \ No newline at end of file diff --git a/tests/data/validation/6.1.2_enum.go.output b/tests/data/validation/6.1.2_enum.go.output index bd61e340..edbf831c 100644 --- a/tests/data/validation/6.1.2_enum.go.output +++ b/tests/data/validation/6.1.2_enum.go.output @@ -1,32 +1,9 @@ // THIS FILE IS AUTOMATICALLY GENERATED. DO NOT EDIT. package test -import "encoding/json" import "fmt" import "reflect" - -// A simple schema. -type 101Description struct { - // MyDescriptionlessField corresponds to the JSON schema field - // "myDescriptionlessField". - MyDescriptionlessField *string `json:"myDescriptionlessField,omitempty"` - - // A string field. - MyField *string `json:"myField,omitempty"` -} - -// UnmarshalJSON implements json.Unmarshaler. -func (j *101Description) UnmarshalJSON(b []byte) error { - var v struct { - } - if err := json.Unmarshal(b, &v); err != nil { return err } - type plain 101Description - var p plain - if err := json.Unmarshal(b, &p); err != nil { return err } - *j = 101Description(p) - return nil -} - +import "encoding/json" type 612EnumMyBooleanTypedEnumEnum bool var enumValues_612EnumMyBooleanTypedEnumEnum = []interface {}{ @@ -194,17 +171,4 @@ type 612Enum struct { // MyStringUntypedEnum corresponds to the JSON schema field "myStringUntypedEnum". MyStringUntypedEnum *612EnumMyStringTypedEnumEnum `json:"myStringUntypedEnum,omitempty"` -} - -// UnmarshalJSON implements json.Unmarshaler. -func (j *612Enum) UnmarshalJSON(b []byte) error { - var v struct { - } - if err := json.Unmarshal(b, &v); err != nil { return err } - type plain 612Enum - var p plain - if err := json.Unmarshal(b, &p); err != nil { return err } - *j = 612Enum(p) - return nil -} - +} \ No newline at end of file diff --git a/tests/data/validation/6.5.3_requiredFields.go.output b/tests/data/validation/6.5.3_requiredFields.go.output index ec7d6375..fa4aac1d 100644 --- a/tests/data/validation/6.5.3_requiredFields.go.output +++ b/tests/data/validation/6.5.3_requiredFields.go.output @@ -1,213 +1,8 @@ // THIS FILE IS AUTOMATICALLY GENERATED. DO NOT EDIT. package test -import "encoding/json" import "fmt" -import "reflect" - -// A simple schema. -type 101Description struct { - // MyDescriptionlessField corresponds to the JSON schema field - // "myDescriptionlessField". - MyDescriptionlessField *string `json:"myDescriptionlessField,omitempty"` - - // A string field. - MyField *string `json:"myField,omitempty"` -} - -// UnmarshalJSON implements json.Unmarshaler. -func (j *101Description) UnmarshalJSON(b []byte) error { - var v struct { - } - if err := json.Unmarshal(b, &v); err != nil { return err } - type plain 101Description - var p plain - if err := json.Unmarshal(b, &p); err != nil { return err } - *j = 101Description(p) - return nil -} - - -type 612EnumMyBooleanTypedEnumEnum bool -var enumValues_612EnumMyBooleanTypedEnumEnum = []interface {}{ - true, - false, -} - -// UnmarshalJSON implements json.Unmarshaler. -func (j *612EnumMyBooleanTypedEnumEnum) UnmarshalJSON(b []byte) error { - var v bool - if err := json.Unmarshal(b, &v); err != nil { return err } - var ok bool - for _, expected := range enumValues_612EnumMyBooleanTypedEnumEnum { - if reflect.DeepEqual(v, expected) { ok = true; break } - } - if !ok { - return fmt.Errorf("invalid value (expected one of %#v): %#v", enumValues_612EnumMyBooleanTypedEnumEnum, v) - } - *j = 612EnumMyBooleanTypedEnumEnum(v) - return nil -} - - -type 612EnumMyMixedUntypedEnumEnum struct { - Value interface{} -} -var enumValues_612EnumMyMixedUntypedEnumEnum = []interface {}{ - "red", - 1, - true, - nil, -} - -// MarshalJSON implements json.Marshaler. -func (j *612EnumMyMixedUntypedEnumEnum) MarshalJSON() ([]byte, error) { - return json.Marshal(j.Value) -} - - - -// UnmarshalJSON implements json.Unmarshaler. -func (j *612EnumMyMixedUntypedEnumEnum) 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_612EnumMyMixedUntypedEnumEnum { - if reflect.DeepEqual(v.Value, expected) { ok = true; break } - } - if !ok { - return fmt.Errorf("invalid value (expected one of %#v): %#v", enumValues_612EnumMyMixedUntypedEnumEnum, v.Value) - } - *j = 612EnumMyMixedUntypedEnumEnum(v) - return nil -} - - -type 612EnumMyNullTypedEnumEnum struct { - Value interface{} -} -var enumValues_612EnumMyNullTypedEnumEnum = []interface {}{ - nil, -} - -// MarshalJSON implements json.Marshaler. -func (j *612EnumMyNullTypedEnumEnum) MarshalJSON() ([]byte, error) { - return json.Marshal(j.Value) -} - - - -// UnmarshalJSON implements json.Unmarshaler. -func (j *612EnumMyNullTypedEnumEnum) 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_612EnumMyNullTypedEnumEnum { - if reflect.DeepEqual(v.Value, expected) { ok = true; break } - } - if !ok { - return fmt.Errorf("invalid value (expected one of %#v): %#v", enumValues_612EnumMyNullTypedEnumEnum, v.Value) - } - *j = 612EnumMyNullTypedEnumEnum(v) - return nil -} - - -type 612EnumMyNumberTypedEnumEnum float64 -var enumValues_612EnumMyNumberTypedEnumEnum = []interface {}{ - 1, - 2, - 3, -} - -// UnmarshalJSON implements json.Unmarshaler. -func (j *612EnumMyNumberTypedEnumEnum) UnmarshalJSON(b []byte) error { - var v float64 - if err := json.Unmarshal(b, &v); err != nil { return err } - var ok bool - for _, expected := range enumValues_612EnumMyNumberTypedEnumEnum { - if reflect.DeepEqual(v, expected) { ok = true; break } - } - if !ok { - return fmt.Errorf("invalid value (expected one of %#v): %#v", enumValues_612EnumMyNumberTypedEnumEnum, v) - } - *j = 612EnumMyNumberTypedEnumEnum(v) - return nil -} - - -type 612EnumMyStringTypedEnumEnum string -var enumValues_612EnumMyStringTypedEnumEnum = []interface {}{ - "red", - "blue", - "green", -} - -// UnmarshalJSON implements json.Unmarshaler. -func (j *612EnumMyStringTypedEnumEnum) UnmarshalJSON(b []byte) error { - var v string - if err := json.Unmarshal(b, &v); err != nil { return err } - var ok bool - for _, expected := range enumValues_612EnumMyStringTypedEnumEnum { - if reflect.DeepEqual(v, expected) { ok = true; break } - } - if !ok { - return fmt.Errorf("invalid value (expected one of %#v): %#v", enumValues_612EnumMyStringTypedEnumEnum, v) - } - *j = 612EnumMyStringTypedEnumEnum(v) - return nil -} - - -const 612EnumMyStringTypedEnumEnumRed 612EnumMyStringTypedEnumEnum = "red" -const 612EnumMyStringTypedEnumEnumBlue 612EnumMyStringTypedEnumEnum = "blue" -const 612EnumMyStringTypedEnumEnumGreen 612EnumMyStringTypedEnumEnum = "green" -type 612Enum struct { - // MyBooleanTypedEnum corresponds to the JSON schema field "myBooleanTypedEnum". - MyBooleanTypedEnum *612EnumMyBooleanTypedEnumEnum `json:"myBooleanTypedEnum,omitempty"` - - // MyBooleanUntypedEnum corresponds to the JSON schema field - // "myBooleanUntypedEnum". - MyBooleanUntypedEnum *612EnumMyBooleanTypedEnumEnum `json:"myBooleanUntypedEnum,omitempty"` - - // MyMixedUntypedEnum corresponds to the JSON schema field "myMixedUntypedEnum". - MyMixedUntypedEnum *612EnumMyMixedUntypedEnumEnum `json:"myMixedUntypedEnum,omitempty"` - - // MyNullTypedEnum corresponds to the JSON schema field "myNullTypedEnum". - MyNullTypedEnum *612EnumMyNullTypedEnumEnum `json:"myNullTypedEnum,omitempty"` - - // MyNullUntypedEnum corresponds to the JSON schema field "myNullUntypedEnum". - MyNullUntypedEnum *612EnumMyNullTypedEnumEnum `json:"myNullUntypedEnum,omitempty"` - - // MyNumberTypedEnum corresponds to the JSON schema field "myNumberTypedEnum". - MyNumberTypedEnum *612EnumMyNumberTypedEnumEnum `json:"myNumberTypedEnum,omitempty"` - - // MyNumberUntypedEnum corresponds to the JSON schema field "myNumberUntypedEnum". - MyNumberUntypedEnum *612EnumMyNumberTypedEnumEnum `json:"myNumberUntypedEnum,omitempty"` - - // MyStringTypedEnum corresponds to the JSON schema field "myStringTypedEnum". - MyStringTypedEnum *612EnumMyStringTypedEnumEnum `json:"myStringTypedEnum,omitempty"` - - // MyStringUntypedEnum corresponds to the JSON schema field "myStringUntypedEnum". - MyStringUntypedEnum *612EnumMyStringTypedEnumEnum `json:"myStringUntypedEnum,omitempty"` -} - -// UnmarshalJSON implements json.Unmarshaler. -func (j *612Enum) UnmarshalJSON(b []byte) error { - var v struct { - } - if err := json.Unmarshal(b, &v); err != nil { return err } - type plain 612Enum - var p plain - if err := json.Unmarshal(b, &p); err != nil { return err } - *j = 612Enum(p) - return nil -} - +import "encoding/json" type 653RequiredFieldsMyObject struct { // MyNestedObjectString corresponds to the JSON schema field @@ -217,17 +12,15 @@ type 653RequiredFieldsMyObject struct { // UnmarshalJSON implements json.Unmarshaler. func (j *653RequiredFieldsMyObject) UnmarshalJSON(b []byte) error { - var v struct { - MyNestedObjectString *string `json:"myNestedObjectString"` - } - if err := json.Unmarshal(b, &v); err != nil { return err } - if v.MyNestedObjectString == nil { - return fmt.Errorf("field myNestedObjectString: must be set") - } - type plain 653RequiredFieldsMyObject - var p plain - if err := json.Unmarshal(b, &p); err != nil { return err } - *j = 653RequiredFieldsMyObject(p) + var raw map[string]interface{} + if err := json.Unmarshal(b, &raw); err != nil { return err } + if v, ok := raw["myNestedObjectString"]; !ok || v == nil { + return fmt.Errorf("field myNestedObjectString: required") + } + type Plain 653RequiredFieldsMyObject + var plain Plain + if err := json.Unmarshal(b, &plain); err != nil { return err } + *j = 653RequiredFieldsMyObject(plain) return nil } @@ -240,17 +33,15 @@ type 653RequiredFieldsMyObjectArrayElem struct { // UnmarshalJSON implements json.Unmarshaler. func (j *653RequiredFieldsMyObjectArrayElem) UnmarshalJSON(b []byte) error { - var v struct { - MyNestedObjectString *string `json:"myNestedObjectString"` - } - if err := json.Unmarshal(b, &v); err != nil { return err } - if v.MyNestedObjectString == nil { - return fmt.Errorf("field myNestedObjectString: must be set") - } - type plain 653RequiredFieldsMyObjectArrayElem - var p plain - if err := json.Unmarshal(b, &p); err != nil { return err } - *j = 653RequiredFieldsMyObjectArrayElem(p) + var raw map[string]interface{} + if err := json.Unmarshal(b, &raw); err != nil { return err } + if v, ok := raw["myNestedObjectString"]; !ok || v == nil { + return fmt.Errorf("field myNestedObjectString: required") + } + type Plain 653RequiredFieldsMyObjectArrayElem + var plain Plain + if err := json.Unmarshal(b, &plain); err != nil { return err } + *j = 653RequiredFieldsMyObjectArrayElem(plain) return nil } @@ -289,44 +80,42 @@ type 653RequiredFields struct { // UnmarshalJSON implements json.Unmarshaler. func (j *653RequiredFields) UnmarshalJSON(b []byte) error { - var v struct { - MyBoolean *bool `json:"myBoolean"` - MyNumber *float64 `json:"myNumber"` - MyObject *653RequiredFieldsMyObject `json:"myObject"` - MyString *string `json:"myString"` + var raw map[string]interface{} + if err := json.Unmarshal(b, &raw); err != nil { return err } + if v, ok := raw["myBoolean"]; !ok || v == nil { + return fmt.Errorf("field myBoolean: required") } - if err := json.Unmarshal(b, &v); err != nil { return err } - if v.MyBoolean == nil { - return fmt.Errorf("field myBoolean: must be set") + if v, ok := raw["myBooleanArray"]; !ok || v == nil { + return fmt.Errorf("field myBooleanArray: required") } - if v.MyNumber == nil { - return fmt.Errorf("field myNumber: must be set") + if v, ok := raw["myNull"]; !ok || v == nil { + return fmt.Errorf("field myNull: required") } - if v.MyObject == nil { - return fmt.Errorf("field myObject: must be set") + if v, ok := raw["myNullArray"]; !ok || v == nil { + return fmt.Errorf("field myNullArray: required") } - if v.MyString == nil { - return fmt.Errorf("field myString: must be set") + if v, ok := raw["myNumber"]; !ok || v == nil { + return fmt.Errorf("field myNumber: required") } - type plain 653RequiredFields - var p plain - if err := json.Unmarshal(b, &p); err != nil { return err } - if len(p.MyBooleanArray) == 0 { - return fmt.Errorf("field myBooleanArray: array cannot be empty") + if v, ok := raw["myNumberArray"]; !ok || v == nil { + return fmt.Errorf("field myNumberArray: required") } - if len(p.MyNullArray) == 0 { - return fmt.Errorf("field myNullArray: array cannot be empty") + if v, ok := raw["myObject"]; !ok || v == nil { + return fmt.Errorf("field myObject: required") } - if len(p.MyNumberArray) == 0 { - return fmt.Errorf("field myNumberArray: array cannot be empty") + if v, ok := raw["myObjectArray"]; !ok || v == nil { + return fmt.Errorf("field myObjectArray: required") } - if len(p.MyObjectArray) == 0 { - return fmt.Errorf("field myObjectArray: array cannot be empty") + if v, ok := raw["myString"]; !ok || v == nil { + return fmt.Errorf("field myString: required") } - if len(p.MyStringArray) == 0 { - return fmt.Errorf("field myStringArray: array cannot be empty") + if v, ok := raw["myStringArray"]; !ok || v == nil { + return fmt.Errorf("field myStringArray: required") } - *j = 653RequiredFields(p) + type Plain 653RequiredFields + var plain Plain + if err := json.Unmarshal(b, &plain); err != nil { return err } + *j = 653RequiredFields(plain) return nil }