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:
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 failf("cannot decode %s `%s` as a %s", shortTag(rtag), in, shortTag(tag))
99 // Any data is accepted as a !!str or !!binary.
100 // Otherwise, the prefix is enough of a hint about what it might be.
103 hint = resolveTable[in[0]]
105 if hint != 0 && tag != yaml_STR_TAG && tag != yaml_BINARY_TAG {
106 // Handle things we can lookup in a map.
107 if item, ok := resolveMap[in]; ok {
108 return item.tag, item.value
111 // Base 60 floats are a bad idea, were dropped in YAML 1.2, and
112 // are purposefully unsupported here. They're still quoted on
113 // the way out for compatibility with other parser, though.
117 // We've already checked the map above.
120 // Not in the map, so maybe a normal float.
121 floatv, err := strconv.ParseFloat(in, 64)
123 return yaml_FLOAT_TAG, floatv
127 // Int, float, or timestamp.
128 plain := strings.Replace(in, "_", "", -1)
129 intv, err := strconv.ParseInt(plain, 0, 64)
131 if intv == int64(int(intv)) {
132 return yaml_INT_TAG, int(intv)
134 return yaml_INT_TAG, intv
137 uintv, err := strconv.ParseUint(plain, 0, 64)
139 return yaml_INT_TAG, uintv
141 if yamlStyleFloat.MatchString(plain) {
142 floatv, err := strconv.ParseFloat(plain, 64)
144 return yaml_FLOAT_TAG, floatv
147 if strings.HasPrefix(plain, "0b") {
148 intv, err := strconv.ParseInt(plain[2:], 2, 64)
150 if intv == int64(int(intv)) {
151 return yaml_INT_TAG, int(intv)
153 return yaml_INT_TAG, intv
156 uintv, err := strconv.ParseUint(plain[2:], 2, 64)
158 return yaml_INT_TAG, uintv
160 } else if strings.HasPrefix(plain, "-0b") {
161 intv, err := strconv.ParseInt(plain[3:], 2, 64)
163 if intv == int64(int(intv)) {
164 return yaml_INT_TAG, -int(intv)
166 return yaml_INT_TAG, -intv
170 // XXX Handle timestamps here.
173 panic("resolveTable item not yet handled: " + string(rune(hint)) + " (with " + in + ")")
176 if tag == yaml_BINARY_TAG {
177 return yaml_BINARY_TAG, in
179 if utf8.ValidString(in) {
180 return yaml_STR_TAG, in
182 return yaml_BINARY_TAG, encodeBase64(in)
185 // encodeBase64 encodes s as base64 that is broken up into multiple lines
186 // as appropriate for the resulting length.
187 func encodeBase64(s string) string {
189 encLen := base64.StdEncoding.EncodedLen(len(s))
190 lines := encLen/lineLen + 1
191 buf := make([]byte, encLen*2+lines)
194 base64.StdEncoding.Encode(in, []byte(s))
196 for i := 0; i < len(in); i += lineLen {
201 k += copy(out[k:], in[i:j])
207 return string(out[:k])