-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaltjson.go
More file actions
126 lines (109 loc) · 2.65 KB
/
Copy pathaltjson.go
File metadata and controls
126 lines (109 loc) · 2.65 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
package main
import (
"encoding/json"
"fmt"
)
// Alternative implementations of `MarshalJSON` and `UnmarshalJSON` for `Action`
func (a *Action) MarshalJSONInternallyTagged2() ([]byte, error) {
var data []byte
var err error
// note: `go-check-sumtype` linter will catch if we miss a case here
switch v := a.value.(type) {
case *CreateObject:
tagged := struct {
Type ActionType `json:"type"`
CreateObject
}{
Type: ActionType_CreateObject,
CreateObject: *v,
}
data, err = json.Marshal(&tagged)
case *UpdateObject:
tagged := struct {
Type ActionType `json:"type"`
UpdateObject
}{
Type: ActionType_UpdateObject,
UpdateObject: *v,
}
data, err = json.Marshal(&tagged)
case *DeleteObject:
tagged := struct {
Type ActionType `json:"type"`
DeleteObject
}{
Type: ActionType_DeleteObject,
DeleteObject: *v,
}
data, err = json.Marshal(&tagged)
case *DeleteAllObjects:
tagged := struct {
Type ActionType `json:"type"`
DeleteAllObjects
}{
Type: ActionType_DeleteAllObjects,
DeleteAllObjects: *v,
}
data, err = json.Marshal(&tagged)
}
return data, err
}
func (a *Action) MarshalJSONAdjacentlyTagged() ([]byte, error) {
var tagged struct {
Type ActionType `json:"type"`
Value json.RawMessage `json:"value,omitempty"`
}
// note: `go-check-sumtype` linter will catch if we miss a case here
switch a.value.(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
}
value, err := json.Marshal(a.value)
if err != nil {
return nil, err
}
// don't output empty structs
if string(value) != "{}" {
tagged.Value = value
}
return json.Marshal(&tagged)
}
func (a *Action) UnmarshalJSONAdjacentlyTagged(data []byte) error {
var tagged struct {
Type ActionType `json:"type"`
Value json.RawMessage `json:"value,omitempty"`
}
if err := json.Unmarshal(data, &tagged); err != nil {
return err
}
var v IsAction
// note: `exhaustive` linter will catch if we miss a case here
switch tagged.Type {
case ActionType_CreateObject:
v = new(CreateObject)
case ActionType_UpdateObject:
v = new(UpdateObject)
case ActionType_DeleteObject:
v = new(DeleteObject)
case ActionType_DeleteAllObjects:
v = new(DeleteAllObjects)
}
if v == nil {
return fmt.Errorf("unknown action type: %s", tagged.Type)
}
if tagged.Value == nil {
a.value = v
return nil
}
if err := json.Unmarshal(tagged.Value, v); err != nil {
return err
}
a.value = v
return nil
}