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

584 lines
8.5 KiB
Go
Raw Normal View History

2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// Copyright (c) 2011-2019 Canonical Ltd
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// Licensed under the Apache License, Version 2.0 (the "License");
2024-02-18 10:42:21 +00:00
// you may not use this file except in compliance with the License.
2024-02-18 10:42:21 +00:00
// You may obtain a copy of the License at
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// http://www.apache.org/licenses/LICENSE-2.0
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// Unless required by applicable law or agreed to in writing, software
2024-02-18 10:42:21 +00:00
// distributed under the License is distributed on an "AS IS" BASIS,
2024-02-18 10:42:21 +00:00
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
2024-02-18 10:42:21 +00:00
// See the License for the specific language governing permissions and
2024-02-18 10:42:21 +00:00
// limitations under the License.
package yaml
import (
"encoding/base64"
"math"
"regexp"
"strconv"
"strings"
"time"
)
type resolveMapItem struct {
value interface{}
tag string
2024-02-18 10:42:21 +00:00
}
var resolveTable = make([]byte, 256)
2024-02-18 10:42:21 +00:00
var resolveMap = make(map[string]resolveMapItem)
func init() {
2024-02-18 10:42:21 +00:00
t := resolveTable
2024-02-18 10:42:21 +00:00
t[int('+')] = 'S' // Sign
2024-02-18 10:42:21 +00:00
t[int('-')] = 'S'
2024-02-18 10:42:21 +00:00
for _, c := range "0123456789" {
2024-02-18 10:42:21 +00:00
t[int(c)] = 'D' // Digit
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
for _, c := range "yYnNtTfFoO~" {
2024-02-18 10:42:21 +00:00
t[int(c)] = 'M' // In map
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
t[int('.')] = '.' // Float (potentially in map)
var resolveMapList = []struct {
v interface{}
2024-02-18 10:42:21 +00:00
tag string
l []string
2024-02-18 10:42:21 +00:00
}{
2024-02-18 10:42:21 +00:00
{true, boolTag, []string{"true", "True", "TRUE"}},
2024-02-18 10:42:21 +00:00
{false, boolTag, []string{"false", "False", "FALSE"}},
2024-02-18 10:42:21 +00:00
{nil, nullTag, []string{"", "~", "null", "Null", "NULL"}},
2024-02-18 10:42:21 +00:00
{math.NaN(), floatTag, []string{".nan", ".NaN", ".NAN"}},
2024-02-18 10:42:21 +00:00
{math.Inf(+1), floatTag, []string{".inf", ".Inf", ".INF"}},
2024-02-18 10:42:21 +00:00
{math.Inf(+1), floatTag, []string{"+.inf", "+.Inf", "+.INF"}},
2024-02-18 10:42:21 +00:00
{math.Inf(-1), floatTag, []string{"-.inf", "-.Inf", "-.INF"}},
2024-02-18 10:42:21 +00:00
{"<<", mergeTag, []string{"<<"}},
}
m := resolveMap
2024-02-18 10:42:21 +00:00
for _, item := range resolveMapList {
2024-02-18 10:42:21 +00:00
for _, s := range item.l {
2024-02-18 10:42:21 +00:00
m[s] = resolveMapItem{item.v, item.tag}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
const (
nullTag = "!!null"
boolTag = "!!bool"
strTag = "!!str"
intTag = "!!int"
floatTag = "!!float"
2024-02-18 10:42:21 +00:00
timestampTag = "!!timestamp"
seqTag = "!!seq"
mapTag = "!!map"
binaryTag = "!!binary"
mergeTag = "!!merge"
2024-02-18 10:42:21 +00:00
)
var longTags = make(map[string]string)
2024-02-18 10:42:21 +00:00
var shortTags = make(map[string]string)
func init() {
2024-02-18 10:42:21 +00:00
for _, stag := range []string{nullTag, boolTag, strTag, intTag, floatTag, timestampTag, seqTag, mapTag, binaryTag, mergeTag} {
2024-02-18 10:42:21 +00:00
ltag := longTag(stag)
2024-02-18 10:42:21 +00:00
longTags[stag] = ltag
2024-02-18 10:42:21 +00:00
shortTags[ltag] = stag
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
const longTagPrefix = "tag:yaml.org,2002:"
func shortTag(tag string) string {
2024-02-18 10:42:21 +00:00
if strings.HasPrefix(tag, longTagPrefix) {
2024-02-18 10:42:21 +00:00
if stag, ok := shortTags[tag]; ok {
2024-02-18 10:42:21 +00:00
return stag
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return "!!" + tag[len(longTagPrefix):]
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return tag
2024-02-18 10:42:21 +00:00
}
func longTag(tag string) string {
2024-02-18 10:42:21 +00:00
if strings.HasPrefix(tag, "!!") {
2024-02-18 10:42:21 +00:00
if ltag, ok := longTags[tag]; ok {
2024-02-18 10:42:21 +00:00
return ltag
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return longTagPrefix + tag[2:]
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return tag
2024-02-18 10:42:21 +00:00
}
func resolvableTag(tag string) bool {
2024-02-18 10:42:21 +00:00
switch tag {
2024-02-18 10:42:21 +00:00
case "", strTag, boolTag, intTag, floatTag, nullTag, timestampTag:
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +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-02-18 10:42:21 +00:00
tag = shortTag(tag)
2024-02-18 10:42:21 +00:00
if !resolvableTag(tag) {
2024-02-18 10:42:21 +00:00
return tag, in
2024-02-18 10:42:21 +00:00
}
defer func() {
2024-02-18 10:42:21 +00:00
switch tag {
2024-02-18 10:42:21 +00:00
case "", rtag, strTag, binaryTag:
2024-02-18 10:42:21 +00:00
return
2024-02-18 10:42:21 +00:00
case floatTag:
2024-02-18 10:42:21 +00:00
if rtag == intTag {
2024-02-18 10:42:21 +00:00
switch v := out.(type) {
2024-02-18 10:42:21 +00:00
case int64:
2024-02-18 10:42:21 +00:00
rtag = floatTag
2024-02-18 10:42:21 +00:00
out = float64(v)
2024-02-18 10:42:21 +00:00
return
2024-02-18 10:42:21 +00:00
case int:
2024-02-18 10:42:21 +00:00
rtag = floatTag
2024-02-18 10:42:21 +00:00
out = float64(v)
2024-02-18 10:42:21 +00:00
return
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
failf("cannot decode %s `%s` as a %s", shortTag(rtag), in, shortTag(tag))
2024-02-18 10:42:21 +00:00
}()
// Any data is accepted as a !!str or !!binary.
2024-02-18 10:42:21 +00:00
// Otherwise, the prefix is enough of a hint about what it might be.
2024-02-18 10:42:21 +00:00
hint := byte('N')
2024-02-18 10:42:21 +00:00
if in != "" {
2024-02-18 10:42:21 +00:00
hint = resolveTable[in[0]]
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if hint != 0 && tag != strTag && tag != binaryTag {
2024-02-18 10:42:21 +00:00
// Handle things we can lookup in a map.
2024-02-18 10:42:21 +00:00
if item, ok := resolveMap[in]; ok {
2024-02-18 10:42:21 +00:00
return item.tag, item.value
2024-02-18 10:42:21 +00:00
}
// Base 60 floats are a bad idea, were dropped in YAML 1.2, and
2024-02-18 10:42:21 +00:00
// are purposefully unsupported here. They're still quoted on
2024-02-18 10:42:21 +00:00
// the way out for compatibility with other parser, though.
switch hint {
2024-02-18 10:42:21 +00:00
case 'M':
2024-02-18 10:42:21 +00:00
// We've already checked the map above.
case '.':
2024-02-18 10:42:21 +00:00
// Not in the map, so maybe a normal float.
2024-02-18 10:42:21 +00:00
floatv, err := strconv.ParseFloat(in, 64)
2024-02-18 10:42:21 +00:00
if err == nil {
2024-02-18 10:42:21 +00:00
return floatTag, floatv
2024-02-18 10:42:21 +00:00
}
case 'D', 'S':
2024-02-18 10:42:21 +00:00
// Int, float, or timestamp.
2024-02-18 10:42:21 +00:00
// Only try values as a timestamp if the value is unquoted or there's an explicit
2024-02-18 10:42:21 +00:00
// !!timestamp tag.
2024-02-18 10:42:21 +00:00
if tag == "" || tag == timestampTag {
2024-02-18 10:42:21 +00:00
t, ok := parseTimestamp(in)
2024-02-18 10:42:21 +00:00
if ok {
2024-02-18 10:42:21 +00:00
return timestampTag, t
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
plain := strings.Replace(in, "_", "", -1)
2024-02-18 10:42:21 +00:00
intv, err := strconv.ParseInt(plain, 0, 64)
2024-02-18 10:42:21 +00:00
if err == nil {
2024-02-18 10:42:21 +00:00
if intv == int64(int(intv)) {
2024-02-18 10:42:21 +00:00
return intTag, int(intv)
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
return intTag, intv
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
uintv, err := strconv.ParseUint(plain, 0, 64)
2024-02-18 10:42:21 +00:00
if err == nil {
2024-02-18 10:42:21 +00:00
return intTag, uintv
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if yamlStyleFloat.MatchString(plain) {
2024-02-18 10:42:21 +00:00
floatv, err := strconv.ParseFloat(plain, 64)
2024-02-18 10:42:21 +00:00
if err == nil {
2024-02-18 10:42:21 +00:00
return floatTag, floatv
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if strings.HasPrefix(plain, "0b") {
2024-02-18 10:42:21 +00:00
intv, err := strconv.ParseInt(plain[2:], 2, 64)
2024-02-18 10:42:21 +00:00
if err == nil {
2024-02-18 10:42:21 +00:00
if intv == int64(int(intv)) {
2024-02-18 10:42:21 +00:00
return intTag, int(intv)
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
return intTag, intv
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
uintv, err := strconv.ParseUint(plain[2:], 2, 64)
2024-02-18 10:42:21 +00:00
if err == nil {
2024-02-18 10:42:21 +00:00
return intTag, uintv
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
} else if strings.HasPrefix(plain, "-0b") {
2024-02-18 10:42:21 +00:00
intv, err := strconv.ParseInt("-"+plain[3:], 2, 64)
2024-02-18 10:42:21 +00:00
if err == nil {
2024-02-18 10:42:21 +00:00
if true || intv == int64(int(intv)) {
2024-02-18 10:42:21 +00:00
return intTag, int(intv)
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
return intTag, intv
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
// Octals as introduced in version 1.2 of the spec.
2024-02-18 10:42:21 +00:00
// Octals from the 1.1 spec, spelled as 0777, are still
2024-02-18 10:42:21 +00:00
// decoded by default in v3 as well for compatibility.
2024-02-18 10:42:21 +00:00
// May be dropped in v4 depending on how usage evolves.
2024-02-18 10:42:21 +00:00
if strings.HasPrefix(plain, "0o") {
2024-02-18 10:42:21 +00:00
intv, err := strconv.ParseInt(plain[2:], 8, 64)
2024-02-18 10:42:21 +00:00
if err == nil {
2024-02-18 10:42:21 +00:00
if intv == int64(int(intv)) {
2024-02-18 10:42:21 +00:00
return intTag, int(intv)
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
return intTag, intv
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
uintv, err := strconv.ParseUint(plain[2:], 8, 64)
2024-02-18 10:42:21 +00:00
if err == nil {
2024-02-18 10:42:21 +00:00
return intTag, uintv
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
} else if strings.HasPrefix(plain, "-0o") {
2024-02-18 10:42:21 +00:00
intv, err := strconv.ParseInt("-"+plain[3:], 8, 64)
2024-02-18 10:42:21 +00:00
if err == nil {
2024-02-18 10:42:21 +00:00
if true || intv == int64(int(intv)) {
2024-02-18 10:42:21 +00:00
return intTag, int(intv)
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
return intTag, intv
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
default:
2024-02-18 10:42:21 +00:00
panic("internal error: missing handler for resolver table: " + string(rune(hint)) + " (with " + in + ")")
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return strTag, in
2024-02-18 10:42:21 +00:00
}
// encodeBase64 encodes s as base64 that is broken up into multiple lines
2024-02-18 10:42:21 +00:00
// as appropriate for the resulting length.
2024-02-18 10:42:21 +00:00
func encodeBase64(s string) string {
2024-02-18 10:42:21 +00:00
const lineLen = 70
2024-02-18 10:42:21 +00:00
encLen := base64.StdEncoding.EncodedLen(len(s))
2024-02-18 10:42:21 +00:00
lines := encLen/lineLen + 1
2024-02-18 10:42:21 +00:00
buf := make([]byte, encLen*2+lines)
2024-02-18 10:42:21 +00:00
in := buf[0:encLen]
2024-02-18 10:42:21 +00:00
out := buf[encLen:]
2024-02-18 10:42:21 +00:00
base64.StdEncoding.Encode(in, []byte(s))
2024-02-18 10:42:21 +00:00
k := 0
2024-02-18 10:42:21 +00:00
for i := 0; i < len(in); i += lineLen {
2024-02-18 10:42:21 +00:00
j := i + lineLen
2024-02-18 10:42:21 +00:00
if j > len(in) {
2024-02-18 10:42:21 +00:00
j = len(in)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
k += copy(out[k:], in[i:j])
2024-02-18 10:42:21 +00:00
if lines > 1 {
2024-02-18 10:42:21 +00:00
out[k] = '\n'
2024-02-18 10:42:21 +00:00
k++
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return string(out[:k])
2024-02-18 10:42:21 +00:00
}
// This is a subset of the formats allowed by the regular expression
2024-02-18 10:42:21 +00:00
// defined at http://yaml.org/type/timestamp.html.
2024-02-18 10:42:21 +00:00
var allowedTimestampFormats = []string{
2024-02-18 10:42:21 +00:00
"2006-1-2T15:4:5.999999999Z07:00", // RCF3339Nano with short date fields.
2024-02-18 10:42:21 +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-02-18 10:42:21 +00:00
// Notable exception: time.Parse cannot handle: "2001-12-14 21:59:43.10 -5"
2024-02-18 10:42:21 +00:00
// from the set of examples.
2024-02-18 10:42:21 +00:00
}
// parseTimestamp parses s as a timestamp string and
2024-02-18 10:42:21 +00:00
// returns the timestamp and reports whether it succeeded.
2024-02-18 10:42:21 +00:00
// Timestamp formats are defined at http://yaml.org/type/timestamp.html
2024-02-18 10:42:21 +00:00
func parseTimestamp(s string) (time.Time, bool) {
2024-02-18 10:42:21 +00:00
// TODO write code to check all the formats supported by
2024-02-18 10:42:21 +00:00
// http://yaml.org/type/timestamp.html instead of using time.Parse.
// Quick check: all date formats start with YYYY-.
2024-02-18 10:42:21 +00:00
i := 0
2024-02-18 10:42:21 +00:00
for ; i < len(s); i++ {
2024-02-18 10:42:21 +00:00
if c := s[i]; c < '0' || c > '9' {
2024-02-18 10:42:21 +00:00
break
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if i != 4 || i == len(s) || s[i] != '-' {
2024-02-18 10:42:21 +00:00
return time.Time{}, false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
for _, format := range allowedTimestampFormats {
2024-02-18 10:42:21 +00:00
if t, err := time.Parse(format, s); err == nil {
2024-02-18 10:42:21 +00:00
return t, true
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return time.Time{}, false
2024-02-18 10:42:21 +00:00
}