Skip to content

Commit

Permalink
interval
Browse files Browse the repository at this point in the history
  • Loading branch information
joelreymont committed Jun 26, 2015
1 parent 1e7ac28 commit 89afe2d
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
44 changes: 44 additions & 0 deletions interval.go
@@ -0,0 +1,44 @@
package main

import (
"fmt"
"strconv"
"time"
)

type RefreshInterval time.Duration

func (self RefreshInterval) String() string {
return time.Duration(self).String()
}

func (self *RefreshInterval) Set(v interface{}) error {
switch v := v.(type) {
case int:
*self = RefreshInterval(time.Duration(v) * time.Second)
return nil
case string:
i, err := strconv.ParseUint(v, 10, 64)
if err == nil {
*self = RefreshInterval(time.Duration(i) * time.Second)
return nil
}
d, err := time.ParseDuration(v)
if err != nil {
return err
}
ii := RefreshInterval(d)
if ii < MinimumRefreshInterval {
return fmt.Errorf("Refresh interval must be greater than %s", MinimumRefreshInterval)

This comment has been minimized.

Copy link
@leonsodhi

leonsodhi Jun 26, 2015

I think this should say "Refresh interval must be greater than or equal to %s"

}
*self = ii
return nil
default:
return fmt.Errorf("Invalid refresh interval: %v", v)
}
}

func (self *RefreshInterval) SetYAML(tag string, v interface{}) bool {
err := self.Set(v)
return err != nil
}
35 changes: 35 additions & 0 deletions interval_test.go
@@ -0,0 +1,35 @@
package main

import (
"time"

. "gopkg.in/check.v1"
"gopkg.in/yaml.v1"
)

type IntervalSuite struct {
}

var _ = Suite(&IntervalSuite{})

type i1 struct {
A RefreshInterval
B RefreshInterval
C RefreshInterval
}

func (s *IntervalSuite) SetUpSuite(c *C) {
}

func (s *SyslogSuite) TestInterval(c *C) {
var data = `
a: 100s
b: 10
c: '20'
`
v := &i1{}
c.Assert(yaml.Unmarshal([]byte(data), &v), IsNil)
c.Assert(v.A, Equals, RefreshInterval(100*time.Second))
c.Assert(v.B, Equals, RefreshInterval(10*time.Second))
c.Assert(v.C, Equals, RefreshInterval(20*time.Second))
}

0 comments on commit 89afe2d

Please sign in to comment.