Skip to content

Commit edaa93d

Browse files
authored
Merge pull request moby#5524 from jsternberg/json-casing-regression
protobuf: fix casing of json attributes with the switch from gogo
2 parents 5d40a3a + 56ffa9f commit edaa93d

File tree

2 files changed

+190
-81
lines changed

2 files changed

+190
-81
lines changed

solver/pb/json.go

Lines changed: 108 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -2,58 +2,100 @@ package pb
22

33
import "encoding/json"
44

5-
func (m *Op) UnmarshalJSON(data []byte) error {
6-
var v struct {
7-
Inputs []*Input `json:"inputs,omitempty"`
8-
Op struct {
9-
*Op_Exec
10-
*Op_Source
11-
*Op_File
12-
*Op_Build
13-
*Op_Merge
14-
*Op_Diff
15-
}
16-
Platform *Platform `json:"platform,omitempty"`
17-
Constraints *WorkerConstraints `json:"constraints,omitempty"`
5+
type jsonOp struct {
6+
Inputs []*Input `json:"inputs,omitempty"`
7+
Op struct {
8+
Exec *ExecOp `json:"exec,omitempty"`
9+
Source *SourceOp `json:"source,omitempty"`
10+
File *FileOp `json:"file,omitempty"`
11+
Build *BuildOp `json:"build,omitempty"`
12+
Merge *MergeOp `json:"merge,omitempty"`
13+
Diff *DiffOp `json:"diff,omitempty"`
1814
}
15+
Platform *Platform `json:"platform,omitempty"`
16+
Constraints *WorkerConstraints `json:"constraints,omitempty"`
17+
}
18+
19+
func (m *Op) MarshalJSON() ([]byte, error) {
20+
var v jsonOp
21+
v.Inputs = m.Inputs
22+
switch op := m.Op.(type) {
23+
case *Op_Exec:
24+
v.Op.Exec = op.Exec
25+
case *Op_Source:
26+
v.Op.Source = op.Source
27+
case *Op_File:
28+
v.Op.File = op.File
29+
case *Op_Build:
30+
v.Op.Build = op.Build
31+
case *Op_Merge:
32+
v.Op.Merge = op.Merge
33+
case *Op_Diff:
34+
v.Op.Diff = op.Diff
35+
}
36+
v.Platform = m.Platform
37+
v.Constraints = m.Constraints
38+
return json.Marshal(v)
39+
}
1940

41+
func (m *Op) UnmarshalJSON(data []byte) error {
42+
var v jsonOp
2043
if err := json.Unmarshal(data, &v); err != nil {
2144
return err
2245
}
2346

2447
m.Inputs = v.Inputs
2548
switch {
26-
case v.Op.Op_Exec != nil:
27-
m.Op = v.Op.Op_Exec
28-
case v.Op.Op_Source != nil:
29-
m.Op = v.Op.Op_Source
30-
case v.Op.Op_File != nil:
31-
m.Op = v.Op.Op_File
32-
case v.Op.Op_Build != nil:
33-
m.Op = v.Op.Op_Build
34-
case v.Op.Op_Merge != nil:
35-
m.Op = v.Op.Op_Merge
36-
case v.Op.Op_Diff != nil:
37-
m.Op = v.Op.Op_Diff
49+
case v.Op.Exec != nil:
50+
m.Op = &Op_Exec{v.Op.Exec}
51+
case v.Op.Source != nil:
52+
m.Op = &Op_Source{v.Op.Source}
53+
case v.Op.File != nil:
54+
m.Op = &Op_File{v.Op.File}
55+
case v.Op.Build != nil:
56+
m.Op = &Op_Build{v.Op.Build}
57+
case v.Op.Merge != nil:
58+
m.Op = &Op_Merge{v.Op.Merge}
59+
case v.Op.Diff != nil:
60+
m.Op = &Op_Diff{v.Op.Diff}
3861
}
3962
m.Platform = v.Platform
4063
m.Constraints = v.Constraints
4164
return nil
4265
}
4366

44-
func (m *FileAction) UnmarshalJSON(data []byte) error {
45-
var v struct {
46-
Input InputIndex `json:"input"`
47-
SecondaryInput InputIndex `json:"secondaryInput"`
48-
Output OutputIndex `json:"output"`
49-
Action struct {
50-
*FileAction_Copy
51-
*FileAction_Mkfile
52-
*FileAction_Mkdir
53-
*FileAction_Rm
54-
}
67+
type jsonFileAction struct {
68+
Input InputIndex `json:"input"`
69+
SecondaryInput InputIndex `json:"secondaryInput"`
70+
Output OutputIndex `json:"output"`
71+
Action struct {
72+
Copy *FileActionCopy `json:"copy,omitempty"`
73+
Mkfile *FileActionMkFile `json:"mkfile,omitempty"`
74+
Mkdir *FileActionMkDir `json:"mkdir,omitempty"`
75+
Rm *FileActionRm `json:"rm,omitempty"`
76+
}
77+
}
78+
79+
func (m *FileAction) MarshalJSON() ([]byte, error) {
80+
var v jsonFileAction
81+
v.Input = InputIndex(m.Input)
82+
v.SecondaryInput = InputIndex(m.SecondaryInput)
83+
v.Output = OutputIndex(m.Output)
84+
switch action := m.Action.(type) {
85+
case *FileAction_Copy:
86+
v.Action.Copy = action.Copy
87+
case *FileAction_Mkfile:
88+
v.Action.Mkfile = action.Mkfile
89+
case *FileAction_Mkdir:
90+
v.Action.Mkdir = action.Mkdir
91+
case *FileAction_Rm:
92+
v.Action.Rm = action.Rm
5593
}
94+
return json.Marshal(v)
95+
}
5696

97+
func (m *FileAction) UnmarshalJSON(data []byte) error {
98+
var v jsonFileAction
5799
if err := json.Unmarshal(data, &v); err != nil {
58100
return err
59101
}
@@ -62,35 +104,47 @@ func (m *FileAction) UnmarshalJSON(data []byte) error {
62104
m.SecondaryInput = int64(v.SecondaryInput)
63105
m.Output = int64(v.Output)
64106
switch {
65-
case v.Action.FileAction_Copy != nil:
66-
m.Action = v.Action.FileAction_Copy
67-
case v.Action.FileAction_Mkfile != nil:
68-
m.Action = v.Action.FileAction_Mkfile
69-
case v.Action.FileAction_Mkdir != nil:
70-
m.Action = v.Action.FileAction_Mkdir
71-
case v.Action.FileAction_Rm != nil:
72-
m.Action = v.Action.FileAction_Rm
107+
case v.Action.Copy != nil:
108+
m.Action = &FileAction_Copy{v.Action.Copy}
109+
case v.Action.Mkfile != nil:
110+
m.Action = &FileAction_Mkfile{v.Action.Mkfile}
111+
case v.Action.Mkdir != nil:
112+
m.Action = &FileAction_Mkdir{v.Action.Mkdir}
113+
case v.Action.Rm != nil:
114+
m.Action = &FileAction_Rm{v.Action.Rm}
73115
}
74116
return nil
75117
}
76118

77-
func (m *UserOpt) UnmarshalJSON(data []byte) error {
78-
var v struct {
79-
User struct {
80-
*UserOpt_ByName
81-
*UserOpt_ByID
82-
}
119+
type jsonUserOpt struct {
120+
User struct {
121+
ByName *NamedUserOpt `json:"byName,omitempty"`
122+
ByID uint32 `json:"byId,omitempty"`
83123
}
124+
}
84125

126+
func (m *UserOpt) MarshalJSON() ([]byte, error) {
127+
var v jsonUserOpt
128+
switch userOpt := m.User.(type) {
129+
case *UserOpt_ByName:
130+
v.User.ByName = userOpt.ByName
131+
case *UserOpt_ByID:
132+
v.User.ByID = userOpt.ByID
133+
}
134+
return json.Marshal(v)
135+
}
136+
137+
func (m *UserOpt) UnmarshalJSON(data []byte) error {
138+
var v jsonUserOpt
85139
if err := json.Unmarshal(data, &v); err != nil {
86140
return err
87141
}
88142

89143
switch {
90-
case v.User.UserOpt_ByName != nil:
91-
m.User = v.User.UserOpt_ByName
92-
case v.User.UserOpt_ByID != nil:
93-
m.User = v.User.UserOpt_ByID
144+
case v.User.ByName != nil:
145+
m.User = &UserOpt_ByName{v.User.ByName}
146+
default:
147+
m.User = &UserOpt_ByID{v.User.ByID}
94148
}
95149
return nil
96150
}

0 commit comments

Comments
 (0)