forked from argoproj/argo-workflows
-
Notifications
You must be signed in to change notification settings - Fork 1
/
item.go
176 lines (156 loc) · 4.27 KB
/
item.go
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
package v1alpha1
import (
"encoding/json"
"fmt"
"strconv"
)
// Type represents the stored type of Item.
type Type int
const (
Number Type = iota
String
Bool
Map
List
)
// Item expands a single workflow step into multiple parallel steps
// The value of Item can be a map, string, bool, or number
//
// +protobuf=true
// +protobuf.options.(gogoproto.goproto_stringer)=false
// +k8s:openapi-gen=true
type Item struct {
Type Type `protobuf:"bytes,1,opt,name=type,casttype=Type"`
NumVal json.Number `protobuf:"bytes,2,opt,name=numVal"`
BoolVal bool `protobuf:"bytes,3,opt,name=boolVal"`
StrVal string `protobuf:"bytes,4,opt,name=strVal"`
MapVal map[string]ItemValue `protobuf:"bytes,5,opt,name=mapVal"`
ListVal []ItemValue `protobuf:"bytes,6,opt,name=listVal"`
}
// UnmarshalJSON implements the json.Unmarshaller interface.
func (i *Item) UnmarshalJSON(value []byte) error {
strValue := string(value)
if _, err := strconv.Atoi(strValue); err == nil {
i.Type = Number
return json.Unmarshal(value, &i.NumVal)
}
if _, err := strconv.ParseFloat(strValue, 64); err == nil {
i.Type = Number
return json.Unmarshal(value, &i.NumVal)
}
if _, err := strconv.ParseBool(strValue); err == nil {
i.Type = Bool
return json.Unmarshal(value, &i.BoolVal)
}
if value[0] == '[' {
i.Type = List
err := json.Unmarshal(value, &i.ListVal)
fmt.Println(err)
return err
}
if value[0] == '{' {
i.Type = Map
return json.Unmarshal(value, &i.MapVal)
}
i.Type = String
return json.Unmarshal(value, &i.StrVal)
}
func (i *Item) String() string {
jsonBytes, err := json.Marshal(i)
if err != nil {
panic(err)
}
if i.Type == String {
// chop off the double quotes
return string(jsonBytes[1 : len(jsonBytes)-1])
}
return string(jsonBytes)
}
func (i Item) Format(s fmt.State, verb rune) {
fmt.Fprintf(s, i.String()) //nolint
}
// MarshalJSON implements the json.Marshaller interface.
func (i Item) MarshalJSON() ([]byte, error) {
switch i.Type {
case String:
return json.Marshal(i.StrVal)
case Bool:
return json.Marshal(i.BoolVal)
case Number:
return json.Marshal(i.NumVal)
case Map:
return json.Marshal(i.MapVal)
case List:
return json.Marshal(i.ListVal)
default:
return []byte{}, fmt.Errorf("impossible Item.Type")
}
}
// +protobuf=true
// +protobuf.options.(gogoproto.goproto_stringer)=false
// +k8s:openapi-gen=true
type ItemValue struct {
Type Type `protobuf:"varint,1,opt,name=type,casttype=Type"`
NumVal json.Number `protobuf:"bytes,2,opt,name=numVal"`
BoolVal bool `protobuf:"bytes,3,opt,name=boolVal"`
StrVal string `protobuf:"bytes,4,opt,name=strVal"`
MapVal map[string]string `protobuf:"bytes,5,opt,name=mapVal"`
ListVal []json.RawMessage `protobuf:"bytes,6,opt,name=listVal"`
}
// UnmarshalJSON implements the json.Unmarshaller interface.
func (iv *ItemValue) UnmarshalJSON(value []byte) error {
strValue := string(value)
if _, err := strconv.Atoi(strValue); err == nil {
iv.Type = Number
return json.Unmarshal(value, &iv.NumVal)
}
if _, err := strconv.ParseFloat(strValue, 64); err == nil {
iv.Type = Number
return json.Unmarshal(value, &iv.NumVal)
}
if _, err := strconv.ParseBool(strValue); err == nil {
iv.Type = Bool
return json.Unmarshal(value, &iv.BoolVal)
}
if value[0] == '[' {
iv.Type = List
err := json.Unmarshal(value, &iv.ListVal)
fmt.Println(err)
return err
}
if value[0] == '{' {
iv.Type = Map
return json.Unmarshal(value, &iv.MapVal)
}
iv.Type = String
return json.Unmarshal(value, &iv.StrVal)
}
func (iv *ItemValue) String() string {
jsonBytes, err := json.Marshal(iv)
if err != nil {
panic(err)
}
if iv.Type == String {
// chop off the double quotes
return string(jsonBytes[1 : len(jsonBytes)-1])
}
return string(jsonBytes)
}
func (iv ItemValue) Format(s fmt.State, verb rune) {
fmt.Fprintf(s, iv.String()) //nolint
}
// MarshalJSON implements the json.Marshaller interface.
func (iv ItemValue) MarshalJSON() ([]byte, error) {
switch iv.Type {
case String:
return json.Marshal(iv.StrVal)
case Bool:
return json.Marshal(iv.BoolVal)
case Number:
return json.Marshal(iv.NumVal)
case Map:
return json.Marshal(iv.MapVal)
default:
return []byte{}, fmt.Errorf("impossible ItemValue.Type")
}
}