/
enum-text-marshal-strict.go
252 lines (232 loc) · 6.44 KB
/
enum-text-marshal-strict.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
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
// Code generated by thriftrw v1.31.0. DO NOT EDIT.
// @generated
package enum_text_marshal_strict
import (
bytes "bytes"
json "encoding/json"
fmt "fmt"
stream "go.uber.org/thriftrw/protocol/stream"
thriftreflect "go.uber.org/thriftrw/thriftreflect"
wire "go.uber.org/thriftrw/wire"
zapcore "go.uber.org/zap/zapcore"
math "math"
strconv "strconv"
)
type EnumMarshalStrict int32
const (
EnumMarshalStrictFoo EnumMarshalStrict = 0
EnumMarshalStrictBar EnumMarshalStrict = 1
EnumMarshalStrictBaz EnumMarshalStrict = 2
EnumMarshalStrictBat EnumMarshalStrict = 3
)
// EnumMarshalStrict_Values returns all recognized values of EnumMarshalStrict.
func EnumMarshalStrict_Values() []EnumMarshalStrict {
return []EnumMarshalStrict{
EnumMarshalStrictFoo,
EnumMarshalStrictBar,
EnumMarshalStrictBaz,
EnumMarshalStrictBat,
}
}
// UnmarshalText tries to decode EnumMarshalStrict from a byte slice
// containing its name.
//
// var v EnumMarshalStrict
// err := v.UnmarshalText([]byte("Foo"))
func (v *EnumMarshalStrict) UnmarshalText(value []byte) error {
switch s := string(value); s {
case "Foo":
*v = EnumMarshalStrictFoo
return nil
case "Bar":
*v = EnumMarshalStrictBar
return nil
case "Baz":
*v = EnumMarshalStrictBaz
return nil
case "Bat":
*v = EnumMarshalStrictBat
return nil
default:
val, err := strconv.ParseInt(s, 10, 32)
if err != nil {
return fmt.Errorf("unknown enum value %q for %q: %v", s, "EnumMarshalStrict", err)
}
*v = EnumMarshalStrict(val)
return nil
}
}
// MarshalText encodes EnumMarshalStrict to text.
//
// If the enum value is recognized, its name is returned.
// Otherwise, an error is returned.
//
// This implements the TextMarshaler interface.
func (v EnumMarshalStrict) MarshalText() ([]byte, error) {
switch int32(v) {
case 0:
return []byte("Foo"), nil
case 1:
return []byte("Bar"), nil
case 2:
return []byte("Baz"), nil
case 3:
return []byte("Bat"), nil
}
return nil, fmt.Errorf("unknown enum value %q for %q", v, "EnumMarshalStrict")
}
// MarshalLogObject implements zapcore.ObjectMarshaler, enabling
// fast logging of EnumMarshalStrict.
// Enums are logged as objects, where the value is logged with key "value", and
// if this value's name is known, the name is logged with key "name".
func (v EnumMarshalStrict) MarshalLogObject(enc zapcore.ObjectEncoder) error {
enc.AddInt32("value", int32(v))
switch int32(v) {
case 0:
enc.AddString("name", "Foo")
case 1:
enc.AddString("name", "Bar")
case 2:
enc.AddString("name", "Baz")
case 3:
enc.AddString("name", "Bat")
}
return nil
}
// Ptr returns a pointer to this enum value.
func (v EnumMarshalStrict) Ptr() *EnumMarshalStrict {
return &v
}
// Encode encodes EnumMarshalStrict directly to bytes.
//
// sWriter := BinaryStreamer.Writer(writer)
//
// var v EnumMarshalStrict
// return v.Encode(sWriter)
func (v EnumMarshalStrict) Encode(sw stream.Writer) error {
return sw.WriteInt32(int32(v))
}
// ToWire translates EnumMarshalStrict into a Thrift-level intermediate
// representation. This intermediate representation may be serialized
// into bytes using a ThriftRW protocol implementation.
//
// Enums are represented as 32-bit integers over the wire.
func (v EnumMarshalStrict) ToWire() (wire.Value, error) {
return wire.NewValueI32(int32(v)), nil
}
// FromWire deserializes EnumMarshalStrict from its Thrift-level
// representation.
//
// x, err := binaryProtocol.Decode(reader, wire.TI32)
// if err != nil {
// return EnumMarshalStrict(0), err
// }
//
// var v EnumMarshalStrict
// if err := v.FromWire(x); err != nil {
// return EnumMarshalStrict(0), err
// }
// return v, nil
func (v *EnumMarshalStrict) FromWire(w wire.Value) error {
*v = (EnumMarshalStrict)(w.GetI32())
return nil
}
// Decode reads off the encoded EnumMarshalStrict directly off of the wire.
//
// sReader := BinaryStreamer.Reader(reader)
//
// var v EnumMarshalStrict
// if err := v.Decode(sReader); err != nil {
// return EnumMarshalStrict(0), err
// }
// return v, nil
func (v *EnumMarshalStrict) Decode(sr stream.Reader) error {
i, err := sr.ReadInt32()
if err != nil {
return err
}
*v = (EnumMarshalStrict)(i)
return nil
}
// String returns a readable string representation of EnumMarshalStrict.
func (v EnumMarshalStrict) String() string {
w := int32(v)
switch w {
case 0:
return "Foo"
case 1:
return "Bar"
case 2:
return "Baz"
case 3:
return "Bat"
}
return fmt.Sprintf("EnumMarshalStrict(%d)", w)
}
// Equals returns true if this EnumMarshalStrict value matches the provided
// value.
func (v EnumMarshalStrict) Equals(rhs EnumMarshalStrict) bool {
return v == rhs
}
// MarshalJSON serializes EnumMarshalStrict into JSON.
//
// If the enum value is recognized, its name is returned.
// Otherwise, an error is returned.
//
// This implements json.Marshaler.
func (v EnumMarshalStrict) MarshalJSON() ([]byte, error) {
switch int32(v) {
case 0:
return ([]byte)("\"Foo\""), nil
case 1:
return ([]byte)("\"Bar\""), nil
case 2:
return ([]byte)("\"Baz\""), nil
case 3:
return ([]byte)("\"Bat\""), nil
}
return nil, fmt.Errorf("unknown enum value %q for %q", v, "EnumMarshalStrict")
}
// UnmarshalJSON attempts to decode EnumMarshalStrict from its JSON
// representation.
//
// This implementation supports both, numeric and string inputs. If a
// string is provided, it must be a known enum name.
//
// This implements json.Unmarshaler.
func (v *EnumMarshalStrict) UnmarshalJSON(text []byte) error {
d := json.NewDecoder(bytes.NewReader(text))
d.UseNumber()
t, err := d.Token()
if err != nil {
return err
}
switch w := t.(type) {
case json.Number:
x, err := w.Int64()
if err != nil {
return err
}
if x > math.MaxInt32 {
return fmt.Errorf("enum overflow from JSON %q for %q", text, "EnumMarshalStrict")
}
if x < math.MinInt32 {
return fmt.Errorf("enum underflow from JSON %q for %q", text, "EnumMarshalStrict")
}
*v = (EnumMarshalStrict)(x)
return nil
case string:
return v.UnmarshalText([]byte(w))
default:
return fmt.Errorf("invalid JSON value %q (%T) to unmarshal into %q", t, t, "EnumMarshalStrict")
}
}
// ThriftModule represents the IDL file used to generate this package.
var ThriftModule = &thriftreflect.ThriftModule{
Name: "enum-text-marshal-strict",
Package: "go.uber.org/thriftrw/gen/internal/tests/enum-text-marshal-strict",
FilePath: "enum-text-marshal-strict.thrift",
SHA1: "7d9566a0ff9eccda2ed5be518f321cfcba028dcb",
Raw: rawIDL,
}
const rawIDL = "enum EnumMarshalStrict {\n Foo, Bar, Baz, Bat\n}"