forked from ebhomengo/niki
1
0
Fork 0
niki/vendor/gopkg.in/yaml.v2/resolve.go

458 lines
6.9 KiB
Go
Raw Normal View History

2024-05-14 13:07:09 +00:00
package yaml
import (
"encoding/base64"
"math"
"regexp"
"strconv"
"strings"
"time"
)
type resolveMapItem struct {
value interface{}
tag string
2024-05-14 13:07:09 +00:00
}
var resolveTable = make([]byte, 256)
2024-05-14 13:07:09 +00:00
var resolveMap = make(map[string]resolveMapItem)
func init() {
2024-05-14 13:07:09 +00:00
t := resolveTable
2024-05-14 13:07:09 +00:00
t[int('+')] = 'S' // Sign
2024-05-14 13:07:09 +00:00
t[int('-')] = 'S'
2024-05-14 13:07:09 +00:00
for _, c := range "0123456789" {
2024-05-14 13:07:09 +00:00
t[int(c)] = 'D' // Digit
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
for _, c := range "yYnNtTfFoO~" {
2024-05-14 13:07:09 +00:00
t[int(c)] = 'M' // In map
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
t[int('.')] = '.' // Float (potentially in map)
var resolveMapList = []struct {
v interface{}
2024-05-14 13:07:09 +00:00
tag string
l []string
2024-05-14 13:07:09 +00:00
}{
2024-05-14 13:07:09 +00:00
{true, yaml_BOOL_TAG, []string{"y", "Y", "yes", "Yes", "YES"}},
2024-05-14 13:07:09 +00:00
{true, yaml_BOOL_TAG, []string{"true", "True", "TRUE"}},
2024-05-14 13:07:09 +00:00
{true, yaml_BOOL_TAG, []string{"on", "On", "ON"}},
2024-05-14 13:07:09 +00:00
{false, yaml_BOOL_TAG, []string{"n", "N", "no", "No", "NO"}},
2024-05-14 13:07:09 +00:00
{false, yaml_BOOL_TAG, []string{"false", "False", "FALSE"}},
2024-05-14 13:07:09 +00:00
{false, yaml_BOOL_TAG, []string{"off", "Off", "OFF"}},
2024-05-14 13:07:09 +00:00
{nil, yaml_NULL_TAG, []string{"", "~", "null", "Null", "NULL"}},
2024-05-14 13:07:09 +00:00
{math.NaN(), yaml_FLOAT_TAG, []string{".nan", ".NaN", ".NAN"}},
2024-05-14 13:07:09 +00:00
{math.Inf(+1), yaml_FLOAT_TAG, []string{".inf", ".Inf", ".INF"}},
2024-05-14 13:07:09 +00:00
{math.Inf(+1), yaml_FLOAT_TAG, []string{"+.inf", "+.Inf", "+.INF"}},
2024-05-14 13:07:09 +00:00
{math.Inf(-1), yaml_FLOAT_TAG, []string{"-.inf", "-.Inf", "-.INF"}},
2024-05-14 13:07:09 +00:00
{"<<", yaml_MERGE_TAG, []string{"<<"}},
}
m := resolveMap
2024-05-14 13:07:09 +00:00
for _, item := range resolveMapList {
2024-05-14 13:07:09 +00:00
for _, s := range item.l {
2024-05-14 13:07:09 +00:00
m[s] = resolveMapItem{item.v, item.tag}
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
}
const longTagPrefix = "tag:yaml.org,2002:"
func shortTag(tag string) string {
2024-05-14 13:07:09 +00:00
// TODO This can easily be made faster and produce less garbage.
2024-05-14 13:07:09 +00:00
if strings.HasPrefix(tag, longTagPrefix) {
2024-05-14 13:07:09 +00:00
return "!!" + tag[len(longTagPrefix):]
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
return tag
2024-05-14 13:07:09 +00:00
}
func longTag(tag string) string {
2024-05-14 13:07:09 +00:00
if strings.HasPrefix(tag, "!!") {
2024-05-14 13:07:09 +00:00
return longTagPrefix + tag[2:]
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
return tag
2024-05-14 13:07:09 +00:00
}
func resolvableTag(tag string) bool {
2024-05-14 13:07:09 +00:00
switch tag {
2024-05-14 13:07:09 +00:00
case "", yaml_STR_TAG, yaml_BOOL_TAG, yaml_INT_TAG, yaml_FLOAT_TAG, yaml_NULL_TAG, yaml_TIMESTAMP_TAG:
2024-05-14 13:07:09 +00:00
return true
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
return false
2024-05-14 13:07:09 +00:00
}
var yamlStyleFloat = regexp.MustCompile(`^[-+]?(\.[0-9]+|[0-9]+(\.[0-9]*)?)([eE][-+]?[0-9]+)?$`)
func resolve(tag string, in string) (rtag string, out interface{}) {
2024-05-14 13:07:09 +00:00
if !resolvableTag(tag) {
2024-05-14 13:07:09 +00:00
return tag, in
2024-05-14 13:07:09 +00:00
}
defer func() {
2024-05-14 13:07:09 +00:00
switch tag {
2024-05-14 13:07:09 +00:00
case "", rtag, yaml_STR_TAG, yaml_BINARY_TAG:
2024-05-14 13:07:09 +00:00
return
2024-05-14 13:07:09 +00:00
case yaml_FLOAT_TAG:
2024-05-14 13:07:09 +00:00
if rtag == yaml_INT_TAG {
2024-05-14 13:07:09 +00:00
switch v := out.(type) {
2024-05-14 13:07:09 +00:00
case int64:
2024-05-14 13:07:09 +00:00
rtag = yaml_FLOAT_TAG
2024-05-14 13:07:09 +00:00
out = float64(v)
2024-05-14 13:07:09 +00:00
return
2024-05-14 13:07:09 +00:00
case int:
2024-05-14 13:07:09 +00:00
rtag = yaml_FLOAT_TAG
2024-05-14 13:07:09 +00:00
out = float64(v)
2024-05-14 13:07:09 +00:00
return
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
failf("cannot decode %s `%s` as a %s", shortTag(rtag), in, shortTag(tag))
2024-05-14 13:07:09 +00:00
}()
// Any data is accepted as a !!str or !!binary.
2024-05-14 13:07:09 +00:00
// Otherwise, the prefix is enough of a hint about what it might be.
2024-05-14 13:07:09 +00:00
hint := byte('N')
2024-05-14 13:07:09 +00:00
if in != "" {
2024-05-14 13:07:09 +00:00
hint = resolveTable[in[0]]
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
if hint != 0 && tag != yaml_STR_TAG && tag != yaml_BINARY_TAG {
2024-05-14 13:07:09 +00:00
// Handle things we can lookup in a map.
2024-05-14 13:07:09 +00:00
if item, ok := resolveMap[in]; ok {
2024-05-14 13:07:09 +00:00
return item.tag, item.value
2024-05-14 13:07:09 +00:00
}
// Base 60 floats are a bad idea, were dropped in YAML 1.2, and
2024-05-14 13:07:09 +00:00
// are purposefully unsupported here. They're still quoted on
2024-05-14 13:07:09 +00:00
// the way out for compatibility with other parser, though.
switch hint {
2024-05-14 13:07:09 +00:00
case 'M':
2024-05-14 13:07:09 +00:00
// We've already checked the map above.
case '.':
2024-05-14 13:07:09 +00:00
// Not in the map, so maybe a normal float.
2024-05-14 13:07:09 +00:00
floatv, err := strconv.ParseFloat(in, 64)
2024-05-14 13:07:09 +00:00
if err == nil {
2024-05-14 13:07:09 +00:00
return yaml_FLOAT_TAG, floatv
2024-05-14 13:07:09 +00:00
}
case 'D', 'S':
2024-05-14 13:07:09 +00:00
// Int, float, or timestamp.
2024-05-14 13:07:09 +00:00
// Only try values as a timestamp if the value is unquoted or there's an explicit
2024-05-14 13:07:09 +00:00
// !!timestamp tag.
2024-05-14 13:07:09 +00:00
if tag == "" || tag == yaml_TIMESTAMP_TAG {
2024-05-14 13:07:09 +00:00
t, ok := parseTimestamp(in)
2024-05-14 13:07:09 +00:00
if ok {
2024-05-14 13:07:09 +00:00
return yaml_TIMESTAMP_TAG, t
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
}
plain := strings.Replace(in, "_", "", -1)
2024-05-14 13:07:09 +00:00
intv, err := strconv.ParseInt(plain, 0, 64)
2024-05-14 13:07:09 +00:00
if err == nil {
2024-05-14 13:07:09 +00:00
if intv == int64(int(intv)) {
2024-05-14 13:07:09 +00:00
return yaml_INT_TAG, int(intv)
2024-05-14 13:07:09 +00:00
} else {
2024-05-14 13:07:09 +00:00
return yaml_INT_TAG, intv
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
uintv, err := strconv.ParseUint(plain, 0, 64)
2024-05-14 13:07:09 +00:00
if err == nil {
2024-05-14 13:07:09 +00:00
return yaml_INT_TAG, uintv
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
if yamlStyleFloat.MatchString(plain) {
2024-05-14 13:07:09 +00:00
floatv, err := strconv.ParseFloat(plain, 64)
2024-05-14 13:07:09 +00:00
if err == nil {
2024-05-14 13:07:09 +00:00
return yaml_FLOAT_TAG, floatv
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
if strings.HasPrefix(plain, "0b") {
2024-05-14 13:07:09 +00:00
intv, err := strconv.ParseInt(plain[2:], 2, 64)
2024-05-14 13:07:09 +00:00
if err == nil {
2024-05-14 13:07:09 +00:00
if intv == int64(int(intv)) {
2024-05-14 13:07:09 +00:00
return yaml_INT_TAG, int(intv)
2024-05-14 13:07:09 +00:00
} else {
2024-05-14 13:07:09 +00:00
return yaml_INT_TAG, intv
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
uintv, err := strconv.ParseUint(plain[2:], 2, 64)
2024-05-14 13:07:09 +00:00
if err == nil {
2024-05-14 13:07:09 +00:00
return yaml_INT_TAG, uintv
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
} else if strings.HasPrefix(plain, "-0b") {
2024-06-14 08:41:36 +00:00
intv, err := strconv.ParseInt("-"+plain[3:], 2, 64)
2024-05-14 13:07:09 +00:00
if err == nil {
2024-05-14 13:07:09 +00:00
if true || intv == int64(int(intv)) {
2024-05-14 13:07:09 +00:00
return yaml_INT_TAG, int(intv)
2024-05-14 13:07:09 +00:00
} else {
2024-05-14 13:07:09 +00:00
return yaml_INT_TAG, intv
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
default:
2024-05-14 13:07:09 +00:00
panic("resolveTable item not yet handled: " + string(rune(hint)) + " (with " + in + ")")
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
return yaml_STR_TAG, in
2024-05-14 13:07:09 +00:00
}
// encodeBase64 encodes s as base64 that is broken up into multiple lines
2024-05-14 13:07:09 +00:00
// as appropriate for the resulting length.
2024-05-14 13:07:09 +00:00
func encodeBase64(s string) string {
2024-05-14 13:07:09 +00:00
const lineLen = 70
2024-05-14 13:07:09 +00:00
encLen := base64.StdEncoding.EncodedLen(len(s))
2024-05-14 13:07:09 +00:00
lines := encLen/lineLen + 1
2024-05-14 13:07:09 +00:00
buf := make([]byte, encLen*2+lines)
2024-05-14 13:07:09 +00:00
in := buf[0:encLen]
2024-05-14 13:07:09 +00:00
out := buf[encLen:]
2024-05-14 13:07:09 +00:00
base64.StdEncoding.Encode(in, []byte(s))
2024-05-14 13:07:09 +00:00
k := 0
2024-05-14 13:07:09 +00:00
for i := 0; i < len(in); i += lineLen {
2024-05-14 13:07:09 +00:00
j := i + lineLen
2024-05-14 13:07:09 +00:00
if j > len(in) {
2024-05-14 13:07:09 +00:00
j = len(in)
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
k += copy(out[k:], in[i:j])
2024-05-14 13:07:09 +00:00
if lines > 1 {
2024-05-14 13:07:09 +00:00
out[k] = '\n'
2024-05-14 13:07:09 +00:00
k++
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
return string(out[:k])
2024-05-14 13:07:09 +00:00
}
// This is a subset of the formats allowed by the regular expression
2024-05-14 13:07:09 +00:00
// defined at http://yaml.org/type/timestamp.html.
2024-05-14 13:07:09 +00:00
var allowedTimestampFormats = []string{
2024-05-14 13:07:09 +00:00
"2006-1-2T15:4:5.999999999Z07:00", // RCF3339Nano with short date fields.
2024-05-14 13:07:09 +00:00
"2006-1-2t15:4:5.999999999Z07:00", // RFC3339Nano with short date fields and lower-case "t".
"2006-1-2 15:4:5.999999999", // space separated with no time zone
"2006-1-2", // date only
2024-05-14 13:07:09 +00:00
// Notable exception: time.Parse cannot handle: "2001-12-14 21:59:43.10 -5"
2024-05-14 13:07:09 +00:00
// from the set of examples.
2024-05-14 13:07:09 +00:00
}
// parseTimestamp parses s as a timestamp string and
2024-05-14 13:07:09 +00:00
// returns the timestamp and reports whether it succeeded.
2024-05-14 13:07:09 +00:00
// Timestamp formats are defined at http://yaml.org/type/timestamp.html
2024-05-14 13:07:09 +00:00
func parseTimestamp(s string) (time.Time, bool) {
2024-05-14 13:07:09 +00:00
// TODO write code to check all the formats supported by
2024-05-14 13:07:09 +00:00
// http://yaml.org/type/timestamp.html instead of using time.Parse.
// Quick check: all date formats start with YYYY-.
2024-05-14 13:07:09 +00:00
i := 0
2024-05-14 13:07:09 +00:00
for ; i < len(s); i++ {
2024-05-14 13:07:09 +00:00
if c := s[i]; c < '0' || c > '9' {
2024-05-14 13:07:09 +00:00
break
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
if i != 4 || i == len(s) || s[i] != '-' {
2024-05-14 13:07:09 +00:00
return time.Time{}, false
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
for _, format := range allowedTimestampFormats {
2024-05-14 13:07:09 +00:00
if t, err := time.Parse(format, s); err == nil {
2024-05-14 13:07:09 +00:00
return t, true
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
return time.Time{}, false
2024-05-14 13:07:09 +00:00
}