Skip to content

Commit 3d09c97

Browse files
Merge #4
4: Add AssertFloat64 and AssertInt64 r=Jaskaranbir a=Jaskaranbir Co-authored-by: Jaskaranbir <jaskaranbir.dhillon@gmail.com>
2 parents bec34e8 + 1c8fbe4 commit 3d09c97

File tree

6 files changed

+163
-5
lines changed

6 files changed

+163
-5
lines changed

Gopkg.lock

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ This package provides some convenience utilities. New utilities will be added as
1313
* **errors**
1414
* [ErrorStackTrace][7]
1515

16+
* **float64**
17+
* [AssertInt64][9]
18+
19+
* **int64**
20+
* [AssertInt64][10]
21+
1622
* **slices**
1723
* [AreElementsInSlice][1]
1824
* [AreElementsInSliceStrict][2]
@@ -31,4 +37,6 @@ This package provides some convenience utilities. New utilities will be added as
3137
[5]: https://godoc.org/github.com/TerrexTech/go-commonutils/commonutil#ParseHosts
3238
[6]: https://godoc.org/github.com/TerrexTech/go-commonutils/commonutil#StandardizeSpaces
3339
[7]: https://godoc.org/github.com/TerrexTech/go-commonutils/commonutil#ErrorStackTrace
34-
[7]: https://godoc.org/github.com/TerrexTech/go-commonutils/commonutil#ValidateEnv
40+
[8]: https://godoc.org/github.com/TerrexTech/go-commonutils/commonutil#ValidateEnv
41+
[9]: https://godoc.org/github.com/TerrexTech/go-commonutils/commonutil#AssertFloat64
42+
[10]: https://godoc.org/github.com/TerrexTech/go-commonutils/commonutil#AssertInt64

commonutil/float64util.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package commonutil
2+
3+
import "errors"
4+
5+
// AssertFloat64 attempts to type-assert the provided interface-argument to int64.
6+
// The provided interface-argument must be a numeric-type or an error is thrown.
7+
func AssertFloat64(num interface{}) (float64, error) {
8+
switch num.(type) {
9+
case int32:
10+
v := num.(int32)
11+
return float64(v), nil
12+
case int64:
13+
v := num.(int64)
14+
return float64(v), nil
15+
case float32:
16+
v := num.(float32)
17+
return float64(v), nil
18+
case float64:
19+
return num.(float64), nil
20+
default:
21+
return 0, errors.New("assertFloat64: Unexpected data-type")
22+
}
23+
}

commonutil/float64util_test.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package commonutil
2+
3+
import (
4+
. "github.com/onsi/ginkgo"
5+
. "github.com/onsi/gomega"
6+
)
7+
8+
var _ = Describe("Float64Util", func() {
9+
Describe("AssertFloat64", func() {
10+
Context("assert given numeric-interface to float64", func() {
11+
Specify("int32", func() {
12+
var v int32 = 4
13+
var v64 float64 = float64(v)
14+
float64Val, err := AssertFloat64(v)
15+
Expect(err).ToNot(HaveOccurred())
16+
Expect(float64Val).To(Equal(v64))
17+
})
18+
19+
Specify("int64", func() {
20+
var v int64 = 78
21+
var v64 float64 = float64(v)
22+
float64Val, err := AssertFloat64(v)
23+
Expect(err).ToNot(HaveOccurred())
24+
Expect(float64Val).To(Equal(v64))
25+
})
26+
27+
Specify("float32", func() {
28+
var v float32 = 234
29+
var v64 float64 = float64(v)
30+
float64Val, err := AssertFloat64(v)
31+
Expect(err).ToNot(HaveOccurred())
32+
Expect(float64Val).To(Equal(v64))
33+
})
34+
35+
Specify("float64", func() {
36+
var v float64 = 457
37+
float64Val, err := AssertFloat64(v)
38+
Expect(err).ToNot(HaveOccurred())
39+
Expect(float64Val).To(Equal(v))
40+
})
41+
})
42+
43+
Context("pass non-numeric interface", func() {
44+
It("should return an error", func() {
45+
v := "invalid"
46+
float64Val, err := AssertFloat64(v)
47+
Expect(err).To(HaveOccurred())
48+
Expect(float64Val).To(Equal(float64(0)))
49+
})
50+
})
51+
})
52+
})

commonutil/int64util.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package commonutil
2+
3+
import "errors"
4+
5+
// AssertInt64 attempts to type-assert the provided interface-argument to int64.
6+
// The provided interface-argument must be a numeric-type or an error is thrown.
7+
func AssertInt64(num interface{}) (int64, error) {
8+
switch num.(type) {
9+
case int32:
10+
v := num.(int32)
11+
return int64(v), nil
12+
case int64:
13+
return num.(int64), nil
14+
case float32:
15+
v := num.(float32)
16+
return int64(v), nil
17+
case float64:
18+
v := num.(float64)
19+
return int64(v), nil
20+
default:
21+
return 0, errors.New("assertInt64: Unexpected data-type")
22+
}
23+
}

commonutil/int64util_test.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package commonutil
2+
3+
import (
4+
. "github.com/onsi/ginkgo"
5+
. "github.com/onsi/gomega"
6+
)
7+
8+
var _ = Describe("Int64Util", func() {
9+
Describe("AssertInt64", func() {
10+
Context("assert given numeric-interface to int64", func() {
11+
Specify("int32", func() {
12+
var v int32 = 4
13+
var v64 int64 = int64(v)
14+
int64Val, err := AssertInt64(v)
15+
Expect(err).ToNot(HaveOccurred())
16+
Expect(int64Val).To(Equal(v64))
17+
})
18+
19+
Specify("int64", func() {
20+
var v int64 = 78
21+
int64Val, err := AssertInt64(v)
22+
Expect(err).ToNot(HaveOccurred())
23+
Expect(int64Val).To(Equal(v))
24+
})
25+
26+
Specify("float32", func() {
27+
var v float32 = 234
28+
var v64 int64 = int64(v)
29+
int64Val, err := AssertInt64(v)
30+
Expect(err).ToNot(HaveOccurred())
31+
Expect(int64Val).To(Equal(v64))
32+
})
33+
34+
Specify("float64", func() {
35+
var v float64 = 457
36+
var v64 int64 = int64(v)
37+
int64Val, err := AssertInt64(v)
38+
Expect(err).ToNot(HaveOccurred())
39+
Expect(int64Val).To(Equal(v64))
40+
})
41+
})
42+
43+
Context("pass non-numeric interface", func() {
44+
It("should return an error", func() {
45+
v := "invalid"
46+
int64Val, err := AssertInt64(v)
47+
Expect(err).To(HaveOccurred())
48+
Expect(int64Val).To(Equal(int64(0)))
49+
})
50+
})
51+
})
52+
})

0 commit comments

Comments
 (0)