|
|
@@ -1,9 +1,12 @@
|
|
|
package yaml
|
|
|
|
|
|
import (
|
|
|
+ "encoding/base64"
|
|
|
+ "fmt"
|
|
|
"math"
|
|
|
"strconv"
|
|
|
"strings"
|
|
|
+ "unicode/utf8"
|
|
|
)
|
|
|
|
|
|
// TODO: merge, timestamps, base 60 floats, omap.
|
|
|
@@ -33,18 +36,18 @@ func init() {
|
|
|
tag string
|
|
|
l []string
|
|
|
}{
|
|
|
- {true, "!!bool", []string{"y", "Y", "yes", "Yes", "YES"}},
|
|
|
- {true, "!!bool", []string{"true", "True", "TRUE"}},
|
|
|
- {true, "!!bool", []string{"on", "On", "ON"}},
|
|
|
- {false, "!!bool", []string{"n", "N", "no", "No", "NO"}},
|
|
|
- {false, "!!bool", []string{"false", "False", "FALSE"}},
|
|
|
- {false, "!!bool", []string{"off", "Off", "OFF"}},
|
|
|
- {nil, "!!null", []string{"~", "null", "Null", "NULL"}},
|
|
|
- {math.NaN(), "!!float", []string{".nan", ".NaN", ".NAN"}},
|
|
|
- {math.Inf(+1), "!!float", []string{".inf", ".Inf", ".INF"}},
|
|
|
- {math.Inf(+1), "!!float", []string{"+.inf", "+.Inf", "+.INF"}},
|
|
|
- {math.Inf(-1), "!!float", []string{"-.inf", "-.Inf", "-.INF"}},
|
|
|
- {"<<", "!!merge", []string{"<<"}},
|
|
|
+ {true, yaml_BOOL_TAG, []string{"y", "Y", "yes", "Yes", "YES"}},
|
|
|
+ {true, yaml_BOOL_TAG, []string{"true", "True", "TRUE"}},
|
|
|
+ {true, yaml_BOOL_TAG, []string{"on", "On", "ON"}},
|
|
|
+ {false, yaml_BOOL_TAG, []string{"n", "N", "no", "No", "NO"}},
|
|
|
+ {false, yaml_BOOL_TAG, []string{"false", "False", "FALSE"}},
|
|
|
+ {false, yaml_BOOL_TAG, []string{"off", "Off", "OFF"}},
|
|
|
+ {nil, yaml_NULL_TAG, []string{"", "~", "null", "Null", "NULL"}},
|
|
|
+ {math.NaN(), yaml_FLOAT_TAG, []string{".nan", ".NaN", ".NAN"}},
|
|
|
+ {math.Inf(+1), yaml_FLOAT_TAG, []string{".inf", ".Inf", ".INF"}},
|
|
|
+ {math.Inf(+1), yaml_FLOAT_TAG, []string{"+.inf", "+.Inf", "+.INF"}},
|
|
|
+ {math.Inf(-1), yaml_FLOAT_TAG, []string{"-.inf", "-.Inf", "-.INF"}},
|
|
|
+ {"<<", yaml_MERGE_TAG, []string{"<<"}},
|
|
|
}
|
|
|
|
|
|
m := resolveMap
|
|
|
@@ -58,90 +61,130 @@ func init() {
|
|
|
const longTagPrefix = "tag:yaml.org,2002:"
|
|
|
|
|
|
func shortTag(tag string) string {
|
|
|
+ // TODO This can easily be made faster and produce less garbage.
|
|
|
if strings.HasPrefix(tag, longTagPrefix) {
|
|
|
return "!!" + tag[len(longTagPrefix):]
|
|
|
}
|
|
|
return tag
|
|
|
}
|
|
|
|
|
|
+func longTag(tag string) string {
|
|
|
+ if strings.HasPrefix(tag, "!!") {
|
|
|
+ return longTagPrefix + tag[2:]
|
|
|
+ }
|
|
|
+ return tag
|
|
|
+}
|
|
|
+
|
|
|
func resolvableTag(tag string) bool {
|
|
|
switch tag {
|
|
|
- case "", "!!str", "!!bool", "!!int", "!!float", "!!null":
|
|
|
+ case "", yaml_STR_TAG, yaml_BOOL_TAG, yaml_INT_TAG, yaml_FLOAT_TAG, yaml_NULL_TAG:
|
|
|
return true
|
|
|
}
|
|
|
return false
|
|
|
}
|
|
|
|
|
|
func resolve(tag string, in string) (rtag string, out interface{}) {
|
|
|
- tag = shortTag(tag)
|
|
|
if !resolvableTag(tag) {
|
|
|
return tag, in
|
|
|
}
|
|
|
|
|
|
defer func() {
|
|
|
- if tag != "" && tag != rtag {
|
|
|
- fail("Can't decode " + rtag + " '" + in + "' as a " + tag)
|
|
|
+ switch tag {
|
|
|
+ case "", rtag, yaml_STR_TAG, yaml_BINARY_TAG:
|
|
|
+ return
|
|
|
}
|
|
|
+ fail(fmt.Sprintf("cannot decode %s `%s` as a %s", shortTag(rtag), in, shortTag(tag)))
|
|
|
}()
|
|
|
|
|
|
- if in == "" {
|
|
|
- return "!!null", nil
|
|
|
- }
|
|
|
-
|
|
|
- c := resolveTable[in[0]]
|
|
|
- if c == 0 {
|
|
|
- // It's a string for sure. Nothing to do.
|
|
|
- return "!!str", in
|
|
|
+ // Any data is accepted as a !!str or !!binary.
|
|
|
+ // Otherwise, the prefix is enough of a hint about what it might be.
|
|
|
+ hint := byte('N')
|
|
|
+ if in != "" {
|
|
|
+ hint = resolveTable[in[0]]
|
|
|
}
|
|
|
+ if hint != 0 && tag != yaml_STR_TAG && tag != yaml_BINARY_TAG {
|
|
|
+ // Handle things we can lookup in a map.
|
|
|
+ if item, ok := resolveMap[in]; ok {
|
|
|
+ return item.tag, item.value
|
|
|
+ }
|
|
|
|
|
|
- // Handle things we can lookup in a map.
|
|
|
- if item, ok := resolveMap[in]; ok {
|
|
|
- return item.tag, item.value
|
|
|
- }
|
|
|
+ // Base 60 floats are a bad idea, were dropped in YAML 1.2, and
|
|
|
+ // are purposefully unsupported here. They're still quoted on
|
|
|
+ // the way out for compatibility with other parser, though.
|
|
|
|
|
|
- switch c {
|
|
|
- case 'M':
|
|
|
- // We've already checked the map above.
|
|
|
+ switch hint {
|
|
|
+ case 'M':
|
|
|
+ // We've already checked the map above.
|
|
|
|
|
|
- case '.':
|
|
|
- // Not in the map, so maybe a normal float.
|
|
|
- floatv, err := strconv.ParseFloat(in, 64)
|
|
|
- if err == nil {
|
|
|
- return "!!float", floatv
|
|
|
- }
|
|
|
- // XXX Handle base 60 floats here (WTF!)
|
|
|
-
|
|
|
- case 'D', 'S':
|
|
|
- // Int, float, or timestamp.
|
|
|
- plain := strings.Replace(in, "_", "", -1)
|
|
|
- intv, err := strconv.ParseInt(plain, 0, 64)
|
|
|
- if err == nil {
|
|
|
- if intv == int64(int(intv)) {
|
|
|
- return "!!int", int(intv)
|
|
|
- } else {
|
|
|
- return "!!int", intv
|
|
|
+ case '.':
|
|
|
+ // Not in the map, so maybe a normal float.
|
|
|
+ floatv, err := strconv.ParseFloat(in, 64)
|
|
|
+ if err == nil {
|
|
|
+ return yaml_FLOAT_TAG, floatv
|
|
|
}
|
|
|
- }
|
|
|
- floatv, err := strconv.ParseFloat(plain, 64)
|
|
|
- if err == nil {
|
|
|
- return "!!float", floatv
|
|
|
- }
|
|
|
- if strings.HasPrefix(plain, "0b") {
|
|
|
- intv, err := strconv.ParseInt(plain[2:], 2, 64)
|
|
|
+
|
|
|
+ case 'D', 'S':
|
|
|
+ // Int, float, or timestamp.
|
|
|
+ plain := strings.Replace(in, "_", "", -1)
|
|
|
+ intv, err := strconv.ParseInt(plain, 0, 64)
|
|
|
if err == nil {
|
|
|
- return "!!int", int(intv)
|
|
|
+ if intv == int64(int(intv)) {
|
|
|
+ return yaml_INT_TAG, int(intv)
|
|
|
+ } else {
|
|
|
+ return yaml_INT_TAG, intv
|
|
|
+ }
|
|
|
}
|
|
|
- } else if strings.HasPrefix(plain, "-0b") {
|
|
|
- intv, err := strconv.ParseInt(plain[3:], 2, 64)
|
|
|
+ floatv, err := strconv.ParseFloat(plain, 64)
|
|
|
if err == nil {
|
|
|
- return "!!int", -int(intv)
|
|
|
+ return yaml_FLOAT_TAG, floatv
|
|
|
+ }
|
|
|
+ if strings.HasPrefix(plain, "0b") {
|
|
|
+ intv, err := strconv.ParseInt(plain[2:], 2, 64)
|
|
|
+ if err == nil {
|
|
|
+ return yaml_INT_TAG, int(intv)
|
|
|
+ }
|
|
|
+ } else if strings.HasPrefix(plain, "-0b") {
|
|
|
+ intv, err := strconv.ParseInt(plain[3:], 2, 64)
|
|
|
+ if err == nil {
|
|
|
+ return yaml_INT_TAG, -int(intv)
|
|
|
+ }
|
|
|
}
|
|
|
+ // XXX Handle timestamps here.
|
|
|
+
|
|
|
+ default:
|
|
|
+ panic("resolveTable item not yet handled: " + string(rune(hint)) + " (with " + in + ")")
|
|
|
}
|
|
|
- // XXX Handle timestamps here.
|
|
|
+ }
|
|
|
+ if tag == yaml_BINARY_TAG {
|
|
|
+ return yaml_BINARY_TAG, in
|
|
|
+ }
|
|
|
+ if utf8.ValidString(in) {
|
|
|
+ return yaml_STR_TAG, in
|
|
|
+ }
|
|
|
+ return yaml_BINARY_TAG, encodeBase64(in)
|
|
|
+}
|
|
|
|
|
|
- default:
|
|
|
- panic("resolveTable item not yet handled: " +
|
|
|
- string([]byte{c}) + " (with " + in + ")")
|
|
|
+// encodeBase64 encodes s as base64 that is broken up into multiple lines
|
|
|
+// as appropriate for the resulting length.
|
|
|
+func encodeBase64(s string) string {
|
|
|
+ const lineLen = 70
|
|
|
+ encLen := base64.StdEncoding.EncodedLen(len(s))
|
|
|
+ lines := encLen/lineLen + 1
|
|
|
+ buf := make([]byte, encLen*2+lines)
|
|
|
+ in := buf[0:encLen]
|
|
|
+ out := buf[encLen:]
|
|
|
+ base64.StdEncoding.Encode(in, []byte(s))
|
|
|
+ k := 0
|
|
|
+ for i := 0; i < len(in); i += lineLen {
|
|
|
+ j := i + lineLen
|
|
|
+ if j > len(in) {
|
|
|
+ j = len(in)
|
|
|
+ }
|
|
|
+ k += copy(out[k:], in[i:j])
|
|
|
+ if lines > 1 {
|
|
|
+ out[k] = '\n'
|
|
|
+ k++
|
|
|
+ }
|
|
|
}
|
|
|
- return "!!str", in
|
|
|
+ return string(out[:k])
|
|
|
}
|