Skip to content

Commit 4c1b38e

Browse files
authored
Add biginteger representation (#72)
1 parent 1a3f80d commit 4c1b38e

11 files changed

Lines changed: 84 additions & 25 deletions

File tree

cmd/ndc-go-sdk/README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ The basic scalar types supported are:
190190
| Int8 | int8, uin8 | A 8-bit signed integer with a minimum value of -2^7 and a maximum value of 2^7 - 1 | int8 |
191191
| Int16 | int16, uint16 | A 16-bit signed integer with a minimum value of -2^15 and a maximum value of 2^15 - 1 | int16 |
192192
| Int32 | int32, uint32 | A 32-bit signed integer with a minimum value of -2^31 and a maximum value of 2^31 - 1 | int32 |
193-
| Int64 | int64, uint64 | A 64-bit signed integer with a minimum value of -2^63 and a maximum value of 2^63 - 1 | int32 |
193+
| Int64 | int64, uint64 | A 64-bit signed integer with a minimum value of -2^63 and a maximum value of 2^63 - 1 | int64 |
194194
| Bigint | scalar.BigInt | A 64-bit signed integer with a minimum value of -2^63 and a maximum value of 2^63 - 1 | string |
195195
| Float32 | float32 | An IEEE-754 single-precision floating-point number | float32 |
196196
| Float64 | float64 | An IEEE-754 double-precision floating-point number | float64 |
@@ -199,8 +199,6 @@ The basic scalar types supported are:
199199
| TimestampTZ | time.Time | ISO 8601 timestamp-with-timezone | string |
200200
| Enum | | Enumeration values | string |
201201

202-
> json.Unmarshaler can't automatically decode string to `int64` value. If you want to parse int64 from string, use `scalar.BigInt` instead
203-
204202
Alias scalar types will be inferred to the origin type in the schema.
205203

206204
```go

cmd/ndc-go-sdk/schema.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ const (
3030
ScalarInt64 ScalarName = "Int64"
3131
ScalarFloat32 ScalarName = "Float32"
3232
ScalarFloat64 ScalarName = "Float64"
33+
ScalarBigInt ScalarName = "BigInt"
3334
ScalarBigDecimal ScalarName = "BigDecimal"
3435
ScalarUUID ScalarName = "UUID"
3536
ScalarDate ScalarName = "Date"
@@ -69,9 +70,7 @@ var defaultScalarTypes = map[ScalarName]schema.ScalarType{
6970
ScalarInt64: {
7071
AggregateFunctions: schema.ScalarTypeAggregateFunctions{},
7172
ComparisonOperators: map[string]schema.ComparisonOperatorDefinition{},
72-
// json.Unmarshaler can't automatically parse integer to int64.
73-
// if you want to parse int64 from string, use scalar.BigInt instead.
74-
Representation: schema.NewTypeRepresentationInt32().Encode(),
73+
Representation: schema.NewTypeRepresentationInt64().Encode(),
7574
},
7675
ScalarFloat32: {
7776
AggregateFunctions: schema.ScalarTypeAggregateFunctions{},
@@ -83,6 +82,11 @@ var defaultScalarTypes = map[ScalarName]schema.ScalarType{
8382
ComparisonOperators: map[string]schema.ComparisonOperatorDefinition{},
8483
Representation: schema.NewTypeRepresentationFloat64().Encode(),
8584
},
85+
ScalarBigInt: {
86+
AggregateFunctions: schema.ScalarTypeAggregateFunctions{},
87+
ComparisonOperators: map[string]schema.ComparisonOperatorDefinition{},
88+
Representation: schema.NewTypeRepresentationBigInteger().Encode(),
89+
},
8690
ScalarBigDecimal: {
8791
AggregateFunctions: schema.ScalarTypeAggregateFunctions{},
8892
ComparisonOperators: map[string]schema.ComparisonOperatorDefinition{},
@@ -587,7 +591,7 @@ func (sp *SchemaParser) parseType(rawSchema *RawConnectorSchema, rootType *TypeI
587591
case "Date":
588592
scalarSchema.Representation = schema.NewTypeRepresentationDate().Encode()
589593
case "BigInt":
590-
scalarSchema.Representation = schema.NewTypeRepresentationInt64().Encode()
594+
scalarSchema.Representation = schema.NewTypeRepresentationBigInteger().Encode()
591595
}
592596
}
593597

cmd/ndc-go-sdk/testdata/basic/expected/functions.go.tmpl

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package functions
33
import (
44
"encoding/json"
55
"errors"
6+
"github.com/hasura/ndc-sdk-go/scalar"
67
"github.com/hasura/ndc-sdk-go/schema"
78
"github.com/hasura/ndc-sdk-go/utils"
89
)
@@ -25,7 +26,7 @@ func (j *GetTypesArguments) FromValue(input map[string]any) error {
2526
if err != nil {
2627
return err
2728
}
28-
j.BigIntPtr = new(BigInt)
29+
j.BigIntPtr = new(scalar.BigInt)
2930
err = functions_Decoder.DecodeNullableObjectValue(j.BigIntPtr, input, "BigIntPtr")
3031
if err != nil {
3132
return err
@@ -364,11 +365,6 @@ func (j HelloResult) ToMap() map[string]any {
364365
return r
365366
}
366367

367-
// ScalarName get the schema name of the scalar
368-
func (j BigInt) ScalarName() string {
369-
return "BigInt"
370-
}
371-
372368
// ScalarName get the schema name of the scalar
373369
func (j CommentText) ScalarName() string {
374370
return "CommentString"

cmd/ndc-go-sdk/testdata/basic/expected/schema.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1067,7 +1067,7 @@
10671067
"aggregate_functions": {},
10681068
"comparison_operators": {},
10691069
"representation": {
1070-
"type": "string"
1070+
"type": "biginteger"
10711071
}
10721072
},
10731073
"Boolean": {
@@ -1123,7 +1123,7 @@
11231123
"aggregate_functions": {},
11241124
"comparison_operators": {},
11251125
"representation": {
1126-
"type": "int32"
1126+
"type": "int64"
11271127
}
11281128
},
11291129
"Int8": {

cmd/ndc-go-sdk/testdata/basic/source/functions/prefix.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,11 @@ import (
88

99
"github.com/google/uuid"
1010
"github.com/hasura/ndc-codegen-test/types"
11+
"github.com/hasura/ndc-sdk-go/scalar"
1112
)
1213

1314
type Text string
1415

15-
// BigInt a big integer
16-
//
17-
// @scalar string
18-
type BigInt string
19-
2016
// A foo scalar
2117
type ScalarFoo struct {
2218
bar string
@@ -105,7 +101,7 @@ type GetTypesArguments struct {
105101
Text Text
106102
CustomScalar CommentText
107103
Enum SomeEnum
108-
BigInt BigInt
104+
BigInt scalar.BigInt
109105

110106
UUIDPtr *uuid.UUID
111107
BoolPtr *bool
@@ -126,7 +122,7 @@ type GetTypesArguments struct {
126122
TextPtr *Text
127123
CustomScalarPtr *CommentText
128124
EnumPtr *SomeEnum
129-
BigIntPtr *BigInt
125+
BigIntPtr *scalar.BigInt
130126

131127
Object struct {
132128
ID uuid.UUID `json:"id"`

example/codegen/functions/types.generated.go

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

example/codegen/schema.generated.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1106,7 +1106,7 @@
11061106
"aggregate_functions": {},
11071107
"comparison_operators": {},
11081108
"representation": {
1109-
"type": "int64"
1109+
"type": "biginteger"
11101110
}
11111111
},
11121112
"Boolean": {

scalar/bigint.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"github.com/hasura/ndc-sdk-go/utils"
88
)
99

10-
// BigInt wraps the scalar implementation for 64-bit signed integer,
10+
// BigInt wraps the scalar implementation for big integer,
1111
// with string representation
1212
//
1313
// @scalar BigInt string

schema/scalar.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ const (
5252
TypeRepresentationTypeFloat32 TypeRepresentationType = "float32"
5353
// An IEEE-754 double-precision floating-point number
5454
TypeRepresentationTypeFloat64 TypeRepresentationType = "float64"
55+
// Arbitrary-precision integer string
56+
TypeRepresentationTypeBigInteger TypeRepresentationType = "biginteger"
5557
// Arbitrary-precision decimal string
5658
TypeRepresentationTypeBigDecimal TypeRepresentationType = "bigdecimal"
5759
// UUID string (8-4-4-4-12)
@@ -84,6 +86,7 @@ var enumValues_TypeRepresentationType = []TypeRepresentationType{
8486
TypeRepresentationTypeInt64,
8587
TypeRepresentationTypeFloat32,
8688
TypeRepresentationTypeFloat64,
89+
TypeRepresentationTypeBigInteger,
8790
TypeRepresentationTypeBigDecimal,
8891
TypeRepresentationTypeUUID,
8992
TypeRepresentationTypeDate,
@@ -346,6 +349,21 @@ func (ty TypeRepresentation) AsFloat64() (*TypeRepresentationFloat64, error) {
346349
}, nil
347350
}
348351

352+
// AsBigInteger tries to convert the current type to TypeRepresentationBigInteger
353+
func (ty TypeRepresentation) AsBigInteger() (*TypeRepresentationBigInteger, error) {
354+
t, err := ty.Type()
355+
if err != nil {
356+
return nil, err
357+
}
358+
if t != TypeRepresentationTypeBigInteger {
359+
return nil, fmt.Errorf("invalid TypeRepresentation type; expected %s, got %s", TypeRepresentationTypeBigInteger, t)
360+
}
361+
362+
return &TypeRepresentationBigInteger{
363+
Type: t,
364+
}, nil
365+
}
366+
349367
// AsBigDecimal tries to convert the current type to TypeRepresentationBigDecimal
350368
func (ty TypeRepresentation) AsBigDecimal() (*TypeRepresentationBigDecimal, error) {
351369
t, err := ty.Type()
@@ -549,6 +567,8 @@ func (ty TypeRepresentation) InterfaceT() (TypeRepresentationEncoder, error) {
549567
return ty.AsFloat32()
550568
case TypeRepresentationTypeFloat64:
551569
return ty.AsFloat64()
570+
case TypeRepresentationTypeBigInteger:
571+
return ty.AsBigInteger()
552572
case TypeRepresentationTypeBigDecimal:
553573
return ty.AsBigDecimal()
554574
case TypeRepresentationTypeUUID:
@@ -775,6 +795,25 @@ func (ty TypeRepresentationFloat64) Encode() TypeRepresentation {
775795
}
776796
}
777797

798+
// TypeRepresentationBigInteger represents an arbitrary-precision integer string
799+
type TypeRepresentationBigInteger struct {
800+
Type TypeRepresentationType `json:"type" yaml:"type" mapstructure:"type"`
801+
}
802+
803+
// NewTypeRepresentationBigInteger creates a new TypeRepresentationBigInteger instance
804+
func NewTypeRepresentationBigInteger() *TypeRepresentationBigInteger {
805+
return &TypeRepresentationBigInteger{
806+
Type: TypeRepresentationTypeBigInteger,
807+
}
808+
}
809+
810+
// Encode returns the raw TypeRepresentation instance
811+
func (ty TypeRepresentationBigInteger) Encode() TypeRepresentation {
812+
return map[string]any{
813+
"type": ty.Type,
814+
}
815+
}
816+
778817
// TypeRepresentationBigDecimal represents an arbitrary-precision decimal string
779818
type TypeRepresentationBigDecimal struct {
780819
Type TypeRepresentationType `json:"type" yaml:"type" mapstructure:"type"`

schema/scalar_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,18 @@ func TestTypeRepresentation(t *testing.T) {
167167
assertDeepEqual(t, anyType, typeRep)
168168
})
169169

170+
t.Run("big integer", func(t *testing.T) {
171+
typeRep := NewTypeRepresentationBigInteger()
172+
rawType := typeRep.Encode()
173+
174+
_, err := rawType.AsString()
175+
assertError(t, err, "invalid TypeRepresentation type; expected string, got biginteger")
176+
177+
anyType, ok := rawType.Interface().(*TypeRepresentationBigInteger)
178+
assertDeepEqual(t, true, ok)
179+
assertDeepEqual(t, anyType, typeRep)
180+
})
181+
170182
t.Run("big decimal", func(t *testing.T) {
171183
typeRep := NewTypeRepresentationBigDecimal()
172184
rawType := typeRep.Encode()

0 commit comments

Comments
 (0)