Skip to content

Commit

Permalink
Merge #4
Browse files Browse the repository at this point in the history
4: Add AssertFloat64 and AssertInt64 r=Jaskaranbir a=Jaskaranbir



Co-authored-by: Jaskaranbir <jaskaranbir.dhillon@gmail.com>
  • Loading branch information
ninja-bruh and Jaskaranbir committed Oct 14, 2018
2 parents bec34e8 + 1c8fbe4 commit 3d09c97
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 5 deletions.
8 changes: 4 additions & 4 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ This package provides some convenience utilities. New utilities will be added as
* **errors**
* [ErrorStackTrace][7]

* **float64**
* [AssertInt64][9]

* **int64**
* [AssertInt64][10]

* **slices**
* [AreElementsInSlice][1]
* [AreElementsInSliceStrict][2]
Expand All @@ -31,4 +37,6 @@ This package provides some convenience utilities. New utilities will be added as
[5]: https://godoc.org/github.com/TerrexTech/go-commonutils/commonutil#ParseHosts
[6]: https://godoc.org/github.com/TerrexTech/go-commonutils/commonutil#StandardizeSpaces
[7]: https://godoc.org/github.com/TerrexTech/go-commonutils/commonutil#ErrorStackTrace
[7]: https://godoc.org/github.com/TerrexTech/go-commonutils/commonutil#ValidateEnv
[8]: https://godoc.org/github.com/TerrexTech/go-commonutils/commonutil#ValidateEnv
[9]: https://godoc.org/github.com/TerrexTech/go-commonutils/commonutil#AssertFloat64
[10]: https://godoc.org/github.com/TerrexTech/go-commonutils/commonutil#AssertInt64
23 changes: 23 additions & 0 deletions commonutil/float64util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package commonutil

import "errors"

// AssertFloat64 attempts to type-assert the provided interface-argument to int64.
// The provided interface-argument must be a numeric-type or an error is thrown.
func AssertFloat64(num interface{}) (float64, error) {
switch num.(type) {
case int32:
v := num.(int32)
return float64(v), nil
case int64:
v := num.(int64)
return float64(v), nil
case float32:
v := num.(float32)
return float64(v), nil
case float64:
return num.(float64), nil
default:
return 0, errors.New("assertFloat64: Unexpected data-type")
}
}
52 changes: 52 additions & 0 deletions commonutil/float64util_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package commonutil

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

var _ = Describe("Float64Util", func() {
Describe("AssertFloat64", func() {
Context("assert given numeric-interface to float64", func() {
Specify("int32", func() {
var v int32 = 4
var v64 float64 = float64(v)
float64Val, err := AssertFloat64(v)
Expect(err).ToNot(HaveOccurred())
Expect(float64Val).To(Equal(v64))
})

Specify("int64", func() {
var v int64 = 78
var v64 float64 = float64(v)
float64Val, err := AssertFloat64(v)
Expect(err).ToNot(HaveOccurred())
Expect(float64Val).To(Equal(v64))
})

Specify("float32", func() {
var v float32 = 234
var v64 float64 = float64(v)
float64Val, err := AssertFloat64(v)
Expect(err).ToNot(HaveOccurred())
Expect(float64Val).To(Equal(v64))
})

Specify("float64", func() {
var v float64 = 457
float64Val, err := AssertFloat64(v)
Expect(err).ToNot(HaveOccurred())
Expect(float64Val).To(Equal(v))
})
})

Context("pass non-numeric interface", func() {
It("should return an error", func() {
v := "invalid"
float64Val, err := AssertFloat64(v)
Expect(err).To(HaveOccurred())
Expect(float64Val).To(Equal(float64(0)))
})
})
})
})
23 changes: 23 additions & 0 deletions commonutil/int64util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package commonutil

import "errors"

// AssertInt64 attempts to type-assert the provided interface-argument to int64.
// The provided interface-argument must be a numeric-type or an error is thrown.
func AssertInt64(num interface{}) (int64, error) {
switch num.(type) {
case int32:
v := num.(int32)
return int64(v), nil
case int64:
return num.(int64), nil
case float32:
v := num.(float32)
return int64(v), nil
case float64:
v := num.(float64)
return int64(v), nil
default:
return 0, errors.New("assertInt64: Unexpected data-type")
}
}
52 changes: 52 additions & 0 deletions commonutil/int64util_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package commonutil

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

var _ = Describe("Int64Util", func() {
Describe("AssertInt64", func() {
Context("assert given numeric-interface to int64", func() {
Specify("int32", func() {
var v int32 = 4
var v64 int64 = int64(v)
int64Val, err := AssertInt64(v)
Expect(err).ToNot(HaveOccurred())
Expect(int64Val).To(Equal(v64))
})

Specify("int64", func() {
var v int64 = 78
int64Val, err := AssertInt64(v)
Expect(err).ToNot(HaveOccurred())
Expect(int64Val).To(Equal(v))
})

Specify("float32", func() {
var v float32 = 234
var v64 int64 = int64(v)
int64Val, err := AssertInt64(v)
Expect(err).ToNot(HaveOccurred())
Expect(int64Val).To(Equal(v64))
})

Specify("float64", func() {
var v float64 = 457
var v64 int64 = int64(v)
int64Val, err := AssertInt64(v)
Expect(err).ToNot(HaveOccurred())
Expect(int64Val).To(Equal(v64))
})
})

Context("pass non-numeric interface", func() {
It("should return an error", func() {
v := "invalid"
int64Val, err := AssertInt64(v)
Expect(err).To(HaveOccurred())
Expect(int64Val).To(Equal(int64(0)))
})
})
})
})

0 comments on commit 3d09c97

Please sign in to comment.