Merge lp:~jbaikge/goyaml/time.Duration-support into lp:goyaml

Proposed by Jake T
Status: Needs review
Proposed branch: lp:~jbaikge/goyaml/time.Duration-support
Merge into: lp:goyaml
Diff against target: 76 lines (+34/-0)
2 files modified
decode.go (+9/-0)
decode_test.go (+25/-0)
To merge this branch: bzr merge lp:~jbaikge/goyaml/time.Duration-support
Reviewer Review Type Date Requested Status
goyaml maintainers Pending
Review via email: mp+171337@code.launchpad.net

Description of the change

Add time.Duration Support

Adds support for parsing time.Duration values (eg 10m, 42s, etc.) into vars or
fields of type time.Duration. Support for using integer values remains.

https://codereview.appspot.com/10548043/

To post a comment you must log in.
Revision history for this message
Jake T (jbaikge) wrote :

Reviewers: mp+171337_code.launchpad.net,

Message:
Please take a look.

Description:
Add time.Duration Support

Adds support for parsing time.Duration values (eg 10m, 42s, etc.) into
vars or
fields of type time.Duration. Support for using integer values remains.

https://code.launchpad.net/~jbaikge/goyaml/time.Duration-support/+merge/171337

(do not edit description out of merge proposal)

Please review this at https://codereview.appspot.com/10548043/

Affected files:
   A [revision details]
   M decode.go
   M decode_test.go

Index: [revision details]
=== added file '[revision details]'
--- [revision details] 2012-01-01 00:00:00 +0000
+++ [revision details] 2012-01-01 00:00:00 +0000
@@ -0,0 +1,2 @@
+Old revision: <email address hidden>
+New revision: jake@sable-20130625151823-ry78brqct2dp1e47

Index: decode.go
=== modified file 'decode.go'
--- decode.go 2013-06-19 17:52:23 +0000
+++ decode.go 2013-06-25 15:18:23 +0000
@@ -3,6 +3,7 @@
  import (
   "reflect"
   "strconv"
+ "time"
  )

  const (
@@ -23,6 +24,8 @@
   anchors map[string]*node
  }

+var durationType = reflect.TypeOf(time.Duration(0))
+
  //
----------------------------------------------------------------------------
  // Parser, produces a node tree out of a libyaml event stream.

@@ -315,6 +318,12 @@
      out.SetInt(resolved)
      good = true
     }
+ case string:
+ if out.Type() == durationType {
+ d, err := time.ParseDuration(resolved)
+ out.SetInt(d.Nanoseconds())
+ good = err == nil
+ }
    }
   case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32,
reflect.Uint64, reflect.Uintptr:
    switch resolved := resolved.(type) {

Index: decode_test.go
=== modified file 'decode_test.go'
--- decode_test.go 2013-06-19 18:05:58 +0000
+++ decode_test.go 2013-06-25 15:18:23 +0000
@@ -5,6 +5,7 @@
   "launchpad.net/goyaml"
   "math"
   "reflect"
+ "time"
  )

  var unmarshalIntTest = 123
@@ -341,6 +342,30 @@
     C inlineB `yaml:",inline"`
    }{1, inlineB{2, inlineC{3}}},
   },
+
+ // Durations
+ {
+ "a: 10m",
+ &struct{ A time.Duration }{10 * time.Minute},
+ },
+ {
+ "a: 10m5s",
+ &struct{ A time.Duration }{10*time.Minute + 5*time.Second},
+ },
+ {
+ "a: 10ms",
+ &struct{ A time.Duration }{10 * time.Millisecond},
+ },
+ {
+ "a: 1h2m3.004005006s",
+ &struct{ A time.Duration }{
+ 1*time.Hour + 2*time.Minute + 3*time.Second + 4*time.Millisecond +
5*time.Microsecond + 6*time.Nanosecond,
+ },
+ },
+ {
+ "a: 42",
+ &struct{ A time.Duration }{time.Duration(42)},
+ },
  }

  type inlineB struct {

Revision history for this message
Jake T (jbaikge) wrote :

Unmerged revisions

47. By Jacob Tews <jake@sable>

Added decode support for time.Duration with supporting tests

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'decode.go'
2--- decode.go 2013-06-19 17:52:23 +0000
3+++ decode.go 2013-06-25 15:33:24 +0000
4@@ -3,6 +3,7 @@
5 import (
6 "reflect"
7 "strconv"
8+ "time"
9 )
10
11 const (
12@@ -23,6 +24,8 @@
13 anchors map[string]*node
14 }
15
16+var durationType = reflect.TypeOf(time.Duration(0))
17+
18 // ----------------------------------------------------------------------------
19 // Parser, produces a node tree out of a libyaml event stream.
20
21@@ -315,6 +318,12 @@
22 out.SetInt(resolved)
23 good = true
24 }
25+ case string:
26+ if out.Type() == durationType {
27+ d, err := time.ParseDuration(resolved)
28+ out.SetInt(d.Nanoseconds())
29+ good = err == nil
30+ }
31 }
32 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
33 switch resolved := resolved.(type) {
34
35=== modified file 'decode_test.go'
36--- decode_test.go 2013-06-19 18:05:58 +0000
37+++ decode_test.go 2013-06-25 15:33:24 +0000
38@@ -5,6 +5,7 @@
39 "launchpad.net/goyaml"
40 "math"
41 "reflect"
42+ "time"
43 )
44
45 var unmarshalIntTest = 123
46@@ -341,6 +342,30 @@
47 C inlineB `yaml:",inline"`
48 }{1, inlineB{2, inlineC{3}}},
49 },
50+
51+ // Durations
52+ {
53+ "a: 10m",
54+ &struct{ A time.Duration }{10 * time.Minute},
55+ },
56+ {
57+ "a: 10m5s",
58+ &struct{ A time.Duration }{10*time.Minute + 5*time.Second},
59+ },
60+ {
61+ "a: 10ms",
62+ &struct{ A time.Duration }{10 * time.Millisecond},
63+ },
64+ {
65+ "a: 1h2m3.004005006s",
66+ &struct{ A time.Duration }{
67+ 1*time.Hour + 2*time.Minute + 3*time.Second + 4*time.Millisecond + 5*time.Microsecond + 6*time.Nanosecond,
68+ },
69+ },
70+ {
71+ "a: 42",
72+ &struct{ A time.Duration }{time.Duration(42)},
73+ },
74 }
75
76 type inlineB struct {

Subscribers

People subscribed via source and target branches

to all changes: