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

1848 lines
25 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"
"encoding/base64"
"fmt"
"io"
"math"
"reflect"
"strconv"
"time"
)
// ----------------------------------------------------------------------------
2024-02-18 10:42:21 +00:00
// Parser, produces a node tree out of a libyaml event stream.
type parser struct {
parser yaml_parser_t
event yaml_event_t
doc *Node
anchors map[string]*Node
2024-02-18 10:42:21 +00:00
doneInit bool
2024-02-18 10:42:21 +00:00
textless bool
}
func newParser(b []byte) *parser {
2024-02-18 10:42:21 +00:00
p := parser{}
2024-02-18 10:42:21 +00:00
if !yaml_parser_initialize(&p.parser) {
2024-02-18 10:42:21 +00:00
panic("failed to initialize YAML emitter")
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if len(b) == 0 {
2024-02-18 10:42:21 +00:00
b = []byte{'\n'}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
yaml_parser_set_input_string(&p.parser, b)
2024-02-18 10:42:21 +00:00
return &p
2024-02-18 10:42:21 +00:00
}
func newParserFromReader(r io.Reader) *parser {
2024-02-18 10:42:21 +00:00
p := parser{}
2024-02-18 10:42:21 +00:00
if !yaml_parser_initialize(&p.parser) {
2024-02-18 10:42:21 +00:00
panic("failed to initialize YAML emitter")
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
yaml_parser_set_input_reader(&p.parser, r)
2024-02-18 10:42:21 +00:00
return &p
2024-02-18 10:42:21 +00:00
}
func (p *parser) init() {
2024-02-18 10:42:21 +00:00
if p.doneInit {
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
p.anchors = make(map[string]*Node)
2024-02-18 10:42:21 +00:00
p.expect(yaml_STREAM_START_EVENT)
2024-02-18 10:42:21 +00:00
p.doneInit = true
2024-02-18 10:42:21 +00:00
}
func (p *parser) destroy() {
2024-02-18 10:42:21 +00:00
if p.event.typ != yaml_NO_EVENT {
2024-02-18 10:42:21 +00:00
yaml_event_delete(&p.event)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
yaml_parser_delete(&p.parser)
2024-02-18 10:42:21 +00:00
}
// expect consumes an event from the event stream and
2024-02-18 10:42:21 +00:00
// checks that it's of the expected type.
2024-02-18 10:42:21 +00:00
func (p *parser) expect(e yaml_event_type_t) {
2024-02-18 10:42:21 +00:00
if p.event.typ == yaml_NO_EVENT {
2024-02-18 10:42:21 +00:00
if !yaml_parser_parse(&p.parser, &p.event) {
2024-02-18 10:42:21 +00:00
p.fail()
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 p.event.typ == yaml_STREAM_END_EVENT {
2024-02-18 10:42:21 +00:00
failf("attempted to go past the end of stream; corrupted value?")
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if p.event.typ != e {
2024-02-18 10:42:21 +00:00
p.parser.problem = fmt.Sprintf("expected %s event but got %s", e, p.event.typ)
2024-02-18 10:42:21 +00:00
p.fail()
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
yaml_event_delete(&p.event)
2024-02-18 10:42:21 +00:00
p.event.typ = yaml_NO_EVENT
2024-02-18 10:42:21 +00:00
}
// peek peeks at the next event in the event stream,
2024-02-18 10:42:21 +00:00
// puts the results into p.event and returns the event type.
2024-02-18 10:42:21 +00:00
func (p *parser) peek() yaml_event_type_t {
2024-02-18 10:42:21 +00:00
if p.event.typ != yaml_NO_EVENT {
2024-02-18 10:42:21 +00:00
return p.event.typ
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
// It's curious choice from the underlying API to generally return a
2024-02-18 10:42:21 +00:00
// positive result on success, but on this case return true in an error
2024-02-18 10:42:21 +00:00
// scenario. This was the source of bugs in the past (issue #666).
2024-02-18 10:42:21 +00:00
if !yaml_parser_parse(&p.parser, &p.event) || p.parser.error != yaml_NO_ERROR {
2024-02-18 10:42:21 +00:00
p.fail()
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return p.event.typ
2024-02-18 10:42:21 +00:00
}
func (p *parser) fail() {
2024-02-18 10:42:21 +00:00
var where string
2024-02-18 10:42:21 +00:00
var line int
2024-02-18 10:42:21 +00:00
if p.parser.context_mark.line != 0 {
2024-02-18 10:42:21 +00:00
line = p.parser.context_mark.line
2024-02-18 10:42:21 +00:00
// Scanner errors don't iterate line before returning error
2024-02-18 10:42:21 +00:00
if p.parser.error == yaml_SCANNER_ERROR {
2024-02-18 10:42:21 +00:00
line++
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
} else if p.parser.problem_mark.line != 0 {
2024-02-18 10:42:21 +00:00
line = p.parser.problem_mark.line
2024-02-18 10:42:21 +00:00
// Scanner errors don't iterate line before returning error
2024-02-18 10:42:21 +00:00
if p.parser.error == yaml_SCANNER_ERROR {
2024-02-18 10:42:21 +00:00
line++
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 line != 0 {
2024-02-18 10:42:21 +00:00
where = "line " + strconv.Itoa(line) + ": "
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
var msg string
2024-02-18 10:42:21 +00:00
if len(p.parser.problem) > 0 {
2024-02-18 10:42:21 +00:00
msg = p.parser.problem
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
msg = "unknown problem parsing YAML content"
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
failf("%s%s", where, msg)
2024-02-18 10:42:21 +00:00
}
func (p *parser) anchor(n *Node, anchor []byte) {
2024-02-18 10:42:21 +00:00
if anchor != nil {
2024-02-18 10:42:21 +00:00
n.Anchor = string(anchor)
2024-02-18 10:42:21 +00:00
p.anchors[n.Anchor] = n
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
func (p *parser) parse() *Node {
2024-02-18 10:42:21 +00:00
p.init()
2024-02-18 10:42:21 +00:00
switch p.peek() {
2024-02-18 10:42:21 +00:00
case yaml_SCALAR_EVENT:
2024-02-18 10:42:21 +00:00
return p.scalar()
2024-02-18 10:42:21 +00:00
case yaml_ALIAS_EVENT:
2024-02-18 10:42:21 +00:00
return p.alias()
2024-02-18 10:42:21 +00:00
case yaml_MAPPING_START_EVENT:
2024-02-18 10:42:21 +00:00
return p.mapping()
2024-02-18 10:42:21 +00:00
case yaml_SEQUENCE_START_EVENT:
2024-02-18 10:42:21 +00:00
return p.sequence()
2024-02-18 10:42:21 +00:00
case yaml_DOCUMENT_START_EVENT:
2024-02-18 10:42:21 +00:00
return p.document()
2024-02-18 10:42:21 +00:00
case yaml_STREAM_END_EVENT:
2024-02-18 10:42:21 +00:00
// Happens when attempting to decode an empty buffer.
2024-02-18 10:42:21 +00:00
return nil
2024-02-18 10:42:21 +00:00
case yaml_TAIL_COMMENT_EVENT:
2024-02-18 10:42:21 +00:00
panic("internal error: unexpected tail comment event (please report)")
2024-02-18 10:42:21 +00:00
default:
2024-02-18 10:42:21 +00:00
panic("internal error: attempted to parse unknown event (please report): " + p.event.typ.String())
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
func (p *parser) node(kind Kind, defaultTag, tag, value string) *Node {
2024-02-18 10:42:21 +00:00
var style Style
2024-02-18 10:42:21 +00:00
if tag != "" && tag != "!" {
2024-02-18 10:42:21 +00:00
tag = shortTag(tag)
2024-02-18 10:42:21 +00:00
style = TaggedStyle
2024-02-18 10:42:21 +00:00
} else if defaultTag != "" {
2024-02-18 10:42:21 +00:00
tag = defaultTag
2024-02-18 10:42:21 +00:00
} else if kind == ScalarNode {
2024-02-18 10:42:21 +00:00
tag, _ = resolve("", value)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
n := &Node{
Kind: kind,
Tag: tag,
2024-02-18 10:42:21 +00:00
Value: value,
2024-02-18 10:42:21 +00:00
Style: style,
}
2024-02-18 10:42:21 +00:00
if !p.textless {
2024-02-18 10:42:21 +00:00
n.Line = p.event.start_mark.line + 1
2024-02-18 10:42:21 +00:00
n.Column = p.event.start_mark.column + 1
2024-02-18 10:42:21 +00:00
n.HeadComment = string(p.event.head_comment)
2024-02-18 10:42:21 +00:00
n.LineComment = string(p.event.line_comment)
2024-02-18 10:42:21 +00:00
n.FootComment = string(p.event.foot_comment)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return n
2024-02-18 10:42:21 +00:00
}
func (p *parser) parseChild(parent *Node) *Node {
2024-02-18 10:42:21 +00:00
child := p.parse()
2024-02-18 10:42:21 +00:00
parent.Content = append(parent.Content, child)
2024-02-18 10:42:21 +00:00
return child
2024-02-18 10:42:21 +00:00
}
func (p *parser) document() *Node {
2024-02-18 10:42:21 +00:00
n := p.node(DocumentNode, "", "", "")
2024-02-18 10:42:21 +00:00
p.doc = n
2024-02-18 10:42:21 +00:00
p.expect(yaml_DOCUMENT_START_EVENT)
2024-02-18 10:42:21 +00:00
p.parseChild(n)
2024-02-18 10:42:21 +00:00
if p.peek() == yaml_DOCUMENT_END_EVENT {
2024-02-18 10:42:21 +00:00
n.FootComment = string(p.event.foot_comment)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
p.expect(yaml_DOCUMENT_END_EVENT)
2024-02-18 10:42:21 +00:00
return n
2024-02-18 10:42:21 +00:00
}
func (p *parser) alias() *Node {
2024-02-18 10:42:21 +00:00
n := p.node(AliasNode, "", "", string(p.event.anchor))
2024-02-18 10:42:21 +00:00
n.Alias = p.anchors[n.Value]
2024-02-18 10:42:21 +00:00
if n.Alias == nil {
2024-02-18 10:42:21 +00:00
failf("unknown anchor '%s' referenced", n.Value)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
p.expect(yaml_ALIAS_EVENT)
2024-02-18 10:42:21 +00:00
return n
2024-02-18 10:42:21 +00:00
}
func (p *parser) scalar() *Node {
2024-02-18 10:42:21 +00:00
var parsedStyle = p.event.scalar_style()
2024-02-18 10:42:21 +00:00
var nodeStyle Style
2024-02-18 10:42:21 +00:00
switch {
2024-02-18 10:42:21 +00:00
case parsedStyle&yaml_DOUBLE_QUOTED_SCALAR_STYLE != 0:
2024-02-18 10:42:21 +00:00
nodeStyle = DoubleQuotedStyle
2024-02-18 10:42:21 +00:00
case parsedStyle&yaml_SINGLE_QUOTED_SCALAR_STYLE != 0:
2024-02-18 10:42:21 +00:00
nodeStyle = SingleQuotedStyle
2024-02-18 10:42:21 +00:00
case parsedStyle&yaml_LITERAL_SCALAR_STYLE != 0:
2024-02-18 10:42:21 +00:00
nodeStyle = LiteralStyle
2024-02-18 10:42:21 +00:00
case parsedStyle&yaml_FOLDED_SCALAR_STYLE != 0:
2024-02-18 10:42:21 +00:00
nodeStyle = FoldedStyle
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
var nodeValue = string(p.event.value)
2024-02-18 10:42:21 +00:00
var nodeTag = string(p.event.tag)
2024-02-18 10:42:21 +00:00
var defaultTag string
2024-02-18 10:42:21 +00:00
if nodeStyle == 0 {
2024-02-18 10:42:21 +00:00
if nodeValue == "<<" {
2024-02-18 10:42:21 +00:00
defaultTag = mergeTag
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
defaultTag = strTag
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
n := p.node(ScalarNode, defaultTag, nodeTag, nodeValue)
2024-02-18 10:42:21 +00:00
n.Style |= nodeStyle
2024-02-18 10:42:21 +00:00
p.anchor(n, p.event.anchor)
2024-02-18 10:42:21 +00:00
p.expect(yaml_SCALAR_EVENT)
2024-02-18 10:42:21 +00:00
return n
2024-02-18 10:42:21 +00:00
}
func (p *parser) sequence() *Node {
2024-02-18 10:42:21 +00:00
n := p.node(SequenceNode, seqTag, string(p.event.tag), "")
2024-02-18 10:42:21 +00:00
if p.event.sequence_style()&yaml_FLOW_SEQUENCE_STYLE != 0 {
2024-02-18 10:42:21 +00:00
n.Style |= FlowStyle
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
p.anchor(n, p.event.anchor)
2024-02-18 10:42:21 +00:00
p.expect(yaml_SEQUENCE_START_EVENT)
2024-02-18 10:42:21 +00:00
for p.peek() != yaml_SEQUENCE_END_EVENT {
2024-02-18 10:42:21 +00:00
p.parseChild(n)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
n.LineComment = string(p.event.line_comment)
2024-02-18 10:42:21 +00:00
n.FootComment = string(p.event.foot_comment)
2024-02-18 10:42:21 +00:00
p.expect(yaml_SEQUENCE_END_EVENT)
2024-02-18 10:42:21 +00:00
return n
2024-02-18 10:42:21 +00:00
}
func (p *parser) mapping() *Node {
2024-02-18 10:42:21 +00:00
n := p.node(MappingNode, mapTag, string(p.event.tag), "")
2024-02-18 10:42:21 +00:00
block := true
2024-02-18 10:42:21 +00:00
if p.event.mapping_style()&yaml_FLOW_MAPPING_STYLE != 0 {
2024-02-18 10:42:21 +00:00
block = false
2024-02-18 10:42:21 +00:00
n.Style |= FlowStyle
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
p.anchor(n, p.event.anchor)
2024-02-18 10:42:21 +00:00
p.expect(yaml_MAPPING_START_EVENT)
2024-02-18 10:42:21 +00:00
for p.peek() != yaml_MAPPING_END_EVENT {
2024-02-18 10:42:21 +00:00
k := p.parseChild(n)
2024-02-18 10:42:21 +00:00
if block && k.FootComment != "" {
2024-02-18 10:42:21 +00:00
// Must be a foot comment for the prior value when being dedented.
2024-02-18 10:42:21 +00:00
if len(n.Content) > 2 {
2024-02-18 10:42:21 +00:00
n.Content[len(n.Content)-3].FootComment = k.FootComment
2024-02-18 10:42:21 +00:00
k.FootComment = ""
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
v := p.parseChild(n)
2024-02-18 10:42:21 +00:00
if k.FootComment == "" && v.FootComment != "" {
2024-02-18 10:42:21 +00:00
k.FootComment = v.FootComment
2024-02-18 10:42:21 +00:00
v.FootComment = ""
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if p.peek() == yaml_TAIL_COMMENT_EVENT {
2024-02-18 10:42:21 +00:00
if k.FootComment == "" {
2024-02-18 10:42:21 +00:00
k.FootComment = string(p.event.foot_comment)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
p.expect(yaml_TAIL_COMMENT_EVENT)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
n.LineComment = string(p.event.line_comment)
2024-02-18 10:42:21 +00:00
n.FootComment = string(p.event.foot_comment)
2024-02-18 10:42:21 +00:00
if n.Style&FlowStyle == 0 && n.FootComment != "" && len(n.Content) > 1 {
2024-02-18 10:42:21 +00:00
n.Content[len(n.Content)-2].FootComment = n.FootComment
2024-02-18 10:42:21 +00:00
n.FootComment = ""
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
p.expect(yaml_MAPPING_END_EVENT)
2024-02-18 10:42:21 +00:00
return n
2024-02-18 10:42:21 +00:00
}
// ----------------------------------------------------------------------------
2024-02-18 10:42:21 +00:00
// Decoder, unmarshals a node into a provided value.
type decoder struct {
doc *Node
2024-02-18 10:42:21 +00:00
aliases map[*Node]bool
2024-02-18 10:42:21 +00:00
terrors []string
stringMapType reflect.Type
2024-02-18 10:42:21 +00:00
generalMapType reflect.Type
knownFields bool
uniqueKeys bool
2024-02-18 10:42:21 +00:00
decodeCount int
aliasCount int
aliasDepth int
2024-02-18 10:42:21 +00:00
mergedFields map[interface{}]bool
}
var (
nodeType = reflect.TypeOf(Node{})
durationType = reflect.TypeOf(time.Duration(0))
stringMapType = reflect.TypeOf(map[string]interface{}{})
2024-02-18 10:42:21 +00:00
generalMapType = reflect.TypeOf(map[interface{}]interface{}{})
ifaceType = generalMapType.Elem()
timeType = reflect.TypeOf(time.Time{})
ptrTimeType = reflect.TypeOf(&time.Time{})
2024-02-18 10:42:21 +00:00
)
func newDecoder() *decoder {
2024-02-18 10:42:21 +00:00
d := &decoder{
stringMapType: stringMapType,
2024-02-18 10:42:21 +00:00
generalMapType: generalMapType,
uniqueKeys: true,
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
d.aliases = make(map[*Node]bool)
2024-02-18 10:42:21 +00:00
return d
2024-02-18 10:42:21 +00:00
}
func (d *decoder) terror(n *Node, tag string, out reflect.Value) {
2024-02-18 10:42:21 +00:00
if n.Tag != "" {
2024-02-18 10:42:21 +00:00
tag = n.Tag
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
value := n.Value
2024-02-18 10:42:21 +00:00
if tag != seqTag && tag != mapTag {
2024-02-18 10:42:21 +00:00
if len(value) > 10 {
2024-02-18 10:42:21 +00:00
value = " `" + value[:7] + "...`"
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
value = " `" + value + "`"
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
d.terrors = append(d.terrors, fmt.Sprintf("line %d: cannot unmarshal %s%s into %s", n.Line, shortTag(tag), value, out.Type()))
2024-02-18 10:42:21 +00:00
}
func (d *decoder) callUnmarshaler(n *Node, u Unmarshaler) (good bool) {
2024-02-18 10:42:21 +00:00
err := u.UnmarshalYAML(n)
2024-02-18 10:42:21 +00:00
if e, ok := err.(*TypeError); ok {
2024-02-18 10:42:21 +00:00
d.terrors = append(d.terrors, e.Errors...)
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
fail(err)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
func (d *decoder) callObsoleteUnmarshaler(n *Node, u obsoleteUnmarshaler) (good bool) {
2024-02-18 10:42:21 +00:00
terrlen := len(d.terrors)
2024-02-18 10:42:21 +00:00
err := u.UnmarshalYAML(func(v interface{}) (err error) {
2024-02-18 10:42:21 +00:00
defer handleErr(&err)
2024-02-18 10:42:21 +00:00
d.unmarshal(n, reflect.ValueOf(v))
2024-02-18 10:42:21 +00:00
if len(d.terrors) > terrlen {
2024-02-18 10:42:21 +00:00
issues := d.terrors[terrlen:]
2024-02-18 10:42:21 +00:00
d.terrors = d.terrors[:terrlen]
2024-02-18 10:42:21 +00:00
return &TypeError{issues}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return nil
2024-02-18 10:42:21 +00:00
})
2024-02-18 10:42:21 +00:00
if e, ok := err.(*TypeError); ok {
2024-02-18 10:42:21 +00:00
d.terrors = append(d.terrors, e.Errors...)
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
fail(err)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
// d.prepare initializes and dereferences pointers and calls UnmarshalYAML
2024-02-18 10:42:21 +00:00
// if a value is found to implement it.
2024-02-18 10:42:21 +00:00
// It returns the initialized and dereferenced out value, whether
2024-02-18 10:42:21 +00:00
// unmarshalling was already done by UnmarshalYAML, and if so whether
2024-02-18 10:42:21 +00:00
// its types unmarshalled appropriately.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// If n holds a null value, prepare returns before doing anything.
2024-02-18 10:42:21 +00:00
func (d *decoder) prepare(n *Node, out reflect.Value) (newout reflect.Value, unmarshaled, good bool) {
2024-02-18 10:42:21 +00:00
if n.ShortTag() == nullTag {
2024-02-18 10:42:21 +00:00
return out, false, false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
again := true
2024-02-18 10:42:21 +00:00
for again {
2024-02-18 10:42:21 +00:00
again = false
2024-02-18 10:42:21 +00:00
if out.Kind() == reflect.Ptr {
2024-02-18 10:42:21 +00:00
if out.IsNil() {
2024-02-18 10:42:21 +00:00
out.Set(reflect.New(out.Type().Elem()))
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
out = out.Elem()
2024-02-18 10:42:21 +00:00
again = true
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if out.CanAddr() {
2024-02-18 10:42:21 +00:00
outi := out.Addr().Interface()
2024-02-18 10:42:21 +00:00
if u, ok := outi.(Unmarshaler); ok {
2024-02-18 10:42:21 +00:00
good = d.callUnmarshaler(n, u)
2024-02-18 10:42:21 +00:00
return out, true, good
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if u, ok := outi.(obsoleteUnmarshaler); ok {
2024-02-18 10:42:21 +00:00
good = d.callObsoleteUnmarshaler(n, u)
2024-02-18 10:42:21 +00:00
return out, true, good
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
return out, false, false
2024-02-18 10:42:21 +00:00
}
func (d *decoder) fieldByIndex(n *Node, v reflect.Value, index []int) (field reflect.Value) {
2024-02-18 10:42:21 +00:00
if n.ShortTag() == nullTag {
2024-02-18 10:42:21 +00:00
return reflect.Value{}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
for _, num := range index {
2024-02-18 10:42:21 +00:00
for {
2024-02-18 10:42:21 +00:00
if v.Kind() == reflect.Ptr {
2024-02-18 10:42:21 +00:00
if v.IsNil() {
2024-02-18 10:42:21 +00:00
v.Set(reflect.New(v.Type().Elem()))
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
v = v.Elem()
2024-02-18 10:42:21 +00:00
continue
2024-02-18 10:42:21 +00:00
}
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
v = v.Field(num)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return v
2024-02-18 10:42:21 +00:00
}
const (
2024-02-18 10:42:21 +00:00
// 400,000 decode operations is ~500kb of dense object declarations, or
2024-02-18 10:42:21 +00:00
// ~5kb of dense object declarations with 10000% alias expansion
2024-02-18 10:42:21 +00:00
alias_ratio_range_low = 400000
// 4,000,000 decode operations is ~5MB of dense object declarations, or
2024-02-18 10:42:21 +00:00
// ~4.5MB of dense object declarations with 10% alias expansion
2024-02-18 10:42:21 +00:00
alias_ratio_range_high = 4000000
// alias_ratio_range is the range over which we scale allowed alias ratios
2024-02-18 10:42:21 +00:00
alias_ratio_range = float64(alias_ratio_range_high - alias_ratio_range_low)
)
func allowedAliasRatio(decodeCount int) float64 {
2024-02-18 10:42:21 +00:00
switch {
2024-02-18 10:42:21 +00:00
case decodeCount <= alias_ratio_range_low:
2024-02-18 10:42:21 +00:00
// allow 99% to come from alias expansion for small-to-medium documents
2024-02-18 10:42:21 +00:00
return 0.99
2024-02-18 10:42:21 +00:00
case decodeCount >= alias_ratio_range_high:
2024-02-18 10:42:21 +00:00
// allow 10% to come from alias expansion for very large documents
2024-02-18 10:42:21 +00:00
return 0.10
2024-02-18 10:42:21 +00:00
default:
2024-02-18 10:42:21 +00:00
// scale smoothly from 99% down to 10% over the range.
2024-02-18 10:42:21 +00:00
// this maps to 396,000 - 400,000 allowed alias-driven decodes over the range.
2024-02-18 10:42:21 +00:00
// 400,000 decode operations is ~100MB of allocations in worst-case scenarios (single-item maps).
2024-02-18 10:42:21 +00:00
return 0.99 - 0.89*(float64(decodeCount-alias_ratio_range_low)/alias_ratio_range)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
func (d *decoder) unmarshal(n *Node, out reflect.Value) (good bool) {
2024-02-18 10:42:21 +00:00
d.decodeCount++
2024-02-18 10:42:21 +00:00
if d.aliasDepth > 0 {
2024-02-18 10:42:21 +00:00
d.aliasCount++
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if d.aliasCount > 100 && d.decodeCount > 1000 && float64(d.aliasCount)/float64(d.decodeCount) > allowedAliasRatio(d.decodeCount) {
2024-02-18 10:42:21 +00:00
failf("document contains excessive aliasing")
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if out.Type() == nodeType {
2024-02-18 10:42:21 +00:00
out.Set(reflect.ValueOf(n).Elem())
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
switch n.Kind {
2024-02-18 10:42:21 +00:00
case DocumentNode:
2024-02-18 10:42:21 +00:00
return d.document(n, out)
2024-02-18 10:42:21 +00:00
case AliasNode:
2024-02-18 10:42:21 +00:00
return d.alias(n, out)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
out, unmarshaled, good := d.prepare(n, out)
2024-02-18 10:42:21 +00:00
if unmarshaled {
2024-02-18 10:42:21 +00:00
return good
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
switch n.Kind {
2024-02-18 10:42:21 +00:00
case ScalarNode:
2024-02-18 10:42:21 +00:00
good = d.scalar(n, out)
2024-02-18 10:42:21 +00:00
case MappingNode:
2024-02-18 10:42:21 +00:00
good = d.mapping(n, out)
2024-02-18 10:42:21 +00:00
case SequenceNode:
2024-02-18 10:42:21 +00:00
good = d.sequence(n, out)
2024-02-18 10:42:21 +00:00
case 0:
2024-02-18 10:42:21 +00:00
if n.IsZero() {
2024-02-18 10:42:21 +00:00
return d.null(out)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
fallthrough
2024-02-18 10:42:21 +00:00
default:
2024-02-18 10:42:21 +00:00
failf("cannot decode node with unknown kind %d", n.Kind)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return good
2024-02-18 10:42:21 +00:00
}
func (d *decoder) document(n *Node, out reflect.Value) (good bool) {
2024-02-18 10:42:21 +00:00
if len(n.Content) == 1 {
2024-02-18 10:42:21 +00:00
d.doc = n
2024-02-18 10:42:21 +00:00
d.unmarshal(n.Content[0], out)
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
}
func (d *decoder) alias(n *Node, out reflect.Value) (good bool) {
2024-02-18 10:42:21 +00:00
if d.aliases[n] {
2024-02-18 10:42:21 +00:00
// TODO this could actually be allowed in some circumstances.
2024-02-18 10:42:21 +00:00
failf("anchor '%s' value contains itself", n.Value)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
d.aliases[n] = true
2024-02-18 10:42:21 +00:00
d.aliasDepth++
2024-02-18 10:42:21 +00:00
good = d.unmarshal(n.Alias, out)
2024-02-18 10:42:21 +00:00
d.aliasDepth--
2024-02-18 10:42:21 +00:00
delete(d.aliases, n)
2024-02-18 10:42:21 +00:00
return good
2024-02-18 10:42:21 +00:00
}
var zeroValue reflect.Value
func resetMap(out reflect.Value) {
2024-02-18 10:42:21 +00:00
for _, k := range out.MapKeys() {
2024-02-18 10:42:21 +00:00
out.SetMapIndex(k, zeroValue)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
func (d *decoder) null(out reflect.Value) bool {
2024-02-18 10:42:21 +00:00
if out.CanAddr() {
2024-02-18 10:42:21 +00:00
switch out.Kind() {
2024-02-18 10:42:21 +00:00
case reflect.Interface, reflect.Ptr, reflect.Map, reflect.Slice:
2024-02-18 10:42:21 +00:00
out.Set(reflect.Zero(out.Type()))
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
}
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
func (d *decoder) scalar(n *Node, out reflect.Value) bool {
2024-02-18 10:42:21 +00:00
var tag string
2024-02-18 10:42:21 +00:00
var resolved interface{}
2024-02-18 10:42:21 +00:00
if n.indicatedString() {
2024-02-18 10:42:21 +00:00
tag = strTag
2024-02-18 10:42:21 +00:00
resolved = n.Value
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
tag, resolved = resolve(n.Tag, n.Value)
2024-02-18 10:42:21 +00:00
if tag == binaryTag {
2024-02-18 10:42:21 +00:00
data, err := base64.StdEncoding.DecodeString(resolved.(string))
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
failf("!!binary value contains invalid base64 data")
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
resolved = string(data)
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 resolved == nil {
2024-02-18 10:42:21 +00:00
return d.null(out)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if resolvedv := reflect.ValueOf(resolved); out.Type() == resolvedv.Type() {
2024-02-18 10:42:21 +00:00
// We've resolved to exactly the type we want, so use that.
2024-02-18 10:42:21 +00:00
out.Set(resolvedv)
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
// Perhaps we can use the value as a TextUnmarshaler to
2024-02-18 10:42:21 +00:00
// set its value.
2024-02-18 10:42:21 +00:00
if out.CanAddr() {
2024-02-18 10:42:21 +00:00
u, ok := out.Addr().Interface().(encoding.TextUnmarshaler)
2024-02-18 10:42:21 +00:00
if ok {
2024-02-18 10:42:21 +00:00
var text []byte
2024-02-18 10:42:21 +00:00
if tag == binaryTag {
2024-02-18 10:42:21 +00:00
text = []byte(resolved.(string))
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
// We let any value be unmarshaled into TextUnmarshaler.
2024-02-18 10:42:21 +00:00
// That might be more lax than we'd like, but the
2024-02-18 10:42:21 +00:00
// TextUnmarshaler itself should bowl out any dubious values.
2024-02-18 10:42:21 +00:00
text = []byte(n.Value)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
err := u.UnmarshalText(text)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
fail(err)
2024-02-18 10:42:21 +00:00
}
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
}
2024-02-18 10:42:21 +00:00
switch out.Kind() {
2024-02-18 10:42:21 +00:00
case reflect.String:
2024-02-18 10:42:21 +00:00
if tag == binaryTag {
2024-02-18 10:42:21 +00:00
out.SetString(resolved.(string))
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
out.SetString(n.Value)
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
case reflect.Interface:
2024-02-18 10:42:21 +00:00
out.Set(reflect.ValueOf(resolved))
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
2024-02-18 10:42:21 +00:00
// This used to work in v2, but it's very unfriendly.
2024-02-18 10:42:21 +00:00
isDuration := out.Type() == durationType
switch resolved := resolved.(type) {
2024-02-18 10:42:21 +00:00
case int:
2024-02-18 10:42:21 +00:00
if !isDuration && !out.OverflowInt(int64(resolved)) {
2024-02-18 10:42:21 +00:00
out.SetInt(int64(resolved))
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
case int64:
2024-02-18 10:42:21 +00:00
if !isDuration && !out.OverflowInt(resolved) {
2024-02-18 10:42:21 +00:00
out.SetInt(resolved)
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
case uint64:
2024-02-18 10:42:21 +00:00
if !isDuration && resolved <= math.MaxInt64 && !out.OverflowInt(int64(resolved)) {
2024-02-18 10:42:21 +00:00
out.SetInt(int64(resolved))
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
case float64:
2024-02-18 10:42:21 +00:00
if !isDuration && resolved <= math.MaxInt64 && !out.OverflowInt(int64(resolved)) {
2024-02-18 10:42:21 +00:00
out.SetInt(int64(resolved))
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
case string:
2024-02-18 10:42:21 +00:00
if out.Type() == durationType {
2024-02-18 10:42:21 +00:00
d, err := time.ParseDuration(resolved)
2024-02-18 10:42:21 +00:00
if err == nil {
2024-02-18 10:42:21 +00:00
out.SetInt(int64(d))
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
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
2024-02-18 10:42:21 +00:00
switch resolved := resolved.(type) {
2024-02-18 10:42:21 +00:00
case int:
2024-02-18 10:42:21 +00:00
if resolved >= 0 && !out.OverflowUint(uint64(resolved)) {
2024-02-18 10:42:21 +00:00
out.SetUint(uint64(resolved))
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
case int64:
2024-02-18 10:42:21 +00:00
if resolved >= 0 && !out.OverflowUint(uint64(resolved)) {
2024-02-18 10:42:21 +00:00
out.SetUint(uint64(resolved))
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
case uint64:
2024-02-18 10:42:21 +00:00
if !out.OverflowUint(uint64(resolved)) {
2024-02-18 10:42:21 +00:00
out.SetUint(uint64(resolved))
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
case float64:
2024-02-18 10:42:21 +00:00
if resolved <= math.MaxUint64 && !out.OverflowUint(uint64(resolved)) {
2024-02-18 10:42:21 +00:00
out.SetUint(uint64(resolved))
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
}
2024-02-18 10:42:21 +00:00
case reflect.Bool:
2024-02-18 10:42:21 +00:00
switch resolved := resolved.(type) {
2024-02-18 10:42:21 +00:00
case bool:
2024-02-18 10:42:21 +00:00
out.SetBool(resolved)
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
case string:
2024-02-18 10:42:21 +00:00
// This offers some compatibility with the 1.1 spec (https://yaml.org/type/bool.html).
2024-02-18 10:42:21 +00:00
// It only works if explicitly attempting to unmarshal into a typed bool value.
2024-02-18 10:42:21 +00:00
switch resolved {
2024-02-18 10:42:21 +00:00
case "y", "Y", "yes", "Yes", "YES", "on", "On", "ON":
2024-02-18 10:42:21 +00:00
out.SetBool(true)
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
case "n", "N", "no", "No", "NO", "off", "Off", "OFF":
2024-02-18 10:42:21 +00:00
out.SetBool(false)
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
}
2024-02-18 10:42:21 +00:00
case reflect.Float32, reflect.Float64:
2024-02-18 10:42:21 +00:00
switch resolved := resolved.(type) {
2024-02-18 10:42:21 +00:00
case int:
2024-02-18 10:42:21 +00:00
out.SetFloat(float64(resolved))
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
case int64:
2024-02-18 10:42:21 +00:00
out.SetFloat(float64(resolved))
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
case uint64:
2024-02-18 10:42:21 +00:00
out.SetFloat(float64(resolved))
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
case float64:
2024-02-18 10:42:21 +00:00
out.SetFloat(resolved)
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
case reflect.Struct:
2024-02-18 10:42:21 +00:00
if resolvedv := reflect.ValueOf(resolved); out.Type() == resolvedv.Type() {
2024-02-18 10:42:21 +00:00
out.Set(resolvedv)
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
case reflect.Ptr:
2024-02-18 10:42:21 +00:00
panic("yaml internal error: please report the issue")
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
d.terror(n, tag, out)
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
func settableValueOf(i interface{}) reflect.Value {
2024-02-18 10:42:21 +00:00
v := reflect.ValueOf(i)
2024-02-18 10:42:21 +00:00
sv := reflect.New(v.Type()).Elem()
2024-02-18 10:42:21 +00:00
sv.Set(v)
2024-02-18 10:42:21 +00:00
return sv
2024-02-18 10:42:21 +00:00
}
func (d *decoder) sequence(n *Node, out reflect.Value) (good bool) {
2024-02-18 10:42:21 +00:00
l := len(n.Content)
var iface reflect.Value
2024-02-18 10:42:21 +00:00
switch out.Kind() {
2024-02-18 10:42:21 +00:00
case reflect.Slice:
2024-02-18 10:42:21 +00:00
out.Set(reflect.MakeSlice(out.Type(), l, l))
2024-02-18 10:42:21 +00:00
case reflect.Array:
2024-02-18 10:42:21 +00:00
if l != out.Len() {
2024-02-18 10:42:21 +00:00
failf("invalid array: want %d elements but got %d", out.Len(), l)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
case reflect.Interface:
2024-02-18 10:42:21 +00:00
// No type hints. Will have to use a generic sequence.
2024-02-18 10:42:21 +00:00
iface = out
2024-02-18 10:42:21 +00:00
out = settableValueOf(make([]interface{}, l))
2024-02-18 10:42:21 +00:00
default:
2024-02-18 10:42:21 +00:00
d.terror(n, seqTag, out)
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
et := out.Type().Elem()
j := 0
2024-02-18 10:42:21 +00:00
for i := 0; i < l; i++ {
2024-02-18 10:42:21 +00:00
e := reflect.New(et).Elem()
2024-02-18 10:42:21 +00:00
if ok := d.unmarshal(n.Content[i], e); ok {
2024-02-18 10:42:21 +00:00
out.Index(j).Set(e)
2024-02-18 10:42:21 +00:00
j++
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 out.Kind() != reflect.Array {
2024-02-18 10:42:21 +00:00
out.Set(out.Slice(0, j))
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if iface.IsValid() {
2024-02-18 10:42:21 +00:00
iface.Set(out)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
func (d *decoder) mapping(n *Node, out reflect.Value) (good bool) {
2024-02-18 10:42:21 +00:00
l := len(n.Content)
2024-02-18 10:42:21 +00:00
if d.uniqueKeys {
2024-02-18 10:42:21 +00:00
nerrs := len(d.terrors)
2024-02-18 10:42:21 +00:00
for i := 0; i < l; i += 2 {
2024-02-18 10:42:21 +00:00
ni := n.Content[i]
2024-02-18 10:42:21 +00:00
for j := i + 2; j < l; j += 2 {
2024-02-18 10:42:21 +00:00
nj := n.Content[j]
2024-02-18 10:42:21 +00:00
if ni.Kind == nj.Kind && ni.Value == nj.Value {
2024-02-18 10:42:21 +00:00
d.terrors = append(d.terrors, fmt.Sprintf("line %d: mapping key %#v already defined at line %d", nj.Line, nj.Value, ni.Line))
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
if len(d.terrors) > nerrs {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
switch out.Kind() {
2024-02-18 10:42:21 +00:00
case reflect.Struct:
2024-02-18 10:42:21 +00:00
return d.mappingStruct(n, out)
2024-02-18 10:42:21 +00:00
case reflect.Map:
2024-02-18 10:42:21 +00:00
// okay
2024-02-18 10:42:21 +00:00
case reflect.Interface:
2024-02-18 10:42:21 +00:00
iface := out
2024-02-18 10:42:21 +00:00
if isStringMap(n) {
2024-02-18 10:42:21 +00:00
out = reflect.MakeMap(d.stringMapType)
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
out = reflect.MakeMap(d.generalMapType)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
iface.Set(out)
2024-02-18 10:42:21 +00:00
default:
2024-02-18 10:42:21 +00:00
d.terror(n, mapTag, out)
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
outt := out.Type()
2024-02-18 10:42:21 +00:00
kt := outt.Key()
2024-02-18 10:42:21 +00:00
et := outt.Elem()
stringMapType := d.stringMapType
2024-02-18 10:42:21 +00:00
generalMapType := d.generalMapType
2024-02-18 10:42:21 +00:00
if outt.Elem() == ifaceType {
2024-02-18 10:42:21 +00:00
if outt.Key().Kind() == reflect.String {
2024-02-18 10:42:21 +00:00
d.stringMapType = outt
2024-02-18 10:42:21 +00:00
} else if outt.Key() == ifaceType {
2024-02-18 10:42:21 +00:00
d.generalMapType = outt
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
mergedFields := d.mergedFields
2024-02-18 10:42:21 +00:00
d.mergedFields = nil
var mergeNode *Node
mapIsNew := false
2024-02-18 10:42:21 +00:00
if out.IsNil() {
2024-02-18 10:42:21 +00:00
out.Set(reflect.MakeMap(outt))
2024-02-18 10:42:21 +00:00
mapIsNew = true
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
for i := 0; i < l; i += 2 {
2024-02-18 10:42:21 +00:00
if isMerge(n.Content[i]) {
2024-02-18 10:42:21 +00:00
mergeNode = n.Content[i+1]
2024-02-18 10:42:21 +00:00
continue
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
k := reflect.New(kt).Elem()
2024-02-18 10:42:21 +00:00
if d.unmarshal(n.Content[i], k) {
2024-02-18 10:42:21 +00:00
if mergedFields != nil {
2024-02-18 10:42:21 +00:00
ki := k.Interface()
2024-02-18 10:42:21 +00:00
if mergedFields[ki] {
2024-02-18 10:42:21 +00:00
continue
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
mergedFields[ki] = true
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
kkind := k.Kind()
2024-02-18 10:42:21 +00:00
if kkind == reflect.Interface {
2024-02-18 10:42:21 +00:00
kkind = k.Elem().Kind()
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if kkind == reflect.Map || kkind == reflect.Slice {
2024-02-18 10:42:21 +00:00
failf("invalid map key: %#v", k.Interface())
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
e := reflect.New(et).Elem()
2024-02-18 10:42:21 +00:00
if d.unmarshal(n.Content[i+1], e) || n.Content[i+1].ShortTag() == nullTag && (mapIsNew || !out.MapIndex(k).IsValid()) {
2024-02-18 10:42:21 +00:00
out.SetMapIndex(k, e)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
d.mergedFields = mergedFields
2024-02-18 10:42:21 +00:00
if mergeNode != nil {
2024-02-18 10:42:21 +00:00
d.merge(n, mergeNode, out)
2024-02-18 10:42:21 +00:00
}
d.stringMapType = stringMapType
2024-02-18 10:42:21 +00:00
d.generalMapType = generalMapType
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
func isStringMap(n *Node) bool {
2024-02-18 10:42:21 +00:00
if n.Kind != MappingNode {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
l := len(n.Content)
2024-02-18 10:42:21 +00:00
for i := 0; i < l; i += 2 {
2024-02-18 10:42:21 +00:00
shortTag := n.Content[i].ShortTag()
2024-02-18 10:42:21 +00:00
if shortTag != strTag && shortTag != mergeTag {
2024-02-18 10:42:21 +00:00
return false
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 true
2024-02-18 10:42:21 +00:00
}
func (d *decoder) mappingStruct(n *Node, out reflect.Value) (good bool) {
2024-02-18 10:42:21 +00:00
sinfo, err := getStructInfo(out.Type())
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
panic(err)
2024-02-18 10:42:21 +00:00
}
var inlineMap reflect.Value
2024-02-18 10:42:21 +00:00
var elemType reflect.Type
2024-02-18 10:42:21 +00:00
if sinfo.InlineMap != -1 {
2024-02-18 10:42:21 +00:00
inlineMap = out.Field(sinfo.InlineMap)
2024-02-18 10:42:21 +00:00
elemType = inlineMap.Type().Elem()
2024-02-18 10:42:21 +00:00
}
for _, index := range sinfo.InlineUnmarshalers {
2024-02-18 10:42:21 +00:00
field := d.fieldByIndex(n, out, index)
2024-02-18 10:42:21 +00:00
d.prepare(n, field)
2024-02-18 10:42:21 +00:00
}
mergedFields := d.mergedFields
2024-02-18 10:42:21 +00:00
d.mergedFields = nil
2024-02-18 10:42:21 +00:00
var mergeNode *Node
2024-02-18 10:42:21 +00:00
var doneFields []bool
2024-02-18 10:42:21 +00:00
if d.uniqueKeys {
2024-02-18 10:42:21 +00:00
doneFields = make([]bool, len(sinfo.FieldsList))
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
name := settableValueOf("")
2024-02-18 10:42:21 +00:00
l := len(n.Content)
2024-02-18 10:42:21 +00:00
for i := 0; i < l; i += 2 {
2024-02-18 10:42:21 +00:00
ni := n.Content[i]
2024-02-18 10:42:21 +00:00
if isMerge(ni) {
2024-02-18 10:42:21 +00:00
mergeNode = n.Content[i+1]
2024-02-18 10:42:21 +00:00
continue
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if !d.unmarshal(ni, name) {
2024-02-18 10:42:21 +00:00
continue
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
sname := name.String()
2024-02-18 10:42:21 +00:00
if mergedFields != nil {
2024-02-18 10:42:21 +00:00
if mergedFields[sname] {
2024-02-18 10:42:21 +00:00
continue
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
mergedFields[sname] = true
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if info, ok := sinfo.FieldsMap[sname]; ok {
2024-02-18 10:42:21 +00:00
if d.uniqueKeys {
2024-02-18 10:42:21 +00:00
if doneFields[info.Id] {
2024-02-18 10:42:21 +00:00
d.terrors = append(d.terrors, fmt.Sprintf("line %d: field %s already set in type %s", ni.Line, name.String(), out.Type()))
2024-02-18 10:42:21 +00:00
continue
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
doneFields[info.Id] = true
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
var field reflect.Value
2024-02-18 10:42:21 +00:00
if info.Inline == nil {
2024-02-18 10:42:21 +00:00
field = out.Field(info.Num)
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
field = d.fieldByIndex(n, out, info.Inline)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
d.unmarshal(n.Content[i+1], field)
2024-02-18 10:42:21 +00:00
} else if sinfo.InlineMap != -1 {
2024-02-18 10:42:21 +00:00
if inlineMap.IsNil() {
2024-02-18 10:42:21 +00:00
inlineMap.Set(reflect.MakeMap(inlineMap.Type()))
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
value := reflect.New(elemType).Elem()
2024-02-18 10:42:21 +00:00
d.unmarshal(n.Content[i+1], value)
2024-02-18 10:42:21 +00:00
inlineMap.SetMapIndex(name, value)
2024-02-18 10:42:21 +00:00
} else if d.knownFields {
2024-02-18 10:42:21 +00:00
d.terrors = append(d.terrors, fmt.Sprintf("line %d: field %s not found in type %s", ni.Line, name.String(), out.Type()))
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
d.mergedFields = mergedFields
2024-02-18 10:42:21 +00:00
if mergeNode != nil {
2024-02-18 10:42:21 +00:00
d.merge(n, mergeNode, out)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
func failWantMap() {
2024-02-18 10:42:21 +00:00
failf("map merge requires map or sequence of maps as the value")
2024-02-18 10:42:21 +00:00
}
func (d *decoder) merge(parent *Node, merge *Node, out reflect.Value) {
2024-02-18 10:42:21 +00:00
mergedFields := d.mergedFields
2024-02-18 10:42:21 +00:00
if mergedFields == nil {
2024-02-18 10:42:21 +00:00
d.mergedFields = make(map[interface{}]bool)
2024-02-18 10:42:21 +00:00
for i := 0; i < len(parent.Content); i += 2 {
2024-02-18 10:42:21 +00:00
k := reflect.New(ifaceType).Elem()
2024-02-18 10:42:21 +00:00
if d.unmarshal(parent.Content[i], k) {
2024-02-18 10:42:21 +00:00
d.mergedFields[k.Interface()] = 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
}
switch merge.Kind {
2024-02-18 10:42:21 +00:00
case MappingNode:
2024-02-18 10:42:21 +00:00
d.unmarshal(merge, out)
2024-02-18 10:42:21 +00:00
case AliasNode:
2024-02-18 10:42:21 +00:00
if merge.Alias != nil && merge.Alias.Kind != MappingNode {
2024-02-18 10:42:21 +00:00
failWantMap()
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
d.unmarshal(merge, out)
2024-02-18 10:42:21 +00:00
case SequenceNode:
2024-02-18 10:42:21 +00:00
for i := 0; i < len(merge.Content); i++ {
2024-02-18 10:42:21 +00:00
ni := merge.Content[i]
2024-02-18 10:42:21 +00:00
if ni.Kind == AliasNode {
2024-02-18 10:42:21 +00:00
if ni.Alias != nil && ni.Alias.Kind != MappingNode {
2024-02-18 10:42:21 +00:00
failWantMap()
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
} else if ni.Kind != MappingNode {
2024-02-18 10:42:21 +00:00
failWantMap()
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
d.unmarshal(ni, out)
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
failWantMap()
2024-02-18 10:42:21 +00:00
}
d.mergedFields = mergedFields
2024-02-18 10:42:21 +00:00
}
func isMerge(n *Node) bool {
2024-02-18 10:42:21 +00:00
return n.Kind == ScalarNode && n.Value == "<<" && (n.Tag == "" || n.Tag == "!" || shortTag(n.Tag) == mergeTag)
2024-02-18 10:42:21 +00:00
}