Loading Gopkg.lock +2 −2 Original line number Diff line number Diff line Loading @@ -153,11 +153,11 @@ revision = "9f0cb55181dd3a0a4c168d3dbc72d4aca4853126" [[projects]] digest = "1:f0620375dd1f6251d9973b5f2596228cc8042e887cd7f827e4220bc1ce8c30e2" digest = "1:cedccf16b71e86db87a24f8d4c70b0a855872eb967cb906a66b95de56aefbd0d" name = "github.com/go-yaml/yaml" packages = ["."] pruneopts = "" revision = "5420a8b6744d3b0345ab293f6fcba19c978f1183" revision = "51d6538a90f86fe93ac480b35f37b2be17fef232" [[projects]] digest = "1:94ab904da3a528888677f39d32f9625ea8b2d639146af9c79ff97bda3e8554ae" Loading Gopkg.toml +1 −1 Original line number Diff line number Diff line Loading @@ -8,7 +8,7 @@ [[constraint]] name = "github.com/go-yaml/yaml" revision = "5420a8b6744d3b0345ab293f6fcba19c978f1183" revision = "51d6538a90f86fe93ac480b35f37b2be17fef232" [[constraint]] name = "github.com/gofrs/uuid" Loading vendor/github.com/go-yaml/yaml/decode_test.go +8 −0 Original line number Diff line number Diff line Loading @@ -714,6 +714,14 @@ var unmarshalTests = []struct { "---\nhello\n...\n}not yaml", "hello", }, { "a: 5\n", &struct{ A jsonNumberT }{"5"}, }, { "a: 5.5\n", &struct{ A jsonNumberT }{"5.5"}, }, } type M map[interface{}]interface{} Loading vendor/github.com/go-yaml/yaml/encode.go +28 −0 Original line number Diff line number Diff line Loading @@ -13,6 +13,19 @@ import ( "unicode/utf8" ) // jsonNumber is the interface of the encoding/json.Number datatype. // Repeating the interface here avoids a dependency on encoding/json, and also // supports other libraries like jsoniter, which use a similar datatype with // the same interface. Detecting this interface is useful when dealing with // structures containing json.Number, which is a string under the hood. The // encoder should prefer the use of Int64(), Float64() and string(), in that // order, when encoding this type. type jsonNumber interface { Float64() (float64, error) Int64() (int64, error) String() string } type encoder struct { emitter yaml_emitter_t event yaml_event_t Loading Loading @@ -89,6 +102,21 @@ func (e *encoder) marshal(tag string, in reflect.Value) { } iface := in.Interface() switch m := iface.(type) { case jsonNumber: integer, err := m.Int64() if err == nil { // In this case the json.Number is a valid int64 in = reflect.ValueOf(integer) break } float, err := m.Float64() if err == nil { // In this case the json.Number is a valid float64 in = reflect.ValueOf(float) break } // fallback case - no number could be obtained in = reflect.ValueOf(m.String()) case time.Time, *time.Time: // Although time.Time implements TextMarshaler, // we don't want to treat it as a string for YAML Loading vendor/github.com/go-yaml/yaml/encode_test.go +30 −0 Original line number Diff line number Diff line Loading @@ -15,6 +15,24 @@ import ( "gopkg.in/yaml.v2" ) type jsonNumberT string func (j jsonNumberT) Int64() (int64, error) { val, err := strconv.Atoi(string(j)) if err != nil { return 0, err } return int64(val), nil } func (j jsonNumberT) Float64() (float64, error) { return strconv.ParseFloat(string(j), 64) } func (j jsonNumberT) String() string { return string(j) } var marshalIntTest = 123 var marshalTests = []struct { Loading Loading @@ -367,6 +385,18 @@ var marshalTests = []struct { map[string]string{"a": "你好 #comment"}, "a: '你好 #comment'\n", }, { map[string]interface{}{"a": jsonNumberT("5")}, "a: 5\n", }, { map[string]interface{}{"a": jsonNumberT("100.5")}, "a: 100.5\n", }, { map[string]interface{}{"a": jsonNumberT("bogus")}, "a: bogus\n", }, } func (s *S) TestMarshal(c *C) { Loading Loading
Gopkg.lock +2 −2 Original line number Diff line number Diff line Loading @@ -153,11 +153,11 @@ revision = "9f0cb55181dd3a0a4c168d3dbc72d4aca4853126" [[projects]] digest = "1:f0620375dd1f6251d9973b5f2596228cc8042e887cd7f827e4220bc1ce8c30e2" digest = "1:cedccf16b71e86db87a24f8d4c70b0a855872eb967cb906a66b95de56aefbd0d" name = "github.com/go-yaml/yaml" packages = ["."] pruneopts = "" revision = "5420a8b6744d3b0345ab293f6fcba19c978f1183" revision = "51d6538a90f86fe93ac480b35f37b2be17fef232" [[projects]] digest = "1:94ab904da3a528888677f39d32f9625ea8b2d639146af9c79ff97bda3e8554ae" Loading
Gopkg.toml +1 −1 Original line number Diff line number Diff line Loading @@ -8,7 +8,7 @@ [[constraint]] name = "github.com/go-yaml/yaml" revision = "5420a8b6744d3b0345ab293f6fcba19c978f1183" revision = "51d6538a90f86fe93ac480b35f37b2be17fef232" [[constraint]] name = "github.com/gofrs/uuid" Loading
vendor/github.com/go-yaml/yaml/decode_test.go +8 −0 Original line number Diff line number Diff line Loading @@ -714,6 +714,14 @@ var unmarshalTests = []struct { "---\nhello\n...\n}not yaml", "hello", }, { "a: 5\n", &struct{ A jsonNumberT }{"5"}, }, { "a: 5.5\n", &struct{ A jsonNumberT }{"5.5"}, }, } type M map[interface{}]interface{} Loading
vendor/github.com/go-yaml/yaml/encode.go +28 −0 Original line number Diff line number Diff line Loading @@ -13,6 +13,19 @@ import ( "unicode/utf8" ) // jsonNumber is the interface of the encoding/json.Number datatype. // Repeating the interface here avoids a dependency on encoding/json, and also // supports other libraries like jsoniter, which use a similar datatype with // the same interface. Detecting this interface is useful when dealing with // structures containing json.Number, which is a string under the hood. The // encoder should prefer the use of Int64(), Float64() and string(), in that // order, when encoding this type. type jsonNumber interface { Float64() (float64, error) Int64() (int64, error) String() string } type encoder struct { emitter yaml_emitter_t event yaml_event_t Loading Loading @@ -89,6 +102,21 @@ func (e *encoder) marshal(tag string, in reflect.Value) { } iface := in.Interface() switch m := iface.(type) { case jsonNumber: integer, err := m.Int64() if err == nil { // In this case the json.Number is a valid int64 in = reflect.ValueOf(integer) break } float, err := m.Float64() if err == nil { // In this case the json.Number is a valid float64 in = reflect.ValueOf(float) break } // fallback case - no number could be obtained in = reflect.ValueOf(m.String()) case time.Time, *time.Time: // Although time.Time implements TextMarshaler, // we don't want to treat it as a string for YAML Loading
vendor/github.com/go-yaml/yaml/encode_test.go +30 −0 Original line number Diff line number Diff line Loading @@ -15,6 +15,24 @@ import ( "gopkg.in/yaml.v2" ) type jsonNumberT string func (j jsonNumberT) Int64() (int64, error) { val, err := strconv.Atoi(string(j)) if err != nil { return 0, err } return int64(val), nil } func (j jsonNumberT) Float64() (float64, error) { return strconv.ParseFloat(string(j), 64) } func (j jsonNumberT) String() string { return string(j) } var marshalIntTest = 123 var marshalTests = []struct { Loading Loading @@ -367,6 +385,18 @@ var marshalTests = []struct { map[string]string{"a": "你好 #comment"}, "a: '你好 #comment'\n", }, { map[string]interface{}{"a": jsonNumberT("5")}, "a: 5\n", }, { map[string]interface{}{"a": jsonNumberT("100.5")}, "a: 100.5\n", }, { map[string]interface{}{"a": jsonNumberT("bogus")}, "a: bogus\n", }, } func (s *S) TestMarshal(c *C) { Loading