12 type resolveMapItem struct {
17 var resolveTable = make([]byte, 256)
18 var resolveMap = make(map[string]resolveMapItem)
22 t[int('+')] = 'S' // Sign
24 for _, c := range "0123456789" {
25 t[int(c)] = 'D' // Digit
27 for _, c := range "yYnNtTfFoO~" {
28 t[int(c)] = 'M' // In map
30 t[int('.')] = '.' // Float (potentially in map)
32 var resolveMapList = []struct {
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{"<<"}},
52 for _, item := range resolveMapList {
53 for _, s := range item.l {
54 m[s] = resolveMapItem{item.v, item.tag}
59 const longTagPrefix = "tag:yaml.org,2002:"
61 func 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):]
69 func longTag(tag string) string {
70 if strings.HasPrefix(tag, "!!") {
71 return longTagPrefix + tag[2:]
76 func resolvableTag(tag string) bool {
78 case "", yaml_STR_TAG, yaml_BOOL_TAG, yaml_INT_TAG, yaml_FLOAT_TAG, yaml_NULL_TAG, yaml_TIMESTAMP_TAG:
84 var yamlStyleFloat = regexp.MustCompile(`^[-+]?[0-9]*\.?[0-9]+([eE][-+][0-9]+)?$`)
86 func resolve(tag string, in string) (rtag string, out interface{}) {
87 if !resolvableTag(tag) {
93 case "", rtag, yaml_STR_TAG, yaml_BINARY_TAG:
96 if rtag == yaml_INT_TAG {
97 switch v := out.(type) {
103 rtag = yaml_FLOAT_TAG
109 failf("cannot decode %s `%s` as a %s", shortTag(rtag), in, shortTag(tag))
112 // Any data is accepted as a !!str or !!binary.
113 // Otherwise, the prefix is enough of a hint about what it might be.
116 hint = resolveTable[in[0]]
118 if hint != 0 && tag != yaml_STR_TAG && tag != yaml_BINARY_TAG {
119 // Handle things we can lookup in a map.
120 if item, ok := resolveMap[in]; ok {
121 return item.tag, item.value
124 // Base 60 floats are a bad idea, were dropped in YAML 1.2, and
125 // are purposefully unsupported here. They're still quoted on
126 // the way out for compatibility with other parser, though.
130 // We've already checked the map above.
133 // Not in the map, so maybe a normal float.
134 floatv, err := strconv.ParseFloat(in, 64)
136 return yaml_FLOAT_TAG, floatv
140 // Int, float, or timestamp.
141 // Only try values as a timestamp if the value is unquoted or there's an explicit
143 if tag == "" || tag == yaml_TIMESTAMP_TAG {
144 t, ok := parseTimestamp(in)
146 return yaml_TIMESTAMP_TAG, t
150 plain := strings.Replace(in, "_", "", -1)
151 intv, err := strconv.ParseInt(plain, 0, 64)
153 if intv == int64(int(intv)) {
154 return yaml_INT_TAG, int(intv)
156 return yaml_INT_TAG, intv
159 uintv, err := strconv.ParseUint(plain, 0, 64)
161 return yaml_INT_TAG, uintv
163 if yamlStyleFloat.MatchString(plain) {
164 floatv, err := strconv.ParseFloat(plain, 64)
166 return yaml_FLOAT_TAG, floatv
169 if strings.HasPrefix(plain, "0b") {
170 intv, err := strconv.ParseInt(plain[2:], 2, 64)
172 if intv == int64(int(intv)) {
173 return yaml_INT_TAG, int(intv)
175 return yaml_INT_TAG, intv
178 uintv, err := strconv.ParseUint(plain[2:], 2, 64)
180 return yaml_INT_TAG, uintv
182 } else if strings.HasPrefix(plain, "-0b") {
183 intv, err := strconv.ParseInt("-" + plain[3:], 2, 64)
185 if true || intv == int64(int(intv)) {
186 return yaml_INT_TAG, int(intv)
188 return yaml_INT_TAG, intv
193 panic("resolveTable item not yet handled: " + string(rune(hint)) + " (with " + in + ")")
196 return yaml_STR_TAG, in
199 // encodeBase64 encodes s as base64 that is broken up into multiple lines
200 // as appropriate for the resulting length.
201 func encodeBase64(s string) string {
203 encLen := base64.StdEncoding.EncodedLen(len(s))
204 lines := encLen/lineLen + 1
205 buf := make([]byte, encLen*2+lines)
208 base64.StdEncoding.Encode(in, []byte(s))
210 for i := 0; i < len(in); i += lineLen {
215 k += copy(out[k:], in[i:j])
221 return string(out[:k])
224 // This is a subset of the formats allowed by the regular expression
225 // defined at http://yaml.org/type/timestamp.html.
226 var allowedTimestampFormats = []string{
227 "2006-1-2T15:4:5.999999999Z07:00", // RCF3339Nano with short date fields.
228 "2006-1-2t15:4:5.999999999Z07:00", // RFC3339Nano with short date fields and lower-case "t".
229 "2006-1-2 15:4:5.999999999", // space separated with no time zone
230 "2006-1-2", // date only
231 // Notable exception: time.Parse cannot handle: "2001-12-14 21:59:43.10 -5"
232 // from the set of examples.
235 // parseTimestamp parses s as a timestamp string and
236 // returns the timestamp and reports whether it succeeded.
237 // Timestamp formats are defined at http://yaml.org/type/timestamp.html
238 func parseTimestamp(s string) (time.Time, bool) {
239 // TODO write code to check all the formats supported by
240 // http://yaml.org/type/timestamp.html instead of using time.Parse.
242 // Quick check: all date formats start with YYYY-.
244 for ; i < len(s); i++ {
245 if c := s[i]; c < '0' || c > '9' {
249 if i != 4 || i == len(s) || s[i] != '-' {
250 return time.Time{}, false
252 for _, format := range allowedTimestampFormats {
253 if t, err := time.Parse(format, s); err == nil {
257 return time.Time{}, false