-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvalue.go
212 lines (186 loc) · 3.61 KB
/
value.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
// Copyright (C) 2016 Kohei YOSHIDA. All rights reserved.
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of The BSD 3-Clause License
// that can be found in the LICENSE file.
package uritemplate
import "strings"
type Values map[string]Value
func (v Values) Set(name string, value Value) {
v[name] = value // TODO(yosida95): canonicalize pct-encoded in the name
}
func (v Values) Get(name string) Value {
if v == nil {
return Value{}
}
return v[name] // TODO(yosida95): canonicalize pct-encoded in the name
}
type ValueType uint8
const (
ValueTypeString = iota
ValueTypeList
ValueTypeKV
valueTypeLast
)
var valueTypeNames = []string{
"String",
"List",
"KV",
}
func (vt ValueType) String() string {
if vt < valueTypeLast {
return valueTypeNames[vt]
}
return ""
}
type Value struct {
T ValueType
V []string
}
func (v Value) String() string {
if v.Valid() && v.T == ValueTypeString {
return v.V[0]
}
return ""
}
func (v Value) List() []string {
if v.Valid() && v.T == ValueTypeList {
return v.V
}
return nil
}
func (v Value) KV() []string {
if v.Valid() && v.T == ValueTypeKV {
return v.V
}
return nil
}
func (v Value) Valid() bool {
switch v.T {
default:
return false
case ValueTypeString:
return len(v.V) > 0
case ValueTypeList:
return len(v.V) > 0
case ValueTypeKV:
return len(v.V) > 0 && len(v.V)%2 == 0
}
}
func (v Value) expand(w *strings.Builder, spec varspec, exp *expression) error {
switch v.T {
case ValueTypeString:
val := v.V[0]
var maxlen int
if max := len(val); spec.maxlen < 1 || spec.maxlen > max {
maxlen = max
} else {
maxlen = spec.maxlen
}
if exp.named {
w.WriteString(spec.name)
if val == "" {
w.WriteString(exp.ifemp)
return nil
}
w.WriteByte('=')
}
return exp.escape(w, val[:maxlen])
case ValueTypeList:
var sep string
if spec.explode {
sep = exp.sep
} else {
sep = ","
}
var pre string
var preifemp string
if spec.explode && exp.named {
pre = spec.name + "="
preifemp = spec.name + exp.ifemp
}
if !spec.explode && exp.named {
w.WriteString(spec.name)
w.WriteByte('=')
}
for i := range v.V {
val := v.V[i]
if i > 0 {
w.WriteString(sep)
}
if val == "" {
w.WriteString(preifemp)
continue
}
w.WriteString(pre)
if err := exp.escape(w, val); err != nil {
return err
}
}
case ValueTypeKV:
var sep string
var kvsep string
if spec.explode {
sep = exp.sep
kvsep = "="
} else {
sep = ","
kvsep = ","
}
var ifemp string
var kescape escapeFunc
if spec.explode && exp.named {
ifemp = exp.ifemp
kescape = escapeLiteral
} else {
ifemp = ","
kescape = exp.escape
}
if !spec.explode && exp.named {
w.WriteString(spec.name)
w.WriteByte('=')
}
for i := 0; i < len(v.V); i += 2 {
if i > 0 {
w.WriteString(sep)
}
if err := kescape(w, v.V[i]); err != nil {
return err
}
if v.V[i+1] == "" {
w.WriteString(ifemp)
continue
}
w.WriteString(kvsep)
if err := exp.escape(w, v.V[i+1]); err != nil {
return err
}
}
}
return nil
}
// String returns Value that represents string.
func String(v string) Value {
return Value{
T: ValueTypeString,
V: []string{v},
}
}
// List returns Value that represents list.
func List(v ...string) Value {
return Value{
T: ValueTypeList,
V: v,
}
}
// KV returns Value that represents associative list.
// KV panics if len(kv) is not even.
func KV(kv ...string) Value {
if len(kv)%2 != 0 {
panic("uritemplate.go: count of the kv must be even number")
}
return Value{
T: ValueTypeKV,
V: kv,
}
}