Skip to content

Commit

Permalink
Fix generated unmarshaling code for synthetic fields.
Browse files Browse the repository at this point in the history
  • Loading branch information
atombender committed Oct 1, 2018
1 parent d8f9ec8 commit 29ff417
Show file tree
Hide file tree
Showing 18 changed files with 450 additions and 445 deletions.
31 changes: 18 additions & 13 deletions pkg/generator/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,30 +331,36 @@ func (g *schemaGenerator) generateDeclaredType(
out.Indent(1)
out.Println("var v struct {")
out.Indent(1)
out.Println("%s", decl.Name)

fields := append([]codegen.StructField{}, structType.Fields...)
for _, f := range structType.Fields {
if f.Synthetic {
f.Generate(out)
out.Newline()
}
}

out.Indent(-1)
out.Println("}")
out.Println("if err := json.Unmarshal(b, &v); err != nil {")
out.Indent(1)
out.Println("return err")
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))
}
}
}
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 {
for _, r := range f.Rules {
r.GenerateValidation(out, fmt.Sprintf("v.%s", f.Name),
fmt.Sprintf("field %s", f.JSONName))
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("*j = v.%s", decl.Name)
out.Println("*j = %s(p)", decl.Name)
out.Println("return nil")
out.Indent(-1)
out.Println("}")
Expand Down Expand Up @@ -489,7 +495,6 @@ func (g *schemaGenerator) generateStructType(
syntheticField := structField
syntheticField.Comment = ""
syntheticField.Synthetic = true
syntheticField.Name = "__synthetic_" + syntheticField.Name
syntheticField.Type = codegen.PointerType{Type: syntheticField.Type}
syntheticField.AddRule(codegen.NilStructFieldRequired{})
structType.AddField(syntheticField)
Expand Down
10 changes: 5 additions & 5 deletions tests/data/core/4.2.1_array.go.output
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ type 421Array struct {
// UnmarshalJSON implements json.Unmarshaler.
func (j *421Array) UnmarshalJSON(b []byte) error {
var v struct {
421Array
}
if err := json.Unmarshal(b, &v); err != nil {
return err
}
*j = v.421Array
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
}

34 changes: 17 additions & 17 deletions tests/data/core/object.go.output
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ type 421Array struct {
// UnmarshalJSON implements json.Unmarshaler.
func (j *421Array) UnmarshalJSON(b []byte) error {
var v struct {
421Array
}
if err := json.Unmarshal(b, &v); err != nil {
return err
}
*j = v.421Array
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
}

Expand All @@ -43,16 +43,16 @@ type ObjectMyObject struct {
// UnmarshalJSON implements json.Unmarshaler.
func (j *ObjectMyObject) UnmarshalJSON(b []byte) error {
var v struct {
ObjectMyObject
__synthetic_MyString *string `json:"myString"`
}
if err := json.Unmarshal(b, &v); err != nil {
return err
MyString *string `json:"myString"`
}
if v.__synthetic_MyString == nil {
if err := json.Unmarshal(b, &v); err != nil { return err }
if v.MyString == nil {
return fmt.Errorf("field myString: must be set")
}
*j = v.ObjectMyObject
type plain ObjectMyObject
var p plain
if err := json.Unmarshal(b, &p); err != nil { return err }
*j = ObjectMyObject(p)
return nil
}

Expand All @@ -65,12 +65,12 @@ type Object struct {
// UnmarshalJSON implements json.Unmarshaler.
func (j *Object) UnmarshalJSON(b []byte) error {
var v struct {
Object
}
if err := json.Unmarshal(b, &v); err != nil {
return err
}
*j = v.Object
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
}

44 changes: 22 additions & 22 deletions tests/data/core/objectEmpty.go.output
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ type 421Array struct {
// UnmarshalJSON implements json.Unmarshaler.
func (j *421Array) UnmarshalJSON(b []byte) error {
var v struct {
421Array
}
if err := json.Unmarshal(b, &v); err != nil {
return err
}
*j = v.421Array
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
}

Expand All @@ -43,16 +43,16 @@ type ObjectMyObject struct {
// UnmarshalJSON implements json.Unmarshaler.
func (j *ObjectMyObject) UnmarshalJSON(b []byte) error {
var v struct {
ObjectMyObject
__synthetic_MyString *string `json:"myString"`
}
if err := json.Unmarshal(b, &v); err != nil {
return err
MyString *string `json:"myString"`
}
if v.__synthetic_MyString == nil {
if err := json.Unmarshal(b, &v); err != nil { return err }
if v.MyString == nil {
return fmt.Errorf("field myString: must be set")
}
*j = v.ObjectMyObject
type plain ObjectMyObject
var p plain
if err := json.Unmarshal(b, &p); err != nil { return err }
*j = ObjectMyObject(p)
return nil
}

Expand All @@ -65,12 +65,12 @@ type Object struct {
// UnmarshalJSON implements json.Unmarshaler.
func (j *Object) UnmarshalJSON(b []byte) error {
var v struct {
Object
}
if err := json.Unmarshal(b, &v); err != nil {
return err
}
*j = v.Object
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
}

Expand All @@ -84,12 +84,12 @@ type ObjectEmpty struct {
// UnmarshalJSON implements json.Unmarshaler.
func (j *ObjectEmpty) UnmarshalJSON(b []byte) error {
var v struct {
ObjectEmpty
}
if err := json.Unmarshal(b, &v); err != nil {
return err
}
*j = v.ObjectEmpty
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
}

74 changes: 37 additions & 37 deletions tests/data/core/objectNested.go.output
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ type 421Array struct {
// UnmarshalJSON implements json.Unmarshaler.
func (j *421Array) UnmarshalJSON(b []byte) error {
var v struct {
421Array
}
if err := json.Unmarshal(b, &v); err != nil {
return err
}
*j = v.421Array
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
}

Expand All @@ -43,16 +43,16 @@ type ObjectMyObject struct {
// UnmarshalJSON implements json.Unmarshaler.
func (j *ObjectMyObject) UnmarshalJSON(b []byte) error {
var v struct {
ObjectMyObject
__synthetic_MyString *string `json:"myString"`
}
if err := json.Unmarshal(b, &v); err != nil {
return err
MyString *string `json:"myString"`
}
if v.__synthetic_MyString == nil {
if err := json.Unmarshal(b, &v); err != nil { return err }
if v.MyString == nil {
return fmt.Errorf("field myString: must be set")
}
*j = v.ObjectMyObject
type plain ObjectMyObject
var p plain
if err := json.Unmarshal(b, &p); err != nil { return err }
*j = ObjectMyObject(p)
return nil
}

Expand All @@ -65,12 +65,12 @@ type Object struct {
// UnmarshalJSON implements json.Unmarshaler.
func (j *Object) UnmarshalJSON(b []byte) error {
var v struct {
Object
}
if err := json.Unmarshal(b, &v); err != nil {
return err
}
*j = v.Object
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
}

Expand All @@ -84,12 +84,12 @@ type ObjectEmpty struct {
// UnmarshalJSON implements json.Unmarshaler.
func (j *ObjectEmpty) UnmarshalJSON(b []byte) error {
var v struct {
ObjectEmpty
}
if err := json.Unmarshal(b, &v); err != nil {
return err
}
*j = v.ObjectEmpty
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
}

Expand All @@ -102,12 +102,12 @@ type ObjectNestedMyObjectMyObject struct {
// UnmarshalJSON implements json.Unmarshaler.
func (j *ObjectNestedMyObjectMyObject) UnmarshalJSON(b []byte) error {
var v struct {
ObjectNestedMyObjectMyObject
}
if err := json.Unmarshal(b, &v); err != nil {
return err
}
*j = v.ObjectNestedMyObjectMyObject
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
}

Expand All @@ -120,12 +120,12 @@ type ObjectNestedMyObject struct {
// UnmarshalJSON implements json.Unmarshaler.
func (j *ObjectNestedMyObject) UnmarshalJSON(b []byte) error {
var v struct {
ObjectNestedMyObject
}
if err := json.Unmarshal(b, &v); err != nil {
return err
}
*j = v.ObjectNestedMyObject
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
}

Expand All @@ -138,12 +138,12 @@ type ObjectNested struct {
// UnmarshalJSON implements json.Unmarshaler.
func (j *ObjectNested) UnmarshalJSON(b []byte) error {
var v struct {
ObjectNested
}
if err := json.Unmarshal(b, &v); err != nil {
return err
}
*j = v.ObjectNested
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
}

Loading

0 comments on commit 29ff417

Please sign in to comment.