blob: bfe67c4f5a9f543b4a5a34bf4d1c23a0a0bd0ff1 [file] [log] [blame]
Marcel van Lohuizen07ee2ab2018-12-10 15:57:15 +01001package yaml
2
3import (
4 "encoding/base64"
5 "math"
6 "regexp"
7 "strconv"
8 "strings"
9 "time"
10)
11
12type resolveMapItem struct {
13 value interface{}
14 tag string
15}
16
17var resolveTable = make([]byte, 256)
18var resolveMap = make(map[string]resolveMapItem)
19
20func init() {
21 t := resolveTable
22 t[int('+')] = 'S' // Sign
23 t[int('-')] = 'S'
24 for _, c := range "0123456789" {
25 t[int(c)] = 'D' // Digit
26 }
27 for _, c := range "yYnNtTfFoO~" {
28 t[int(c)] = 'M' // In map
29 }
30 t[int('.')] = '.' // Float (potentially in map)
31
32 var resolveMapList = []struct {
33 v interface{}
34 tag string
35 l []string
36 }{
37 {true, yaml_BOOL_TAG, []string{"y", "Y", "yes", "Yes", "YES"}},
38 {true, yaml_BOOL_TAG, []string{"true", "True", "TRUE"}},
39 {true, yaml_BOOL_TAG, []string{"on", "On", "ON"}},
40 {false, yaml_BOOL_TAG, []string{"n", "N", "no", "No", "NO"}},
41 {false, yaml_BOOL_TAG, []string{"false", "False", "FALSE"}},
42 {false, yaml_BOOL_TAG, []string{"off", "Off", "OFF"}},
43 {nil, yaml_NULL_TAG, []string{"", "~", "null", "Null", "NULL"}},
44 {math.NaN(), yaml_FLOAT_TAG, []string{".nan", ".NaN", ".NAN"}},
45 {math.Inf(+1), yaml_FLOAT_TAG, []string{".inf", ".Inf", ".INF"}},
46 {math.Inf(+1), yaml_FLOAT_TAG, []string{"+.inf", "+.Inf", "+.INF"}},
47 {math.Inf(-1), yaml_FLOAT_TAG, []string{"-.inf", "-.Inf", "-.INF"}},
48 {"<<", yaml_MERGE_TAG, []string{"<<"}},
49 }
50
51 m := resolveMap
52 for _, item := range resolveMapList {
53 for _, s := range item.l {
54 m[s] = resolveMapItem{item.v, item.tag}
55 }
56 }
57}
58
59const longTagPrefix = "tag:yaml.org,2002:"
60
61func shortTag(tag string) string {
62 // TODO This can easily be made faster and produce less garbage.
63 if strings.HasPrefix(tag, longTagPrefix) {
64 return "!!" + tag[len(longTagPrefix):]
65 }
66 return tag
67}
68
69func longTag(tag string) string {
70 if strings.HasPrefix(tag, "!!") {
71 return longTagPrefix + tag[2:]
72 }
73 return tag
74}
75
76func resolvableTag(tag string) bool {
77 switch tag {
78 case "", yaml_STR_TAG, yaml_BOOL_TAG, yaml_INT_TAG, yaml_FLOAT_TAG, yaml_NULL_TAG, yaml_TIMESTAMP_TAG:
79 return true
80 }
81 return false
82}
83
84var yamlStyleFloat = regexp.MustCompile(`^[-+]?[0-9]*\.?[0-9]+([eE][-+][0-9]+)?$`)
85
Marcel van Lohuizen2156c812018-12-10 16:05:07 +010086func (d *decoder) resolve(n *node) (rtag string, out interface{}) {
87 tag := n.tag
88 in := n.value
Marcel van Lohuizen07ee2ab2018-12-10 15:57:15 +010089 if !resolvableTag(tag) {
90 return tag, in
91 }
92
93 defer func() {
94 switch tag {
95 case "", rtag, yaml_STR_TAG, yaml_BINARY_TAG:
96 return
97 case yaml_FLOAT_TAG:
98 if rtag == yaml_INT_TAG {
99 switch v := out.(type) {
100 case int64:
101 rtag = yaml_FLOAT_TAG
102 out = float64(v)
103 return
104 case int:
105 rtag = yaml_FLOAT_TAG
106 out = float64(v)
107 return
108 }
109 }
110 }
Marcel van Lohuizen2156c812018-12-10 16:05:07 +0100111 d.p.failf(n.startPos.line, "cannot decode %s `%s` as a %s", shortTag(rtag), in, shortTag(tag))
Marcel van Lohuizen07ee2ab2018-12-10 15:57:15 +0100112 }()
113
114 // Any data is accepted as a !!str or !!binary.
115 // Otherwise, the prefix is enough of a hint about what it might be.
116 hint := byte('N')
117 if in != "" {
118 hint = resolveTable[in[0]]
119 }
120 if hint != 0 && tag != yaml_STR_TAG && tag != yaml_BINARY_TAG {
121 // Handle things we can lookup in a map.
122 if item, ok := resolveMap[in]; ok {
123 return item.tag, item.value
124 }
125
126 // Base 60 floats are a bad idea, were dropped in YAML 1.2, and
127 // are purposefully unsupported here. They're still quoted on
128 // the way out for compatibility with other parser, though.
129
130 switch hint {
131 case 'M':
132 // We've already checked the map above.
133
134 case '.':
135 // Not in the map, so maybe a normal float.
136 floatv, err := strconv.ParseFloat(in, 64)
137 if err == nil {
138 return yaml_FLOAT_TAG, floatv
139 }
140
141 case 'D', 'S':
142 // Int, float, or timestamp.
143 // Only try values as a timestamp if the value is unquoted or there's an explicit
144 // !!timestamp tag.
145 if tag == "" || tag == yaml_TIMESTAMP_TAG {
146 t, ok := parseTimestamp(in)
147 if ok {
148 return yaml_TIMESTAMP_TAG, t
149 }
150 }
151
152 plain := strings.Replace(in, "_", "", -1)
153 intv, err := strconv.ParseInt(plain, 0, 64)
154 if err == nil {
155 if intv == int64(int(intv)) {
156 return yaml_INT_TAG, int(intv)
157 } else {
158 return yaml_INT_TAG, intv
159 }
160 }
161 uintv, err := strconv.ParseUint(plain, 0, 64)
162 if err == nil {
163 return yaml_INT_TAG, uintv
164 }
165 if yamlStyleFloat.MatchString(plain) {
166 floatv, err := strconv.ParseFloat(plain, 64)
167 if err == nil {
168 return yaml_FLOAT_TAG, floatv
169 }
170 }
171 if strings.HasPrefix(plain, "0b") {
172 intv, err := strconv.ParseInt(plain[2:], 2, 64)
173 if err == nil {
174 if intv == int64(int(intv)) {
175 return yaml_INT_TAG, int(intv)
176 } else {
177 return yaml_INT_TAG, intv
178 }
179 }
180 uintv, err := strconv.ParseUint(plain[2:], 2, 64)
181 if err == nil {
182 return yaml_INT_TAG, uintv
183 }
184 } else if strings.HasPrefix(plain, "-0b") {
Marcel van Lohuizen2156c812018-12-10 16:05:07 +0100185 intv, err := strconv.ParseInt("-"+plain[3:], 2, 64)
Marcel van Lohuizen07ee2ab2018-12-10 15:57:15 +0100186 if err == nil {
187 if true || intv == int64(int(intv)) {
188 return yaml_INT_TAG, int(intv)
189 } else {
190 return yaml_INT_TAG, intv
191 }
192 }
193 }
194 default:
195 panic("resolveTable item not yet handled: " + string(rune(hint)) + " (with " + in + ")")
196 }
197 }
198 return yaml_STR_TAG, in
199}
200
201// encodeBase64 encodes s as base64 that is broken up into multiple lines
202// as appropriate for the resulting length.
203func encodeBase64(s string) string {
204 const lineLen = 70
205 encLen := base64.StdEncoding.EncodedLen(len(s))
206 lines := encLen/lineLen + 1
207 buf := make([]byte, encLen*2+lines)
208 in := buf[0:encLen]
209 out := buf[encLen:]
210 base64.StdEncoding.Encode(in, []byte(s))
211 k := 0
212 for i := 0; i < len(in); i += lineLen {
213 j := i + lineLen
214 if j > len(in) {
215 j = len(in)
216 }
217 k += copy(out[k:], in[i:j])
218 if lines > 1 {
219 out[k] = '\n'
220 k++
221 }
222 }
223 return string(out[:k])
224}
225
226// This is a subset of the formats allowed by the regular expression
227// defined at http://yaml.org/type/timestamp.html.
228var allowedTimestampFormats = []string{
229 "2006-1-2T15:4:5.999999999Z07:00", // RCF3339Nano with short date fields.
230 "2006-1-2t15:4:5.999999999Z07:00", // RFC3339Nano with short date fields and lower-case "t".
231 "2006-1-2 15:4:5.999999999", // space separated with no time zone
232 "2006-1-2", // date only
233 // Notable exception: time.Parse cannot handle: "2001-12-14 21:59:43.10 -5"
234 // from the set of examples.
235}
236
237// parseTimestamp parses s as a timestamp string and
238// returns the timestamp and reports whether it succeeded.
239// Timestamp formats are defined at http://yaml.org/type/timestamp.html
240func parseTimestamp(s string) (time.Time, bool) {
241 // TODO write code to check all the formats supported by
242 // http://yaml.org/type/timestamp.html instead of using time.Parse.
243
244 // Quick check: all date formats start with YYYY-.
245 i := 0
246 for ; i < len(s); i++ {
247 if c := s[i]; c < '0' || c > '9' {
248 break
249 }
250 }
251 if i != 4 || i == len(s) || s[i] != '-' {
252 return time.Time{}, false
253 }
254 for _, format := range allowedTimestampFormats {
255 if t, err := time.Parse(format, s); err == nil {
256 return t, true
257 }
258 }
259 return time.Time{}, false
260}