Bump up yaml.v2 version

pull/6/head
Thomas Liu 2016-04-11 17:05:05 -07:00
parent 31de62216d
commit a57bba498f
10 changed files with 116 additions and 55 deletions

2
Godeps/Godeps.json generated
View File

@ -1129,7 +1129,7 @@
}, },
{ {
"ImportPath": "gopkg.in/yaml.v2", "ImportPath": "gopkg.in/yaml.v2",
"Rev": "d466437aa4adc35830964cffc5b5f262c63ddcb4" "Rev": "a83829b6f1293c91addabc89d0571c246397bbf4"
}, },
{ {
"ImportPath": "k8s.io/heapster/api/v1/types", "ImportPath": "k8s.io/heapster/api/v1/types",

9
Godeps/_workspace/src/gopkg.in/yaml.v2/.travis.yml generated vendored Normal file
View File

@ -0,0 +1,9 @@
language: go
go:
- 1.4
- 1.5
- 1.6
- tip
go_import_path: gopkg.in/yaml.v2

View File

@ -67,7 +67,10 @@ b:
type T struct { type T struct {
A string A string
B struct{C int; D []int ",flow"} B struct {
RenamedC int `yaml:"c"`
D []int `yaml:",flow"`
}
} }
func main() { func main() {

View File

@ -32,9 +32,9 @@ type node struct {
// Parser, produces a node tree out of a libyaml event stream. // Parser, produces a node tree out of a libyaml event stream.
type parser struct { type parser struct {
parser yaml_parser_t parser yaml_parser_t
event yaml_event_t event yaml_event_t
doc *node doc *node
} }
func newParser(b []byte) *parser { func newParser(b []byte) *parser {
@ -194,10 +194,10 @@ type decoder struct {
} }
var ( var (
mapItemType = reflect.TypeOf(MapItem{}) mapItemType = reflect.TypeOf(MapItem{})
durationType = reflect.TypeOf(time.Duration(0)) durationType = reflect.TypeOf(time.Duration(0))
defaultMapType = reflect.TypeOf(map[interface{}]interface{}{}) defaultMapType = reflect.TypeOf(map[interface{}]interface{}{})
ifaceType = defaultMapType.Elem() ifaceType = defaultMapType.Elem()
) )
func newDecoder() *decoder { func newDecoder() *decoder {
@ -476,35 +476,37 @@ func settableValueOf(i interface{}) reflect.Value {
} }
func (d *decoder) sequence(n *node, out reflect.Value) (good bool) { func (d *decoder) sequence(n *node, out reflect.Value) (good bool) {
l := len(n.children)
var iface reflect.Value var iface reflect.Value
switch out.Kind() { switch out.Kind() {
case reflect.Slice: case reflect.Slice:
// okay out.Set(reflect.MakeSlice(out.Type(), l, l))
case reflect.Interface: case reflect.Interface:
// No type hints. Will have to use a generic sequence. // No type hints. Will have to use a generic sequence.
iface = out iface = out
out = settableValueOf(make([]interface{}, 0)) out = settableValueOf(make([]interface{}, l))
default: default:
d.terror(n, yaml_SEQ_TAG, out) d.terror(n, yaml_SEQ_TAG, out)
return false return false
} }
et := out.Type().Elem() et := out.Type().Elem()
l := len(n.children) j := 0
for i := 0; i < l; i++ { for i := 0; i < l; i++ {
e := reflect.New(et).Elem() e := reflect.New(et).Elem()
if ok := d.unmarshal(n.children[i], e); ok { if ok := d.unmarshal(n.children[i], e); ok {
out.Set(reflect.Append(out, e)) out.Index(j).Set(e)
j++
} }
} }
out.Set(out.Slice(0, j))
if iface.IsValid() { if iface.IsValid() {
iface.Set(out) iface.Set(out)
} }
return true return true
} }
func (d *decoder) mapping(n *node, out reflect.Value) (good bool) { func (d *decoder) mapping(n *node, out reflect.Value) (good bool) {
switch out.Kind() { switch out.Kind() {
case reflect.Struct: case reflect.Struct:
@ -605,6 +607,15 @@ func (d *decoder) mappingStruct(n *node, out reflect.Value) (good bool) {
} }
name := settableValueOf("") name := settableValueOf("")
l := len(n.children) l := len(n.children)
var inlineMap reflect.Value
var elemType reflect.Type
if sinfo.InlineMap != -1 {
inlineMap = out.Field(sinfo.InlineMap)
inlineMap.Set(reflect.New(inlineMap.Type()).Elem())
elemType = inlineMap.Type().Elem()
}
for i := 0; i < l; i += 2 { for i := 0; i < l; i += 2 {
ni := n.children[i] ni := n.children[i]
if isMerge(ni) { if isMerge(ni) {
@ -622,6 +633,13 @@ func (d *decoder) mappingStruct(n *node, out reflect.Value) (good bool) {
field = out.FieldByIndex(info.Inline) field = out.FieldByIndex(info.Inline)
} }
d.unmarshal(n.children[i+1], field) d.unmarshal(n.children[i+1], field)
} else if sinfo.InlineMap != -1 {
if inlineMap.IsNil() {
inlineMap.Set(reflect.MakeMap(inlineMap.Type()))
}
value := reflect.New(elemType).Elem()
d.unmarshal(n.children[i+1], value)
inlineMap.SetMapIndex(name, value)
} }
} }
return true return true

View File

@ -1019,7 +1019,7 @@ func yaml_emitter_analyze_scalar(emitter *yaml_emitter_t, value []byte) bool {
preceeded_by_whitespace = true preceeded_by_whitespace = true
for i, w := 0, 0; i < len(value); i += w { for i, w := 0, 0; i < len(value); i += w {
w = width(value[0]) w = width(value[i])
followed_by_whitespace = i+w >= len(value) || is_blank(value, i+w) followed_by_whitespace = i+w >= len(value) || is_blank(value, i+w)
if i == 0 { if i == 0 {

View File

@ -2,6 +2,7 @@ package yaml
import ( import (
"encoding" "encoding"
"fmt"
"reflect" "reflect"
"regexp" "regexp"
"sort" "sort"
@ -74,8 +75,7 @@ func (e *encoder) marshal(tag string, in reflect.Value) {
return return
} }
in = reflect.ValueOf(v) in = reflect.ValueOf(v)
} } else if m, ok := iface.(encoding.TextMarshaler); ok {
if m, ok := iface.(encoding.TextMarshaler); ok {
text, err := m.MarshalText() text, err := m.MarshalText()
if err != nil { if err != nil {
fail(err) fail(err)
@ -165,6 +165,22 @@ func (e *encoder) structv(tag string, in reflect.Value) {
e.flow = info.Flow e.flow = info.Flow
e.marshal("", value) e.marshal("", value)
} }
if sinfo.InlineMap >= 0 {
m := in.Field(sinfo.InlineMap)
if m.Len() > 0 {
e.flow = false
keys := keyList(m.MapKeys())
sort.Sort(keys)
for _, k := range keys {
if _, found := sinfo.FieldsMap[k.String()]; found {
panic(fmt.Sprintf("Can't have key %q in inlined map; conflicts with struct field", k.String()))
}
e.marshal("", k)
e.flow = false
e.marshal("", m.MapIndex(k))
}
}
}
}) })
} }

View File

@ -247,7 +247,7 @@ func yaml_parser_update_buffer(parser *yaml_parser_t, length int) bool {
if parser.encoding == yaml_UTF16LE_ENCODING { if parser.encoding == yaml_UTF16LE_ENCODING {
low, high = 0, 1 low, high = 0, 1
} else { } else {
high, low = 1, 0 low, high = 1, 0
} }
// The UTF-16 encoding is not as simple as one might // The UTF-16 encoding is not as simple as one might
@ -357,23 +357,26 @@ func yaml_parser_update_buffer(parser *yaml_parser_t, length int) bool {
if value <= 0x7F { if value <= 0x7F {
// 0000 0000-0000 007F . 0xxxxxxx // 0000 0000-0000 007F . 0xxxxxxx
parser.buffer[buffer_len+0] = byte(value) parser.buffer[buffer_len+0] = byte(value)
buffer_len += 1
} else if value <= 0x7FF { } else if value <= 0x7FF {
// 0000 0080-0000 07FF . 110xxxxx 10xxxxxx // 0000 0080-0000 07FF . 110xxxxx 10xxxxxx
parser.buffer[buffer_len+0] = byte(0xC0 + (value >> 6)) parser.buffer[buffer_len+0] = byte(0xC0 + (value >> 6))
parser.buffer[buffer_len+1] = byte(0x80 + (value & 0x3F)) parser.buffer[buffer_len+1] = byte(0x80 + (value & 0x3F))
buffer_len += 2
} else if value <= 0xFFFF { } else if value <= 0xFFFF {
// 0000 0800-0000 FFFF . 1110xxxx 10xxxxxx 10xxxxxx // 0000 0800-0000 FFFF . 1110xxxx 10xxxxxx 10xxxxxx
parser.buffer[buffer_len+0] = byte(0xE0 + (value >> 12)) parser.buffer[buffer_len+0] = byte(0xE0 + (value >> 12))
parser.buffer[buffer_len+1] = byte(0x80 + ((value >> 6) & 0x3F)) parser.buffer[buffer_len+1] = byte(0x80 + ((value >> 6) & 0x3F))
parser.buffer[buffer_len+2] = byte(0x80 + (value & 0x3F)) parser.buffer[buffer_len+2] = byte(0x80 + (value & 0x3F))
buffer_len += 3
} else { } else {
// 0001 0000-0010 FFFF . 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx // 0001 0000-0010 FFFF . 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
parser.buffer[buffer_len+0] = byte(0xF0 + (value >> 18)) parser.buffer[buffer_len+0] = byte(0xF0 + (value >> 18))
parser.buffer[buffer_len+1] = byte(0x80 + ((value >> 12) & 0x3F)) parser.buffer[buffer_len+1] = byte(0x80 + ((value >> 12) & 0x3F))
parser.buffer[buffer_len+2] = byte(0x80 + ((value >> 6) & 0x3F)) parser.buffer[buffer_len+2] = byte(0x80 + ((value >> 6) & 0x3F))
parser.buffer[buffer_len+3] = byte(0x80 + (value & 0x3F)) parser.buffer[buffer_len+3] = byte(0x80 + (value & 0x3F))
buffer_len += 4
} }
buffer_len += width
parser.unread++ parser.unread++
} }

View File

@ -961,7 +961,7 @@ func yaml_parser_roll_indent(parser *yaml_parser_t, column, number int, typ yaml
} }
// Pop indentation levels from the indents stack until the current level // Pop indentation levels from the indents stack until the current level
// becomes less or equal to the column. For each intendation level, append // becomes less or equal to the column. For each indentation level, append
// the BLOCK-END token. // the BLOCK-END token.
func yaml_parser_unroll_indent(parser *yaml_parser_t, column int) bool { func yaml_parser_unroll_indent(parser *yaml_parser_t, column int) bool {
// In the flow context, do nothing. // In the flow context, do nothing.
@ -969,7 +969,7 @@ func yaml_parser_unroll_indent(parser *yaml_parser_t, column int) bool {
return true return true
} }
// Loop through the intendation levels in the stack. // Loop through the indentation levels in the stack.
for parser.indent > column { for parser.indent > column {
// Create a token and append it to the queue. // Create a token and append it to the queue.
token := yaml_token_t{ token := yaml_token_t{
@ -1546,7 +1546,7 @@ func yaml_parser_scan_directive(parser *yaml_parser_t, token *yaml_token_t) bool
// Unknown directive. // Unknown directive.
} else { } else {
yaml_parser_set_scanner_error(parser, "while scanning a directive", yaml_parser_set_scanner_error(parser, "while scanning a directive",
start_mark, "found uknown directive name") start_mark, "found unknown directive name")
return false return false
} }
@ -2085,14 +2085,14 @@ func yaml_parser_scan_block_scalar(parser *yaml_parser_t, token *yaml_token_t, l
return false return false
} }
if is_digit(parser.buffer, parser.buffer_pos) { if is_digit(parser.buffer, parser.buffer_pos) {
// Check that the intendation is greater than 0. // Check that the indentation is greater than 0.
if parser.buffer[parser.buffer_pos] == '0' { if parser.buffer[parser.buffer_pos] == '0' {
yaml_parser_set_scanner_error(parser, "while scanning a block scalar", yaml_parser_set_scanner_error(parser, "while scanning a block scalar",
start_mark, "found an intendation indicator equal to 0") start_mark, "found an indentation indicator equal to 0")
return false return false
} }
// Get the intendation level and eat the indicator. // Get the indentation level and eat the indicator.
increment = as_digit(parser.buffer, parser.buffer_pos) increment = as_digit(parser.buffer, parser.buffer_pos)
skip(parser) skip(parser)
} }
@ -2102,7 +2102,7 @@ func yaml_parser_scan_block_scalar(parser *yaml_parser_t, token *yaml_token_t, l
if parser.buffer[parser.buffer_pos] == '0' { if parser.buffer[parser.buffer_pos] == '0' {
yaml_parser_set_scanner_error(parser, "while scanning a block scalar", yaml_parser_set_scanner_error(parser, "while scanning a block scalar",
start_mark, "found an intendation indicator equal to 0") start_mark, "found an indentation indicator equal to 0")
return false return false
} }
increment = as_digit(parser.buffer, parser.buffer_pos) increment = as_digit(parser.buffer, parser.buffer_pos)
@ -2157,7 +2157,7 @@ func yaml_parser_scan_block_scalar(parser *yaml_parser_t, token *yaml_token_t, l
end_mark := parser.mark end_mark := parser.mark
// Set the intendation level if it was specified. // Set the indentation level if it was specified.
var indent int var indent int
if increment > 0 { if increment > 0 {
if parser.indent >= 0 { if parser.indent >= 0 {
@ -2217,7 +2217,7 @@ func yaml_parser_scan_block_scalar(parser *yaml_parser_t, token *yaml_token_t, l
leading_break = read_line(parser, leading_break) leading_break = read_line(parser, leading_break)
// Eat the following intendation spaces and line breaks. // Eat the following indentation spaces and line breaks.
if !yaml_parser_scan_block_scalar_breaks(parser, &indent, &trailing_breaks, start_mark, &end_mark) { if !yaml_parser_scan_block_scalar_breaks(parser, &indent, &trailing_breaks, start_mark, &end_mark) {
return false return false
} }
@ -2245,15 +2245,15 @@ func yaml_parser_scan_block_scalar(parser *yaml_parser_t, token *yaml_token_t, l
return true return true
} }
// Scan intendation spaces and line breaks for a block scalar. Determine the // Scan indentation spaces and line breaks for a block scalar. Determine the
// intendation level if needed. // indentation level if needed.
func yaml_parser_scan_block_scalar_breaks(parser *yaml_parser_t, indent *int, breaks *[]byte, start_mark yaml_mark_t, end_mark *yaml_mark_t) bool { func yaml_parser_scan_block_scalar_breaks(parser *yaml_parser_t, indent *int, breaks *[]byte, start_mark yaml_mark_t, end_mark *yaml_mark_t) bool {
*end_mark = parser.mark *end_mark = parser.mark
// Eat the intendation spaces and line breaks. // Eat the indentation spaces and line breaks.
max_indent := 0 max_indent := 0
for { for {
// Eat the intendation spaces. // Eat the indentation spaces.
if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) { if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
return false return false
} }
@ -2267,10 +2267,10 @@ func yaml_parser_scan_block_scalar_breaks(parser *yaml_parser_t, indent *int, br
max_indent = parser.mark.column max_indent = parser.mark.column
} }
// Check for a tab character messing the intendation. // Check for a tab character messing the indentation.
if (*indent == 0 || parser.mark.column < *indent) && is_tab(parser.buffer, parser.buffer_pos) { if (*indent == 0 || parser.mark.column < *indent) && is_tab(parser.buffer, parser.buffer_pos) {
return yaml_parser_set_scanner_error(parser, "while scanning a block scalar", return yaml_parser_set_scanner_error(parser, "while scanning a block scalar",
start_mark, "found a tab character where an intendation space is expected") start_mark, "found a tab character where an indentation space is expected")
} }
// Have we found a non-empty line? // Have we found a non-empty line?
@ -2655,10 +2655,10 @@ func yaml_parser_scan_plain_scalar(parser *yaml_parser_t, token *yaml_token_t) b
for is_blank(parser.buffer, parser.buffer_pos) || is_break(parser.buffer, parser.buffer_pos) { for is_blank(parser.buffer, parser.buffer_pos) || is_break(parser.buffer, parser.buffer_pos) {
if is_blank(parser.buffer, parser.buffer_pos) { if is_blank(parser.buffer, parser.buffer_pos) {
// Check for tab character that abuse intendation. // Check for tab character that abuse indentation.
if leading_blanks && parser.mark.column < indent && is_tab(parser.buffer, parser.buffer_pos) { if leading_blanks && parser.mark.column < indent && is_tab(parser.buffer, parser.buffer_pos) {
yaml_parser_set_scanner_error(parser, "while scanning a plain scalar", yaml_parser_set_scanner_error(parser, "while scanning a plain scalar",
start_mark, "found a tab character that violate intendation") start_mark, "found a tab character that violate indentation")
return false return false
} }
@ -2687,7 +2687,7 @@ func yaml_parser_scan_plain_scalar(parser *yaml_parser_t, token *yaml_token_t) b
} }
} }
// Check intendation level. // Check indentation level.
if parser.flow_level == 0 && parser.mark.column < indent { if parser.flow_level == 0 && parser.mark.column < indent {
break break
} }

View File

@ -32,7 +32,6 @@ type Unmarshaler interface {
UnmarshalYAML(unmarshal func(interface{}) error) error UnmarshalYAML(unmarshal func(interface{}) error) error
} }
// The Marshaler interface may be implemented by types to customize their // The Marshaler interface may be implemented by types to customize their
// behavior when being marshaled into a YAML document. The returned value // behavior when being marshaled into a YAML document. The returned value
// is marshaled in place of the original value implementing Marshaler. // is marshaled in place of the original value implementing Marshaler.
@ -90,7 +89,7 @@ func Unmarshal(in []byte, out interface{}) (err error) {
} }
d.unmarshal(node, v) d.unmarshal(node, v)
} }
if d.terrors != nil { if len(d.terrors) > 0 {
return &TypeError{d.terrors} return &TypeError{d.terrors}
} }
return nil return nil
@ -118,11 +117,12 @@ func Unmarshal(in []byte, out interface{}) (err error) {
// Does not apply to zero valued structs. // Does not apply to zero valued structs.
// //
// flow Marshal using a flow style (useful for structs, // flow Marshal using a flow style (useful for structs,
// sequences and maps. // sequences and maps).
// //
// inline Inline the struct it's applied to, so its fields // inline Inline the field, which must be a struct or a map,
// are processed as if they were part of the outer // causing all of its fields or keys to be processed as if
// struct. // they were part of the outer struct. For maps, keys must
// not conflict with the yaml keys of other struct fields.
// //
// In addition, if the key is "-", the field is ignored. // In addition, if the key is "-", the field is ignored.
// //
@ -164,7 +164,7 @@ func fail(err error) {
} }
func failf(format string, args ...interface{}) { func failf(format string, args ...interface{}) {
panic(yamlError{fmt.Errorf("yaml: " + format, args...)}) panic(yamlError{fmt.Errorf("yaml: "+format, args...)})
} }
// A TypeError is returned by Unmarshal when one or more fields in // A TypeError is returned by Unmarshal when one or more fields in
@ -222,7 +222,7 @@ func getStructInfo(st reflect.Type) (*structInfo, error) {
inlineMap := -1 inlineMap := -1
for i := 0; i != n; i++ { for i := 0; i != n; i++ {
field := st.Field(i) field := st.Field(i)
if field.PkgPath != "" { if field.PkgPath != "" && !field.Anonymous {
continue // Private field continue // Private field
} }
@ -256,15 +256,14 @@ func getStructInfo(st reflect.Type) (*structInfo, error) {
if inline { if inline {
switch field.Type.Kind() { switch field.Type.Kind() {
// TODO: Implement support for inline maps. case reflect.Map:
//case reflect.Map: if inlineMap >= 0 {
// if inlineMap >= 0 { return nil, errors.New("Multiple ,inline maps in struct " + st.String())
// return nil, errors.New("Multiple ,inline maps in struct " + st.String()) }
// } if field.Type.Key() != reflect.TypeOf("") {
// if field.Type.Key() != reflect.TypeOf("") { return nil, errors.New("Option ,inline needs a map with string keys in struct " + st.String())
// return nil, errors.New("Option ,inline needs a map with string keys in struct " + st.String()) }
// } inlineMap = info.Num
// inlineMap = info.Num
case reflect.Struct: case reflect.Struct:
sinfo, err := getStructInfo(field.Type) sinfo, err := getStructInfo(field.Type)
if err != nil { if err != nil {
@ -325,10 +324,23 @@ func isZero(v reflect.Value) bool {
return v.Len() == 0 return v.Len() == 0
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return v.Int() == 0 return v.Int() == 0
case reflect.Float32, reflect.Float64:
return v.Float() == 0
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
return v.Uint() == 0 return v.Uint() == 0
case reflect.Bool: case reflect.Bool:
return !v.Bool() return !v.Bool()
case reflect.Struct:
vt := v.Type()
for i := v.NumField() - 1; i >= 0; i-- {
if vt.Field(i).PkgPath != "" {
continue // Private field
}
if !isZero(v.Field(i)) {
return false
}
}
return true
} }
return false return false
} }

View File

@ -296,7 +296,7 @@ const (
// Not in original libyaml. // Not in original libyaml.
yaml_BINARY_TAG = "tag:yaml.org,2002:binary" yaml_BINARY_TAG = "tag:yaml.org,2002:binary"
yaml_MERGE_TAG = "tag:yaml.org,2002:merge" yaml_MERGE_TAG = "tag:yaml.org,2002:merge"
yaml_DEFAULT_SCALAR_TAG = yaml_STR_TAG // The default scalar tag is !!str. yaml_DEFAULT_SCALAR_TAG = yaml_STR_TAG // The default scalar tag is !!str.
yaml_DEFAULT_SEQUENCE_TAG = yaml_SEQ_TAG // The default sequence tag is !!seq. yaml_DEFAULT_SEQUENCE_TAG = yaml_SEQ_TAG // The default sequence tag is !!seq.