Skip to content

Commit

Permalink
rename marshaler methods
Browse files Browse the repository at this point in the history
  • Loading branch information
vektah committed Feb 16, 2018
1 parent e0b7c25 commit 83b001a
Show file tree
Hide file tree
Showing 14 changed files with 36 additions and 33 deletions.
8 changes: 7 additions & 1 deletion codegen/input_build.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package codegen

import (
"fmt"
"go/types"
"os"
"sort"
"strings"

Expand All @@ -17,7 +19,11 @@ func buildInputs(namedTypes NamedTypes, s *schema.Schema, prog *loader.Program)
case *schema.InputObject:
input := buildInput(namedTypes, typ)

if def := findGoType(prog, input.Package, input.GoType); def != nil {
def, err := findGoType(prog, input.Package, input.GoType)
if err != nil {
fmt.Fprintf(os.Stderr, err.Error())
}
if def != nil {
input.Marshaler = buildInputMarshaler(typ, def)
bindObject(def.Type(), input)
}
Expand Down
8 changes: 7 additions & 1 deletion codegen/object_build.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package codegen

import (
"fmt"
"os"
"sort"
"strings"

Expand All @@ -16,7 +18,11 @@ func buildObjects(types NamedTypes, s *schema.Schema, prog *loader.Program) Obje
case *schema.Object:
obj := buildObject(types, typ)

if def := findGoType(prog, obj.Package, obj.GoType); def != nil {
def, err := findGoType(prog, obj.Package, obj.GoType)
if err != nil {
fmt.Fprintf(os.Stderr, err.Error())
}
if def != nil {
bindObject(def.Type(), obj)
}

Expand Down
2 changes: 1 addition & 1 deletion codegen/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (t Type) Unmarshal(result, raw string) string {

func (t Type) Marshal(result, val string) string {
if t.Marshaler != nil {
return result + " = " + t.Marshaler.pkgDot() + "" /* Marshal */ + t.Marshaler.GoType + "(" + val + ")"
return result + " = " + t.Marshaler.pkgDot() + "Marshal" + t.Marshaler.GoType + "(" + val + ")"
}

return result + " = " + val
Expand Down
11 changes: 1 addition & 10 deletions codegen/type_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,18 @@ func buildNamedTypes(s *schema.Schema, userTypes map[string]string) NamedTypes {
}

func bindTypes(imports Imports, namedTypes NamedTypes, prog *loader.Program) {
fmt.Println(namedTypes)
for _, t := range namedTypes {
if t.Package == "" {
fmt.Println("NO PKG", t)
continue
}

def := findGoType(prog, t.Package, t.GoType)
if def == nil {

}
fmt.Println("Looking at " + t.FullName())
def, _ := findGoType(prog, t.Package, "Marshal"+t.GoType)
switch def := def.(type) {
case *types.Func:

fmt.Println(def.String())
sig := def.Type().(*types.Signature)
cpy := t.Ref
t.Marshaler = &cpy

fmt.Println("sig: " + sig.Params().At(0).Type().String())
t.Package, t.GoType = pkgAndType(sig.Params().At(0).Type().String())
t.Import = imports.findByName(t.Package)
}
Expand Down
16 changes: 8 additions & 8 deletions codegen/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,33 @@ import (
"golang.org/x/tools/go/loader"
)

func findGoType(prog *loader.Program, pkgName string, typeName string) types.Object {
func findGoType(prog *loader.Program, pkgName string, typeName string) (types.Object, error) {
if pkgName == "" {
return nil, nil
}
fullName := typeName
if pkgName != "" {
fullName = pkgName + "." + typeName
}

pkgName, err := resolvePkg(pkgName)
if err != nil {
fmt.Fprintf(os.Stderr, "unable to resolve package for %s: %s\n", fullName, err.Error())
return nil
return nil, fmt.Errorf("unable to resolve package for %s: %s\n", fullName, err.Error())
}

pkg := prog.Imported[pkgName]
if pkg == nil {
fmt.Fprintf(os.Stderr, "required package was not loaded: %s", fullName)
return nil
return nil, fmt.Errorf("required package was not loaded: %s", fullName)
}

for astNode, def := range pkg.Defs {
if astNode.Name != typeName || isMethod(def) {
continue
}

return def
return def, nil
}
fmt.Fprintf(os.Stderr, "unable to find type %s\n", fullName)
return nil
return nil, fmt.Errorf("unable to find type %s\n", fullName)
}

func isMethod(t types.Object) bool {
Expand Down
2 changes: 1 addition & 1 deletion graphql/bool.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"strings"
)

func Boolean(b bool) Marshaler {
func MarshalBoolean(b bool) Marshaler {
return WriterFunc(func(w io.Writer) {
if b {
w.Write(trueLit)
Expand Down
8 changes: 4 additions & 4 deletions graphql/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"github.com/vektah/gqlgen/neelance/errors"
)

func Errors(errs []*errors.QueryError) Marshaler {
func MarshalErrors(errs []*errors.QueryError) Marshaler {
res := Array{}

for _, err := range errs {
Expand All @@ -15,14 +15,14 @@ func Errors(errs []*errors.QueryError) Marshaler {

errObj := &OrderedMap{}

errObj.Add("message", String(err.Message))
errObj.Add("message", MarshalString(err.Message))

if len(err.Locations) > 0 {
locations := Array{}
for _, location := range err.Locations {
locationObj := &OrderedMap{}
locationObj.Add("line", Int(location.Line))
locationObj.Add("column", Int(location.Column))
locationObj.Add("line", MarshalInt(location.Line))
locationObj.Add("column", MarshalInt(location.Column))

locations = append(locations, locationObj)
}
Expand Down
2 changes: 1 addition & 1 deletion graphql/float.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"strconv"
)

func Float(f float64) Marshaler {
func MarshalFloat(f float64) Marshaler {
return WriterFunc(func(w io.Writer) {
io.WriteString(w, fmt.Sprintf("%f", f))
})
Expand Down
2 changes: 1 addition & 1 deletion graphql/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"strconv"
)

func ID(s string) Marshaler {
func MarshalID(s string) Marshaler {
return WriterFunc(func(w io.Writer) {
io.WriteString(w, strconv.Quote(s))
})
Expand Down
2 changes: 1 addition & 1 deletion graphql/int.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"strconv"
)

func Int(i int) Marshaler {
func MarshalInt(i int) Marshaler {
return WriterFunc(func(w io.Writer) {
io.WriteString(w, strconv.Itoa(i))
})
Expand Down
2 changes: 1 addition & 1 deletion graphql/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"strconv"
)

func String(s string) Marshaler {
func MarshalString(s string) Marshaler {
return WriterFunc(func(w io.Writer) {
io.WriteString(w, strconv.Quote(s))
})
Expand Down
2 changes: 1 addition & 1 deletion graphql/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"time"
)

func Time(t time.Time) Marshaler {
func MarshalTime(t time.Time) Marshaler {
return WriterFunc(func(w io.Writer) {
io.WriteString(w, strconv.Quote(t.Format(time.RFC3339)))
})
Expand Down
2 changes: 1 addition & 1 deletion templates/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func NewExecutor(resolvers Resolvers) func(context.Context, string, string, map[
result.Add("data", data)
if len(c.Errors) > 0 {
result.Add("errors", graphql.Errors(c.Errors))
result.Add("errors", graphql.MarshalErrors(c.Errors))
}
result.MarshalGQL(w)
Expand Down
2 changes: 1 addition & 1 deletion templates/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func (ec *executionContext) _{{$object.GQLType|lcFirst}}(sel []query.Selection,
switch field.Name {
case "__typename":
out.Values[i] = graphql.String({{$object.GQLType|quote}})
out.Values[i] = graphql.MarshalString({{$object.GQLType|quote}})
{{- range $field := $object.Fields }}
case "{{$field.GQLName}}":
{{- template "args" $field.Args }}
Expand Down

0 comments on commit 83b001a

Please sign in to comment.