Skip to content

Commit

Permalink
add URL scalar
Browse files Browse the repository at this point in the history
  • Loading branch information
hgiasac committed Jul 13, 2024
1 parent c9e64bd commit 44d8fa9
Show file tree
Hide file tree
Showing 17 changed files with 333 additions and 171 deletions.
2 changes: 2 additions & 0 deletions cmd/hasura-ndc-go/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,8 @@ func (sp *SchemaParser) parseType(rootType *TypeInfo, ty types.Type, fieldPaths
scalarSchema.Representation = schema.NewTypeRepresentationBigInteger().Encode()
case "Bytes":
scalarSchema.Representation = schema.NewTypeRepresentationBytes().Encode()
case "URL":
scalarSchema.Representation = schema.NewTypeRepresentationString().Encode()
}
}

Expand Down
5 changes: 5 additions & 0 deletions cmd/hasura-ndc-go/testdata/basic/expected/functions.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,10 @@ func (j *GetTypesArguments) FromValue(input map[string]any) error {
if err != nil {
return err
}
err = functions_Decoder.DecodeObjectValue(&j.URL, input, "URL")
if err != nil {
return err
}
j.UUID, err = utils.GetUUID(input, "UUID")
if err != nil {
return err
Expand Down Expand Up @@ -792,6 +796,7 @@ func (j GetTypesArguments) ToMap() map[string]any {
r["TextPtr"] = j.TextPtr
r["Time"] = j.Time
r["TimePtr"] = j.TimePtr
r["URL"] = j.URL
r["UUID"] = j.UUID
r["UUIDArray"] = j.UUIDArray
r["UUIDPtr"] = j.UUIDPtr
Expand Down
26 changes: 24 additions & 2 deletions cmd/hasura-ndc-go/testdata/basic/expected/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -1287,6 +1287,12 @@
}
}
},
"URL": {
"type": {
"name": "URL",
"type": "named"
}
},
"UUID": {
"type": {
"name": "UUID",
Expand Down Expand Up @@ -2795,6 +2801,12 @@
}
}
},
"URL": {
"type": {
"name": "URL",
"type": "named"
}
},
"UUID": {
"type": {
"name": "UUID",
Expand Down Expand Up @@ -3144,7 +3156,10 @@
"aggregate_functions": {},
"comparison_operators": {},
"representation": {
"one_of": ["foo", "bar"],
"one_of": [
"foo",
"bar"
],
"type": "enum"
}
},
Expand All @@ -3162,6 +3177,13 @@
"type": "timestamptz"
}
},
"URL": {
"aggregate_functions": {},
"comparison_operators": {},
"representation": {
"type": "string"
}
},
"UUID": {
"aggregate_functions": {},
"comparison_operators": {},
Expand All @@ -3170,4 +3192,4 @@
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ type GetTypesArguments struct {
CustomScalar CommentText
Enum SomeEnum
BigInt scalar.BigInt
URL scalar.URL

UUIDPtr *uuid.UUID
BoolPtr *bool
Expand Down
55 changes: 34 additions & 21 deletions example/codegen/connector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,10 @@ func TestQueryGetTypes(t *testing.T) {
"type": "literal",
"value": "2024-04-02"
},
"URL": {
"type": "literal",
"value": "https://example.com"
},
"UUIDPtr": {
"type": "literal",
"value": "b085b0b9-007c-440e-9661-0d8f2de98a5b"
Expand Down Expand Up @@ -774,6 +778,10 @@ func TestQueryGetTypes(t *testing.T) {
"column": "DatePtr",
"type": "column"
},
"URL": {
"column": "URL",
"type": "column"
},
"NamedArray": {
"column": "NamedArray",
"fields": {
Expand Down Expand Up @@ -1301,27 +1309,32 @@ func TestQueryGetTypes(t *testing.T) {
"collection_relationships": {}
}`,
response: arguments.GetTypesArguments{
UUID: uuid.MustParse("b085b0b9-007c-440e-9661-0d8f2de98a5a"),
Bool: true,
String: "hello",
Int: 1,
Int8: 2,
Int16: 3,
Int32: 4,
Int64: 5,
Uint: 6,
Uint8: 7,
Uint16: 8,
Uint32: 9,
Uint64: 10,
Float32: 1.1,
Float64: 2.2,
Time: time.Date(2024, 3, 5, 7, 0, 56, 0, time.UTC),
Text: "text",
CustomScalar: commentText,
Enum: types.SomeEnumFoo,
BigInt: 10000,
Date: *scalar.NewDate(2024, 04, 02),
UUID: uuid.MustParse("b085b0b9-007c-440e-9661-0d8f2de98a5a"),
Bool: true,
String: "hello",
Int: 1,
Int8: 2,
Int16: 3,
Int32: 4,
Int64: 5,
Uint: 6,
Uint8: 7,
Uint16: 8,
Uint32: 9,
Uint64: 10,
Float32: 1.1,
Float64: 2.2,
Time: time.Date(2024, 3, 5, 7, 0, 56, 0, time.UTC),
Text: "text",
CustomScalar: commentText,
Enum: types.SomeEnumFoo,
BigInt: 10000,
Date: *scalar.NewDate(2024, 04, 02),
URL: func() scalar.URL {
r, _ := scalar.NewURL("https://example.com")
return *r
}(),

UUIDPtr: utils.ToPtr(uuid.MustParse("b085b0b9-007c-440e-9661-0d8f2de98a5b")),
BoolPtr: utils.ToPtr(true),
StringPtr: utils.ToPtr("world"),
Expand Down
19 changes: 19 additions & 0 deletions example/codegen/schema.generated.json
Original file line number Diff line number Diff line change
Expand Up @@ -1302,6 +1302,12 @@
}
}
},
"URL": {
"type": {
"name": "URL",
"type": "named"
}
},
"UUID": {
"type": {
"name": "UUID",
Expand Down Expand Up @@ -2834,6 +2840,12 @@
}
}
},
"URL": {
"type": {
"name": "URL",
"type": "named"
}
},
"UUID": {
"type": {
"name": "UUID",
Expand Down Expand Up @@ -3202,6 +3214,13 @@
"type": "timestamptz"
}
},
"URL": {
"aggregate_functions": {},
"comparison_operators": {},
"representation": {
"type": "string"
}
},
"UUID": {
"aggregate_functions": {},
"comparison_operators": {},
Expand Down
1 change: 1 addition & 0 deletions example/codegen/types/arguments/arguments.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type GetTypesArguments struct {
Enum types.SomeEnum
BigInt scalar.BigInt
Date scalar.Date
URL scalar.URL

UUIDPtr *uuid.UUID
BoolPtr *bool
Expand Down
5 changes: 5 additions & 0 deletions example/codegen/types/arguments/types.generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions scalar/bigint.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ func NewBigInt(value int64) BigInt {
return BigInt(value)
}

// ScalarName get the schema name of the scalar
func (bi BigInt) ScalarName() string {
return "BigInt"
}

// Stringer implements fmt.Stringer interface.
func (bi BigInt) String() string {
return strconv.FormatInt(int64(bi), 10)
Expand Down
8 changes: 5 additions & 3 deletions scalar/bigint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import (
func TestBigInt(t *testing.T) {
expected := "9999"
value := NewBigInt(0)
scalarName := value.ScalarName()

if err := json.Unmarshal([]byte(expected), &value); err != nil {
t.Errorf("failed to parse bigint: %s", err)
t.Errorf("failed to parse %s: %s", scalarName, err)
t.FailNow()
}
if expected != value.String() {
Expand All @@ -19,7 +21,7 @@ func TestBigInt(t *testing.T) {
}
rawValue, err := json.Marshal(value)
if err != nil {
t.Errorf("failed to encode bigint: %s", err)
t.Errorf("failed to encode %s: %s", scalarName, err)
t.FailNow()
}

Expand All @@ -30,7 +32,7 @@ func TestBigInt(t *testing.T) {

var value1 BigInt
if err := value1.FromValue(expected); err != nil {
t.Errorf("failed to decode bigint: %s", err)
t.Errorf("failed to decode %s: %s", scalarName, err)
t.FailNow()
}

Expand Down
5 changes: 5 additions & 0 deletions scalar/bytes.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ func ParseBytes(value string) (*Bytes, error) {
return &Bytes{data: bs}, nil
}

// ScalarName get the schema name of the scalar
func (bs Bytes) ScalarName() string {
return "Bytes"
}

// Bytes get the inner bytes value.
func (bs Bytes) Bytes() []byte {
return bs.data
Expand Down
5 changes: 3 additions & 2 deletions scalar/bytes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ func TestBytes(t *testing.T) {

rawJSON := fmt.Sprintf(`"%s"`, expectedB64)
var value Bytes
scalarName := value.ScalarName()
if err := json.Unmarshal([]byte(rawJSON), &value); err != nil {
t.Errorf("failed to parse Bytes: %s", err)
t.Errorf("failed to parse %s: %s", scalarName, err)
t.FailNow()
}
if plain != value.String() {
Expand All @@ -25,7 +26,7 @@ func TestBytes(t *testing.T) {
}
rawValue, err := json.Marshal(value)
if err != nil {
t.Errorf("failed to encode Bytes: %s", err)
t.Errorf("failed to encode %s: %s", scalarName, err)
t.FailNow()
}

Expand Down
5 changes: 5 additions & 0 deletions scalar/date.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ func ParseDate(value string) (*Date, error) {
return &Date{Time: t}, nil
}

// ScalarName get the schema name of the scalar
func (d Date) ScalarName() string {
return "Date"
}

// Stringer implements fmt.Stringer interface.
func (d Date) String() string {
return d.Format(dateFormat)
Expand Down
8 changes: 5 additions & 3 deletions scalar/date_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ func TestDate(t *testing.T) {
expected := NewDate(2014, 04, 1).String()
rawJSON := fmt.Sprintf(`"%s"`, expected)
var value Date
scalarName := value.ScalarName()

if err := json.Unmarshal([]byte(rawJSON), &value); err != nil {
t.Errorf("failed to parse Date: %s", err)
t.Errorf("failed to parse %s: %s", scalarName, err)
t.FailNow()
}
if expected != value.String() {
Expand All @@ -21,7 +23,7 @@ func TestDate(t *testing.T) {
}
rawValue, err := json.Marshal(value)
if err != nil {
t.Errorf("failed to encode Date: %s", err)
t.Errorf("failed to encode %s: %s", scalarName, err)
t.FailNow()
}

Expand All @@ -32,7 +34,7 @@ func TestDate(t *testing.T) {

var value1 Date
if err := value1.FromValue(expected); err != nil {
t.Errorf("failed to decode Date: %s", err)
t.Errorf("failed to decode %s: %s", scalarName, err)
t.FailNow()
}

Expand Down
Loading

0 comments on commit 44d8fa9

Please sign in to comment.