Skip to content

Commit 29ff417

Browse files
committed
Fix generated unmarshaling code for synthetic fields.
1 parent d8f9ec8 commit 29ff417

18 files changed

+450
-445
lines changed

pkg/generator/generate.go

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -331,30 +331,36 @@ func (g *schemaGenerator) generateDeclaredType(
331331
out.Indent(1)
332332
out.Println("var v struct {")
333333
out.Indent(1)
334-
out.Println("%s", decl.Name)
335-
336334
fields := append([]codegen.StructField{}, structType.Fields...)
337335
for _, f := range structType.Fields {
338336
if f.Synthetic {
339337
f.Generate(out)
340338
out.Newline()
341339
}
342340
}
343-
344-
out.Indent(-1)
345-
out.Println("}")
346-
out.Println("if err := json.Unmarshal(b, &v); err != nil {")
347-
out.Indent(1)
348-
out.Println("return err")
349341
out.Indent(-1)
350342
out.Println("}")
343+
out.Println("if err := json.Unmarshal(b, &v); err != nil { return err }")
344+
for _, f := range fields {
345+
if f.Synthetic {
346+
for _, r := range f.Rules {
347+
r.GenerateValidation(out, fmt.Sprintf("v.%s", f.Name),
348+
fmt.Sprintf("field %s", f.JSONName))
349+
}
350+
}
351+
}
352+
out.Println("type plain %s", decl.Name)
353+
out.Println("var p plain")
354+
out.Println("if err := json.Unmarshal(b, &p); err != nil { return err }")
351355
for _, f := range fields {
352-
for _, r := range f.Rules {
353-
r.GenerateValidation(out, fmt.Sprintf("v.%s", f.Name),
354-
fmt.Sprintf("field %s", f.JSONName))
356+
if !f.Synthetic {
357+
for _, r := range f.Rules {
358+
r.GenerateValidation(out, fmt.Sprintf("p.%s", f.Name),
359+
fmt.Sprintf("field %s", f.JSONName))
360+
}
355361
}
356362
}
357-
out.Println("*j = v.%s", decl.Name)
363+
out.Println("*j = %s(p)", decl.Name)
358364
out.Println("return nil")
359365
out.Indent(-1)
360366
out.Println("}")
@@ -489,7 +495,6 @@ func (g *schemaGenerator) generateStructType(
489495
syntheticField := structField
490496
syntheticField.Comment = ""
491497
syntheticField.Synthetic = true
492-
syntheticField.Name = "__synthetic_" + syntheticField.Name
493498
syntheticField.Type = codegen.PointerType{Type: syntheticField.Type}
494499
syntheticField.AddRule(codegen.NilStructFieldRequired{})
495500
structType.AddField(syntheticField)

tests/data/core/4.2.1_array.go.output

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ type 421Array struct {
2424
// UnmarshalJSON implements json.Unmarshaler.
2525
func (j *421Array) UnmarshalJSON(b []byte) error {
2626
var v struct {
27-
421Array
2827
}
29-
if err := json.Unmarshal(b, &v); err != nil {
30-
return err
31-
}
32-
*j = v.421Array
28+
if err := json.Unmarshal(b, &v); err != nil { return err }
29+
type plain 421Array
30+
var p plain
31+
if err := json.Unmarshal(b, &p); err != nil { return err }
32+
*j = 421Array(p)
3333
return nil
3434
}
3535

tests/data/core/object.go.output

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ type 421Array struct {
2525
// UnmarshalJSON implements json.Unmarshaler.
2626
func (j *421Array) UnmarshalJSON(b []byte) error {
2727
var v struct {
28-
421Array
2928
}
30-
if err := json.Unmarshal(b, &v); err != nil {
31-
return err
32-
}
33-
*j = v.421Array
29+
if err := json.Unmarshal(b, &v); err != nil { return err }
30+
type plain 421Array
31+
var p plain
32+
if err := json.Unmarshal(b, &p); err != nil { return err }
33+
*j = 421Array(p)
3434
return nil
3535
}
3636

@@ -43,16 +43,16 @@ type ObjectMyObject struct {
4343
// UnmarshalJSON implements json.Unmarshaler.
4444
func (j *ObjectMyObject) UnmarshalJSON(b []byte) error {
4545
var v struct {
46-
ObjectMyObject
47-
__synthetic_MyString *string `json:"myString"`
48-
}
49-
if err := json.Unmarshal(b, &v); err != nil {
50-
return err
46+
MyString *string `json:"myString"`
5147
}
52-
if v.__synthetic_MyString == nil {
48+
if err := json.Unmarshal(b, &v); err != nil { return err }
49+
if v.MyString == nil {
5350
return fmt.Errorf("field myString: must be set")
5451
}
55-
*j = v.ObjectMyObject
52+
type plain ObjectMyObject
53+
var p plain
54+
if err := json.Unmarshal(b, &p); err != nil { return err }
55+
*j = ObjectMyObject(p)
5656
return nil
5757
}
5858

@@ -65,12 +65,12 @@ type Object struct {
6565
// UnmarshalJSON implements json.Unmarshaler.
6666
func (j *Object) UnmarshalJSON(b []byte) error {
6767
var v struct {
68-
Object
69-
}
70-
if err := json.Unmarshal(b, &v); err != nil {
71-
return err
7268
}
73-
*j = v.Object
69+
if err := json.Unmarshal(b, &v); err != nil { return err }
70+
type plain Object
71+
var p plain
72+
if err := json.Unmarshal(b, &p); err != nil { return err }
73+
*j = Object(p)
7474
return nil
7575
}
7676

tests/data/core/objectEmpty.go.output

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ type 421Array struct {
2525
// UnmarshalJSON implements json.Unmarshaler.
2626
func (j *421Array) UnmarshalJSON(b []byte) error {
2727
var v struct {
28-
421Array
2928
}
30-
if err := json.Unmarshal(b, &v); err != nil {
31-
return err
32-
}
33-
*j = v.421Array
29+
if err := json.Unmarshal(b, &v); err != nil { return err }
30+
type plain 421Array
31+
var p plain
32+
if err := json.Unmarshal(b, &p); err != nil { return err }
33+
*j = 421Array(p)
3434
return nil
3535
}
3636

@@ -43,16 +43,16 @@ type ObjectMyObject struct {
4343
// UnmarshalJSON implements json.Unmarshaler.
4444
func (j *ObjectMyObject) UnmarshalJSON(b []byte) error {
4545
var v struct {
46-
ObjectMyObject
47-
__synthetic_MyString *string `json:"myString"`
48-
}
49-
if err := json.Unmarshal(b, &v); err != nil {
50-
return err
46+
MyString *string `json:"myString"`
5147
}
52-
if v.__synthetic_MyString == nil {
48+
if err := json.Unmarshal(b, &v); err != nil { return err }
49+
if v.MyString == nil {
5350
return fmt.Errorf("field myString: must be set")
5451
}
55-
*j = v.ObjectMyObject
52+
type plain ObjectMyObject
53+
var p plain
54+
if err := json.Unmarshal(b, &p); err != nil { return err }
55+
*j = ObjectMyObject(p)
5656
return nil
5757
}
5858

@@ -65,12 +65,12 @@ type Object struct {
6565
// UnmarshalJSON implements json.Unmarshaler.
6666
func (j *Object) UnmarshalJSON(b []byte) error {
6767
var v struct {
68-
Object
6968
}
70-
if err := json.Unmarshal(b, &v); err != nil {
71-
return err
72-
}
73-
*j = v.Object
69+
if err := json.Unmarshal(b, &v); err != nil { return err }
70+
type plain Object
71+
var p plain
72+
if err := json.Unmarshal(b, &p); err != nil { return err }
73+
*j = Object(p)
7474
return nil
7575
}
7676

@@ -84,12 +84,12 @@ type ObjectEmpty struct {
8484
// UnmarshalJSON implements json.Unmarshaler.
8585
func (j *ObjectEmpty) UnmarshalJSON(b []byte) error {
8686
var v struct {
87-
ObjectEmpty
88-
}
89-
if err := json.Unmarshal(b, &v); err != nil {
90-
return err
9187
}
92-
*j = v.ObjectEmpty
88+
if err := json.Unmarshal(b, &v); err != nil { return err }
89+
type plain ObjectEmpty
90+
var p plain
91+
if err := json.Unmarshal(b, &p); err != nil { return err }
92+
*j = ObjectEmpty(p)
9393
return nil
9494
}
9595

tests/data/core/objectNested.go.output

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ type 421Array struct {
2525
// UnmarshalJSON implements json.Unmarshaler.
2626
func (j *421Array) UnmarshalJSON(b []byte) error {
2727
var v struct {
28-
421Array
2928
}
30-
if err := json.Unmarshal(b, &v); err != nil {
31-
return err
32-
}
33-
*j = v.421Array
29+
if err := json.Unmarshal(b, &v); err != nil { return err }
30+
type plain 421Array
31+
var p plain
32+
if err := json.Unmarshal(b, &p); err != nil { return err }
33+
*j = 421Array(p)
3434
return nil
3535
}
3636

@@ -43,16 +43,16 @@ type ObjectMyObject struct {
4343
// UnmarshalJSON implements json.Unmarshaler.
4444
func (j *ObjectMyObject) UnmarshalJSON(b []byte) error {
4545
var v struct {
46-
ObjectMyObject
47-
__synthetic_MyString *string `json:"myString"`
48-
}
49-
if err := json.Unmarshal(b, &v); err != nil {
50-
return err
46+
MyString *string `json:"myString"`
5147
}
52-
if v.__synthetic_MyString == nil {
48+
if err := json.Unmarshal(b, &v); err != nil { return err }
49+
if v.MyString == nil {
5350
return fmt.Errorf("field myString: must be set")
5451
}
55-
*j = v.ObjectMyObject
52+
type plain ObjectMyObject
53+
var p plain
54+
if err := json.Unmarshal(b, &p); err != nil { return err }
55+
*j = ObjectMyObject(p)
5656
return nil
5757
}
5858

@@ -65,12 +65,12 @@ type Object struct {
6565
// UnmarshalJSON implements json.Unmarshaler.
6666
func (j *Object) UnmarshalJSON(b []byte) error {
6767
var v struct {
68-
Object
6968
}
70-
if err := json.Unmarshal(b, &v); err != nil {
71-
return err
72-
}
73-
*j = v.Object
69+
if err := json.Unmarshal(b, &v); err != nil { return err }
70+
type plain Object
71+
var p plain
72+
if err := json.Unmarshal(b, &p); err != nil { return err }
73+
*j = Object(p)
7474
return nil
7575
}
7676

@@ -84,12 +84,12 @@ type ObjectEmpty struct {
8484
// UnmarshalJSON implements json.Unmarshaler.
8585
func (j *ObjectEmpty) UnmarshalJSON(b []byte) error {
8686
var v struct {
87-
ObjectEmpty
88-
}
89-
if err := json.Unmarshal(b, &v); err != nil {
90-
return err
9187
}
92-
*j = v.ObjectEmpty
88+
if err := json.Unmarshal(b, &v); err != nil { return err }
89+
type plain ObjectEmpty
90+
var p plain
91+
if err := json.Unmarshal(b, &p); err != nil { return err }
92+
*j = ObjectEmpty(p)
9393
return nil
9494
}
9595

@@ -102,12 +102,12 @@ type ObjectNestedMyObjectMyObject struct {
102102
// UnmarshalJSON implements json.Unmarshaler.
103103
func (j *ObjectNestedMyObjectMyObject) UnmarshalJSON(b []byte) error {
104104
var v struct {
105-
ObjectNestedMyObjectMyObject
106105
}
107-
if err := json.Unmarshal(b, &v); err != nil {
108-
return err
109-
}
110-
*j = v.ObjectNestedMyObjectMyObject
106+
if err := json.Unmarshal(b, &v); err != nil { return err }
107+
type plain ObjectNestedMyObjectMyObject
108+
var p plain
109+
if err := json.Unmarshal(b, &p); err != nil { return err }
110+
*j = ObjectNestedMyObjectMyObject(p)
111111
return nil
112112
}
113113

@@ -120,12 +120,12 @@ type ObjectNestedMyObject struct {
120120
// UnmarshalJSON implements json.Unmarshaler.
121121
func (j *ObjectNestedMyObject) UnmarshalJSON(b []byte) error {
122122
var v struct {
123-
ObjectNestedMyObject
124-
}
125-
if err := json.Unmarshal(b, &v); err != nil {
126-
return err
127123
}
128-
*j = v.ObjectNestedMyObject
124+
if err := json.Unmarshal(b, &v); err != nil { return err }
125+
type plain ObjectNestedMyObject
126+
var p plain
127+
if err := json.Unmarshal(b, &p); err != nil { return err }
128+
*j = ObjectNestedMyObject(p)
129129
return nil
130130
}
131131

@@ -138,12 +138,12 @@ type ObjectNested struct {
138138
// UnmarshalJSON implements json.Unmarshaler.
139139
func (j *ObjectNested) UnmarshalJSON(b []byte) error {
140140
var v struct {
141-
ObjectNested
142-
}
143-
if err := json.Unmarshal(b, &v); err != nil {
144-
return err
145141
}
146-
*j = v.ObjectNested
142+
if err := json.Unmarshal(b, &v); err != nil { return err }
143+
type plain ObjectNested
144+
var p plain
145+
if err := json.Unmarshal(b, &p); err != nil { return err }
146+
*j = ObjectNested(p)
147147
return nil
148148
}
149149

0 commit comments

Comments
 (0)