Skip to content

Commit

Permalink
fix indenting
Browse files Browse the repository at this point in the history
  • Loading branch information
vektah committed Feb 11, 2018
1 parent fb2d581 commit 743b2cf
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 115 deletions.
5 changes: 1 addition & 4 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,5 @@ insert_final_newline = true
indent_style = space
indent_size = 4

[*.go]
indent_style = tab

[*.twig]
[*.{go,gotpl}]
indent_style = tab
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ This is a library for quickly creating a strictly typed graphql servers in golan
Please use [dep](https://github.com/golang/dep) to pin your versions, the apis here should be considered unstable.

Ideally you should version the binary used to generate the code, as well as the library itself. Version mismatches
between the generated code and the runtime will be ugly. (gorunpkg)[https://github.com/vektah/gorunpkg] makes this
between the generated code and the runtime will be ugly. [gorunpkg](https://github.com/vektah/gorunpkg) makes this
as easy as:

Gopkg.toml
Expand Down
84 changes: 42 additions & 42 deletions templates/args.gotpl
Original file line number Diff line number Diff line change
@@ -1,44 +1,44 @@
{{- define "args" }}
{{- range $i, $arg := . }}
var arg{{$i}} {{$arg.Type.Local }}
{{- if eq $arg.Type.FullName "time.Time" }}
if tmp, ok := field.Args[{{$arg.Name|quote}}]; ok {
if tmpStr, ok := tmp.(string); ok {
tmpDate, err := time.Parse(time.RFC3339, tmpStr)
if err != nil {
ec.Error(err)
continue
}
arg{{$i}} = {{if $arg.Type.IsPtr}}&{{end}}tmpDate
} else {
ec.Errorf("Time '{{$arg.Name}}' should be RFC3339 formatted string")
continue
}
}
{{- else if eq $arg.Type.Name "map[string]interface{}" }}
if tmp, ok := field.Args[{{$arg.Name|quote}}]; ok {
{{- if $arg.Type.IsPtr }}
tmp2 := tmp.({{$arg.Type.Name}})
arg{{$i}} = &tmp2
{{- else }}
arg{{$i}} = tmp.({{$arg.Type.Name}})
{{- end }}
}
{{- else if $arg.Type.Scalar }}
if tmp, ok := field.Args[{{$arg.Name|quote}}]; ok {
tmp2, err := coerce{{$arg.Type.Name|ucFirst}}(tmp)
if err != nil {
ec.Error(err)
continue
}
arg{{$i}} = {{if $arg.Type.IsPtr}}&{{end}}tmp2
}
{{- else }}
err := unpackComplexArg(&arg{{$i}}, field.Args[{{$arg.Name|quote}}])
if err != nil {
ec.Error(err)
continue
}
{{- end}}
{{- end }}
{{- range $i, $arg := . }}
var arg{{$i}} {{$arg.Type.Local }}
{{- if eq $arg.Type.FullName "time.Time" }}
if tmp, ok := field.Args[{{$arg.Name|quote}}]; ok {
if tmpStr, ok := tmp.(string); ok {
tmpDate, err := time.Parse(time.RFC3339, tmpStr)
if err != nil {
ec.Error(err)
continue
}
arg{{$i}} = {{if $arg.Type.IsPtr}}&{{end}}tmpDate
} else {
ec.Errorf("Time '{{$arg.Name}}' should be RFC3339 formatted string")
continue
}
}
{{- else if eq $arg.Type.Name "map[string]interface{}" }}
if tmp, ok := field.Args[{{$arg.Name|quote}}]; ok {
{{- if $arg.Type.IsPtr }}
tmp2 := tmp.({{$arg.Type.Name}})
arg{{$i}} = &tmp2
{{- else }}
arg{{$i}} = tmp.({{$arg.Type.Name}})
{{- end }}
}
{{- else if $arg.Type.Scalar }}
if tmp, ok := field.Args[{{$arg.Name|quote}}]; ok {
tmp2, err := coerce{{$arg.Type.Name|ucFirst}}(tmp)
if err != nil {
ec.Error(err)
continue
}
arg{{$i}} = {{if $arg.Type.IsPtr}}&{{end}}tmp2
}
{{- else }}
err := unpackComplexArg(&arg{{$i}}, field.Args[{{$arg.Name|quote}}])
if err != nil {
ec.Error(err)
continue
}
{{- end}}
{{- end }}
{{- end }}
28 changes: 14 additions & 14 deletions templates/file.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ package {{ .PackageName }}

import (
{{- range $as, $import := .Imports }}
{{- $as }} "{{ $import }}"
{{- $as }} "{{ $import }}"
{{ end }}
)

type Resolvers interface {
{{- range $object := .Objects -}}
{{ range $field := $object.Fields -}}
{{ $field.ResolverDeclaration }}
{{ end }}
{{ range $field := $object.Fields -}}
{{ $field.ResolverDeclaration }}
{{ end }}
{{- end }}
}

Expand Down Expand Up @@ -43,22 +43,22 @@ func NewExecutor(resolvers Resolvers) func(context.Context, string, string, map[

var data jsonw.Writer
if op.Type == query.Query {
data = c._{{.QueryRoot|lcFirst}}(op.Selections, nil)
{{- if .MutationRoot}}
data = c._{{.QueryRoot|lcFirst}}(op.Selections, nil)
{{- if .MutationRoot}}
} else if op.Type == query.Mutation {
data = c._{{.MutationRoot|lcFirst}}(op.Selections, nil)
{{- end}}
data = c._{{.MutationRoot|lcFirst}}(op.Selections, nil)
{{- end}}
} else {
return []*errors.QueryError{errors.Errorf("unsupported operation type")}
}

c.wg.Wait()
c.wg.Wait()

result := &jsonw.OrderedMap{}
result.Add("data", data)
result := &jsonw.OrderedMap{}
result.Add("data", data)

if len(c.Errors) > 0 {
result.Add("errors", errors.ErrorWriter(c.Errors))
result.Add("errors", errors.ErrorWriter(c.Errors))
}

result.WriteJson(w)
Expand All @@ -76,11 +76,11 @@ type executionContext struct {
}

{{- range $object := .Objects }}
{{ template "object" $object }}
{{ template "object" $object }}
{{- end}}

{{- range $interface := .Interfaces }}
{{ template "interface" $interface }}
{{ template "interface" $interface }}
{{- end }}

var parsedSchema = schema.MustParse({{.SchemaRaw|quote}})
Expand Down
24 changes: 12 additions & 12 deletions templates/interface.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@
{{- $interface := . }}

func (ec *executionContext) _{{$interface.Type.GraphQLName|lcFirst}}(sel []query.Selection, it *{{$interface.Type.Local}}) jsonw.Writer {
switch it := (*it).(type) {
case nil:
return jsonw.Null
{{- range $implementor := $interface.Type.Implementors }}
case {{$implementor.Local}}:
return ec._{{$implementor.GraphQLName|lcFirst}}(sel, &it)
switch it := (*it).(type) {
case nil:
return jsonw.Null
{{- range $implementor := $interface.Type.Implementors }}
case {{$implementor.Local}}:
return ec._{{$implementor.GraphQLName|lcFirst}}(sel, &it)

case *{{$implementor.Local}}:
return ec._{{$implementor.GraphQLName|lcFirst}}(sel, it)
case *{{$implementor.Local}}:
return ec._{{$implementor.GraphQLName|lcFirst}}(sel, it)

{{- end }}
default:
panic(fmt.Errorf("unexpected type %T", it))
}
{{- end }}
default:
panic(fmt.Errorf("unexpected type %T", it))
}
}

{{- end}}
84 changes: 42 additions & 42 deletions templates/object.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -5,55 +5,55 @@ var {{ $object.Type.GraphQLName|lcFirst}}Implementors = {{$object.Implementors}}

// nolint: gocyclo, errcheck, gas, goconst
func (ec *executionContext) _{{$object.Type.GraphQLName|lcFirst}}(sel []query.Selection, it *{{$object.Type.Local}}) jsonw.Writer {
fields := ec.collectFields(sel, {{$object.Type.GraphQLName|lcFirst}}Implementors, map[string]bool{})
out := jsonw.NewOrderedMap(len(fields))
fields := ec.collectFields(sel, {{$object.Type.GraphQLName|lcFirst}}Implementors, map[string]bool{})
out := jsonw.NewOrderedMap(len(fields))
for i, field := range fields {
out.Keys[i] = field.Alias
out.Values[i] = jsonw.Null
out.Keys[i] = field.Alias
out.Values[i] = jsonw.Null

switch field.Name {
{{- range $field := $object.Fields }}
{{- range $field := $object.Fields }}
case "{{$field.GraphQLName}}":
{{- template "args" $field.Args }}

{{- if $field.IsConcurrent }}
ec.wg.Add(1)
go func(i int, field collectedField) {
defer ec.wg.Done()
{{- end }}

{{- if $field.VarName }}
res := {{$field.VarName}}
{{- else if $field.MethodName }}
{{- if $field.NoErr }}
res := {{$field.MethodName}}({{ $field.CallArgs }})
{{- else }}
res, err := {{$field.MethodName}}({{ $field.CallArgs }})
if err != nil {
ec.Error(err)
{{ if $field.IsConcurrent }}return{{ else }}continue{{end}}
}
{{- end }}
{{- else }}
res, err := ec.resolvers.{{ $object.Name }}_{{ $field.GraphQLName }}({{ $field.CallArgs }})
if err != nil {
ec.Error(err)
{{ if $field.IsConcurrent }}return{{ else }}continue{{end}}
}
{{- end }}

{{ $field.WriteJson "out.Values[i]" }}

{{- if $field.IsConcurrent }}
}(i, field)
{{- end }}
{{- end }}
default:
panic("unknown field " + strconv.Quote(field.Name))
{{- template "args" $field.Args }}

{{- if $field.IsConcurrent }}
ec.wg.Add(1)
go func(i int, field collectedField) {
defer ec.wg.Done()
{{- end }}

{{- if $field.VarName }}
res := {{$field.VarName}}
{{- else if $field.MethodName }}
{{- if $field.NoErr }}
res := {{$field.MethodName}}({{ $field.CallArgs }})
{{- else }}
res, err := {{$field.MethodName}}({{ $field.CallArgs }})
if err != nil {
ec.Error(err)
{{ if $field.IsConcurrent }}return{{ else }}continue{{end}}
}
{{- end }}
{{- else }}
res, err := ec.resolvers.{{ $object.Name }}_{{ $field.GraphQLName }}({{ $field.CallArgs }})
if err != nil {
ec.Error(err)
{{ if $field.IsConcurrent }}return{{ else }}continue{{end}}
}
{{- end }}

{{ $field.WriteJson "out.Values[i]" }}

{{- if $field.IsConcurrent }}
}(i, field)
{{- end }}
{{- end }}
default:
panic("unknown field " + strconv.Quote(field.Name))
}
}

return out
return out
}


Expand Down

0 comments on commit 743b2cf

Please sign in to comment.