Skip to content

Commit

Permalink
Merge pull request #49 from aman00323/master
Browse files Browse the repository at this point in the history
feat: add function to return int values
  • Loading branch information
satyrius committed Sep 5, 2022
2 parents 422f42f + cec47b8 commit 78a4d7a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
10 changes: 10 additions & 0 deletions entry.go
Expand Up @@ -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
Expand Down
20 changes: 19 additions & 1 deletion 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) {
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit 78a4d7a

Please sign in to comment.