-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.go
231 lines (200 loc) · 3.71 KB
/
parser.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
package dotenv
import (
"errors"
"fmt"
"reflect"
"strconv"
"strings"
"unicode"
)
type Kv struct {
Key string
Value string
}
type KvSlice []Kv
func (s KvSlice) Get(key string) (string, bool) {
for _, v := range s {
if v.Key == key {
return v.Value, true
}
}
return "", false
}
var (
buffer []byte = nil
line = 1
ip = 0
)
func makeError(text string) error { return fmt.Errorf(text, line) }
func end() bool { return ip >= len(buffer) }
func now() byte {
if ip >= len(buffer) {
return '\x00'
}
return buffer[ip]
}
func isLetter() bool {
c := now()
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_'
}
func whiteSpace() {
for now() == ' ' {
ip++
}
for now() == '\n' {
ip++
line++
}
}
func markComment() {
if now() == '#' {
for !end() && now() != '\n' {
ip++
}
ip++
line++
}
}
func markKeyValue() (*Kv, error) {
key := []byte{}
for isLetter() {
key = append(key, now())
ip++
}
if len(key) == 0 {
return nil, makeError("key is empty (line %d)")
}
whiteSpace()
if now() != '=' {
return nil, makeError("expect equal(=) sign after key at line %d")
}
ip++
if now() == '\n' || end() {
return nil, makeError("value is empty (line %d)")
}
whiteSpace()
value := []byte{}
for !end() && now() != '\n' {
if now() != '"' && now() != '\'' {
value = append(value, now())
}
ip++
}
line++
kv := Kv{
Key: string(key),
Value: string(value),
}
return &kv, nil
}
func unmarshal() (KvSlice, error) {
slice := KvSlice{}
for !end() {
if whiteSpace(); ip >= len(buffer) {
return slice, nil
}
if now() == '#' {
markComment()
continue
}
v, err := markKeyValue()
if err != nil {
return nil, err
}
slice = append(slice, *v)
ip++
}
return slice, nil
}
func restore(src []byte) {
buffer = src
line = 1
ip = 0
}
func Unmarshal(src []byte, dst any) error {
restore(src)
slice, err := unmarshal()
if err != nil {
return err
}
v := reflect.ValueOf(dst)
if v.Kind() != reflect.Ptr || v.IsNil() {
return errors.New("encoding structure type is incorrect")
}
v = v.Elem() // value pointed to by the pointer
for i := 0; i < v.NumField(); i++ {
fieldType := v.Type().Field(i)
fieldValue := v.Field(i)
if !unicode.IsUpper(rune(fieldType.Name[0])) {
continue
}
err := encodeToStruct(slice, fieldType, fieldValue)
if err != nil {
return err
}
}
return nil
}
func UnmarshalSlice(src []byte) (KvSlice, error) {
restore(src)
return unmarshal()
}
func encodeToStruct(
slice KvSlice,
ft reflect.StructField,
fv reflect.Value,
) error {
var ok bool
var value string
err := fmt.Errorf("undefined key '%s'", ft.Name)
if ft.Tag == "" {
value, ok = slice.Get(strings.ToLower(ft.Name))
if !ok {
value, ok = slice.Get(strings.ToUpper(ft.Name))
}
if !ok {
return err
}
} else {
value, ok = slice.Get(ft.Tag.Get("env"))
if !ok {
return err
}
}
return valueType(value, ft.Type, fv)
}
func valueType(value string, p reflect.Type, val reflect.Value) error {
switch p.Kind() {
// string
case reflect.String:
val.SetString(value)
return nil
// uint
case reflect.Uint,
reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
v, err := strconv.ParseUint(value, 0, 64)
if err != nil {
return err
}
val.SetUint(v)
return nil
// int
case reflect.Int,
reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
v, err := strconv.ParseInt(value, 0, 64)
if err != nil {
return err
}
val.SetInt(v)
return nil
// float
case reflect.Float32, reflect.Float64:
v, err := strconv.ParseFloat(value, 64)
if err != nil {
return err
}
val.SetFloat(v)
return nil
}
return errors.New("only supports parsing number and string")
}