-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalt2.go
More file actions
257 lines (214 loc) · 4.82 KB
/
Copy pathalt2.go
File metadata and controls
257 lines (214 loc) · 4.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package alt2
import (
"encoding/json"
"fmt"
)
type ObjectType string
const (
ObjectType_User ObjectType = "user"
ObjectType_Group ObjectType = "group"
)
type Object struct {
Type ObjectType `json:"type"`
ID string `json:"id"`
Name string `json:"name"`
}
type ActionType string
const (
ActionType_CreateObject ActionType = "create_object"
ActionType_UpdateObject ActionType = "update_object"
ActionType_DeleteObject ActionType = "delete_object"
ActionType_DeleteAllObjects ActionType = "delete_all_objects"
)
// "Bag of all the branches" approach
type Action struct {
createObject *CreateObject
updateObject *UpdateObject
deleteObject *DeleteObject
deleteAllObjects *DeleteAllObjects
}
type CreateObject struct {
Object Object `json:"object"`
}
type UpdateObject struct {
Object Object `json:"object"`
}
type DeleteObject struct {
ID string `json:"id"`
}
type DeleteAllObjects struct{}
func NewActionFromCreateObject(v *CreateObject) Action {
return Action{
createObject: v,
}
}
func NewActionFromUpdateObject(v *UpdateObject) Action {
return Action{
updateObject: v,
}
}
func NewActionFromDeleteObject(v *DeleteObject) Action {
return Action{
deleteObject: v,
}
}
func NewActionFromDeleteAllObjects(v *DeleteAllObjects) Action {
return Action{
deleteAllObjects: v,
}
}
func (a *Action) Value() any {
if a.createObject != nil {
return a.createObject
}
if a.updateObject != nil {
return a.updateObject
}
if a.deleteObject != nil {
return a.deleteObject
}
if a.deleteAllObjects != nil {
return a.deleteAllObjects
}
return nil
}
func (a *Action) MarshalJSON2() ([]byte, error) {
if a.createObject != nil {
return json.Marshal(&a.createObject)
}
if a.updateObject != nil {
return json.Marshal(&a.updateObject)
}
if a.deleteObject != nil {
return json.Marshal(&a.deleteObject)
}
if a.deleteAllObjects != nil {
return json.Marshal(&a.deleteAllObjects)
}
return nil, nil
}
func (a *Action) MarshalJSON() ([]byte, error) {
v := a.Value()
data, err := json.Marshal(&v)
if err != nil {
return nil, err
}
var tagged map[string]any
if err := json.Unmarshal(data, &tagged); err != nil {
return nil, err
}
switch v.(type) {
case *CreateObject:
tagged["type"] = ActionType_CreateObject
case *UpdateObject:
tagged["type"] = ActionType_UpdateObject
case *DeleteObject:
tagged["type"] = ActionType_DeleteObject
case *DeleteAllObjects:
tagged["type"] = ActionType_DeleteAllObjects
}
return json.Marshal(&tagged)
}
func (a *Action) UnmarshalJSON(data []byte) error {
var tagged struct {
Type ActionType `json:"type"`
}
if err := json.Unmarshal(data, &tagged); err != nil {
return err
}
var err error
switch tagged.Type {
case ActionType_CreateObject:
err = json.Unmarshal(data, &a.createObject)
case ActionType_UpdateObject:
err = json.Unmarshal(data, &a.updateObject)
case ActionType_DeleteObject:
err = json.Unmarshal(data, &a.deleteObject)
case ActionType_DeleteAllObjects:
err = json.Unmarshal(data, &a.deleteAllObjects)
}
if err != nil {
a.createObject = nil
a.updateObject = nil
a.deleteObject = nil
a.deleteAllObjects = nil
return err
}
return nil
}
func TransformAction(action *Action) string {
var result string
switch v := action.Value().(type) {
case *CreateObject:
result = fmt.Sprintf(
"create_object %s %s %s", v.Object.Type, v.Object.ID, v.Object.Name,
)
case *UpdateObject:
result = fmt.Sprintf(
"update_object %s %s %s", v.Object.Type, v.Object.ID, v.Object.Name,
)
case *DeleteObject:
result = fmt.Sprintf("delete_object %s", v.ID)
case *DeleteAllObjects:
result = "delete_all_objects"
}
return result
}
func exampleActions() []Action {
return []Action{
NewActionFromCreateObject(&CreateObject{
Object: Object{
Type: ObjectType_User,
ID: "1",
Name: "user1",
},
}),
NewActionFromUpdateObject(&UpdateObject{
Object: Object{
Type: ObjectType_User,
ID: "1",
Name: "user1 updated",
},
}),
NewActionFromDeleteObject(&DeleteObject{
ID: "1",
}),
NewActionFromDeleteAllObjects(&DeleteAllObjects{}),
}
}
func Run() {
actions := exampleActions()
// JSON encode
data, err := json.MarshalIndent(actions, "", " ")
if err != nil {
panic(err)
}
fmt.Println("## JSON")
fmt.Println()
fmt.Println("```json")
fmt.Println(string(data))
fmt.Println("```")
fmt.Println()
// JSON decode
actions2 := []Action{}
if err := json.Unmarshal(data, &actions2); err != nil {
panic(err)
}
fmt.Println("## Debug")
fmt.Println()
fmt.Println("```go")
for _, action := range actions2 {
fmt.Printf("%#v\n", action.Value())
}
fmt.Println("```")
fmt.Println()
// Transform
fmt.Println("## Transformed")
fmt.Println()
fmt.Println("```")
for _, action := range actions {
fmt.Println(TransformAction(&action))
}
fmt.Println("```")
fmt.Println()
}