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

Added Marshal/Unmarshal for graphQL #9

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
20 changes: 20 additions & 0 deletions decimal.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package alpacadecimal

import (
"database/sql/driver"
"io"
"math"
"math/big"
"regexp"
Expand Down Expand Up @@ -574,6 +575,11 @@ func (d Decimal) MarshalBinary() (data []byte, err error) {
return d.asFallback().MarshalBinary()
}

func (a Decimal) MarshalGQL(w io.Writer) {
j, _ := a.MarshalText()
_, _ = w.Write(j)
}

// optimized:
func (d Decimal) MarshalJSON() ([]byte, error) {
var str string
Expand Down Expand Up @@ -954,6 +960,10 @@ func (d *Decimal) UnmarshalBinary(data []byte) error {
return nil
}

func (a *Decimal) UnmarshalGQL(v interface{}) error {
return a.Scan(v)
}

// optimized:
// UnmarshalJSON implements the json.Unmarshaler interface.
func (d *Decimal) UnmarshalJSON(decimalBytes []byte) error {
Expand Down Expand Up @@ -1031,6 +1041,12 @@ func NewNullDecimal(d Decimal) NullDecimal {
}
}

func (a NullDecimal) MarshalGQL(w io.Writer) {
j, _ := a.MarshalJSON()
_, _ = w.Write(j)
}


func (d NullDecimal) MarshalJSON() ([]byte, error) {
if !d.Valid {
return []byte("null"), nil
Expand All @@ -1054,6 +1070,10 @@ func (d *NullDecimal) Scan(value interface{}) error {
return d.Decimal.Scan(value)
}

func (a *NullDecimal) UnmarshalGQL(v interface{}) error {
return a.Scan(v)
}

func (d *NullDecimal) UnmarshalJSON(decimalBytes []byte) error {
if string(decimalBytes) == "null" {
d.Valid = false
Expand Down
33 changes: 33 additions & 0 deletions decimal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"math/big"
"regexp"
"strings"
"testing"

"github.com/alpacahq/alpacadecimal"
Expand Down Expand Up @@ -651,6 +652,22 @@ func TestDecimal(t *testing.T) {
shouldEqual(t, x, y)
})

t.Run("Decimal.MarshalGQL", func(t *testing.T) {
{
var x alpacadecimal.Decimal
err := x.UnmarshalGQL("123.456")
require.NoError(t, err)
shouldEqual(t, x, alpacadecimal.New(123456, -3))
}

{
var x alpacadecimal.Decimal
err := x.UnmarshalGQL([]byte("error"))
require.Error(t, err)
shouldEqual(t, alpacadecimal.Zero, x)
}
})

t.Run("Decimal.MarshalJSON", func(t *testing.T) {
{
var x alpacadecimal.Decimal
Expand Down Expand Up @@ -1028,6 +1045,22 @@ func TestDecimal(t *testing.T) {
shouldEqual(t, x, y)
})

t.Run("Decimal.UnmarshalGQL", func(t *testing.T) {
{
var b strings.Builder
x := alpacadecimal.NewFromInt(123)
x.MarshalGQL(&b)
require.Equal(t, "123", b.String())
}

{
var b strings.Builder
x := alpacadecimal.NewFromInt(123456789)
x.MarshalGQL(&b)
require.Equal(t, "123456789", b.String())
}
})

t.Run("Decimal.UnmarshalJSON", func(t *testing.T) {
{
x := alpacadecimal.NewFromInt(123)
Expand Down