Skip to content

Commit

Permalink
Merge pull request #310 from rogpeppe/018-timestamps
Browse files Browse the repository at this point in the history
Add support for alternative timestamp formats
  • Loading branch information
rogpeppe authored Jan 8, 2018
2 parents 7b07923 + 1f2a25b commit 87e4a22
Show file tree
Hide file tree
Showing 5 changed files with 238 additions and 59 deletions.
82 changes: 55 additions & 27 deletions decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ var (
durationType = reflect.TypeOf(time.Duration(0))
defaultMapType = reflect.TypeOf(map[interface{}]interface{}{})
ifaceType = defaultMapType.Elem()
timeType = reflect.TypeOf(time.Time{})
)

func newDecoder(strict bool) *decoder {
Expand Down Expand Up @@ -360,7 +361,7 @@ func resetMap(out reflect.Value) {
}
}

func (d *decoder) scalar(n *node, out reflect.Value) (good bool) {
func (d *decoder) scalar(n *node, out reflect.Value) bool {
var tag string
var resolved interface{}
if n.tag == "" && !n.implicit {
Expand All @@ -384,9 +385,26 @@ func (d *decoder) scalar(n *node, out reflect.Value) (good bool) {
}
return true
}
if s, ok := resolved.(string); ok && out.CanAddr() {
if u, ok := out.Addr().Interface().(encoding.TextUnmarshaler); ok {
err := u.UnmarshalText([]byte(s))
if resolvedv := reflect.ValueOf(resolved); out.Type() == resolvedv.Type() {
// We've resolved to exactly the type we want, so use that.
out.Set(resolvedv)
return true
}
// Perhaps we can use the value as a TextUnmarshaler to
// set its value.
if out.CanAddr() {
u, ok := out.Addr().Interface().(encoding.TextUnmarshaler)
if ok {
var text []byte
if tag == yaml_BINARY_TAG {
text = []byte(resolved.(string))
} else {
// We let any value be unmarshaled into TextUnmarshaler.
// That might be more lax than we'd like, but the
// TextUnmarshaler itself should bowl out any dubious values.
text = []byte(n.value)
}
err := u.UnmarshalText(text)
if err != nil {
fail(err)
}
Expand All @@ -397,46 +415,53 @@ func (d *decoder) scalar(n *node, out reflect.Value) (good bool) {
case reflect.String:
if tag == yaml_BINARY_TAG {
out.SetString(resolved.(string))
good = true
} else if resolved != nil {
return true
}
if resolved != nil {
out.SetString(n.value)
good = true
return true
}
case reflect.Interface:
if resolved == nil {
out.Set(reflect.Zero(out.Type()))
} else if tag == yaml_TIMESTAMP_TAG {
// It looks like a timestamp but for backward compatibility
// reasons we set it as a string, so that code that unmarshals
// timestamp-like values into interface{} will continue to
// see a string and not a time.Time.
out.Set(reflect.ValueOf(n.value))
} else {
out.Set(reflect.ValueOf(resolved))
}
good = true
return true
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
switch resolved := resolved.(type) {
case int:
if !out.OverflowInt(int64(resolved)) {
out.SetInt(int64(resolved))
good = true
return true
}
case int64:
if !out.OverflowInt(resolved) {
out.SetInt(resolved)
good = true
return true
}
case uint64:
if resolved <= math.MaxInt64 && !out.OverflowInt(int64(resolved)) {
out.SetInt(int64(resolved))
good = true
return true
}
case float64:
if resolved <= math.MaxInt64 && !out.OverflowInt(int64(resolved)) {
out.SetInt(int64(resolved))
good = true
return true
}
case string:
if out.Type() == durationType {
d, err := time.ParseDuration(resolved)
if err == nil {
out.SetInt(int64(d))
good = true
return true
}
}
}
Expand All @@ -445,58 +470,61 @@ func (d *decoder) scalar(n *node, out reflect.Value) (good bool) {
case int:
if resolved >= 0 && !out.OverflowUint(uint64(resolved)) {
out.SetUint(uint64(resolved))
good = true
return true
}
case int64:
if resolved >= 0 && !out.OverflowUint(uint64(resolved)) {
out.SetUint(uint64(resolved))
good = true
return true
}
case uint64:
if !out.OverflowUint(uint64(resolved)) {
out.SetUint(uint64(resolved))
good = true
return true
}
case float64:
if resolved <= math.MaxUint64 && !out.OverflowUint(uint64(resolved)) {
out.SetUint(uint64(resolved))
good = true
return true
}
}
case reflect.Bool:
switch resolved := resolved.(type) {
case bool:
out.SetBool(resolved)
good = true
return true
}
case reflect.Float32, reflect.Float64:
switch resolved := resolved.(type) {
case int:
out.SetFloat(float64(resolved))
good = true
return true
case int64:
out.SetFloat(float64(resolved))
good = true
return true
case uint64:
out.SetFloat(float64(resolved))
good = true
return true
case float64:
out.SetFloat(resolved)
good = true
return true
}
case reflect.Struct:
if resolvedv := reflect.ValueOf(resolved); out.Type() == resolvedv.Type() {
out.Set(resolvedv)
return true
}
case reflect.Ptr:
if out.Type().Elem() == reflect.TypeOf(resolved) {
// TODO DOes this make sense? When is out a Ptr except when decoding a nil value?
elem := reflect.New(out.Type().Elem())
elem.Elem().Set(reflect.ValueOf(resolved))
out.Set(elem)
good = true
return true
}
}
if !good {
d.terror(n, tag, out)
}
return good
d.terror(n, tag, out)
return false
}

func settableValueOf(i interface{}) reflect.Value {
Expand Down
85 changes: 81 additions & 4 deletions decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"errors"
"io"
"math"
"net"
"reflect"
"strings"
"time"
Expand Down Expand Up @@ -576,11 +575,80 @@ var unmarshalTests = []struct {
// Support encoding.TextUnmarshaler.
{
"a: 1.2.3.4\n",
map[string]net.IP{"a": net.IPv4(1, 2, 3, 4)},
map[string]textUnmarshaler{"a": textUnmarshaler{S: "1.2.3.4"}},
},
{
"a: 2015-02-24T18:19:39Z\n",
map[string]time.Time{"a": time.Unix(1424801979, 0).In(time.UTC)},
map[string]textUnmarshaler{"a": textUnmarshaler{"2015-02-24T18:19:39Z"}},
},

// Timestamps
{
// Date only.
"a: 2015-01-01\n",
map[string]time.Time{"a": time.Date(2015, 1, 1, 0, 0, 0, 0, time.UTC)},
},
{
// RFC3339
"a: 2015-02-24T18:19:39.12Z\n",
map[string]time.Time{"a": time.Date(2015, 2, 24, 18, 19, 39, .12e9, time.UTC)},
},
{
// RFC3339 with short dates.
"a: 2015-2-3T3:4:5Z",
map[string]time.Time{"a": time.Date(2015, 2, 3, 3, 4, 5, 0, time.UTC)},
},
{
// ISO8601 lower case t
"a: 2015-02-24t18:19:39Z\n",
map[string]time.Time{"a": time.Date(2015, 2, 24, 18, 19, 39, 0, time.UTC)},
},
{
// space separate, no time zone
"a: 2015-02-24 18:19:39\n",
map[string]time.Time{"a": time.Date(2015, 2, 24, 18, 19, 39, 0, time.UTC)},
},
// Some cases not currently handled. Uncomment these when
// the code is fixed.
// {
// // space separated with time zone
// "a: 2001-12-14 21:59:43.10 -5",
// map[string]interface{}{"a": time.Date(2001, 12, 14, 21, 59, 43, .1e9, time.UTC)},
// },
// {
// // arbitrary whitespace between fields
// "a: 2001-12-14 \t\t \t21:59:43.10 \t Z",
// map[string]interface{}{"a": time.Date(2001, 12, 14, 21, 59, 43, .1e9, time.UTC)},
// },
{
// explicit string tag
"a: !!str 2015-01-01",
map[string]interface{}{"a": "2015-01-01"},
},
{
// explicit timestamp tag on quoted string
"a: !!timestamp \"2015-01-01\"",
map[string]time.Time{"a": time.Date(2015, 1, 1, 0, 0, 0, 0, time.UTC)},
},
{
// explicit timestamp tag on unquoted string
"a: !!timestamp 2015-01-01",
map[string]time.Time{"a": time.Date(2015, 1, 1, 0, 0, 0, 0, time.UTC)},
},
{
// quoted string that's a valid timestamp
"a: \"2015-01-01\"",
map[string]interface{}{"a": "2015-01-01"},
},
{
// explicit timestamp tag into interface.
"a: !!timestamp \"2015-01-01\"",
map[string]interface{}{"a": "2015-01-01"},
},
{
// implicit timestamp tag into interface.
"a: 2015-01-01",
map[string]interface{}{"a": "2015-01-01"},
},

// Encode empty lists as zero-length slices.
Expand Down Expand Up @@ -656,7 +724,7 @@ func (s *S) TestUnmarshal(c *C) {
if _, ok := err.(*yaml.TypeError); !ok {
c.Assert(err, IsNil)
}
c.Assert(value.Elem().Interface(), DeepEquals, item.value)
c.Assert(value.Elem().Interface(), DeepEquals, item.value, Commentf("error: %v", err))
}
}

Expand Down Expand Up @@ -1165,6 +1233,15 @@ func (s *S) TestUnmarshalStrict(c *C) {
}
}

type textUnmarshaler struct {
S string
}

func (t *textUnmarshaler) UnmarshalText(s []byte) error {
t.S = string(s)
return nil
}

//var data []byte
//func init() {
// var err error
Expand Down
Loading

0 comments on commit 87e4a22

Please sign in to comment.