Skip to content

Commit

Permalink
feat: add function to return int values
Browse files Browse the repository at this point in the history
  • Loading branch information
aman00323 committed Sep 5, 2022
1 parent 422f42f commit cec47b8
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
10 changes: 10 additions & 0 deletions entry.go
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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 cec47b8

Please sign in to comment.