-
Notifications
You must be signed in to change notification settings - Fork 108
/
string.go
202 lines (171 loc) · 3.77 KB
/
string.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
package field
import (
"encoding/json"
"fmt"
"reflect"
"strconv"
"strings"
"github.com/moov-io/iso8583/utils"
)
var (
_ Field = (*String)(nil)
_ json.Marshaler = (*String)(nil)
_ json.Unmarshaler = (*String)(nil)
)
type String struct {
value string
spec *Spec
}
func NewString(spec *Spec) *String {
return &String{
spec: spec,
}
}
func NewStringValue(val string) *String {
return &String{
value: val,
}
}
func (f *String) Spec() *Spec {
return f.spec
}
func (f *String) SetSpec(spec *Spec) {
f.spec = spec
}
func (f *String) SetBytes(b []byte) error {
f.value = string(b)
return nil
}
func (f *String) Bytes() ([]byte, error) {
if f == nil {
return nil, nil
}
return []byte(f.value), nil
}
func (f *String) String() (string, error) {
if f == nil {
return "", nil
}
return f.value, nil
}
func (f *String) Value() string {
if f == nil {
return ""
}
return f.value
}
func (f *String) SetValue(v string) {
f.value = v
}
func (f *String) Pack() ([]byte, error) {
data := []byte(f.value)
packer := f.spec.getPacker()
return packer.Pack(data, f.spec)
}
func (f *String) Unpack(data []byte) (int, error) {
unpacker := f.spec.getUnpacker()
raw, bytesRead, err := unpacker.Unpack(data, f.spec)
if err != nil {
return 0, err
}
if err := f.SetBytes(raw); err != nil {
return 0, fmt.Errorf("failed to set bytes: %w", err)
}
return bytesRead, nil
}
// Deprecated. Use Marshal instead
func (f *String) SetData(data interface{}) error {
return f.Marshal(data)
}
func (f *String) Unmarshal(v interface{}) error {
switch val := v.(type) {
case reflect.Value:
if !val.CanSet() {
return fmt.Errorf("cannot set reflect.Value of type %s", val.Kind())
}
switch val.Kind() { //nolint:exhaustive
case reflect.String:
val.SetString(f.value)
case reflect.Int, reflect.Int64:
i, err := strconv.Atoi(f.value)
if err != nil {
return fmt.Errorf("failed to convert string to int: %w", err)
}
val.SetInt(int64(i))
default:
return fmt.Errorf("unsupported reflect.Value type: %s", val.Kind())
}
case *string:
*val = f.value
case *int:
i, err := strconv.Atoi(f.value)
if err != nil {
return fmt.Errorf("failed to convert string to int: %w", err)
}
*val = i
case *int64:
i, err := strconv.ParseInt(f.value, 10, 64)
if err != nil {
return fmt.Errorf("failed to convert string to int64: %w", err)
}
*val = i
case *String:
val.value = f.value
default:
return fmt.Errorf("unsupported type: expected *String, *string, or reflect.Value, got %T", v)
}
return nil
}
func (f *String) Marshal(v interface{}) error {
if v == nil {
f.value = ""
return nil
} else if reflect.ValueOf(v).IsZero() {
if !strings.Contains(reflect.ValueOf(v).Type().String(), "int") {
f.value = ""
return nil
}
}
switch v := v.(type) {
case *String:
f.value = v.value
case string:
f.value = v
case *string:
f.value = *v
case int:
f.value = strconv.FormatInt(int64(v), 10)
case int64:
f.value = strconv.FormatInt(v, 10)
case *int:
if v == nil {
f.value = strconv.FormatInt(0, 10)
} else {
f.value = strconv.FormatInt(int64(*v), 10)
}
case *int64:
if v == nil {
f.value = strconv.FormatInt(0, 10)
} else {
f.value = strconv.FormatInt(*v, 10)
}
default:
return fmt.Errorf("data does not match required *String or (string, *string, int, *int) type")
}
return nil
}
func (f *String) MarshalJSON() ([]byte, error) {
bytes, err := json.Marshal(f.value)
if err != nil {
return nil, utils.NewSafeError(err, "failed to JSON marshal string to bytes")
}
return bytes, nil
}
func (f *String) UnmarshalJSON(b []byte) error {
var v string
err := json.Unmarshal(b, &v)
if err != nil {
return utils.NewSafeError(err, "failed to JSON unmarshal bytes to string")
}
return f.SetBytes([]byte(v))
}