14 documentNode = 1 << iota
28 anchors map[string]*node
31 // ----------------------------------------------------------------------------
32 // Parser, produces a node tree out of a libyaml event stream.
40 func newParser(b []byte) *parser {
42 if !yaml_parser_initialize(&p.parser) {
43 panic("failed to initialize YAML emitter")
50 yaml_parser_set_input_string(&p.parser, b)
53 if p.event.typ != yaml_STREAM_START_EVENT {
54 panic("expected stream start event, got " + strconv.Itoa(int(p.event.typ)))
60 func (p *parser) destroy() {
61 if p.event.typ != yaml_NO_EVENT {
62 yaml_event_delete(&p.event)
64 yaml_parser_delete(&p.parser)
67 func (p *parser) skip() {
68 if p.event.typ != yaml_NO_EVENT {
69 if p.event.typ == yaml_STREAM_END_EVENT {
70 failf("attempted to go past the end of stream; corrupted value?")
72 yaml_event_delete(&p.event)
74 if !yaml_parser_parse(&p.parser, &p.event) {
79 func (p *parser) fail() {
82 if p.parser.problem_mark.line != 0 {
83 line = p.parser.problem_mark.line
84 } else if p.parser.context_mark.line != 0 {
85 line = p.parser.context_mark.line
88 where = "line " + strconv.Itoa(line) + ": "
91 if len(p.parser.problem) > 0 {
92 msg = p.parser.problem
94 msg = "unknown problem parsing YAML content"
96 failf("%s%s", where, msg)
99 func (p *parser) anchor(n *node, anchor []byte) {
101 p.doc.anchors[string(anchor)] = n
105 func (p *parser) parse() *node {
107 case yaml_SCALAR_EVENT:
109 case yaml_ALIAS_EVENT:
111 case yaml_MAPPING_START_EVENT:
113 case yaml_SEQUENCE_START_EVENT:
115 case yaml_DOCUMENT_START_EVENT:
117 case yaml_STREAM_END_EVENT:
118 // Happens when attempting to decode an empty buffer.
121 panic("attempted to parse unknown event: " + strconv.Itoa(int(p.event.typ)))
125 func (p *parser) node(kind int) *node {
128 line: p.event.start_mark.line,
129 column: p.event.start_mark.column,
133 func (p *parser) document() *node {
134 n := p.node(documentNode)
135 n.anchors = make(map[string]*node)
138 n.children = append(n.children, p.parse())
139 if p.event.typ != yaml_DOCUMENT_END_EVENT {
140 panic("expected end of document event but got " + strconv.Itoa(int(p.event.typ)))
146 func (p *parser) alias() *node {
147 n := p.node(aliasNode)
148 n.value = string(p.event.anchor)
153 func (p *parser) scalar() *node {
154 n := p.node(scalarNode)
155 n.value = string(p.event.value)
156 n.tag = string(p.event.tag)
157 n.implicit = p.event.implicit
158 p.anchor(n, p.event.anchor)
163 func (p *parser) sequence() *node {
164 n := p.node(sequenceNode)
165 p.anchor(n, p.event.anchor)
167 for p.event.typ != yaml_SEQUENCE_END_EVENT {
168 n.children = append(n.children, p.parse())
174 func (p *parser) mapping() *node {
175 n := p.node(mappingNode)
176 p.anchor(n, p.event.anchor)
178 for p.event.typ != yaml_MAPPING_END_EVENT {
179 n.children = append(n.children, p.parse(), p.parse())
185 // ----------------------------------------------------------------------------
186 // Decoder, unmarshals a node into a provided value.
188 type decoder struct {
190 aliases map[string]bool
197 mapItemType = reflect.TypeOf(MapItem{})
198 durationType = reflect.TypeOf(time.Duration(0))
199 defaultMapType = reflect.TypeOf(map[interface{}]interface{}{})
200 ifaceType = defaultMapType.Elem()
203 func newDecoder(strict bool) *decoder {
204 d := &decoder{mapType: defaultMapType, strict: strict}
205 d.aliases = make(map[string]bool)
209 func (d *decoder) terror(n *node, tag string, out reflect.Value) {
214 if tag != yaml_SEQ_TAG && tag != yaml_MAP_TAG {
216 value = " `" + value[:7] + "...`"
218 value = " `" + value + "`"
221 d.terrors = append(d.terrors, fmt.Sprintf("line %d: cannot unmarshal %s%s into %s", n.line+1, shortTag(tag), value, out.Type()))
224 func (d *decoder) callUnmarshaler(n *node, u Unmarshaler) (good bool) {
225 terrlen := len(d.terrors)
226 err := u.UnmarshalYAML(func(v interface{}) (err error) {
227 defer handleErr(&err)
228 d.unmarshal(n, reflect.ValueOf(v))
229 if len(d.terrors) > terrlen {
230 issues := d.terrors[terrlen:]
231 d.terrors = d.terrors[:terrlen]
232 return &TypeError{issues}
236 if e, ok := err.(*TypeError); ok {
237 d.terrors = append(d.terrors, e.Errors...)
246 // d.prepare initializes and dereferences pointers and calls UnmarshalYAML
247 // if a value is found to implement it.
248 // It returns the initialized and dereferenced out value, whether
249 // unmarshalling was already done by UnmarshalYAML, and if so whether
250 // its types unmarshalled appropriately.
252 // If n holds a null value, prepare returns before doing anything.
253 func (d *decoder) prepare(n *node, out reflect.Value) (newout reflect.Value, unmarshaled, good bool) {
254 if n.tag == yaml_NULL_TAG || n.kind == scalarNode && n.tag == "" && (n.value == "null" || n.value == "" && n.implicit) {
255 return out, false, false
260 if out.Kind() == reflect.Ptr {
262 out.Set(reflect.New(out.Type().Elem()))
268 if u, ok := out.Addr().Interface().(Unmarshaler); ok {
269 good = d.callUnmarshaler(n, u)
270 return out, true, good
274 return out, false, false
277 func (d *decoder) unmarshal(n *node, out reflect.Value) (good bool) {
280 return d.document(n, out)
282 return d.alias(n, out)
284 out, unmarshaled, good := d.prepare(n, out)
290 good = d.scalar(n, out)
292 good = d.mapping(n, out)
294 good = d.sequence(n, out)
296 panic("internal error: unknown node kind: " + strconv.Itoa(n.kind))
301 func (d *decoder) document(n *node, out reflect.Value) (good bool) {
302 if len(n.children) == 1 {
304 d.unmarshal(n.children[0], out)
310 func (d *decoder) alias(n *node, out reflect.Value) (good bool) {
311 an, ok := d.doc.anchors[n.value]
313 failf("unknown anchor '%s' referenced", n.value)
315 if d.aliases[n.value] {
316 failf("anchor '%s' value contains itself", n.value)
318 d.aliases[n.value] = true
319 good = d.unmarshal(an, out)
320 delete(d.aliases, n.value)
324 var zeroValue reflect.Value
326 func resetMap(out reflect.Value) {
327 for _, k := range out.MapKeys() {
328 out.SetMapIndex(k, zeroValue)
332 func (d *decoder) scalar(n *node, out reflect.Value) (good bool) {
334 var resolved interface{}
335 if n.tag == "" && !n.implicit {
339 tag, resolved = resolve(n.tag, n.value)
340 if tag == yaml_BINARY_TAG {
341 data, err := base64.StdEncoding.DecodeString(resolved.(string))
343 failf("!!binary value contains invalid base64 data")
345 resolved = string(data)
349 if out.Kind() == reflect.Map && !out.CanAddr() {
352 out.Set(reflect.Zero(out.Type()))
356 if s, ok := resolved.(string); ok && out.CanAddr() {
357 if u, ok := out.Addr().Interface().(encoding.TextUnmarshaler); ok {
358 err := u.UnmarshalText([]byte(s))
367 if tag == yaml_BINARY_TAG {
368 out.SetString(resolved.(string))
370 } else if resolved != nil {
371 out.SetString(n.value)
374 case reflect.Interface:
376 out.Set(reflect.Zero(out.Type()))
378 out.Set(reflect.ValueOf(resolved))
381 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
382 switch resolved := resolved.(type) {
384 if !out.OverflowInt(int64(resolved)) {
385 out.SetInt(int64(resolved))
389 if !out.OverflowInt(resolved) {
394 if resolved <= math.MaxInt64 && !out.OverflowInt(int64(resolved)) {
395 out.SetInt(int64(resolved))
399 if resolved <= math.MaxInt64 && !out.OverflowInt(int64(resolved)) {
400 out.SetInt(int64(resolved))
404 if out.Type() == durationType {
405 d, err := time.ParseDuration(resolved)
412 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
413 switch resolved := resolved.(type) {
415 if resolved >= 0 && !out.OverflowUint(uint64(resolved)) {
416 out.SetUint(uint64(resolved))
420 if resolved >= 0 && !out.OverflowUint(uint64(resolved)) {
421 out.SetUint(uint64(resolved))
425 if !out.OverflowUint(uint64(resolved)) {
426 out.SetUint(uint64(resolved))
430 if resolved <= math.MaxUint64 && !out.OverflowUint(uint64(resolved)) {
431 out.SetUint(uint64(resolved))
436 switch resolved := resolved.(type) {
438 out.SetBool(resolved)
441 case reflect.Float32, reflect.Float64:
442 switch resolved := resolved.(type) {
444 out.SetFloat(float64(resolved))
447 out.SetFloat(float64(resolved))
450 out.SetFloat(float64(resolved))
453 out.SetFloat(resolved)
457 if out.Type().Elem() == reflect.TypeOf(resolved) {
458 // TODO DOes this make sense? When is out a Ptr except when decoding a nil value?
459 elem := reflect.New(out.Type().Elem())
460 elem.Elem().Set(reflect.ValueOf(resolved))
466 d.terror(n, tag, out)
471 func settableValueOf(i interface{}) reflect.Value {
472 v := reflect.ValueOf(i)
473 sv := reflect.New(v.Type()).Elem()
478 func (d *decoder) sequence(n *node, out reflect.Value) (good bool) {
481 var iface reflect.Value
484 out.Set(reflect.MakeSlice(out.Type(), l, l))
485 case reflect.Interface:
486 // No type hints. Will have to use a generic sequence.
488 out = settableValueOf(make([]interface{}, l))
490 d.terror(n, yaml_SEQ_TAG, out)
493 et := out.Type().Elem()
496 for i := 0; i < l; i++ {
497 e := reflect.New(et).Elem()
498 if ok := d.unmarshal(n.children[i], e); ok {
503 out.Set(out.Slice(0, j))
510 func (d *decoder) mapping(n *node, out reflect.Value) (good bool) {
513 return d.mappingStruct(n, out)
515 return d.mappingSlice(n, out)
518 case reflect.Interface:
519 if d.mapType.Kind() == reflect.Map {
521 out = reflect.MakeMap(d.mapType)
524 slicev := reflect.New(d.mapType).Elem()
525 if !d.mappingSlice(n, slicev) {
532 d.terror(n, yaml_MAP_TAG, out)
540 if outt.Key() == ifaceType && outt.Elem() == ifaceType {
545 out.Set(reflect.MakeMap(outt))
548 for i := 0; i < l; i += 2 {
549 if isMerge(n.children[i]) {
550 d.merge(n.children[i+1], out)
553 k := reflect.New(kt).Elem()
554 if d.unmarshal(n.children[i], k) {
556 if kkind == reflect.Interface {
557 kkind = k.Elem().Kind()
559 if kkind == reflect.Map || kkind == reflect.Slice {
560 failf("invalid map key: %#v", k.Interface())
562 e := reflect.New(et).Elem()
563 if d.unmarshal(n.children[i+1], e) {
564 out.SetMapIndex(k, e)
572 func (d *decoder) mappingSlice(n *node, out reflect.Value) (good bool) {
574 if outt.Elem() != mapItemType {
575 d.terror(n, yaml_MAP_TAG, out)
583 var l = len(n.children)
584 for i := 0; i < l; i += 2 {
585 if isMerge(n.children[i]) {
586 d.merge(n.children[i+1], out)
590 k := reflect.ValueOf(&item.Key).Elem()
591 if d.unmarshal(n.children[i], k) {
592 v := reflect.ValueOf(&item.Value).Elem()
593 if d.unmarshal(n.children[i+1], v) {
594 slice = append(slice, item)
598 out.Set(reflect.ValueOf(slice))
603 func (d *decoder) mappingStruct(n *node, out reflect.Value) (good bool) {
604 sinfo, err := getStructInfo(out.Type())
608 name := settableValueOf("")
611 var inlineMap reflect.Value
612 var elemType reflect.Type
613 if sinfo.InlineMap != -1 {
614 inlineMap = out.Field(sinfo.InlineMap)
615 inlineMap.Set(reflect.New(inlineMap.Type()).Elem())
616 elemType = inlineMap.Type().Elem()
619 for i := 0; i < l; i += 2 {
622 d.merge(n.children[i+1], out)
625 if !d.unmarshal(ni, name) {
628 if info, ok := sinfo.FieldsMap[name.String()]; ok {
629 var field reflect.Value
630 if info.Inline == nil {
631 field = out.Field(info.Num)
633 field = out.FieldByIndex(info.Inline)
635 d.unmarshal(n.children[i+1], field)
636 } else if sinfo.InlineMap != -1 {
637 if inlineMap.IsNil() {
638 inlineMap.Set(reflect.MakeMap(inlineMap.Type()))
640 value := reflect.New(elemType).Elem()
641 d.unmarshal(n.children[i+1], value)
642 inlineMap.SetMapIndex(name, value)
644 d.terrors = append(d.terrors, fmt.Sprintf("line %d: field %s not found in struct %s", n.line+1, name.String(), out.Type()))
651 failf("map merge requires map or sequence of maps as the value")
654 func (d *decoder) merge(n *node, out reflect.Value) {
659 an, ok := d.doc.anchors[n.value]
660 if ok && an.kind != mappingNode {
665 // Step backwards as earlier nodes take precedence.
666 for i := len(n.children) - 1; i >= 0; i-- {
668 if ni.kind == aliasNode {
669 an, ok := d.doc.anchors[ni.value]
670 if ok && an.kind != mappingNode {
673 } else if ni.kind != mappingNode {
683 func isMerge(n *node) bool {
684 return n.kind == scalarNode && n.value == "<<" && (n.implicit == true || n.tag == yaml_MERGE_TAG)