|
@@ -34,14 +34,14 @@ func init() {
|
|
|
tag string
|
|
tag string
|
|
|
l []string
|
|
l []string
|
|
|
}{
|
|
}{
|
|
|
- {true, yaml_BOOL_TAG, []string{"true", "True", "TRUE"}},
|
|
|
|
|
- {false, yaml_BOOL_TAG, []string{"false", "False", "FALSE"}},
|
|
|
|
|
- {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{"<<"}},
|
|
|
|
|
|
|
+ {true, boolTag, []string{"true", "True", "TRUE"}},
|
|
|
|
|
+ {false, boolTag, []string{"false", "False", "FALSE"}},
|
|
|
|
|
+ {nil, nullTag, []string{"", "~", "null", "Null", "NULL"}},
|
|
|
|
|
+ {math.NaN(), floatTag, []string{".nan", ".NaN", ".NAN"}},
|
|
|
|
|
+ {math.Inf(+1), floatTag, []string{".inf", ".Inf", ".INF"}},
|
|
|
|
|
+ {math.Inf(+1), floatTag, []string{"+.inf", "+.Inf", "+.INF"}},
|
|
|
|
|
+ {math.Inf(-1), floatTag, []string{"-.inf", "-.Inf", "-.INF"}},
|
|
|
|
|
+ {"<<", mergeTag, []string{"<<"}},
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
m := resolveMap
|
|
m := resolveMap
|
|
@@ -52,11 +52,37 @@ func init() {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+const (
|
|
|
|
|
+ nullTag = "!!null"
|
|
|
|
|
+ boolTag = "!!bool"
|
|
|
|
|
+ strTag = "!!str"
|
|
|
|
|
+ intTag = "!!int"
|
|
|
|
|
+ floatTag = "!!float"
|
|
|
|
|
+ timestampTag = "!!timestamp"
|
|
|
|
|
+ seqTag = "!!seq"
|
|
|
|
|
+ mapTag = "!!map"
|
|
|
|
|
+ binaryTag = "!!binary"
|
|
|
|
|
+ mergeTag = "!!merge"
|
|
|
|
|
+)
|
|
|
|
|
+
|
|
|
|
|
+var longTags = make(map[string]string)
|
|
|
|
|
+var shortTags = make(map[string]string)
|
|
|
|
|
+
|
|
|
|
|
+func init() {
|
|
|
|
|
+ for _, stag := range []string{nullTag, boolTag, strTag, intTag, floatTag, timestampTag, seqTag, mapTag, binaryTag, mergeTag} {
|
|
|
|
|
+ ltag := longTag(stag)
|
|
|
|
|
+ longTags[stag] = ltag
|
|
|
|
|
+ shortTags[ltag] = stag
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
const longTagPrefix = "tag:yaml.org,2002:"
|
|
const longTagPrefix = "tag:yaml.org,2002:"
|
|
|
|
|
|
|
|
func shortTag(tag string) string {
|
|
func shortTag(tag string) string {
|
|
|
- // TODO This can easily be made faster and produce less garbage.
|
|
|
|
|
if strings.HasPrefix(tag, longTagPrefix) {
|
|
if strings.HasPrefix(tag, longTagPrefix) {
|
|
|
|
|
+ if stag, ok := shortTags[tag]; ok {
|
|
|
|
|
+ return stag
|
|
|
|
|
+ }
|
|
|
return "!!" + tag[len(longTagPrefix):]
|
|
return "!!" + tag[len(longTagPrefix):]
|
|
|
}
|
|
}
|
|
|
return tag
|
|
return tag
|
|
@@ -64,6 +90,9 @@ func shortTag(tag string) string {
|
|
|
|
|
|
|
|
func longTag(tag string) string {
|
|
func longTag(tag string) string {
|
|
|
if strings.HasPrefix(tag, "!!") {
|
|
if strings.HasPrefix(tag, "!!") {
|
|
|
|
|
+ if ltag, ok := longTags[tag]; ok {
|
|
|
|
|
+ return ltag
|
|
|
|
|
+ }
|
|
|
return longTagPrefix + tag[2:]
|
|
return longTagPrefix + tag[2:]
|
|
|
}
|
|
}
|
|
|
return tag
|
|
return tag
|
|
@@ -71,7 +100,7 @@ func longTag(tag string) string {
|
|
|
|
|
|
|
|
func resolvableTag(tag string) bool {
|
|
func resolvableTag(tag string) bool {
|
|
|
switch tag {
|
|
switch tag {
|
|
|
- case "", yaml_STR_TAG, yaml_BOOL_TAG, yaml_INT_TAG, yaml_FLOAT_TAG, yaml_NULL_TAG, yaml_TIMESTAMP_TAG:
|
|
|
|
|
|
|
+ case "", strTag, boolTag, intTag, floatTag, nullTag, timestampTag:
|
|
|
return true
|
|
return true
|
|
|
}
|
|
}
|
|
|
return false
|
|
return false
|
|
@@ -80,23 +109,24 @@ func resolvableTag(tag string) bool {
|
|
|
var yamlStyleFloat = regexp.MustCompile(`^[-+]?(\.[0-9]+|[0-9]+(\.[0-9]*)?)([eE][-+]?[0-9]+)?$`)
|
|
var yamlStyleFloat = regexp.MustCompile(`^[-+]?(\.[0-9]+|[0-9]+(\.[0-9]*)?)([eE][-+]?[0-9]+)?$`)
|
|
|
|
|
|
|
|
func resolve(tag string, in string) (rtag string, out interface{}) {
|
|
func resolve(tag string, in string) (rtag string, out interface{}) {
|
|
|
|
|
+ tag = shortTag(tag)
|
|
|
if !resolvableTag(tag) {
|
|
if !resolvableTag(tag) {
|
|
|
return tag, in
|
|
return tag, in
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
defer func() {
|
|
|
switch tag {
|
|
switch tag {
|
|
|
- case "", rtag, yaml_STR_TAG, yaml_BINARY_TAG:
|
|
|
|
|
|
|
+ case "", rtag, strTag, binaryTag:
|
|
|
return
|
|
return
|
|
|
- case yaml_FLOAT_TAG:
|
|
|
|
|
- if rtag == yaml_INT_TAG {
|
|
|
|
|
|
|
+ case floatTag:
|
|
|
|
|
+ if rtag == intTag {
|
|
|
switch v := out.(type) {
|
|
switch v := out.(type) {
|
|
|
case int64:
|
|
case int64:
|
|
|
- rtag = yaml_FLOAT_TAG
|
|
|
|
|
|
|
+ rtag = floatTag
|
|
|
out = float64(v)
|
|
out = float64(v)
|
|
|
return
|
|
return
|
|
|
case int:
|
|
case int:
|
|
|
- rtag = yaml_FLOAT_TAG
|
|
|
|
|
|
|
+ rtag = floatTag
|
|
|
out = float64(v)
|
|
out = float64(v)
|
|
|
return
|
|
return
|
|
|
}
|
|
}
|
|
@@ -111,7 +141,7 @@ func resolve(tag string, in string) (rtag string, out interface{}) {
|
|
|
if in != "" {
|
|
if in != "" {
|
|
|
hint = resolveTable[in[0]]
|
|
hint = resolveTable[in[0]]
|
|
|
}
|
|
}
|
|
|
- if hint != 0 && tag != yaml_STR_TAG && tag != yaml_BINARY_TAG {
|
|
|
|
|
|
|
+ if hint != 0 && tag != strTag && tag != binaryTag {
|
|
|
// Handle things we can lookup in a map.
|
|
// Handle things we can lookup in a map.
|
|
|
if item, ok := resolveMap[in]; ok {
|
|
if item, ok := resolveMap[in]; ok {
|
|
|
return item.tag, item.value
|
|
return item.tag, item.value
|
|
@@ -129,17 +159,17 @@ func resolve(tag string, in string) (rtag string, out interface{}) {
|
|
|
// Not in the map, so maybe a normal float.
|
|
// Not in the map, so maybe a normal float.
|
|
|
floatv, err := strconv.ParseFloat(in, 64)
|
|
floatv, err := strconv.ParseFloat(in, 64)
|
|
|
if err == nil {
|
|
if err == nil {
|
|
|
- return yaml_FLOAT_TAG, floatv
|
|
|
|
|
|
|
+ return floatTag, floatv
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
case 'D', 'S':
|
|
case 'D', 'S':
|
|
|
// Int, float, or timestamp.
|
|
// Int, float, or timestamp.
|
|
|
// Only try values as a timestamp if the value is unquoted or there's an explicit
|
|
// Only try values as a timestamp if the value is unquoted or there's an explicit
|
|
|
// !!timestamp tag.
|
|
// !!timestamp tag.
|
|
|
- if tag == "" || tag == yaml_TIMESTAMP_TAG {
|
|
|
|
|
|
|
+ if tag == "" || tag == timestampTag {
|
|
|
t, ok := parseTimestamp(in)
|
|
t, ok := parseTimestamp(in)
|
|
|
if ok {
|
|
if ok {
|
|
|
- return yaml_TIMESTAMP_TAG, t
|
|
|
|
|
|
|
+ return timestampTag, t
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -147,41 +177,41 @@ func resolve(tag string, in string) (rtag string, out interface{}) {
|
|
|
intv, err := strconv.ParseInt(plain, 0, 64)
|
|
intv, err := strconv.ParseInt(plain, 0, 64)
|
|
|
if err == nil {
|
|
if err == nil {
|
|
|
if intv == int64(int(intv)) {
|
|
if intv == int64(int(intv)) {
|
|
|
- return yaml_INT_TAG, int(intv)
|
|
|
|
|
|
|
+ return intTag, int(intv)
|
|
|
} else {
|
|
} else {
|
|
|
- return yaml_INT_TAG, intv
|
|
|
|
|
|
|
+ return intTag, intv
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
uintv, err := strconv.ParseUint(plain, 0, 64)
|
|
uintv, err := strconv.ParseUint(plain, 0, 64)
|
|
|
if err == nil {
|
|
if err == nil {
|
|
|
- return yaml_INT_TAG, uintv
|
|
|
|
|
|
|
+ return intTag, uintv
|
|
|
}
|
|
}
|
|
|
if yamlStyleFloat.MatchString(plain) {
|
|
if yamlStyleFloat.MatchString(plain) {
|
|
|
floatv, err := strconv.ParseFloat(plain, 64)
|
|
floatv, err := strconv.ParseFloat(plain, 64)
|
|
|
if err == nil {
|
|
if err == nil {
|
|
|
- return yaml_FLOAT_TAG, floatv
|
|
|
|
|
|
|
+ return floatTag, floatv
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
if strings.HasPrefix(plain, "0b") {
|
|
if strings.HasPrefix(plain, "0b") {
|
|
|
intv, err := strconv.ParseInt(plain[2:], 2, 64)
|
|
intv, err := strconv.ParseInt(plain[2:], 2, 64)
|
|
|
if err == nil {
|
|
if err == nil {
|
|
|
if intv == int64(int(intv)) {
|
|
if intv == int64(int(intv)) {
|
|
|
- return yaml_INT_TAG, int(intv)
|
|
|
|
|
|
|
+ return intTag, int(intv)
|
|
|
} else {
|
|
} else {
|
|
|
- return yaml_INT_TAG, intv
|
|
|
|
|
|
|
+ return intTag, intv
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
uintv, err := strconv.ParseUint(plain[2:], 2, 64)
|
|
uintv, err := strconv.ParseUint(plain[2:], 2, 64)
|
|
|
if err == nil {
|
|
if err == nil {
|
|
|
- return yaml_INT_TAG, uintv
|
|
|
|
|
|
|
+ return intTag, uintv
|
|
|
}
|
|
}
|
|
|
} else if strings.HasPrefix(plain, "-0b") {
|
|
} else if strings.HasPrefix(plain, "-0b") {
|
|
|
intv, err := strconv.ParseInt("-" + plain[3:], 2, 64)
|
|
intv, err := strconv.ParseInt("-" + plain[3:], 2, 64)
|
|
|
if err == nil {
|
|
if err == nil {
|
|
|
if true || intv == int64(int(intv)) {
|
|
if true || intv == int64(int(intv)) {
|
|
|
- return yaml_INT_TAG, int(intv)
|
|
|
|
|
|
|
+ return intTag, int(intv)
|
|
|
} else {
|
|
} else {
|
|
|
- return yaml_INT_TAG, intv
|
|
|
|
|
|
|
+ return intTag, intv
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -189,7 +219,7 @@ func resolve(tag string, in string) (rtag string, out interface{}) {
|
|
|
panic("internal error: missing handler for resolver table: " + string(rune(hint)) + " (with " + in + ")")
|
|
panic("internal error: missing handler for resolver table: " + string(rune(hint)) + " (with " + in + ")")
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
- return yaml_STR_TAG, in
|
|
|
|
|
|
|
+ return strTag, in
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// encodeBase64 encodes s as base64 that is broken up into multiple lines
|
|
// encodeBase64 encodes s as base64 that is broken up into multiple lines
|