15 documentNode = 1 << iota
26 // For an alias node, alias holds the resolved alias.
31 anchors map[string]*node
34 // ----------------------------------------------------------------------------
35 // Parser, produces a node tree out of a libyaml event stream.
44 func newParser(b []byte) *parser {
46 if !yaml_parser_initialize(&p.parser) {
47 panic("failed to initialize YAML emitter")
52 yaml_parser_set_input_string(&p.parser, b)
56 func newParserFromReader(r io.Reader) *parser {
58 if !yaml_parser_initialize(&p.parser) {
59 panic("failed to initialize YAML emitter")
61 yaml_parser_set_input_reader(&p.parser, r)
65 func (p *parser) init() {
69 p.expect(yaml_STREAM_START_EVENT)
73 func (p *parser) destroy() {
74 if p.event.typ != yaml_NO_EVENT {
75 yaml_event_delete(&p.event)
77 yaml_parser_delete(&p.parser)
80 // expect consumes an event from the event stream and
81 // checks that it's of the expected type.
82 func (p *parser) expect(e yaml_event_type_t) {
83 if p.event.typ == yaml_NO_EVENT {
84 if !yaml_parser_parse(&p.parser, &p.event) {
88 if p.event.typ == yaml_STREAM_END_EVENT {
89 failf("attempted to go past the end of stream; corrupted value?")
92 p.parser.problem = fmt.Sprintf("expected %s event but got %s", e, p.event.typ)
95 yaml_event_delete(&p.event)
96 p.event.typ = yaml_NO_EVENT
99 // peek peeks at the next event in the event stream,
100 // puts the results into p.event and returns the event type.
101 func (p *parser) peek() yaml_event_type_t {
102 if p.event.typ != yaml_NO_EVENT {
105 if !yaml_parser_parse(&p.parser, &p.event) {
111 func (p *parser) fail() {
114 if p.parser.problem_mark.line != 0 {
115 line = p.parser.problem_mark.line
116 // Scanner errors don't iterate line before returning error
117 if p.parser.error == yaml_SCANNER_ERROR {
120 } else if p.parser.context_mark.line != 0 {
121 line = p.parser.context_mark.line
124 where = "line " + strconv.Itoa(line) + ": "
127 if len(p.parser.problem) > 0 {
128 msg = p.parser.problem
130 msg = "unknown problem parsing YAML content"
132 failf("%s%s", where, msg)
135 func (p *parser) anchor(n *node, anchor []byte) {
137 p.doc.anchors[string(anchor)] = n
141 func (p *parser) parse() *node {
144 case yaml_SCALAR_EVENT:
146 case yaml_ALIAS_EVENT:
148 case yaml_MAPPING_START_EVENT:
150 case yaml_SEQUENCE_START_EVENT:
152 case yaml_DOCUMENT_START_EVENT:
154 case yaml_STREAM_END_EVENT:
155 // Happens when attempting to decode an empty buffer.
158 panic("attempted to parse unknown event: " + p.event.typ.String())
162 func (p *parser) node(kind int) *node {
165 line: p.event.start_mark.line,
166 column: p.event.start_mark.column,
170 func (p *parser) document() *node {
171 n := p.node(documentNode)
172 n.anchors = make(map[string]*node)
174 p.expect(yaml_DOCUMENT_START_EVENT)
175 n.children = append(n.children, p.parse())
176 p.expect(yaml_DOCUMENT_END_EVENT)
180 func (p *parser) alias() *node {
181 n := p.node(aliasNode)
182 n.value = string(p.event.anchor)
183 n.alias = p.doc.anchors[n.value]
185 failf("unknown anchor '%s' referenced", n.value)
187 p.expect(yaml_ALIAS_EVENT)
191 func (p *parser) scalar() *node {
192 n := p.node(scalarNode)
193 n.value = string(p.event.value)
194 n.tag = string(p.event.tag)
195 n.implicit = p.event.implicit
196 p.anchor(n, p.event.anchor)
197 p.expect(yaml_SCALAR_EVENT)
201 func (p *parser) sequence() *node {
202 n := p.node(sequenceNode)
203 p.anchor(n, p.event.anchor)
204 p.expect(yaml_SEQUENCE_START_EVENT)
205 for p.peek() != yaml_SEQUENCE_END_EVENT {
206 n.children = append(n.children, p.parse())
208 p.expect(yaml_SEQUENCE_END_EVENT)
212 func (p *parser) mapping() *node {
213 n := p.node(mappingNode)
214 p.anchor(n, p.event.anchor)
215 p.expect(yaml_MAPPING_START_EVENT)
216 for p.peek() != yaml_MAPPING_END_EVENT {
217 n.children = append(n.children, p.parse(), p.parse())
219 p.expect(yaml_MAPPING_END_EVENT)
223 // ----------------------------------------------------------------------------
224 // Decoder, unmarshals a node into a provided value.
226 type decoder struct {
228 aliases map[*node]bool
235 mapItemType = reflect.TypeOf(MapItem{})
236 durationType = reflect.TypeOf(time.Duration(0))
237 defaultMapType = reflect.TypeOf(map[interface{}]interface{}{})
238 ifaceType = defaultMapType.Elem()
239 timeType = reflect.TypeOf(time.Time{})
240 ptrTimeType = reflect.TypeOf(&time.Time{})
243 func newDecoder(strict bool) *decoder {
244 d := &decoder{mapType: defaultMapType, strict: strict}
245 d.aliases = make(map[*node]bool)
249 func (d *decoder) terror(n *node, tag string, out reflect.Value) {
254 if tag != yaml_SEQ_TAG && tag != yaml_MAP_TAG {
256 value = " `" + value[:7] + "...`"
258 value = " `" + value + "`"
261 d.terrors = append(d.terrors, fmt.Sprintf("line %d: cannot unmarshal %s%s into %s", n.line+1, shortTag(tag), value, out.Type()))
264 func (d *decoder) callUnmarshaler(n *node, u Unmarshaler) (good bool) {
265 terrlen := len(d.terrors)
266 err := u.UnmarshalYAML(func(v interface{}) (err error) {
267 defer handleErr(&err)
268 d.unmarshal(n, reflect.ValueOf(v))
269 if len(d.terrors) > terrlen {
270 issues := d.terrors[terrlen:]
271 d.terrors = d.terrors[:terrlen]
272 return &TypeError{issues}
276 if e, ok := err.(*TypeError); ok {
277 d.terrors = append(d.terrors, e.Errors...)
286 // d.prepare initializes and dereferences pointers and calls UnmarshalYAML
287 // if a value is found to implement it.
288 // It returns the initialized and dereferenced out value, whether
289 // unmarshalling was already done by UnmarshalYAML, and if so whether
290 // its types unmarshalled appropriately.
292 // If n holds a null value, prepare returns before doing anything.
293 func (d *decoder) prepare(n *node, out reflect.Value) (newout reflect.Value, unmarshaled, good bool) {
294 if n.tag == yaml_NULL_TAG || n.kind == scalarNode && n.tag == "" && (n.value == "null" || n.value == "~" || n.value == "" && n.implicit) {
295 return out, false, false
300 if out.Kind() == reflect.Ptr {
302 out.Set(reflect.New(out.Type().Elem()))
308 if u, ok := out.Addr().Interface().(Unmarshaler); ok {
309 good = d.callUnmarshaler(n, u)
310 return out, true, good
314 return out, false, false
317 func (d *decoder) unmarshal(n *node, out reflect.Value) (good bool) {
320 return d.document(n, out)
322 return d.alias(n, out)
324 out, unmarshaled, good := d.prepare(n, out)
330 good = d.scalar(n, out)
332 good = d.mapping(n, out)
334 good = d.sequence(n, out)
336 panic("internal error: unknown node kind: " + strconv.Itoa(n.kind))
341 func (d *decoder) document(n *node, out reflect.Value) (good bool) {
342 if len(n.children) == 1 {
344 d.unmarshal(n.children[0], out)
350 func (d *decoder) alias(n *node, out reflect.Value) (good bool) {
352 // TODO this could actually be allowed in some circumstances.
353 failf("anchor '%s' value contains itself", n.value)
356 good = d.unmarshal(n.alias, out)
361 var zeroValue reflect.Value
363 func resetMap(out reflect.Value) {
364 for _, k := range out.MapKeys() {
365 out.SetMapIndex(k, zeroValue)
369 func (d *decoder) scalar(n *node, out reflect.Value) bool {
371 var resolved interface{}
372 if n.tag == "" && !n.implicit {
376 tag, resolved = resolve(n.tag, n.value)
377 if tag == yaml_BINARY_TAG {
378 data, err := base64.StdEncoding.DecodeString(resolved.(string))
380 failf("!!binary value contains invalid base64 data")
382 resolved = string(data)
386 if out.Kind() == reflect.Map && !out.CanAddr() {
389 out.Set(reflect.Zero(out.Type()))
393 if resolvedv := reflect.ValueOf(resolved); out.Type() == resolvedv.Type() {
394 // We've resolved to exactly the type we want, so use that.
398 // Perhaps we can use the value as a TextUnmarshaler to
401 u, ok := out.Addr().Interface().(encoding.TextUnmarshaler)
404 if tag == yaml_BINARY_TAG {
405 text = []byte(resolved.(string))
407 // We let any value be unmarshaled into TextUnmarshaler.
408 // That might be more lax than we'd like, but the
409 // TextUnmarshaler itself should bowl out any dubious values.
410 text = []byte(n.value)
412 err := u.UnmarshalText(text)
421 if tag == yaml_BINARY_TAG {
422 out.SetString(resolved.(string))
426 out.SetString(n.value)
429 case reflect.Interface:
431 out.Set(reflect.Zero(out.Type()))
432 } else if tag == yaml_TIMESTAMP_TAG {
433 // It looks like a timestamp but for backward compatibility
434 // reasons we set it as a string, so that code that unmarshals
435 // timestamp-like values into interface{} will continue to
436 // see a string and not a time.Time.
437 // TODO(v3) Drop this.
438 out.Set(reflect.ValueOf(n.value))
440 out.Set(reflect.ValueOf(resolved))
443 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
444 switch resolved := resolved.(type) {
446 if !out.OverflowInt(int64(resolved)) {
447 out.SetInt(int64(resolved))
451 if !out.OverflowInt(resolved) {
456 if resolved <= math.MaxInt64 && !out.OverflowInt(int64(resolved)) {
457 out.SetInt(int64(resolved))
461 if resolved <= math.MaxInt64 && !out.OverflowInt(int64(resolved)) {
462 out.SetInt(int64(resolved))
466 if out.Type() == durationType {
467 d, err := time.ParseDuration(resolved)
474 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
475 switch resolved := resolved.(type) {
477 if resolved >= 0 && !out.OverflowUint(uint64(resolved)) {
478 out.SetUint(uint64(resolved))
482 if resolved >= 0 && !out.OverflowUint(uint64(resolved)) {
483 out.SetUint(uint64(resolved))
487 if !out.OverflowUint(uint64(resolved)) {
488 out.SetUint(uint64(resolved))
492 if resolved <= math.MaxUint64 && !out.OverflowUint(uint64(resolved)) {
493 out.SetUint(uint64(resolved))
498 switch resolved := resolved.(type) {
500 out.SetBool(resolved)
503 case reflect.Float32, reflect.Float64:
504 switch resolved := resolved.(type) {
506 out.SetFloat(float64(resolved))
509 out.SetFloat(float64(resolved))
512 out.SetFloat(float64(resolved))
515 out.SetFloat(resolved)
519 if resolvedv := reflect.ValueOf(resolved); out.Type() == resolvedv.Type() {
524 if out.Type().Elem() == reflect.TypeOf(resolved) {
525 // TODO DOes this make sense? When is out a Ptr except when decoding a nil value?
526 elem := reflect.New(out.Type().Elem())
527 elem.Elem().Set(reflect.ValueOf(resolved))
532 d.terror(n, tag, out)
536 func settableValueOf(i interface{}) reflect.Value {
537 v := reflect.ValueOf(i)
538 sv := reflect.New(v.Type()).Elem()
543 func (d *decoder) sequence(n *node, out reflect.Value) (good bool) {
546 var iface reflect.Value
549 out.Set(reflect.MakeSlice(out.Type(), l, l))
552 failf("invalid array: want %d elements but got %d", out.Len(), l)
554 case reflect.Interface:
555 // No type hints. Will have to use a generic sequence.
557 out = settableValueOf(make([]interface{}, l))
559 d.terror(n, yaml_SEQ_TAG, out)
562 et := out.Type().Elem()
565 for i := 0; i < l; i++ {
566 e := reflect.New(et).Elem()
567 if ok := d.unmarshal(n.children[i], e); ok {
572 if out.Kind() != reflect.Array {
573 out.Set(out.Slice(0, j))
581 func (d *decoder) mapping(n *node, out reflect.Value) (good bool) {
584 return d.mappingStruct(n, out)
586 return d.mappingSlice(n, out)
589 case reflect.Interface:
590 if d.mapType.Kind() == reflect.Map {
592 out = reflect.MakeMap(d.mapType)
595 slicev := reflect.New(d.mapType).Elem()
596 if !d.mappingSlice(n, slicev) {
603 d.terror(n, yaml_MAP_TAG, out)
611 if outt.Key() == ifaceType && outt.Elem() == ifaceType {
616 out.Set(reflect.MakeMap(outt))
619 for i := 0; i < l; i += 2 {
620 if isMerge(n.children[i]) {
621 d.merge(n.children[i+1], out)
624 k := reflect.New(kt).Elem()
625 if d.unmarshal(n.children[i], k) {
627 if kkind == reflect.Interface {
628 kkind = k.Elem().Kind()
630 if kkind == reflect.Map || kkind == reflect.Slice {
631 failf("invalid map key: %#v", k.Interface())
633 e := reflect.New(et).Elem()
634 if d.unmarshal(n.children[i+1], e) {
635 d.setMapIndex(n.children[i+1], out, k, e)
643 func (d *decoder) setMapIndex(n *node, out, k, v reflect.Value) {
644 if d.strict && out.MapIndex(k) != zeroValue {
645 d.terrors = append(d.terrors, fmt.Sprintf("line %d: key %#v already set in map", n.line+1, k.Interface()))
648 out.SetMapIndex(k, v)
651 func (d *decoder) mappingSlice(n *node, out reflect.Value) (good bool) {
653 if outt.Elem() != mapItemType {
654 d.terror(n, yaml_MAP_TAG, out)
662 var l = len(n.children)
663 for i := 0; i < l; i += 2 {
664 if isMerge(n.children[i]) {
665 d.merge(n.children[i+1], out)
669 k := reflect.ValueOf(&item.Key).Elem()
670 if d.unmarshal(n.children[i], k) {
671 v := reflect.ValueOf(&item.Value).Elem()
672 if d.unmarshal(n.children[i+1], v) {
673 slice = append(slice, item)
677 out.Set(reflect.ValueOf(slice))
682 func (d *decoder) mappingStruct(n *node, out reflect.Value) (good bool) {
683 sinfo, err := getStructInfo(out.Type())
687 name := settableValueOf("")
690 var inlineMap reflect.Value
691 var elemType reflect.Type
692 if sinfo.InlineMap != -1 {
693 inlineMap = out.Field(sinfo.InlineMap)
694 inlineMap.Set(reflect.New(inlineMap.Type()).Elem())
695 elemType = inlineMap.Type().Elem()
698 var doneFields []bool
700 doneFields = make([]bool, len(sinfo.FieldsList))
702 for i := 0; i < l; i += 2 {
705 d.merge(n.children[i+1], out)
708 if !d.unmarshal(ni, name) {
711 if info, ok := sinfo.FieldsMap[name.String()]; ok {
713 if doneFields[info.Id] {
714 d.terrors = append(d.terrors, fmt.Sprintf("line %d: field %s already set in type %s", ni.line+1, name.String(), out.Type()))
717 doneFields[info.Id] = true
719 var field reflect.Value
720 if info.Inline == nil {
721 field = out.Field(info.Num)
723 field = out.FieldByIndex(info.Inline)
725 d.unmarshal(n.children[i+1], field)
726 } else if sinfo.InlineMap != -1 {
727 if inlineMap.IsNil() {
728 inlineMap.Set(reflect.MakeMap(inlineMap.Type()))
730 value := reflect.New(elemType).Elem()
731 d.unmarshal(n.children[i+1], value)
732 d.setMapIndex(n.children[i+1], inlineMap, name, value)
734 d.terrors = append(d.terrors, fmt.Sprintf("line %d: field %s not found in type %s", ni.line+1, name.String(), out.Type()))
741 failf("map merge requires map or sequence of maps as the value")
744 func (d *decoder) merge(n *node, out reflect.Value) {
749 an, ok := d.doc.anchors[n.value]
750 if ok && an.kind != mappingNode {
755 // Step backwards as earlier nodes take precedence.
756 for i := len(n.children) - 1; i >= 0; i-- {
758 if ni.kind == aliasNode {
759 an, ok := d.doc.anchors[ni.value]
760 if ok && an.kind != mappingNode {
763 } else if ni.kind != mappingNode {
773 func isMerge(n *node) bool {
774 return n.kind == scalarNode && n.value == "<<" && (n.implicit == true || n.tag == yaml_MERGE_TAG)