Skip to content

Commit

Permalink
Merge pull request #97 from vektah/add-input-defaults
Browse files Browse the repository at this point in the history
Default values for input unmarshalers
  • Loading branch information
vektah committed Apr 28, 2018
2 parents 79c69d1 + 1cd80c4 commit ab6e65b
Show file tree
Hide file tree
Showing 14 changed files with 257 additions and 10 deletions.
3 changes: 2 additions & 1 deletion codegen/enum_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"sort"
"strings"

"github.com/vektah/gqlgen/codegen/templates"
"github.com/vektah/gqlgen/neelance/schema"
)

Expand All @@ -26,7 +27,7 @@ func (cfg *Config) buildEnums(types NamedTypes) []Enum {
NamedType: namedType,
Values: values,
}
enum.GoType = ucFirst(enum.GQLType)
enum.GoType = templates.ToCamel(enum.GQLType)
enums = append(enums, enum)
}

Expand Down
4 changes: 4 additions & 0 deletions codegen/input_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ func buildInput(types NamedTypes, typ *schema.InputObject) (*Object, error) {
Object: obj,
}

if field.Default != nil {
newField.Default = field.Default.Value(nil)
}

if !newField.Type.IsInput && !newField.Type.IsScalar {
return nil, errors.Errorf("%s cannot be used as a field of %s. only input and scalar types are allowed", newField.GQLType, obj.GQLType)
}
Expand Down
1 change: 1 addition & 0 deletions codegen/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type Field struct {
Args []FieldArgument // A list of arguments to be passed to this field
NoErr bool // If this is bound to a go method, does that method have an error as the second argument
Object *Object // A link back to the parent object
Default interface{} // The default value
}

type FieldArgument struct {
Expand Down
2 changes: 1 addition & 1 deletion codegen/templates/data.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion codegen/templates/input.gotpl
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
{{- if .IsMarshaled }}
func Unmarshal{{ .GQLType }}(v interface{}) ({{.FullName}}, error) {
var it {{.FullName}}
var asMap = v.(map[string]interface{})
{{ range $field := .Fields}}
{{- if $field.Default}}
if _, present := asMap[{{$field.GQLName|quote}}] ; !present {
asMap[{{$field.GQLName|quote}}] = {{ $field.Default | dump }}
}
{{- end}}
{{- end }}

for k, v := range v.(map[string]interface{}) {
for k, v := range asMap {
switch k {
{{- range $field := .Fields }}
case {{$field.GQLName|quote}}:
Expand Down
9 changes: 7 additions & 2 deletions codegen/templates/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func Run(name string, tpldata interface{}) (*bytes.Buffer, error) {
"lcFirst": lcFirst,
"quote": strconv.Quote,
"rawQuote": rawQuote,
"toCamel": toCamel,
"toCamel": ToCamel,
"dump": dump,
})

Expand Down Expand Up @@ -60,22 +60,27 @@ func isDelimiter(c rune) bool {
return c == '-' || c == '_' || unicode.IsSpace(c)
}

func toCamel(s string) string {
func ToCamel(s string) string {
buffer := make([]rune, 0, len(s))
upper := true
lastWasUpper := false

for _, c := range s {
if isDelimiter(c) {
upper = true
continue
}
if !lastWasUpper && unicode.IsUpper(c) {
upper = true
}

if upper {
buffer = append(buffer, unicode.ToUpper(c))
} else {
buffer = append(buffer, unicode.ToLower(c))
}
upper = false
lastWasUpper = unicode.IsUpper(c)
}

return string(buffer)
Expand Down
15 changes: 15 additions & 0 deletions codegen/templates/templates_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package templates

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestToUpper(t *testing.T) {
require.Equal(t, "ToCamel", ToCamel("TO_CAMEL"))
require.Equal(t, "ToCamel", ToCamel("to_camel"))
require.Equal(t, "ToCamel", ToCamel("toCamel"))
require.Equal(t, "ToCamel", ToCamel("ToCamel"))
require.Equal(t, "ToCamel", ToCamel("to-camel"))
}
3 changes: 2 additions & 1 deletion example/scalars/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion example/starwars/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion example/todo/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit ab6e65b

Please sign in to comment.