-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring_decode.go
More file actions
180 lines (138 loc) · 3.1 KB
/
string_decode.go
File metadata and controls
180 lines (138 loc) · 3.1 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
/*
* Copyright (c) 2026 Mikhail Knyazhev <markus621@yandex.com>. All rights reserved.
* Use of this source code is governed by a BSD 3-Clause license that can be found in the LICENSE file.
*/
package cast
import (
"encoding"
"encoding/json"
"encoding/xml"
"fmt"
"io"
"reflect"
"strconv"
"strings"
"time"
)
func StrTo[T any](s string) (T, error) {
var v T
if len(s) == 0 {
return v, nil
}
err := StringDecode(&v, s)
return v, err
}
func StrToSlice[T any](s, sep string) ([]T, error) {
if s == "" {
return nil, nil
}
count := strings.Count(s, sep) + 1
result := make([]T, 0, count)
for _, ss := range strings.Split(s, sep) {
val, err := StrTo[T](ss)
if err != nil {
return nil, err
}
result = append(result, val)
}
return result, nil
}
func StringDecode(obj any, s string) (err error) {
if len(s) == 0 {
return
}
ref := reflect.ValueOf(obj)
if ref.Kind() != reflect.Ptr {
return fmt.Errorf("got not a pointer")
}
if ref.IsNil() {
return fmt.Errorf("got nil pointer")
}
if in, ok := obj.(Initializer); ok {
if err = in.Initialize(); err != nil {
return
}
}
switch p := obj.(type) {
case *string:
*p = s
case *[]byte:
*p = []byte(s)
case *int:
var val int64
val, err = strconv.ParseInt(s, 10, strconv.IntSize)
*p = int(val)
case *int8:
var val int64
val, err = strconv.ParseInt(s, 10, 8)
*p = int8(val)
case *int16:
var val int64
val, err = strconv.ParseInt(s, 10, 16)
*p = int16(val)
case *int32:
var val int64
val, err = strconv.ParseInt(s, 10, 32)
*p = int32(val)
case *int64:
*p, err = strconv.ParseInt(s, 10, 64)
case *uint:
var val uint64
val, err = strconv.ParseUint(s, 10, strconv.IntSize)
*p = uint(val)
case *uint8:
var val uint64
val, err = strconv.ParseUint(s, 10, 8)
*p = uint8(val)
case *uint16:
var val uint64
val, err = strconv.ParseUint(s, 10, 16)
*p = uint16(val)
case *uint32:
var val uint64
val, err = strconv.ParseUint(s, 10, 32)
*p = uint32(val)
case *uint64:
*p, err = strconv.ParseUint(s, 10, 64)
case *float32:
var val float64
val, err = strconv.ParseFloat(s, 32)
*p = float32(val)
case *float64:
*p, err = strconv.ParseFloat(s, 64)
case *complex64:
var val complex128
val, err = strconv.ParseComplex(s, 64)
*p = complex64(val)
case *complex128:
*p, err = strconv.ParseComplex(s, 128)
case *bool:
*p, err = strconv.ParseBool(s)
case *time.Duration:
*p, err = time.ParseDuration(s)
case *time.Time:
*p, err = time.Parse(time.RFC3339, s)
case io.Writer:
_, err = p.Write([]byte(s))
case io.StringWriter:
_, err = p.WriteString(s)
case UnStringer:
p.UnString(s)
case encoding.BinaryUnmarshaler:
err = p.UnmarshalBinary([]byte(s))
case encoding.TextUnmarshaler:
err = p.UnmarshalText([]byte(s))
case json.Unmarshaler:
err = p.UnmarshalJSON([]byte(s))
case xml.Unmarshaler:
err = xml.Unmarshal([]byte(s), p)
default:
switch ref.Elem().Kind() {
case reflect.Struct, reflect.Map, reflect.Array, reflect.Slice:
err = json.Unmarshal([]byte(s), obj)
default:
err = fmt.Errorf("unsupported type: %T", obj)
}
}
return
}