-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.go
171 lines (144 loc) · 3.37 KB
/
parse.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
// Copyright (c) 2020, Roel Schut. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package env
import (
"fmt"
"strings"
"unicode"
"github.com/go-pogo/errors"
"github.com/go-pogo/rawconv"
)
const (
ErrInvalidFormat errors.Msg = "invalid format"
ErrMissingEndQuote errors.Msg = "missing end quote"
ErrEmptyKey errors.Msg = "empty key"
)
// Value is an alias of rawconv.Value.
type Value = rawconv.Value
type NamedValue struct {
Name string
Value Value
}
func (nv NamedValue) GoString() string {
return `env.NamedValue(` + nv.Name + `="` + nv.Value.String() + `")`
}
// Parse parses a string containing a possible key value pair. Any whitespace
// at the start and/or end of str is trimmed. It returns an empty NamedValue
// when the provided str, after trimming, begins with #.
func Parse(str string) (NamedValue, error) {
str = strings.TrimSpace(str)
if str == "" || str[0] == '#' {
return NamedValue{}, nil
}
return parse(str)
}
type ParseError struct {
Err error
Str string
}
func (e *ParseError) Unwrap() error { return e.Err }
func (e *ParseError) Error() string {
return fmt.Sprintf("error while parsing `%s`", e.Str)
}
func parse(str string) (NamedValue, error) {
parts := strings.SplitAfterN(str, "=", 2)
if len(parts) != 2 {
return NamedValue{}, errors.WithStack(&ParseError{
Err: ErrInvalidFormat,
Str: str,
})
}
n := len(parts[0]) - 1
if n == 0 {
return NamedValue{}, errors.WithStack(&ParseError{
Err: ErrEmptyKey,
Str: str,
})
}
// strip `=` from end of key part
key := parts[0][:n]
if n > 6 &&
strings.HasPrefix(key, "export") &&
unicode.IsSpace(rune(key[6])) {
// strip `export ` from start and optional whitespace from end
key = strings.TrimSpace(key[6:])
} else if unicode.IsSpace(rune(key[n-1])) {
// strip any possible extra whitespace from end
key = strings.TrimSpace(key[:n-1])
}
if key == "" {
return NamedValue{}, errors.WithStack(&ParseError{
Err: ErrEmptyKey,
Str: str,
})
}
val := parts[1]
if val == "" {
return NamedValue{key, Value(val)}, nil
}
if unicode.IsSpace(rune(val[0])) {
val = strings.TrimSpace(val[1:])
}
var err error
switch true {
case val[0] == '\'':
val, err = parseQuotedValue(val[1:], '\'')
if err != nil {
return NamedValue{}, errors.WithStack(&ParseError{
Err: err,
Str: str,
})
}
case val[0] == '"':
val, err = parseQuotedValue(val[1:], '"')
if err != nil {
return NamedValue{}, errors.WithStack(&ParseError{
Err: err,
Str: str,
})
}
default:
i := strings.IndexRune(val, '#')
if i == 0 {
val = ""
} else if i > 0 {
val = val[:i-1]
}
}
return NamedValue{key, Value(val)}, nil
}
func parseQuotedValue(val string, q rune) (string, error) {
// first rune q is already stripped from val
if len(val) == 1 && rune(val[0]) == q {
return "", nil
}
if !strings.ContainsRune(val, q) {
return "", ErrMissingEndQuote
}
var e bool
var s strings.Builder
s.Grow(len(val))
loop:
for _, char := range val {
switch char {
case q:
if !e {
// quote is not escaped, we've reached the end of the value
break loop
}
e = false
s.WriteRune(char)
case '\\':
if e {
// slash is escaped which means we'll keep it
s.WriteRune(char)
}
e = !e
default:
e = false
s.WriteRune(char)
}
}
return s.String(), nil
}