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
=== modified file 'decode.go'
--- decode.go 2013-06-19 17:52:23 +0000
+++ decode.go 2013-06-25 15:33:24 +0000
@@ -3,6 +3,7 @@
3import (3import (
4 "reflect"4 "reflect"
5 "strconv"5 "strconv"
6 "time"
6)7)
78
8const (9const (
@@ -23,6 +24,8 @@
23 anchors map[string]*node24 anchors map[string]*node
24}25}
2526
27var durationType = reflect.TypeOf(time.Duration(0))
28
26// ----------------------------------------------------------------------------29// ----------------------------------------------------------------------------
27// Parser, produces a node tree out of a libyaml event stream.30// Parser, produces a node tree out of a libyaml event stream.
2831
@@ -315,6 +318,12 @@
315 out.SetInt(resolved)318 out.SetInt(resolved)
316 good = true319 good = true
317 }320 }
321 case string:
322 if out.Type() == durationType {
323 d, err := time.ParseDuration(resolved)
324 out.SetInt(d.Nanoseconds())
325 good = err == nil
326 }
318 }327 }
319 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:328 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
320 switch resolved := resolved.(type) {329 switch resolved := resolved.(type) {
321330
=== modified file 'decode_test.go'
--- decode_test.go 2013-06-19 18:05:58 +0000
+++ decode_test.go 2013-06-25 15:33:24 +0000
@@ -5,6 +5,7 @@
5 "launchpad.net/goyaml"5 "launchpad.net/goyaml"
6 "math"6 "math"
7 "reflect"7 "reflect"
8 "time"
8)9)
910
10var unmarshalIntTest = 12311var unmarshalIntTest = 123
@@ -341,6 +342,30 @@
341 C inlineB `yaml:",inline"`342 C inlineB `yaml:",inline"`
342 }{1, inlineB{2, inlineC{3}}},343 }{1, inlineB{2, inlineC{3}}},
343 },344 },
345
346 // Durations
347 {
348 "a: 10m",
349 &struct{ A time.Duration }{10 * time.Minute},
350 },
351 {
352 "a: 10m5s",
353 &struct{ A time.Duration }{10*time.Minute + 5*time.Second},
354 },
355 {
356 "a: 10ms",
357 &struct{ A time.Duration }{10 * time.Millisecond},
358 },
359 {
360 "a: 1h2m3.004005006s",
361 &struct{ A time.Duration }{
362 1*time.Hour + 2*time.Minute + 3*time.Second + 4*time.Millisecond + 5*time.Microsecond + 6*time.Nanosecond,
363 },
364 },
365 {
366 "a: 42",
367 &struct{ A time.Duration }{time.Duration(42)},
368 },
344}369}
345370
346type inlineB struct {371type inlineB struct {

Subscribers

People subscribed via source and target branches

to all changes: