Skip to content

Commit

Permalink
parallel execution
Browse files Browse the repository at this point in the history
  • Loading branch information
vektah committed Feb 8, 2018
1 parent 4468127 commit dd9a8e4
Show file tree
Hide file tree
Showing 13 changed files with 2,287 additions and 1,000 deletions.
1 change: 1 addition & 0 deletions cmd/ggraphqlc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ func main() {
"time": "time",
"reflect": "reflect",
"strings": "strings",
"sync": "sync",

"mapstructure": "github.com/mitchellh/mapstructure",
"errors": "github.com/vektah/graphql-go/errors",
Expand Down
108 changes: 66 additions & 42 deletions cmd/ggraphqlc/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,55 +29,42 @@ func (t kind) IsPtr() bool {
return len(t.Modifiers) > 0 && t.Modifiers[0] == modPtr
}

func (t kind) FullName() string {
if t.ImportedAs == "" {
return t.Name
}
return t.ImportedAs + "." + t.Name
func (t kind) IsSlice() bool {
return len(t.Modifiers) > 0 && t.Modifiers[0] == modList
}

func (t kind) WriteJson(val string) string {
return t.doWriteJson(val, t.Modifiers, false)
}

// should be in the template, but its recursive and has a bunch fo args
func (t kind) doWriteJson(val string, remainingMods []string, isPtr bool) string {
switch {
case len(remainingMods) > 0 && remainingMods[0] == modPtr:
return fmt.Sprintf(
"if %s == nil { ec.json.Null() } else { %s } ",
val, t.doWriteJson(val, remainingMods[1:], true),
)
func (t kind) Elem() kind {
if len(t.Modifiers) == 0 {
return t
}

case len(remainingMods) > 0 && remainingMods[0] == modList:
if isPtr {
val = "*" + val
}
t.Modifiers = t.Modifiers[1:]
return t
}

return strings.Join([]string{
"ec.json.BeginArray()",
fmt.Sprintf("for _, val := range %s {", val),
t.doWriteJson("val", remainingMods[1:], false),
"}",
"ec.json.EndArray()",
}, "\n")
func (t kind) ByRef(name string) string {
needPtr := len(t.Implementors) == 0
if needPtr && !t.IsPtr() {
return "&" + name
}
if !needPtr && t.IsPtr() {
return "*" + name
}
return name
}

case t.Scalar:
if isPtr {
val = "*" + val
}
return fmt.Sprintf("ec.json.%s(%s)", ucFirst(t.Name), val)
func (t kind) ByVal(name string) string {
if t.IsPtr() {
return "*" + name
}
return name
}

default:
needPtr := len(t.Implementors) == 0
if needPtr && !isPtr {
val = "&" + val
}
if !needPtr && isPtr {
val = "*" + val
}
return fmt.Sprintf("ec._%s(field.Selections, %s)", lcFirst(t.GraphQLName), val)
func (t kind) FullName() string {
if t.ImportedAs == "" {
return t.Name
}
return t.ImportedAs + "." + t.Name
}

type object struct {
Expand Down Expand Up @@ -136,6 +123,43 @@ func (f *Field) CallArgs(object object) string {
return strings.Join(args, ", ")
}

// should be in the template, but its recursive and has a bunch fo args
func (f *Field) WriteJson() string {
return f.doWriteJson("t."+ucFirst(f.GraphQLName), f.Type.Modifiers, false)
}

func (f *Field) doWriteJson(val string, remainingMods []string, isPtr bool) string {
switch {
case len(remainingMods) > 0 && remainingMods[0] == modPtr:
return fmt.Sprintf(
"if %s == nil { w.Null() } else { %s } ",
val, f.doWriteJson(val, remainingMods[1:], true),
)

case len(remainingMods) > 0 && remainingMods[0] == modList:
if isPtr {
val = "*" + val
}

return strings.Join([]string{
"w.BeginArray()",
fmt.Sprintf("for _, val := range %s {", val),
f.doWriteJson("val", remainingMods[1:], false),
"}",
"w.EndArray()",
}, "\n")

case f.Type.Scalar:
if isPtr {
val = "*" + val
}
return fmt.Sprintf("w.%s(%s)", ucFirst(f.Type.Name), val)

default:
return fmt.Sprintf("%s.Write(w)", val)
}
}

func (o *object) GetField(name string) *Field {
for i, field := range o.Fields {
if strings.EqualFold(field.GraphQLName, name) {
Expand Down
Loading

0 comments on commit dd9a8e4

Please sign in to comment.