Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add function to return int values #49

Merged
merged 1 commit into from
Sep 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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