Repository: threefoldtech/zos
Pull request #390 (2019-11-20 15:43:12)

add BigInt method to schema.(*Numeric)

codecov[bot]: # [Codecov](https://codecov.io/gh/threefoldtech/zos/pull/390?src=pr&el=h1) Report
> Merging [#390](https://codecov.io/gh/threefoldtech/zos/pull/390?src=pr&el=desc) into [master](https://codecov.io/gh/threefoldtech/zos/commit/937e4116e56356ddf2786d7e73cd00d66640328b?src=pr&el=desc) will **increase** coverage by `0.14%`.
> The diff coverage is `66.66%`.

[![Impacted file tree graph](https://codecov.io/gh/threefoldtech/zos/pull/390/graphs/tree.svg?width=650&token=f7Vmq1OAEm&height=150&src=pr)](https://codecov.io/gh/threefoldtech/zos/pull/390?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master     #390      +/-   ##
==========================================
+ Coverage   40.92%   41.06%   +0.14%     
==========================================
  Files          62       62              
  Lines        3851     3857       +6     
==========================================
+ Hits         1576     1584       +8     
+ Misses       2062     2058       -4     
- Partials      213      215       +2
```


| [Impacted Files](https://codecov.io/gh/threefoldtech/zos/pull/390?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [pkg/schema/types.go](https://codecov.io/gh/threefoldtech/zos/pull/390/diff?src=pr&el=tree#diff-cGtnL3NjaGVtYS90eXBlcy5nbw==) | `63.2% <66.66%> (+4.2%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/threefoldtech/zos/pull/390?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/threefoldtech/zos/pull/390?src=pr&el=footer). Last update [937e411...1dce42a](https://codecov.io/gh/threefoldtech/zos/pull/390?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/pkg/schema/types.go b/pkg/schema/types.go
index 5e9f9cf1..7e4ee264 100644
--- a/pkg/schema/types.go
+++ b/pkg/schema/types.go
@@ -7,6 +7,8 @@ import (
 	"regexp"
 	"strconv"
 	"time"
+
+	"math/big"
 )
 
 var (
@@ -32,6 +34,16 @@ func (n Numeric) Float64() (float64, error) {
 	return strconv.ParseFloat(string(n), 64)
 }
 
+// BigInt returns parsed big.Int value
+func (n Numeric) BigInt() (*big.Int, error) {
+	bi := new(big.Int)
+	_, ok := bi.SetString(string(n), 10)
+	if !ok {
+		return nil, fmt.Errorf("failed to return '%s' as a big Int", n)
+	}
+	return bi, nil
+}
+
 // UnmarshalJSON method
 func (n *Numeric) UnmarshalJSON(in []byte) error {
 	var v interface{}


Test patch:
diff --git a/pkg/schema/types_test.go b/pkg/schema/types_test.go
index 7c3dc092..22c7b60d 100644
--- a/pkg/schema/types_test.go
+++ b/pkg/schema/types_test.go
@@ -2,6 +2,7 @@ package schema
 
 import (
 	"encoding/json"
+	"fmt"
 	"net"
 	"testing"
 	"time"
@@ -38,6 +39,23 @@ func TestParseDate(t *testing.T) {
 	}
 }
 
+func TestNumericBigInt(t *testing.T) {
+	const inputValue = "1344719667586153181419716641724567886890850696275767987106294472017884974410332069524504824747437757"
+	var n Numeric
+	err := n.UnmarshalJSON([]byte(fmt.Sprintf(`"%s"`, inputValue)))
+	if err != nil {
+		t.Fatal(err)
+	}
+	bi, err := n.BigInt()
+	if err != nil {
+		t.Fatal(err)
+	}
+	output := bi.String()
+	if inputValue != output {
+		t.Fatalf("%s != %s", inputValue, output)
+	}
+}
+
 func TestDateNil(t *testing.T) {
 	var d Date