diff --git a/entry.go b/entry.go index 0a7ef3a..5c93c5f 100644 --- a/entry.go +++ b/entry.go @@ -50,6 +50,16 @@ func (entry *Entry) FloatField(name string) (value float64, err error) { return } +// IntField returns an entry field value as float64. Return nil if field does not exist +// and conversion error if cannot cast a type. +func (entry *Entry) IntField(name string) (value int64, err error) { + tmp, err := entry.Field(name) + if err == nil { + value, err = strconv.ParseInt(tmp, 0, 64) + } + return +} + // SetField sets the value of a field func (entry *Entry) SetField(name string, value string) { entry.fields[name] = value diff --git a/entry_test.go b/entry_test.go index 9893841..a3edc89 100644 --- a/entry_test.go +++ b/entry_test.go @@ -1,8 +1,9 @@ package gonx import ( - . "github.com/smartystreets/goconvey/convey" "testing" + + . "github.com/smartystreets/goconvey/convey" ) func TestEntry(t *testing.T) { @@ -45,6 +46,23 @@ func TestEntry(t *testing.T) { So(err, ShouldNotBeNil) So(val, ShouldEqual, 0.0) }) + + Convey("Get int values", func() { + // Get existings field + val, err := entry.IntField("foo") + So(err, ShouldBeNil) + So(val, ShouldEqual, 1) + + // Type casting eror + val, err = entry.IntField("bar") + So(err, ShouldNotBeNil) + So(val, ShouldEqual, 0) + + // Get field that does not exist + val, err = entry.IntField("baz") + So(err, ShouldNotBeNil) + So(val, ShouldEqual, 0) + }) }) Convey("Test set Entry fields", func() {