-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvalue.go
103 lines (83 loc) · 2.09 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
// 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
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 nil
}
return v[name] // TODO(yosida95): canonicalize pct-encoded in the name
}
type Value interface {
defined() bool
empty() bool
expand(int, func(k string, v string, first bool) error) error
}
// String returns Value that represents string.
func String(v string) Value {
return valueString(v)
}
type valueString string
func (v valueString) defined() bool {
return true
}
func (v valueString) empty() bool {
return len(v) == 0
}
func (v valueString) expand(maxlen int, found func(string, string, bool) error) error {
if maxlen < 1 {
maxlen = len(v)
}
if max := len(v); maxlen > max {
maxlen = max
}
return found("", string(v)[:maxlen], true)
}
// List returns Value that represents list.
func List(v ...string) Value {
return valueList(v)
}
type valueList []string
func (v valueList) defined() bool {
return len(v) > 0
}
func (v valueList) empty() bool {
return !v.defined()
}
func (v valueList) expand(_ int, found func(string, string, bool) error) error {
for i := 0; i < len(v); i++ {
if err := found("", v[i], i == 0); err != nil {
return err
}
}
return nil
}
// 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 valueKV(kv)
}
type valueKV []string
func (v valueKV) defined() bool {
return len(v) > 0
}
func (v valueKV) empty() bool {
return !v.defined()
}
func (v valueKV) expand(_ int, found func(string, string, bool) error) error {
for i := 0; i < len(v); i += 2 {
if err := found(v[i], v[i+1], i == 0); err != nil {
return err
}
}
return nil
}