-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
197 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package gq | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"reflect" | ||
) | ||
|
||
type Pointer struct { | ||
Type Schema | ||
} | ||
|
||
func (self Pointer) Key() string { | ||
return fmt.Sprintf("Pointer[%s]", self.Type.Key()) | ||
} | ||
|
||
func (self Pointer) Do(params *DoParams) Result { | ||
res := self.Type.Do(params) | ||
data := reflect.ValueOf(res.Data) | ||
|
||
if data.IsValid() && data.Kind() != reflect.Pointer { | ||
pointer := reflect.New(data.Type()) | ||
pointer.Elem().Set(data) | ||
res.Data = pointer.Interface() | ||
} | ||
|
||
return res | ||
} | ||
|
||
func (self Pointer) Resolve(params *ResolveParams) Result { | ||
res := self.Type.Resolve(params) | ||
data := reflect.ValueOf(res.Data) | ||
|
||
if data.IsValid() && data.Kind() != reflect.Pointer { | ||
pointer := reflect.New(data.Type()) | ||
pointer.Elem().Set(data) | ||
res.Data = pointer.Interface() | ||
} | ||
|
||
return res | ||
} | ||
|
||
func (self Pointer) MarshalJSON() ([]byte, error) { | ||
return json.Marshal(self.Key()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
package gq_test | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/thegogod/rum/gq" | ||
) | ||
|
||
func TestPointer(t *testing.T) { | ||
t.Run("should resolve string as pointer", func(t *testing.T) { | ||
schema := gq.Pointer{gq.String{}} | ||
res := schema.Do(&gq.DoParams{ | ||
Value: "testing", | ||
}) | ||
|
||
if res.Error != nil { | ||
t.Fatal(res.Error) | ||
} | ||
|
||
v, ok := res.Data.(*string) | ||
|
||
if !ok { | ||
t.Fatal(res.Data) | ||
} | ||
|
||
if *v != "testing" { | ||
t.Fatalf("expected `%s`, received `%s`", "testing", *v) | ||
} | ||
}) | ||
|
||
t.Run("should resolve bool as pointer", func(t *testing.T) { | ||
schema := gq.Pointer{gq.Bool{}} | ||
res := schema.Do(&gq.DoParams{ | ||
Value: true, | ||
}) | ||
|
||
if res.Error != nil { | ||
t.Fatal(res.Error) | ||
} | ||
|
||
v, ok := res.Data.(*bool) | ||
|
||
if !ok { | ||
t.Fatal(res.Data) | ||
} | ||
|
||
if *v != true { | ||
t.Fatalf("expected `%v`, received `%v`", true, *v) | ||
} | ||
}) | ||
|
||
t.Run("should resolve int as pointer", func(t *testing.T) { | ||
schema := gq.Pointer{gq.Int{}} | ||
res := schema.Do(&gq.DoParams{ | ||
Value: 102, | ||
}) | ||
|
||
if res.Error != nil { | ||
t.Fatal(res.Error) | ||
} | ||
|
||
v, ok := res.Data.(*int) | ||
|
||
if !ok { | ||
t.Fatal(res.Data) | ||
} | ||
|
||
if *v != 102 { | ||
t.Fatalf("expected `%v`, received `%v`", 102, *v) | ||
} | ||
}) | ||
|
||
t.Run("should resolve float as pointer", func(t *testing.T) { | ||
schema := gq.Pointer{gq.Float{}} | ||
res := schema.Do(&gq.DoParams{ | ||
Value: 11.123, | ||
}) | ||
|
||
if res.Error != nil { | ||
t.Fatal(res.Error) | ||
} | ||
|
||
v, ok := res.Data.(*float64) | ||
|
||
if !ok { | ||
t.Fatal(res.Data) | ||
} | ||
|
||
if *v != 11.123 { | ||
t.Fatalf("expected `%v`, received `%v`", 11.123, *v) | ||
} | ||
}) | ||
|
||
t.Run("should resolve `time.Time` as pointer", func(t *testing.T) { | ||
schema := gq.Pointer{gq.Date{}} | ||
res := schema.Do(&gq.DoParams{ | ||
Value: time.Now(), | ||
}) | ||
|
||
if res.Error != nil { | ||
t.Fatal(res.Error) | ||
} | ||
|
||
_, ok := res.Data.(*time.Time) | ||
|
||
if !ok { | ||
t.Fatal(res.Data) | ||
} | ||
}) | ||
|
||
t.Run("should resolve struct as pointer", func(t *testing.T) { | ||
type User struct { | ||
Name string `json:"name"` | ||
Email string `json:"email"` | ||
} | ||
|
||
schema := gq.Pointer{gq.Object[User]{ | ||
Name: "User", | ||
Fields: gq.Fields{ | ||
"name": gq.Field{Type: gq.String{}}, | ||
"email": gq.Field{Type: gq.String{}}, | ||
}, | ||
}} | ||
|
||
res := schema.Do(&gq.DoParams{ | ||
Query: "{name,email}", | ||
Value: User{ | ||
Name: "test", | ||
Email: "[email protected]", | ||
}, | ||
}) | ||
|
||
if res.Error != nil { | ||
t.Fatal(res.Error) | ||
} | ||
|
||
user, ok := res.Data.(*User) | ||
|
||
if !ok { | ||
t.Fatal(res.Data) | ||
} | ||
|
||
if user.Name != "test" { | ||
t.Fatalf("expected `%s`, received `%s`", "test", user.Name) | ||
} | ||
|
||
if user.Email != "[email protected]" { | ||
t.Fatalf("expected `%s`, received `%s`", "[email protected]", user.Email) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters