feat(graphql): add JS-safe uint64 marshal helpers
Adds MarshalUint64, UnmarshalUint64, and Uint64Marshaler for gqlgen- compatible uint64 string scalars without a gqlgen dependency. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
package dbx
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestMarshalUint64(t *testing.T) {
|
||||
var buf bytes.Buffer
|
||||
MarshalUint64(9007199254740993).MarshalGQL(&buf)
|
||||
assert.Equal(t, `"9007199254740993"`, buf.String())
|
||||
}
|
||||
|
||||
func TestUnmarshalUint64_String(t *testing.T) {
|
||||
n, err := UnmarshalUint64("42")
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, uint64(42), n)
|
||||
}
|
||||
|
||||
func TestUnmarshalUint64_Number(t *testing.T) {
|
||||
n, err := UnmarshalUint64(float64(42))
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, uint64(42), n)
|
||||
}
|
||||
|
||||
func TestUnmarshalUint64_RoundTripJSON(t *testing.T) {
|
||||
const original uint64 = 9007199254740993
|
||||
var buf bytes.Buffer
|
||||
MarshalUint64(original).MarshalGQL(&buf)
|
||||
|
||||
var wire string
|
||||
require.NoError(t, json.Unmarshal(buf.Bytes(), &wire))
|
||||
|
||||
got, err := UnmarshalUint64(wire)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, original, got)
|
||||
}
|
||||
|
||||
func TestUnmarshalUint64_Invalid(t *testing.T) {
|
||||
_, err := UnmarshalUint64(true)
|
||||
assert.Error(t, err)
|
||||
|
||||
_, err = UnmarshalUint64(float64(-1))
|
||||
assert.Error(t, err)
|
||||
}
|
||||
Reference in New Issue
Block a user