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