Skip to content
This repository has been archived by the owner on Apr 2, 2018. It is now read-only.

Commit

Permalink
implement goyaml's unmarshaler
Browse files Browse the repository at this point in the history
  • Loading branch information
vito committed Apr 6, 2015
1 parent f2cf940 commit f59c7d7
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 6 deletions.
13 changes: 13 additions & 0 deletions blackbox_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package blackbox_test

import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

"testing"
)

func TestBlackbox(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Blackbox Suite")
}
13 changes: 7 additions & 6 deletions config.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package blackbox

import (
"fmt"
"errors"
"io/ioutil"
"os"
"time"
Expand All @@ -12,15 +12,16 @@ import (

type Duration time.Duration

func (d *Duration) UnmarshalYAML(tag string, value interface{}) error {
if num, ok := value.(int64); ok {
func (d *Duration) UnmarshalYAML(unmarshal func(interface{}) error) error {
var num int64
if err := unmarshal(&num); err == nil {
*d = Duration(num)
return nil
}

str, ok := value.(string)
if !ok {
return fmt.Errorf("invalid duration: %T (%#v)", value, value)
var str string
if err := unmarshal(&str); err != nil {
return errors.New("invalid duration; must be string or number")
}

duration, err := time.ParseDuration(str)
Expand Down
31 changes: 31 additions & 0 deletions config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package blackbox_test

import (
"time"

. "github.com/concourse/blackbox"
"gopkg.in/yaml.v2"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

var _ = Describe("Config", func() {
Describe("Duration", func() {
It("can be unmarshalled from YAML as a string", func() {
var duration Duration
err := yaml.Unmarshal([]byte("10s"), &duration)
Expect(err).ToNot(HaveOccurred())

Expect(time.Duration(duration)).To(Equal(10 * time.Second))
})

It("can be unmarshalled from YAML as an integer", func() {
var duration Duration
err := yaml.Unmarshal([]byte("10"), &duration)
Expect(err).ToNot(HaveOccurred())

Expect(time.Duration(duration)).To(Equal(10 * time.Nanosecond))
})
})
})

0 comments on commit f59c7d7

Please sign in to comment.