Skip to content

Commit

Permalink
fix for pointer input
Browse files Browse the repository at this point in the history
  • Loading branch information
aacebo committed Dec 2, 2024
1 parent b24bc7d commit 0a90aa2
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 0 deletions.
14 changes: 14 additions & 0 deletions gq/pointer.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ func (self Pointer) Key() string {
}

func (self Pointer) Do(params *DoParams) Result {
value := reflect.ValueOf(params.Value)

if value.Kind() == reflect.Pointer {
value = value.Elem()
params.Value = value.Interface()
}

res := self.Type.Do(params)
data := reflect.ValueOf(res.Data)

Expand All @@ -28,6 +35,13 @@ func (self Pointer) Do(params *DoParams) Result {
}

func (self Pointer) Resolve(params *ResolveParams) Result {
value := reflect.ValueOf(params.Value)

if value.Kind() == reflect.Pointer {
value = value.Elem()
params.Value = value.Interface()
}

res := self.Type.Resolve(params)
data := reflect.ValueOf(res.Data)

Expand Down
106 changes: 106 additions & 0 deletions gq/pointer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,28 @@ func TestPointer(t *testing.T) {
}
})

t.Run("should resolve *string as pointer", func(t *testing.T) {
schema := gq.Pointer{gq.String{}}
value := "testing"
res := schema.Do(&gq.DoParams{
Value: &value,
})

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{
Expand All @@ -51,6 +73,28 @@ func TestPointer(t *testing.T) {
}
})

t.Run("should resolve *bool as pointer", func(t *testing.T) {
schema := gq.Pointer{gq.Bool{}}
value := true
res := schema.Do(&gq.DoParams{
Value: &value,
})

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{
Expand All @@ -72,6 +116,28 @@ func TestPointer(t *testing.T) {
}
})

t.Run("should resolve *int as pointer", func(t *testing.T) {
schema := gq.Pointer{gq.Int{}}
value := 102
res := schema.Do(&gq.DoParams{
Value: &value,
})

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{
Expand All @@ -93,6 +159,28 @@ func TestPointer(t *testing.T) {
}
})

t.Run("should resolve *float as pointer", func(t *testing.T) {
schema := gq.Pointer{gq.Float{}}
value := 11.123
res := schema.Do(&gq.DoParams{
Value: &value,
})

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{
Expand All @@ -110,6 +198,24 @@ func TestPointer(t *testing.T) {
}
})

t.Run("should resolve `*time.Time` as pointer", func(t *testing.T) {
schema := gq.Pointer{gq.Date{}}
value := time.Now()
res := schema.Do(&gq.DoParams{
Value: &value,
})

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"`
Expand Down

0 comments on commit 0a90aa2

Please sign in to comment.