diff --git a/initialize/config.go b/initialize/config.go index 9d82084..0ccaef4 100644 --- a/initialize/config.go +++ b/initialize/config.go @@ -6,7 +6,7 @@ import ( "log" "path" - "github.com/coreos/coreos-cloudinit/third_party/launchpad.net/goyaml" + "github.com/coreos/coreos-cloudinit/third_party/gopkg.in/yaml.v1" "github.com/coreos/coreos-cloudinit/network" "github.com/coreos/coreos-cloudinit/system" @@ -51,12 +51,12 @@ type warner func(format string, v ...interface{}) func warnOnUnrecognizedKeys(contents string, warn warner) { // Generate a map of all understood cloud config options var cc map[string]interface{} - b, _ := goyaml.Marshal(&CloudConfig{}) - goyaml.Unmarshal(b, &cc) + b, _ := yaml.Marshal(&CloudConfig{}) + yaml.Unmarshal(b, &cc) // Now unmarshal the entire provided contents var c map[string]interface{} - goyaml.Unmarshal([]byte(contents), &c) + yaml.Unmarshal([]byte(contents), &c) // Check that every key in the contents exists in the cloud config for k, _ := range c { @@ -84,8 +84,8 @@ func warnOnUnrecognizedKeys(contents string, warn warner) { // Check for any badly-specified users, if any are set if users, ok := c["users"]; ok { var known map[string]interface{} - b, _ := goyaml.Marshal(&system.User{}) - goyaml.Unmarshal(b, &known) + b, _ := yaml.Marshal(&system.User{}) + yaml.Unmarshal(b, &known) if set, ok := users.([]interface{}); ok { for _, u := range set { @@ -107,8 +107,8 @@ func warnOnUnrecognizedKeys(contents string, warn warner) { // Check for any badly-specified files, if any are set if files, ok := c["write_files"]; ok { var known map[string]interface{} - b, _ := goyaml.Marshal(&system.File{}) - goyaml.Unmarshal(b, &known) + b, _ := yaml.Marshal(&system.File{}) + yaml.Unmarshal(b, &known) if set, ok := files.([]interface{}); ok { for _, f := range set { @@ -133,7 +133,7 @@ func warnOnUnrecognizedKeys(contents string, warn warner) { // fields but log encountering them. func NewCloudConfig(contents string) (*CloudConfig, error) { var cfg CloudConfig - err := goyaml.Unmarshal([]byte(contents), &cfg) + err := yaml.Unmarshal([]byte(contents), &cfg) if err != nil { return &cfg, err } @@ -142,7 +142,7 @@ func NewCloudConfig(contents string) (*CloudConfig, error) { } func (cc CloudConfig) String() string { - bytes, err := goyaml.Marshal(cc) + bytes, err := yaml.Marshal(cc) if err != nil { return "" } diff --git a/initialize/user_data.go b/initialize/user_data.go index 280cad6..7592602 100644 --- a/initialize/user_data.go +++ b/initialize/user_data.go @@ -16,7 +16,7 @@ func ParseUserData(contents string) (interface{}, error) { // Explicitly trim the header so we can handle user-data from // non-unix operating systems. The rest of the file is parsed - // by goyaml, which correctly handles CRLF. + // by yaml, which correctly handles CRLF. header = strings.TrimSpace(header) if strings.HasPrefix(header, "#!") { diff --git a/third_party/launchpad.net/goyaml/LICENSE b/third_party/gopkg.in/yaml.v1/LICENSE similarity index 100% rename from third_party/launchpad.net/goyaml/LICENSE rename to third_party/gopkg.in/yaml.v1/LICENSE diff --git a/third_party/launchpad.net/goyaml/LICENSE.libyaml b/third_party/gopkg.in/yaml.v1/LICENSE.libyaml similarity index 80% rename from third_party/launchpad.net/goyaml/LICENSE.libyaml rename to third_party/gopkg.in/yaml.v1/LICENSE.libyaml index 050ced2..8da58fb 100644 --- a/third_party/launchpad.net/goyaml/LICENSE.libyaml +++ b/third_party/gopkg.in/yaml.v1/LICENSE.libyaml @@ -1,3 +1,15 @@ +The following files were ported to Go from C files of libyaml, and thus +are still covered by their original copyright and license: + + apic.go + emitterc.go + parserc.go + readerc.go + scannerc.go + writerc.go + yamlh.go + yamlprivateh.go + Copyright (c) 2006 Kirill Simonov Permission is hereby granted, free of charge, to any person obtaining a copy of diff --git a/third_party/gopkg.in/yaml.v1/README.md b/third_party/gopkg.in/yaml.v1/README.md new file mode 100644 index 0000000..896687b --- /dev/null +++ b/third_party/gopkg.in/yaml.v1/README.md @@ -0,0 +1,128 @@ +# YAML support for the Go language + +Introduction +------------ + +The yaml package enables Go programs to comfortably encode and decode YAML +values. It was developed within [Canonical](https://www.canonical.com) as +part of the [juju](https://juju.ubuntu.com) project, and is based on a +pure Go port of the well-known [libyaml](http://pyyaml.org/wiki/LibYAML) +C library to parse and generate YAML data quickly and reliably. + +Compatibility +------------- + +The yaml package is almost compatible with YAML 1.1, including support for +anchors, tags, etc. There are still a few missing bits, such as document +merging, base-60 floats (huh?), and multi-document unmarshalling. These +features are not hard to add, and will be introduced as necessary. + +Installation and usage +---------------------- + +The import path for the package is *gopkg.in/yaml.v1*. + +To install it, run: + + go get gopkg.in/yaml.v1 + +API documentation +----------------- + +If opened in a browser, the import path itself leads to the API documentation: + + * [https://gopkg.in/yaml.v1](https://gopkg.in/yaml.v1) + +API stability +------------- + +The package API for yaml v1 will remain stable as described in [gopkg.in](https://gopkg.in). + + +License +------- + +The yaml package is licensed under the LGPL with an exception that allows it to be linked statically. Please see the LICENSE file for details. + + +Example +------- + +```Go +package main + +import ( + "fmt" + "log" + + "gopkg.in/yaml.v1" +) + +var data = ` +a: Easy! +b: + c: 2 + d: [3, 4] +` + +type T struct { + A string + B struct{C int; D []int ",flow"} +} + +func main() { + t := T{} + + err := yaml.Unmarshal([]byte(data), &t) + if err != nil { + log.Fatalf("error: %v", err) + } + fmt.Printf("--- t:\n%v\n\n", t) + + d, err := yaml.Marshal(&t) + if err != nil { + log.Fatalf("error: %v", err) + } + fmt.Printf("--- t dump:\n%s\n\n", string(d)) + + m := make(map[interface{}]interface{}) + + err = yaml.Unmarshal([]byte(data), &m) + if err != nil { + log.Fatalf("error: %v", err) + } + fmt.Printf("--- m:\n%v\n\n", m) + + d, err = yaml.Marshal(&m) + if err != nil { + log.Fatalf("error: %v", err) + } + fmt.Printf("--- m dump:\n%s\n\n", string(d)) +} +``` + +This example will generate the following output: + +``` +--- t: +{Easy! {2 [3 4]}} + +--- t dump: +a: Easy! +b: + c: 2 + d: [3, 4] + + +--- m: +map[a:Easy! b:map[c:2 d:[3 4]]] + +--- m dump: +a: Easy! +b: + c: 2 + d: + - 3 + - 4 +``` + diff --git a/third_party/launchpad.net/goyaml/apic.go b/third_party/gopkg.in/yaml.v1/apic.go similarity index 99% rename from third_party/launchpad.net/goyaml/apic.go rename to third_party/gopkg.in/yaml.v1/apic.go index 29be91d..95ec014 100644 --- a/third_party/launchpad.net/goyaml/apic.go +++ b/third_party/gopkg.in/yaml.v1/apic.go @@ -1,4 +1,4 @@ -package goyaml +package yaml import ( "io" diff --git a/third_party/launchpad.net/goyaml/decode.go b/third_party/gopkg.in/yaml.v1/decode.go similarity index 86% rename from third_party/launchpad.net/goyaml/decode.go rename to third_party/gopkg.in/yaml.v1/decode.go index 6586be4..74eda3c 100644 --- a/third_party/launchpad.net/goyaml/decode.go +++ b/third_party/gopkg.in/yaml.v1/decode.go @@ -1,8 +1,9 @@ -package goyaml +package yaml import ( "reflect" "strconv" + "time" ) const ( @@ -211,6 +212,16 @@ func newDecoder() *decoder { // returned to call SetYAML() with the value of *out once it's defined. // func (d *decoder) setter(tag string, out *reflect.Value, good *bool) (set func()) { + if (*out).Kind() != reflect.Ptr && (*out).CanAddr() { + setter, _ := (*out).Addr().Interface().(Setter) + if setter != nil { + var arg interface{} + *out = reflect.ValueOf(&arg).Elem() + return func() { + *good = setter.SetYAML(tag, arg) + } + } + } again := true for again { again = false @@ -279,16 +290,19 @@ func (d *decoder) alias(n *node, out reflect.Value) (good bool) { return good } +var durationType = reflect.TypeOf(time.Duration(0)) + func (d *decoder) scalar(n *node, out reflect.Value) (good bool) { var tag string var resolved interface{} if n.tag == "" && !n.implicit { + tag = "!!str" resolved = n.value } else { tag, resolved = resolve(n.tag, n.value) - if set := d.setter(tag, &out, &good); set != nil { - defer set() - } + } + if set := d.setter(tag, &out, &good); set != nil { + defer set() } switch out.Kind() { case reflect.String: @@ -320,6 +334,14 @@ func (d *decoder) scalar(n *node, out reflect.Value) (good bool) { out.SetInt(int64(resolved)) good = true } + case string: + if out.Type() == durationType { + d, err := time.ParseDuration(resolved) + if err == nil { + out.SetInt(int64(d)) + good = true + } + } } case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: switch resolved := resolved.(type) { @@ -437,6 +459,10 @@ func (d *decoder) mapping(n *node, out reflect.Value) (good bool) { } l := len(n.children) for i := 0; i < l; i += 2 { + if isMerge(n.children[i]) { + d.merge(n.children[i+1], out) + continue + } k := reflect.New(kt).Elem() if d.unmarshal(n.children[i], k) { e := reflect.New(et).Elem() @@ -456,7 +482,12 @@ func (d *decoder) mappingStruct(n *node, out reflect.Value) (good bool) { name := settableValueOf("") l := len(n.children) for i := 0; i < l; i += 2 { - if !d.unmarshal(n.children[i], name) { + ni := n.children[i] + if isMerge(ni) { + d.merge(n.children[i+1], out) + continue + } + if !d.unmarshal(ni, name) { continue } if info, ok := sinfo.FieldsMap[name.String()]; ok { @@ -471,3 +502,37 @@ func (d *decoder) mappingStruct(n *node, out reflect.Value) (good bool) { } return true } + +func (d *decoder) merge(n *node, out reflect.Value) { + const wantMap = "map merge requires map or sequence of maps as the value" + switch n.kind { + case mappingNode: + d.unmarshal(n, out) + case aliasNode: + an, ok := d.doc.anchors[n.value] + if ok && an.kind != mappingNode { + panic(wantMap) + } + d.unmarshal(n, out) + case sequenceNode: + // Step backwards as earlier nodes take precedence. + for i := len(n.children)-1; i >= 0; i-- { + ni := n.children[i] + if ni.kind == aliasNode { + an, ok := d.doc.anchors[ni.value] + if ok && an.kind != mappingNode { + panic(wantMap) + } + } else if ni.kind != mappingNode { + panic(wantMap) + } + d.unmarshal(ni, out) + } + default: + panic(wantMap) + } +} + +func isMerge(n *node) bool { + return n.kind == scalarNode && n.value == "<<" && (n.implicit == true || n.tag == "!!merge" || n.tag == "tag:yaml.org,2002:merge") +} diff --git a/third_party/launchpad.net/goyaml/decode_test.go b/third_party/gopkg.in/yaml.v1/decode_test.go similarity index 76% rename from third_party/launchpad.net/goyaml/decode_test.go rename to third_party/gopkg.in/yaml.v1/decode_test.go index 6b13d50..a290645 100644 --- a/third_party/launchpad.net/goyaml/decode_test.go +++ b/third_party/gopkg.in/yaml.v1/decode_test.go @@ -1,10 +1,11 @@ -package goyaml_test +package yaml_test import ( - . "launchpad.net/gocheck" - "github.com/coreos/coreos-cloudinit/third_party/launchpad.net/goyaml" + . "gopkg.in/check.v1" + "gopkg.in/yaml.v1" "math" "reflect" + "time" ) var unmarshalIntTest = 123 @@ -350,6 +351,32 @@ var unmarshalTests = []struct { C inlineB `yaml:",inline"` }{1, inlineB{2, inlineC{3}}}, }, + + // bug 1243827 + { + "a: -b_c", + map[string]interface{}{"a": "-b_c"}, + }, + { + "a: +b_c", + map[string]interface{}{"a": "+b_c"}, + }, + { + "a: 50cent_of_dollar", + map[string]interface{}{"a": "50cent_of_dollar"}, + }, + + // Duration + { + "a: 3s", + map[string]time.Duration{"a": 3 * time.Second}, + }, + + // Issue #24. + { + "a: ", + map[string]string{"a": ""}, + }, } type inlineB struct { @@ -377,7 +404,7 @@ func (s *S) TestUnmarshal(c *C) { pv := reflect.New(pt.Elem()) value = pv.Interface() } - err := goyaml.Unmarshal([]byte(item.data), value) + err := yaml.Unmarshal([]byte(item.data), value) c.Assert(err, IsNil, Commentf("Item #%d", i)) if t.Kind() == reflect.String { c.Assert(*value.(*string), Equals, item.value, Commentf("Item #%d", i)) @@ -389,7 +416,7 @@ func (s *S) TestUnmarshal(c *C) { func (s *S) TestUnmarshalNaN(c *C) { value := map[string]interface{}{} - err := goyaml.Unmarshal([]byte("notanum: .NaN"), &value) + err := yaml.Unmarshal([]byte("notanum: .NaN"), &value) c.Assert(err, IsNil) c.Assert(math.IsNaN(value["notanum"].(float64)), Equals, true) } @@ -408,7 +435,7 @@ var unmarshalErrorTests = []struct { func (s *S) TestUnmarshalErrors(c *C) { for _, item := range unmarshalErrorTests { var value interface{} - err := goyaml.Unmarshal([]byte(item.data), &value) + err := yaml.Unmarshal([]byte(item.data), &value) c.Assert(err, ErrorMatches, item.error, Commentf("Partial unmarshal: %#v", value)) } } @@ -421,6 +448,8 @@ var setterTests = []struct { {"_: [1,A]", "!!seq", []interface{}{1, "A"}}, {"_: 10", "!!int", 10}, {"_: null", "!!null", nil}, + {`_: BAR!`, "!!str", "BAR!"}, + {`_: "BAR!"`, "!!str", "BAR!"}, {"_: !!foo 'BAR!'", "!!foo", "BAR!"}, } @@ -442,17 +471,31 @@ func (o *typeWithSetter) SetYAML(tag string, value interface{}) (ok bool) { return true } -type typeWithSetterField struct { +type setterPointerType struct { Field *typeWithSetter "_" } -func (s *S) TestUnmarshalWithSetter(c *C) { +type setterValueType struct { + Field typeWithSetter "_" +} + +func (s *S) TestUnmarshalWithPointerSetter(c *C) { for _, item := range setterTests { - obj := &typeWithSetterField{} - err := goyaml.Unmarshal([]byte(item.data), obj) + obj := &setterPointerType{} + err := yaml.Unmarshal([]byte(item.data), obj) c.Assert(err, IsNil) - c.Assert(obj.Field, NotNil, - Commentf("Pointer not initialized (%#v)", item.value)) + c.Assert(obj.Field, NotNil, Commentf("Pointer not initialized (%#v)", item.value)) + c.Assert(obj.Field.tag, Equals, item.tag) + c.Assert(obj.Field.value, DeepEquals, item.value) + } +} + +func (s *S) TestUnmarshalWithValueSetter(c *C) { + for _, item := range setterTests { + obj := &setterValueType{} + err := yaml.Unmarshal([]byte(item.data), obj) + c.Assert(err, IsNil) + c.Assert(obj.Field, NotNil, Commentf("Pointer not initialized (%#v)", item.value)) c.Assert(obj.Field.tag, Equals, item.tag) c.Assert(obj.Field.value, DeepEquals, item.value) } @@ -460,7 +503,7 @@ func (s *S) TestUnmarshalWithSetter(c *C) { func (s *S) TestUnmarshalWholeDocumentWithSetter(c *C) { obj := &typeWithSetter{} - err := goyaml.Unmarshal([]byte(setterTests[0].data), obj) + err := yaml.Unmarshal([]byte(setterTests[0].data), obj) c.Assert(err, IsNil) c.Assert(obj.tag, Equals, setterTests[0].tag) value, ok := obj.value.(map[interface{}]interface{}) @@ -477,8 +520,8 @@ func (s *S) TestUnmarshalWithFalseSetterIgnoresValue(c *C) { }() m := map[string]*typeWithSetter{} - data := "{abc: 1, def: 2, ghi: 3, jkl: 4}" - err := goyaml.Unmarshal([]byte(data), m) + data := `{abc: 1, def: 2, ghi: 3, jkl: 4}` + err := yaml.Unmarshal([]byte(data), m) c.Assert(err, IsNil) c.Assert(m["abc"], NotNil) c.Assert(m["def"], IsNil) @@ -489,6 +532,98 @@ func (s *S) TestUnmarshalWithFalseSetterIgnoresValue(c *C) { c.Assert(m["ghi"].value, Equals, 3) } +// From http://yaml.org/type/merge.html +var mergeTests = ` +anchors: + - &CENTER { "x": 1, "y": 2 } + - &LEFT { "x": 0, "y": 2 } + - &BIG { "r": 10 } + - &SMALL { "r": 1 } + +# All the following maps are equal: + +plain: + # Explicit keys + "x": 1 + "y": 2 + "r": 10 + label: center/big + +mergeOne: + # Merge one map + << : *CENTER + "r": 10 + label: center/big + +mergeMultiple: + # Merge multiple maps + << : [ *CENTER, *BIG ] + label: center/big + +override: + # Override + << : [ *BIG, *LEFT, *SMALL ] + "x": 1 + label: center/big + +shortTag: + # Explicit short merge tag + !!merge "<<" : [ *CENTER, *BIG ] + label: center/big + +longTag: + # Explicit merge long tag + ! "<<" : [ *CENTER, *BIG ] + label: center/big + +inlineMap: + # Inlined map + << : {"x": 1, "y": 2, "r": 10} + label: center/big + +inlineSequenceMap: + # Inlined map in sequence + << : [ *CENTER, {"r": 10} ] + label: center/big +` + +func (s *S) TestMerge(c *C) { + var want = map[interface{}]interface{}{ + "x": 1, + "y": 2, + "r": 10, + "label": "center/big", + } + + var m map[string]interface{} + err := yaml.Unmarshal([]byte(mergeTests), &m) + c.Assert(err, IsNil) + for name, test := range m { + if name == "anchors" { + continue + } + c.Assert(test, DeepEquals, want, Commentf("test %q failed", name)) + } +} + +func (s *S) TestMergeStruct(c *C) { + type Data struct { + X, Y, R int + Label string + } + want := Data{1, 2, 10, "center/big"} + + var m map[string]Data + err := yaml.Unmarshal([]byte(mergeTests), &m) + c.Assert(err, IsNil) + for name, test := range m { + if name == "anchors" { + continue + } + c.Assert(test, Equals, want, Commentf("test %q failed", name)) + } +} + //var data []byte //func init() { // var err error @@ -502,7 +637,7 @@ func (s *S) TestUnmarshalWithFalseSetterIgnoresValue(c *C) { // var err error // for i := 0; i < c.N; i++ { // var v map[string]interface{} -// err = goyaml.Unmarshal(data, &v) +// err = yaml.Unmarshal(data, &v) // } // if err != nil { // panic(err) @@ -511,9 +646,9 @@ func (s *S) TestUnmarshalWithFalseSetterIgnoresValue(c *C) { // //func (s *S) BenchmarkMarshal(c *C) { // var v map[string]interface{} -// goyaml.Unmarshal(data, &v) +// yaml.Unmarshal(data, &v) // c.ResetTimer() // for i := 0; i < c.N; i++ { -// goyaml.Marshal(&v) +// yaml.Marshal(&v) // } //} diff --git a/third_party/launchpad.net/goyaml/emitterc.go b/third_party/gopkg.in/yaml.v1/emitterc.go similarity index 99% rename from third_party/launchpad.net/goyaml/emitterc.go rename to third_party/gopkg.in/yaml.v1/emitterc.go index 49c7f46..542ffd2 100644 --- a/third_party/launchpad.net/goyaml/emitterc.go +++ b/third_party/gopkg.in/yaml.v1/emitterc.go @@ -1,4 +1,4 @@ -package goyaml +package yaml import ( "bytes" diff --git a/third_party/launchpad.net/goyaml/encode.go b/third_party/gopkg.in/yaml.v1/encode.go similarity index 96% rename from third_party/launchpad.net/goyaml/encode.go rename to third_party/gopkg.in/yaml.v1/encode.go index b228a10..1d928b0 100644 --- a/third_party/launchpad.net/goyaml/encode.go +++ b/third_party/gopkg.in/yaml.v1/encode.go @@ -1,9 +1,10 @@ -package goyaml +package yaml import ( "reflect" "sort" "strconv" + "time" ) type encoder struct { @@ -85,7 +86,11 @@ func (e *encoder) marshal(tag string, in reflect.Value) { case reflect.String: e.stringv(tag, in) case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - e.intv(tag, in) + if in.Type() == durationType { + e.stringv(tag, reflect.ValueOf(in.Interface().(time.Duration).String())) + } else { + e.intv(tag, in) + } case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: e.uintv(tag, in) case reflect.Float32, reflect.Float64: diff --git a/third_party/launchpad.net/goyaml/encode_test.go b/third_party/gopkg.in/yaml.v1/encode_test.go similarity index 93% rename from third_party/launchpad.net/goyaml/encode_test.go rename to third_party/gopkg.in/yaml.v1/encode_test.go index 91944b0..83dc490 100644 --- a/third_party/launchpad.net/goyaml/encode_test.go +++ b/third_party/gopkg.in/yaml.v1/encode_test.go @@ -1,12 +1,13 @@ -package goyaml_test +package yaml_test import ( "fmt" - . "launchpad.net/gocheck" - "github.com/coreos/coreos-cloudinit/third_party/launchpad.net/goyaml" + "gopkg.in/yaml.v1" + . "gopkg.in/check.v1" "math" "strconv" "strings" + "time" ) var marshalIntTest = 123 @@ -212,11 +213,23 @@ var marshalTests = []struct { }{1, inlineB{2, inlineC{3}}}, "a: 1\nb: 2\nc: 3\n", }, + + // Duration + { + map[string]time.Duration{"a": 3 * time.Second}, + "a: 3s\n", + }, + + // Issue #24. + { + map[string]string{"a": ""}, + "a: \n", + }, } func (s *S) TestMarshal(c *C) { for _, item := range marshalTests { - data, err := goyaml.Marshal(item.value) + data, err := yaml.Marshal(item.value) c.Assert(err, IsNil) c.Assert(string(data), Equals, item.data) } @@ -237,7 +250,7 @@ var marshalErrorTests = []struct { func (s *S) TestMarshalErrors(c *C) { for _, item := range marshalErrorTests { - _, err := goyaml.Marshal(item.value) + _, err := yaml.Marshal(item.value) c.Assert(err, ErrorMatches, item.error) } } @@ -269,12 +282,12 @@ func (s *S) TestMarshalTypeCache(c *C) { var err error func() { type T struct{ A int } - data, err = goyaml.Marshal(&T{}) + data, err = yaml.Marshal(&T{}) c.Assert(err, IsNil) }() func() { type T struct{ B int } - data, err = goyaml.Marshal(&T{}) + data, err = yaml.Marshal(&T{}) c.Assert(err, IsNil) }() c.Assert(string(data), Equals, "b: 0\n") @@ -298,7 +311,7 @@ func (s *S) TestMashalWithGetter(c *C) { obj := &typeWithGetterField{} obj.Field.tag = item.tag obj.Field.value = item.value - data, err := goyaml.Marshal(obj) + data, err := yaml.Marshal(obj) c.Assert(err, IsNil) c.Assert(string(data), Equals, string(item.data)) } @@ -308,7 +321,7 @@ func (s *S) TestUnmarshalWholeDocumentWithGetter(c *C) { obj := &typeWithGetter{} obj.tag = "" obj.value = map[string]string{"hello": "world!"} - data, err := goyaml.Marshal(obj) + data, err := yaml.Marshal(obj) c.Assert(err, IsNil) c.Assert(string(data), Equals, "hello: world!\n") } @@ -356,7 +369,7 @@ func (s *S) TestSortedOutput(c *C) { for _, k := range order { m[k] = 1 } - data, err := goyaml.Marshal(m) + data, err := yaml.Marshal(m) c.Assert(err, IsNil) out := "\n" + string(data) last := 0 diff --git a/third_party/launchpad.net/goyaml/parserc.go b/third_party/gopkg.in/yaml.v1/parserc.go similarity index 99% rename from third_party/launchpad.net/goyaml/parserc.go rename to third_party/gopkg.in/yaml.v1/parserc.go index 0fdfa4e..0a7037a 100644 --- a/third_party/launchpad.net/goyaml/parserc.go +++ b/third_party/gopkg.in/yaml.v1/parserc.go @@ -1,4 +1,4 @@ -package goyaml +package yaml import ( "bytes" diff --git a/third_party/launchpad.net/goyaml/readerc.go b/third_party/gopkg.in/yaml.v1/readerc.go similarity index 99% rename from third_party/launchpad.net/goyaml/readerc.go rename to third_party/gopkg.in/yaml.v1/readerc.go index c732935..d5fb097 100644 --- a/third_party/launchpad.net/goyaml/readerc.go +++ b/third_party/gopkg.in/yaml.v1/readerc.go @@ -1,4 +1,4 @@ -package goyaml +package yaml import ( "io" diff --git a/third_party/launchpad.net/goyaml/resolve.go b/third_party/gopkg.in/yaml.v1/resolve.go similarity index 86% rename from third_party/launchpad.net/goyaml/resolve.go rename to third_party/gopkg.in/yaml.v1/resolve.go index a31a533..863c314 100644 --- a/third_party/launchpad.net/goyaml/resolve.go +++ b/third_party/gopkg.in/yaml.v1/resolve.go @@ -1,4 +1,4 @@ -package goyaml +package yaml import ( "math" @@ -27,7 +27,6 @@ func init() { t[int(c)] = 'M' // In map } t[int('.')] = '.' // Float (potentially in map) - t[int('<')] = '<' // Merge var resolveMapList = []struct { v interface{} @@ -45,6 +44,7 @@ func init() { {math.Inf(+1), "!!float", []string{".inf", ".Inf", ".INF"}}, {math.Inf(+1), "!!float", []string{"+.inf", "+.Inf", "+.INF"}}, {math.Inf(-1), "!!float", []string{"-.inf", "-.Inf", "-.INF"}}, + {"<<", "!!merge", []string{"<<"}}, } m := resolveMap @@ -113,13 +113,8 @@ func resolve(tag string, in string) (rtag string, out interface{}) { case 'D', 'S': // Int, float, or timestamp. - for i := 0; i != len(in); i++ { - if in[i] == '_' { - in = strings.Replace(in, "_", "", -1) - break - } - } - intv, err := strconv.ParseInt(in, 0, 64) + plain := strings.Replace(in, "_", "", -1) + intv, err := strconv.ParseInt(plain, 0, 64) if err == nil { if intv == int64(int(intv)) { return "!!int", int(intv) @@ -127,26 +122,23 @@ func resolve(tag string, in string) (rtag string, out interface{}) { return "!!int", intv } } - floatv, err := strconv.ParseFloat(in, 64) + floatv, err := strconv.ParseFloat(plain, 64) if err == nil { return "!!float", floatv } - if strings.HasPrefix(in, "0b") { - intv, err := strconv.ParseInt(in[2:], 2, 64) + if strings.HasPrefix(plain, "0b") { + intv, err := strconv.ParseInt(plain[2:], 2, 64) if err == nil { return "!!int", int(intv) } - } else if strings.HasPrefix(in, "-0b") { - intv, err := strconv.ParseInt(in[3:], 2, 64) + } else if strings.HasPrefix(plain, "-0b") { + intv, err := strconv.ParseInt(plain[3:], 2, 64) if err == nil { return "!!int", -int(intv) } } // XXX Handle timestamps here. - case '<': - // XXX Handle merge (<<) here. - default: panic("resolveTable item not yet handled: " + string([]byte{c}) + " (with " + in + ")") diff --git a/third_party/launchpad.net/goyaml/scannerc.go b/third_party/gopkg.in/yaml.v1/scannerc.go similarity index 99% rename from third_party/launchpad.net/goyaml/scannerc.go rename to third_party/gopkg.in/yaml.v1/scannerc.go index 943a597..fe93b19 100644 --- a/third_party/launchpad.net/goyaml/scannerc.go +++ b/third_party/gopkg.in/yaml.v1/scannerc.go @@ -1,4 +1,4 @@ -package goyaml +package yaml import ( "bytes" diff --git a/third_party/launchpad.net/goyaml/sorter.go b/third_party/gopkg.in/yaml.v1/sorter.go similarity index 99% rename from third_party/launchpad.net/goyaml/sorter.go rename to third_party/gopkg.in/yaml.v1/sorter.go index e1ee85f..5958822 100644 --- a/third_party/launchpad.net/goyaml/sorter.go +++ b/third_party/gopkg.in/yaml.v1/sorter.go @@ -1,4 +1,4 @@ -package goyaml +package yaml import ( "reflect" diff --git a/third_party/launchpad.net/goyaml/suite_test.go b/third_party/gopkg.in/yaml.v1/suite_test.go similarity index 68% rename from third_party/launchpad.net/goyaml/suite_test.go rename to third_party/gopkg.in/yaml.v1/suite_test.go index 963ab0b..c5cf1ed 100644 --- a/third_party/launchpad.net/goyaml/suite_test.go +++ b/third_party/gopkg.in/yaml.v1/suite_test.go @@ -1,7 +1,7 @@ -package goyaml_test +package yaml_test import ( - . "launchpad.net/gocheck" + . "gopkg.in/check.v1" "testing" ) diff --git a/third_party/launchpad.net/goyaml/writerc.go b/third_party/gopkg.in/yaml.v1/writerc.go similarity index 99% rename from third_party/launchpad.net/goyaml/writerc.go rename to third_party/gopkg.in/yaml.v1/writerc.go index 4809bfb..190362f 100644 --- a/third_party/launchpad.net/goyaml/writerc.go +++ b/third_party/gopkg.in/yaml.v1/writerc.go @@ -1,4 +1,4 @@ -package goyaml +package yaml // Set the writer error and return false. func yaml_emitter_set_writer_error(emitter *yaml_emitter_t, problem string) bool { diff --git a/third_party/launchpad.net/goyaml/goyaml.go b/third_party/gopkg.in/yaml.v1/yaml.go similarity index 75% rename from third_party/launchpad.net/goyaml/goyaml.go rename to third_party/gopkg.in/yaml.v1/yaml.go index dbc633e..44b0cc6 100644 --- a/third_party/launchpad.net/goyaml/goyaml.go +++ b/third_party/gopkg.in/yaml.v1/yaml.go @@ -1,5 +1,10 @@ -// Package goyaml implements YAML support for the Go language. -package goyaml +// Package yaml implements YAML support for the Go language. +// +// Source code and other details for the project are available at GitHub: +// +// https://github.com/go-yaml/yaml +// +package yaml import ( "errors" @@ -28,32 +33,31 @@ func handleErr(err *error) { } } -// Objects implementing the goyaml.Setter interface will receive the YAML -// tag and value via the SetYAML method during unmarshaling, rather than -// being implicitly assigned by the goyaml machinery. If setting the value -// works, the method should return true. If it returns false, the given -// value will be omitted from maps and slices. +// The Setter interface may be implemented by types to do their own custom +// unmarshalling of YAML values, rather than being implicitly assigned by +// the yaml package machinery. If setting the value works, the method should +// return true. If it returns false, the value is considered unsupported +// and is omitted from maps and slices. type Setter interface { SetYAML(tag string, value interface{}) bool } -// Objects implementing the goyaml.Getter interface will get the GetYAML() -// method called when goyaml is requested to marshal the given value, and -// the result of this method will be marshaled in place of the actual object. +// The Getter interface is implemented by types to do their own custom +// marshalling into a YAML tag and value. type Getter interface { GetYAML() (tag string, value interface{}) } // Unmarshal decodes the first document found within the in byte slice -// and assigns decoded values into the object pointed by out. +// and assigns decoded values into the out value. // -// Maps, pointers to structs and ints, etc, may all be used as out values. -// If an internal pointer within a struct is not initialized, goyaml -// will initialize it if necessary for unmarshalling the provided data, -// but the struct provided as out must not be a nil pointer. +// Maps and pointers (to a struct, string, int, etc) are accepted as out +// values. If an internal pointer within a struct is not initialized, +// the yaml package will initialize it if necessary for unmarshalling +// the provided data. The out parameter must not be nil. // // The type of the decoded values and the type of out will be considered, -// and Unmarshal() will do the best possible job to unmarshal values +// and Unmarshal will do the best possible job to unmarshal values // appropriately. It is NOT considered an error, though, to skip values // because they are not available in the decoded YAML, or if they are not // compatible with the out value. To ensure something was properly @@ -61,11 +65,11 @@ type Getter interface { // field (usually the zero value). // // Struct fields are only unmarshalled if they are exported (have an -// upper case first letter), and will be unmarshalled using the field -// name lowercased by default. When custom field names are desired, the -// tag value may be used to tweak the name. Everything before the first -// comma in the field tag will be used as the name. The values following -// the comma are used to tweak the marshalling process (see Marshal). +// upper case first letter), and are unmarshalled using the field name +// lowercased as the default key. Custom keys may be defined via the +// "yaml" name in the field tag: the content preceding the first comma +// is used as the key, and the following comma-separated options are +// used to tweak the marshalling process (see Marshal). // Conflicting names result in a runtime error. // // For example: @@ -75,7 +79,7 @@ type Getter interface { // B int // } // var T t -// goyaml.Unmarshal([]byte("a: 1\nb: 2"), &t) +// yaml.Unmarshal([]byte("a: 1\nb: 2"), &t) // // See the documentation of Marshal for the format of tags and a list of // supported tag options. @@ -94,14 +98,16 @@ func Unmarshal(in []byte, out interface{}) (err error) { // Marshal serializes the value provided into a YAML document. The structure // of the generated document will reflect the structure of the value itself. -// Maps, pointers to structs and ints, etc, may all be used as the in value. +// Maps and pointers (to struct, string, int, etc) are accepted as the in value. // -// In the case of struct values, only exported fields will be serialized. -// The lowercased field name is used as the key for each exported field, -// but this behavior may be changed using the respective field tag. -// The tag may also contain flags to tweak the marshalling behavior for -// the field. Conflicting names result in a runtime error. The tag format -// accepted is: +// Struct fields are only unmarshalled if they are exported (have an upper case +// first letter), and are unmarshalled using the field name lowercased as the +// default key. Custom keys may be defined via the "yaml" name in the field +// tag: the content preceding the first comma is used as the key, and the +// following comma-separated options are used to tweak the marshalling process. +// Conflicting names result in a runtime error. +// +// The field tag format accepted is: // // `(...) yaml:"[][,[,]]" (...)` // @@ -126,8 +132,8 @@ func Unmarshal(in []byte, out interface{}) (err error) { // F int "a,omitempty" // B int // } -// goyaml.Marshal(&T{B: 2}) // Returns "b: 2\n" -// goyaml.Marshal(&T{F: 1}} // Returns "a: 1\nb: 0\n" +// yaml.Marshal(&T{B: 2}) // Returns "b: 2\n" +// yaml.Marshal(&T{F: 1}} // Returns "a: 1\nb: 0\n" // func Marshal(in interface{}) (out []byte, err error) { defer handleErr(&err) @@ -142,7 +148,7 @@ func Marshal(in interface{}) (out []byte, err error) { // -------------------------------------------------------------------------- // Maintain a mapping of keys to structure field indexes -// The code in this section was copied from gobson. +// The code in this section was copied from mgo/bson. // structInfo holds details for the serialization of fields of // a given struct. diff --git a/third_party/launchpad.net/goyaml/yamlh.go b/third_party/gopkg.in/yaml.v1/yamlh.go similarity index 99% rename from third_party/launchpad.net/goyaml/yamlh.go rename to third_party/gopkg.in/yaml.v1/yamlh.go index 1fea365..6624d6c 100644 --- a/third_party/launchpad.net/goyaml/yamlh.go +++ b/third_party/gopkg.in/yaml.v1/yamlh.go @@ -1,4 +1,4 @@ -package goyaml +package yaml import ( "io" diff --git a/third_party/launchpad.net/goyaml/yamlprivateh.go b/third_party/gopkg.in/yaml.v1/yamlprivateh.go similarity index 99% rename from third_party/launchpad.net/goyaml/yamlprivateh.go rename to third_party/gopkg.in/yaml.v1/yamlprivateh.go index 1c0b23d..8110ce3 100644 --- a/third_party/launchpad.net/goyaml/yamlprivateh.go +++ b/third_party/gopkg.in/yaml.v1/yamlprivateh.go @@ -1,4 +1,4 @@ -package goyaml +package yaml const ( // The size of the input raw buffer. diff --git a/third_party/launchpad.net/goyaml/.bzrignore b/third_party/launchpad.net/goyaml/.bzrignore deleted file mode 100644 index 52c98ab..0000000 --- a/third_party/launchpad.net/goyaml/.bzrignore +++ /dev/null @@ -1,14 +0,0 @@ -[568].out -_* -*.cgo*.* - -yaml-*/stamp-h1 -yaml-*/Makefile -yaml-*/*/Makefile -yaml-*/libtool -yaml-*/config* -yaml-*/*/*.lo -yaml-*/*/*.la -yaml-*/*/.libs -yaml-*/*/.deps -yaml-*/tests/* diff --git a/third_party/launchpad.net/goyaml/.lbox b/third_party/launchpad.net/goyaml/.lbox deleted file mode 100644 index 90ebc09..0000000 --- a/third_party/launchpad.net/goyaml/.lbox +++ /dev/null @@ -1 +0,0 @@ -propose -cr -for=lp:goyaml diff --git a/third_party/launchpad.net/goyaml/.lbox.check b/third_party/launchpad.net/goyaml/.lbox.check deleted file mode 100755 index b4211c2..0000000 --- a/third_party/launchpad.net/goyaml/.lbox.check +++ /dev/null @@ -1,20 +0,0 @@ -#!/bin/sh - -set -e - -BADFMT=`find * -name '*.go' | xargs gofmt -l` -if [ -n "$BADFMT" ]; then - BADFMT=`echo "$BADFMT" | sed "s/^/ /"` - echo -e "gofmt is sad:\n\n$BADFMT" - exit 1 -fi - -VERSION=`go version | awk '{print $3}'` -if [ $VERSION == 'devel' ]; then - go tool vet \ - -methods \ - -printf \ - -rangeloops \ - -printfuncs 'ErrorContextf:1,notFoundf:0,badReqErrorf:0,Commitf:0,Snapshotf:0,Debugf:0' \ - . -fi diff --git a/third_party/launchpad.net/goyaml/Makefile b/third_party/launchpad.net/goyaml/Makefile deleted file mode 100644 index 873d53b..0000000 --- a/third_party/launchpad.net/goyaml/Makefile +++ /dev/null @@ -1,39 +0,0 @@ -include $(GOROOT)/src/Make.inc - -YAML=yaml-0.1.3 -LIBYAML=$(PWD)/$(YAML)/src/.libs/libyaml.a - -TARG=launchpad.net/goyaml - -GOFILES=\ - goyaml.go\ - resolve.go\ - -CGOFILES=\ - decode.go\ - encode.go\ - -CGO_OFILES+=\ - helpers.o\ - api.o\ - scanner.o\ - reader.o\ - parser.o\ - writer.o\ - emitter.o\ - -GOFMT=gofmt - -BADFMT:=$(shell $(GOFMT) -l $(GOFILES) $(CGOFILES) $(wildcard *_test.go)) - -all: package -gofmt: $(BADFMT) - @for F in $(BADFMT); do $(GOFMT) -w $$F && echo $$F; done - -include $(GOROOT)/src/Make.pkg - -ifneq ($(BADFMT),) -ifneq ($(MAKECMDGOALS),gofmt) -$(warning WARNING: make gofmt: $(BADFMT)) -endif -endif