Skip to content

Commit

Permalink
add pointer type
Browse files Browse the repository at this point in the history
  • Loading branch information
aacebo committed Dec 1, 2024
1 parent 7f6941a commit 763a229
Show file tree
Hide file tree
Showing 8 changed files with 197 additions and 60 deletions.
10 changes: 0 additions & 10 deletions gq/any.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package gq

import (
"encoding/json"

"github.com/thegogod/rum/gq/query"
)

type Any struct{}
Expand All @@ -13,15 +11,7 @@ func (self Any) Key() string {
}

func (self Any) Do(params *DoParams) Result {
parser := query.Parser([]byte(params.Query))
query, err := parser.Parse()

if err != nil {
return Result{Error: err}
}

return self.Resolve(&ResolveParams{
Query: query,
Value: params.Value,
Context: params.Context,
})
Expand Down
10 changes: 0 additions & 10 deletions gq/bool.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package gq

import (
"encoding/json"

"github.com/thegogod/rum/gq/query"
)

type Bool struct{}
Expand All @@ -13,15 +11,7 @@ func (self Bool) Key() string {
}

func (self Bool) Do(params *DoParams) Result {
parser := query.Parser([]byte(params.Query))
query, err := parser.Parse()

if err != nil {
return Result{Error: err}
}

return self.Resolve(&ResolveParams{
Query: query,
Value: params.Value,
Context: params.Context,
})
Expand Down
10 changes: 0 additions & 10 deletions gq/date.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package gq
import (
"encoding/json"
"time"

"github.com/thegogod/rum/gq/query"
)

type Date struct{}
Expand All @@ -14,15 +12,7 @@ func (self Date) Key() string {
}

func (self Date) Do(params *DoParams) Result {
parser := query.Parser([]byte(params.Query))
query, err := parser.Parse()

if err != nil {
return Result{Error: err}
}

return self.Resolve(&ResolveParams{
Query: query,
Value: params.Value,
Context: params.Context,
})
Expand Down
10 changes: 0 additions & 10 deletions gq/float.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package gq

import (
"encoding/json"

"github.com/thegogod/rum/gq/query"
)

type Float struct{}
Expand All @@ -13,15 +11,7 @@ func (self Float) Key() string {
}

func (self Float) Do(params *DoParams) Result {
parser := query.Parser([]byte(params.Query))
query, err := parser.Parse()

if err != nil {
return Result{Error: err}
}

return self.Resolve(&ResolveParams{
Query: query,
Value: params.Value,
Context: params.Context,
})
Expand Down
10 changes: 0 additions & 10 deletions gq/int.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package gq

import (
"encoding/json"

"github.com/thegogod/rum/gq/query"
)

type Int struct{}
Expand All @@ -13,15 +11,7 @@ func (self Int) Key() string {
}

func (self Int) Do(params *DoParams) Result {
parser := query.Parser([]byte(params.Query))
query, err := parser.Parse()

if err != nil {
return Result{Error: err}
}

return self.Resolve(&ResolveParams{
Query: query,
Value: params.Value,
Context: params.Context,
})
Expand Down
45 changes: 45 additions & 0 deletions gq/pointer.go
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())
}
152 changes: 152 additions & 0 deletions gq/pointer_test.go
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)
}
})
}
10 changes: 0 additions & 10 deletions gq/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package gq

import (
"encoding/json"

"github.com/thegogod/rum/gq/query"
)

type String struct{}
Expand All @@ -13,15 +11,7 @@ func (self String) Key() string {
}

func (self String) Do(params *DoParams) Result {
parser := query.Parser([]byte(params.Query))
query, err := parser.Parse()

if err != nil {
return Result{Error: err}
}

return self.Resolve(&ResolveParams{
Query: query,
Value: params.Value,
Context: params.Context,
})
Expand Down

0 comments on commit 763a229

Please sign in to comment.