Merge pull request #223 from crawford/yaml

third_party: sync third_party/gopkg.in/yaml.v1
This commit is contained in:
Alex Crawford 2014-09-08 19:28:59 -07:00
commit c17b93b5c0
25 changed files with 462 additions and 180 deletions

View File

@ -6,7 +6,7 @@ import (
"log" "log"
"path" "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/network"
"github.com/coreos/coreos-cloudinit/system" "github.com/coreos/coreos-cloudinit/system"
@ -51,12 +51,12 @@ type warner func(format string, v ...interface{})
func warnOnUnrecognizedKeys(contents string, warn warner) { func warnOnUnrecognizedKeys(contents string, warn warner) {
// Generate a map of all understood cloud config options // Generate a map of all understood cloud config options
var cc map[string]interface{} var cc map[string]interface{}
b, _ := goyaml.Marshal(&CloudConfig{}) b, _ := yaml.Marshal(&CloudConfig{})
goyaml.Unmarshal(b, &cc) yaml.Unmarshal(b, &cc)
// Now unmarshal the entire provided contents // Now unmarshal the entire provided contents
var c map[string]interface{} 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 // Check that every key in the contents exists in the cloud config
for k, _ := range c { for k, _ := range c {
@ -84,8 +84,8 @@ func warnOnUnrecognizedKeys(contents string, warn warner) {
// Check for any badly-specified users, if any are set // Check for any badly-specified users, if any are set
if users, ok := c["users"]; ok { if users, ok := c["users"]; ok {
var known map[string]interface{} var known map[string]interface{}
b, _ := goyaml.Marshal(&system.User{}) b, _ := yaml.Marshal(&system.User{})
goyaml.Unmarshal(b, &known) yaml.Unmarshal(b, &known)
if set, ok := users.([]interface{}); ok { if set, ok := users.([]interface{}); ok {
for _, u := range set { for _, u := range set {
@ -107,8 +107,8 @@ func warnOnUnrecognizedKeys(contents string, warn warner) {
// Check for any badly-specified files, if any are set // Check for any badly-specified files, if any are set
if files, ok := c["write_files"]; ok { if files, ok := c["write_files"]; ok {
var known map[string]interface{} var known map[string]interface{}
b, _ := goyaml.Marshal(&system.File{}) b, _ := yaml.Marshal(&system.File{})
goyaml.Unmarshal(b, &known) yaml.Unmarshal(b, &known)
if set, ok := files.([]interface{}); ok { if set, ok := files.([]interface{}); ok {
for _, f := range set { for _, f := range set {
@ -133,7 +133,7 @@ func warnOnUnrecognizedKeys(contents string, warn warner) {
// fields but log encountering them. // fields but log encountering them.
func NewCloudConfig(contents string) (*CloudConfig, error) { func NewCloudConfig(contents string) (*CloudConfig, error) {
var cfg CloudConfig var cfg CloudConfig
err := goyaml.Unmarshal([]byte(contents), &cfg) err := yaml.Unmarshal([]byte(contents), &cfg)
if err != nil { if err != nil {
return &cfg, err return &cfg, err
} }
@ -142,7 +142,7 @@ func NewCloudConfig(contents string) (*CloudConfig, error) {
} }
func (cc CloudConfig) String() string { func (cc CloudConfig) String() string {
bytes, err := goyaml.Marshal(cc) bytes, err := yaml.Marshal(cc)
if err != nil { if err != nil {
return "" return ""
} }

View File

@ -16,7 +16,7 @@ func ParseUserData(contents string) (interface{}, error) {
// Explicitly trim the header so we can handle user-data from // Explicitly trim the header so we can handle user-data from
// non-unix operating systems. The rest of the file is parsed // 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) header = strings.TrimSpace(header)
if strings.HasPrefix(header, "#!") { if strings.HasPrefix(header, "#!") {

View File

@ -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 Copyright (c) 2006 Kirill Simonov
Permission is hereby granted, free of charge, to any person obtaining a copy of Permission is hereby granted, free of charge, to any person obtaining a copy of

128
third_party/gopkg.in/yaml.v1/README.md vendored Normal file
View File

@ -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
```

View File

@ -1,4 +1,4 @@
package goyaml package yaml
import ( import (
"io" "io"

View File

@ -1,8 +1,9 @@
package goyaml package yaml
import ( import (
"reflect" "reflect"
"strconv" "strconv"
"time"
) )
const ( const (
@ -211,6 +212,16 @@ func newDecoder() *decoder {
// returned to call SetYAML() with the value of *out once it's defined. // 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()) { 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 again := true
for again { for again {
again = false again = false
@ -279,16 +290,19 @@ func (d *decoder) alias(n *node, out reflect.Value) (good bool) {
return good return good
} }
var durationType = reflect.TypeOf(time.Duration(0))
func (d *decoder) scalar(n *node, out reflect.Value) (good bool) { func (d *decoder) scalar(n *node, out reflect.Value) (good bool) {
var tag string var tag string
var resolved interface{} var resolved interface{}
if n.tag == "" && !n.implicit { if n.tag == "" && !n.implicit {
tag = "!!str"
resolved = n.value resolved = n.value
} else { } else {
tag, resolved = resolve(n.tag, n.value) 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() { switch out.Kind() {
case reflect.String: case reflect.String:
@ -320,6 +334,14 @@ func (d *decoder) scalar(n *node, out reflect.Value) (good bool) {
out.SetInt(int64(resolved)) out.SetInt(int64(resolved))
good = true 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: case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
switch resolved := resolved.(type) { switch resolved := resolved.(type) {
@ -437,6 +459,10 @@ func (d *decoder) mapping(n *node, out reflect.Value) (good bool) {
} }
l := len(n.children) l := len(n.children)
for i := 0; i < l; i += 2 { 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() k := reflect.New(kt).Elem()
if d.unmarshal(n.children[i], k) { if d.unmarshal(n.children[i], k) {
e := reflect.New(et).Elem() e := reflect.New(et).Elem()
@ -456,7 +482,12 @@ func (d *decoder) mappingStruct(n *node, out reflect.Value) (good bool) {
name := settableValueOf("") name := settableValueOf("")
l := len(n.children) l := len(n.children)
for i := 0; i < l; i += 2 { 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 continue
} }
if info, ok := sinfo.FieldsMap[name.String()]; ok { 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 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")
}

View File

@ -1,10 +1,11 @@
package goyaml_test package yaml_test
import ( import (
. "launchpad.net/gocheck" . "gopkg.in/check.v1"
"github.com/coreos/coreos-cloudinit/third_party/launchpad.net/goyaml" "gopkg.in/yaml.v1"
"math" "math"
"reflect" "reflect"
"time"
) )
var unmarshalIntTest = 123 var unmarshalIntTest = 123
@ -350,6 +351,32 @@ var unmarshalTests = []struct {
C inlineB `yaml:",inline"` C inlineB `yaml:",inline"`
}{1, inlineB{2, inlineC{3}}}, }{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: <foo>",
map[string]string{"a": "<foo>"},
},
} }
type inlineB struct { type inlineB struct {
@ -377,7 +404,7 @@ func (s *S) TestUnmarshal(c *C) {
pv := reflect.New(pt.Elem()) pv := reflect.New(pt.Elem())
value = pv.Interface() 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)) c.Assert(err, IsNil, Commentf("Item #%d", i))
if t.Kind() == reflect.String { if t.Kind() == reflect.String {
c.Assert(*value.(*string), Equals, item.value, Commentf("Item #%d", i)) 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) { func (s *S) TestUnmarshalNaN(c *C) {
value := map[string]interface{}{} value := map[string]interface{}{}
err := goyaml.Unmarshal([]byte("notanum: .NaN"), &value) err := yaml.Unmarshal([]byte("notanum: .NaN"), &value)
c.Assert(err, IsNil) c.Assert(err, IsNil)
c.Assert(math.IsNaN(value["notanum"].(float64)), Equals, true) c.Assert(math.IsNaN(value["notanum"].(float64)), Equals, true)
} }
@ -408,7 +435,7 @@ var unmarshalErrorTests = []struct {
func (s *S) TestUnmarshalErrors(c *C) { func (s *S) TestUnmarshalErrors(c *C) {
for _, item := range unmarshalErrorTests { for _, item := range unmarshalErrorTests {
var value interface{} 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)) c.Assert(err, ErrorMatches, item.error, Commentf("Partial unmarshal: %#v", value))
} }
} }
@ -421,6 +448,8 @@ var setterTests = []struct {
{"_: [1,A]", "!!seq", []interface{}{1, "A"}}, {"_: [1,A]", "!!seq", []interface{}{1, "A"}},
{"_: 10", "!!int", 10}, {"_: 10", "!!int", 10},
{"_: null", "!!null", nil}, {"_: null", "!!null", nil},
{`_: BAR!`, "!!str", "BAR!"},
{`_: "BAR!"`, "!!str", "BAR!"},
{"_: !!foo 'BAR!'", "!!foo", "BAR!"}, {"_: !!foo 'BAR!'", "!!foo", "BAR!"},
} }
@ -442,17 +471,31 @@ func (o *typeWithSetter) SetYAML(tag string, value interface{}) (ok bool) {
return true return true
} }
type typeWithSetterField struct { type setterPointerType struct {
Field *typeWithSetter "_" Field *typeWithSetter "_"
} }
func (s *S) TestUnmarshalWithSetter(c *C) { type setterValueType struct {
Field typeWithSetter "_"
}
func (s *S) TestUnmarshalWithPointerSetter(c *C) {
for _, item := range setterTests { for _, item := range setterTests {
obj := &typeWithSetterField{} obj := &setterPointerType{}
err := goyaml.Unmarshal([]byte(item.data), obj) err := yaml.Unmarshal([]byte(item.data), obj)
c.Assert(err, IsNil) c.Assert(err, IsNil)
c.Assert(obj.Field, NotNil, c.Assert(obj.Field, NotNil, Commentf("Pointer not initialized (%#v)", item.value))
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.tag, Equals, item.tag)
c.Assert(obj.Field.value, DeepEquals, item.value) c.Assert(obj.Field.value, DeepEquals, item.value)
} }
@ -460,7 +503,7 @@ func (s *S) TestUnmarshalWithSetter(c *C) {
func (s *S) TestUnmarshalWholeDocumentWithSetter(c *C) { func (s *S) TestUnmarshalWholeDocumentWithSetter(c *C) {
obj := &typeWithSetter{} obj := &typeWithSetter{}
err := goyaml.Unmarshal([]byte(setterTests[0].data), obj) err := yaml.Unmarshal([]byte(setterTests[0].data), obj)
c.Assert(err, IsNil) c.Assert(err, IsNil)
c.Assert(obj.tag, Equals, setterTests[0].tag) c.Assert(obj.tag, Equals, setterTests[0].tag)
value, ok := obj.value.(map[interface{}]interface{}) value, ok := obj.value.(map[interface{}]interface{})
@ -477,8 +520,8 @@ func (s *S) TestUnmarshalWithFalseSetterIgnoresValue(c *C) {
}() }()
m := map[string]*typeWithSetter{} m := map[string]*typeWithSetter{}
data := "{abc: 1, def: 2, ghi: 3, jkl: 4}" data := `{abc: 1, def: 2, ghi: 3, jkl: 4}`
err := goyaml.Unmarshal([]byte(data), m) err := yaml.Unmarshal([]byte(data), m)
c.Assert(err, IsNil) c.Assert(err, IsNil)
c.Assert(m["abc"], NotNil) c.Assert(m["abc"], NotNil)
c.Assert(m["def"], IsNil) c.Assert(m["def"], IsNil)
@ -489,6 +532,98 @@ func (s *S) TestUnmarshalWithFalseSetterIgnoresValue(c *C) {
c.Assert(m["ghi"].value, Equals, 3) 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
!<tag:yaml.org,2002:merge> "<<" : [ *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 //var data []byte
//func init() { //func init() {
// var err error // var err error
@ -502,7 +637,7 @@ func (s *S) TestUnmarshalWithFalseSetterIgnoresValue(c *C) {
// var err error // var err error
// for i := 0; i < c.N; i++ { // for i := 0; i < c.N; i++ {
// var v map[string]interface{} // var v map[string]interface{}
// err = goyaml.Unmarshal(data, &v) // err = yaml.Unmarshal(data, &v)
// } // }
// if err != nil { // if err != nil {
// panic(err) // panic(err)
@ -511,9 +646,9 @@ func (s *S) TestUnmarshalWithFalseSetterIgnoresValue(c *C) {
// //
//func (s *S) BenchmarkMarshal(c *C) { //func (s *S) BenchmarkMarshal(c *C) {
// var v map[string]interface{} // var v map[string]interface{}
// goyaml.Unmarshal(data, &v) // yaml.Unmarshal(data, &v)
// c.ResetTimer() // c.ResetTimer()
// for i := 0; i < c.N; i++ { // for i := 0; i < c.N; i++ {
// goyaml.Marshal(&v) // yaml.Marshal(&v)
// } // }
//} //}

View File

@ -1,4 +1,4 @@
package goyaml package yaml
import ( import (
"bytes" "bytes"

View File

@ -1,9 +1,10 @@
package goyaml package yaml
import ( import (
"reflect" "reflect"
"sort" "sort"
"strconv" "strconv"
"time"
) )
type encoder struct { type encoder struct {
@ -85,7 +86,11 @@ func (e *encoder) marshal(tag string, in reflect.Value) {
case reflect.String: case reflect.String:
e.stringv(tag, in) e.stringv(tag, in)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: 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: case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
e.uintv(tag, in) e.uintv(tag, in)
case reflect.Float32, reflect.Float64: case reflect.Float32, reflect.Float64:

View File

@ -1,12 +1,13 @@
package goyaml_test package yaml_test
import ( import (
"fmt" "fmt"
. "launchpad.net/gocheck" "gopkg.in/yaml.v1"
"github.com/coreos/coreos-cloudinit/third_party/launchpad.net/goyaml" . "gopkg.in/check.v1"
"math" "math"
"strconv" "strconv"
"strings" "strings"
"time"
) )
var marshalIntTest = 123 var marshalIntTest = 123
@ -212,11 +213,23 @@ var marshalTests = []struct {
}{1, inlineB{2, inlineC{3}}}, }{1, inlineB{2, inlineC{3}}},
"a: 1\nb: 2\nc: 3\n", "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": "<foo>"},
"a: <foo>\n",
},
} }
func (s *S) TestMarshal(c *C) { func (s *S) TestMarshal(c *C) {
for _, item := range marshalTests { for _, item := range marshalTests {
data, err := goyaml.Marshal(item.value) data, err := yaml.Marshal(item.value)
c.Assert(err, IsNil) c.Assert(err, IsNil)
c.Assert(string(data), Equals, item.data) c.Assert(string(data), Equals, item.data)
} }
@ -237,7 +250,7 @@ var marshalErrorTests = []struct {
func (s *S) TestMarshalErrors(c *C) { func (s *S) TestMarshalErrors(c *C) {
for _, item := range marshalErrorTests { for _, item := range marshalErrorTests {
_, err := goyaml.Marshal(item.value) _, err := yaml.Marshal(item.value)
c.Assert(err, ErrorMatches, item.error) c.Assert(err, ErrorMatches, item.error)
} }
} }
@ -269,12 +282,12 @@ func (s *S) TestMarshalTypeCache(c *C) {
var err error var err error
func() { func() {
type T struct{ A int } type T struct{ A int }
data, err = goyaml.Marshal(&T{}) data, err = yaml.Marshal(&T{})
c.Assert(err, IsNil) c.Assert(err, IsNil)
}() }()
func() { func() {
type T struct{ B int } type T struct{ B int }
data, err = goyaml.Marshal(&T{}) data, err = yaml.Marshal(&T{})
c.Assert(err, IsNil) c.Assert(err, IsNil)
}() }()
c.Assert(string(data), Equals, "b: 0\n") c.Assert(string(data), Equals, "b: 0\n")
@ -298,7 +311,7 @@ func (s *S) TestMashalWithGetter(c *C) {
obj := &typeWithGetterField{} obj := &typeWithGetterField{}
obj.Field.tag = item.tag obj.Field.tag = item.tag
obj.Field.value = item.value obj.Field.value = item.value
data, err := goyaml.Marshal(obj) data, err := yaml.Marshal(obj)
c.Assert(err, IsNil) c.Assert(err, IsNil)
c.Assert(string(data), Equals, string(item.data)) c.Assert(string(data), Equals, string(item.data))
} }
@ -308,7 +321,7 @@ func (s *S) TestUnmarshalWholeDocumentWithGetter(c *C) {
obj := &typeWithGetter{} obj := &typeWithGetter{}
obj.tag = "" obj.tag = ""
obj.value = map[string]string{"hello": "world!"} obj.value = map[string]string{"hello": "world!"}
data, err := goyaml.Marshal(obj) data, err := yaml.Marshal(obj)
c.Assert(err, IsNil) c.Assert(err, IsNil)
c.Assert(string(data), Equals, "hello: world!\n") c.Assert(string(data), Equals, "hello: world!\n")
} }
@ -356,7 +369,7 @@ func (s *S) TestSortedOutput(c *C) {
for _, k := range order { for _, k := range order {
m[k] = 1 m[k] = 1
} }
data, err := goyaml.Marshal(m) data, err := yaml.Marshal(m)
c.Assert(err, IsNil) c.Assert(err, IsNil)
out := "\n" + string(data) out := "\n" + string(data)
last := 0 last := 0

View File

@ -1,4 +1,4 @@
package goyaml package yaml
import ( import (
"bytes" "bytes"

View File

@ -1,4 +1,4 @@
package goyaml package yaml
import ( import (
"io" "io"

View File

@ -1,4 +1,4 @@
package goyaml package yaml
import ( import (
"math" "math"
@ -27,7 +27,6 @@ func init() {
t[int(c)] = 'M' // In map t[int(c)] = 'M' // In map
} }
t[int('.')] = '.' // Float (potentially in map) t[int('.')] = '.' // Float (potentially in map)
t[int('<')] = '<' // Merge
var resolveMapList = []struct { var resolveMapList = []struct {
v interface{} 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"}}, {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 m := resolveMap
@ -113,13 +113,8 @@ func resolve(tag string, in string) (rtag string, out interface{}) {
case 'D', 'S': case 'D', 'S':
// Int, float, or timestamp. // Int, float, or timestamp.
for i := 0; i != len(in); i++ { plain := strings.Replace(in, "_", "", -1)
if in[i] == '_' { intv, err := strconv.ParseInt(plain, 0, 64)
in = strings.Replace(in, "_", "", -1)
break
}
}
intv, err := strconv.ParseInt(in, 0, 64)
if err == nil { if err == nil {
if intv == int64(int(intv)) { if intv == int64(int(intv)) {
return "!!int", int(intv) return "!!int", int(intv)
@ -127,26 +122,23 @@ func resolve(tag string, in string) (rtag string, out interface{}) {
return "!!int", intv return "!!int", intv
} }
} }
floatv, err := strconv.ParseFloat(in, 64) floatv, err := strconv.ParseFloat(plain, 64)
if err == nil { if err == nil {
return "!!float", floatv return "!!float", floatv
} }
if strings.HasPrefix(in, "0b") { if strings.HasPrefix(plain, "0b") {
intv, err := strconv.ParseInt(in[2:], 2, 64) intv, err := strconv.ParseInt(plain[2:], 2, 64)
if err == nil { if err == nil {
return "!!int", int(intv) return "!!int", int(intv)
} }
} else if strings.HasPrefix(in, "-0b") { } else if strings.HasPrefix(plain, "-0b") {
intv, err := strconv.ParseInt(in[3:], 2, 64) intv, err := strconv.ParseInt(plain[3:], 2, 64)
if err == nil { if err == nil {
return "!!int", -int(intv) return "!!int", -int(intv)
} }
} }
// XXX Handle timestamps here. // XXX Handle timestamps here.
case '<':
// XXX Handle merge (<<) here.
default: default:
panic("resolveTable item not yet handled: " + panic("resolveTable item not yet handled: " +
string([]byte{c}) + " (with " + in + ")") string([]byte{c}) + " (with " + in + ")")

View File

@ -1,4 +1,4 @@
package goyaml package yaml
import ( import (
"bytes" "bytes"

View File

@ -1,4 +1,4 @@
package goyaml package yaml
import ( import (
"reflect" "reflect"

View File

@ -1,7 +1,7 @@
package goyaml_test package yaml_test
import ( import (
. "launchpad.net/gocheck" . "gopkg.in/check.v1"
"testing" "testing"
) )

View File

@ -1,4 +1,4 @@
package goyaml package yaml
// Set the writer error and return false. // Set the writer error and return false.
func yaml_emitter_set_writer_error(emitter *yaml_emitter_t, problem string) bool { func yaml_emitter_set_writer_error(emitter *yaml_emitter_t, problem string) bool {

View File

@ -1,5 +1,10 @@
// Package goyaml implements YAML support for the Go language. // Package yaml implements YAML support for the Go language.
package goyaml //
// Source code and other details for the project are available at GitHub:
//
// https://github.com/go-yaml/yaml
//
package yaml
import ( import (
"errors" "errors"
@ -28,32 +33,31 @@ func handleErr(err *error) {
} }
} }
// Objects implementing the goyaml.Setter interface will receive the YAML // The Setter interface may be implemented by types to do their own custom
// tag and value via the SetYAML method during unmarshaling, rather than // unmarshalling of YAML values, rather than being implicitly assigned by
// being implicitly assigned by the goyaml machinery. If setting the value // the yaml package machinery. If setting the value works, the method should
// works, the method should return true. If it returns false, the given // return true. If it returns false, the value is considered unsupported
// value will be omitted from maps and slices. // and is omitted from maps and slices.
type Setter interface { type Setter interface {
SetYAML(tag string, value interface{}) bool SetYAML(tag string, value interface{}) bool
} }
// Objects implementing the goyaml.Getter interface will get the GetYAML() // The Getter interface is implemented by types to do their own custom
// method called when goyaml is requested to marshal the given value, and // marshalling into a YAML tag and value.
// the result of this method will be marshaled in place of the actual object.
type Getter interface { type Getter interface {
GetYAML() (tag string, value interface{}) GetYAML() (tag string, value interface{})
} }
// Unmarshal decodes the first document found within the in byte slice // 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. // Maps and pointers (to a struct, string, int, etc) are accepted as out
// If an internal pointer within a struct is not initialized, goyaml // values. If an internal pointer within a struct is not initialized,
// will initialize it if necessary for unmarshalling the provided data, // the yaml package will initialize it if necessary for unmarshalling
// but the struct provided as out must not be a nil pointer. // the provided data. The out parameter must not be nil.
// //
// The type of the decoded values and the type of out will be considered, // 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 // 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 // because they are not available in the decoded YAML, or if they are not
// compatible with the out value. To ensure something was properly // compatible with the out value. To ensure something was properly
@ -61,11 +65,11 @@ type Getter interface {
// field (usually the zero value). // field (usually the zero value).
// //
// Struct fields are only unmarshalled if they are exported (have an // Struct fields are only unmarshalled if they are exported (have an
// upper case first letter), and will be unmarshalled using the field // upper case first letter), and are unmarshalled using the field name
// name lowercased by default. When custom field names are desired, the // lowercased as the default key. Custom keys may be defined via the
// tag value may be used to tweak the name. Everything before the first // "yaml" name in the field tag: the content preceding the first comma
// comma in the field tag will be used as the name. The values following // is used as the key, and the following comma-separated options are
// the comma are used to tweak the marshalling process (see Marshal). // used to tweak the marshalling process (see Marshal).
// Conflicting names result in a runtime error. // Conflicting names result in a runtime error.
// //
// For example: // For example:
@ -75,7 +79,7 @@ type Getter interface {
// B int // B int
// } // }
// var T t // 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 // See the documentation of Marshal for the format of tags and a list of
// supported tag options. // 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 // Marshal serializes the value provided into a YAML document. The structure
// of the generated document will reflect the structure of the value itself. // 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. // Struct fields are only unmarshalled if they are exported (have an upper case
// The lowercased field name is used as the key for each exported field, // first letter), and are unmarshalled using the field name lowercased as the
// but this behavior may be changed using the respective field tag. // default key. Custom keys may be defined via the "yaml" name in the field
// The tag may also contain flags to tweak the marshalling behavior for // tag: the content preceding the first comma is used as the key, and the
// the field. Conflicting names result in a runtime error. The tag format // following comma-separated options are used to tweak the marshalling process.
// accepted is: // Conflicting names result in a runtime error.
//
// The field tag format accepted is:
// //
// `(...) yaml:"[<key>][,<flag1>[,<flag2>]]" (...)` // `(...) yaml:"[<key>][,<flag1>[,<flag2>]]" (...)`
// //
@ -126,8 +132,8 @@ func Unmarshal(in []byte, out interface{}) (err error) {
// F int "a,omitempty" // F int "a,omitempty"
// B int // B int
// } // }
// goyaml.Marshal(&T{B: 2}) // Returns "b: 2\n" // yaml.Marshal(&T{B: 2}) // Returns "b: 2\n"
// goyaml.Marshal(&T{F: 1}} // Returns "a: 1\nb: 0\n" // yaml.Marshal(&T{F: 1}} // Returns "a: 1\nb: 0\n"
// //
func Marshal(in interface{}) (out []byte, err error) { func Marshal(in interface{}) (out []byte, err error) {
defer handleErr(&err) defer handleErr(&err)
@ -142,7 +148,7 @@ func Marshal(in interface{}) (out []byte, err error) {
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
// Maintain a mapping of keys to structure field indexes // 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 // structInfo holds details for the serialization of fields of
// a given struct. // a given struct.

View File

@ -1,4 +1,4 @@
package goyaml package yaml
import ( import (
"io" "io"

View File

@ -1,4 +1,4 @@
package goyaml package yaml
const ( const (
// The size of the input raw buffer. // The size of the input raw buffer.

View File

@ -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/*

View File

@ -1 +0,0 @@
propose -cr -for=lp:goyaml

View File

@ -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

View File

@ -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