Skip to content

Commit

Permalink
fix template error on generated defaults (#146)
Browse files Browse the repository at this point in the history
* fix template error on generated defaults

* go fmt

* add test for default fix

* .

* add key sort for default values
  • Loading branch information
jonlundy authored and vektah committed Jun 26, 2018
1 parent a39c63a commit e8c30ac
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 5 deletions.
2 changes: 1 addition & 1 deletion codegen/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ type Model struct {

type ModelField struct {
*Type
GQLName string
GQLName string
GoVarName string
GoFKName string
GoFKType string
Expand Down
12 changes: 11 additions & 1 deletion codegen/templates/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strings"
"text/template"
"unicode"
"sort"
)

func Run(name string, tpldata interface{}) (*bytes.Buffer, error) {
Expand Down Expand Up @@ -111,10 +112,19 @@ func dump(val interface{}) string {
case map[string]interface{}:
buf := bytes.Buffer{}
buf.WriteString("map[string]interface{}{")
for key, data := range val {
var keys []string
for key := range val {
keys = append(keys, key)
}
sort.Strings(keys)

for _, key := range keys {
data := val[key]

buf.WriteString(strconv.Quote(key))
buf.WriteString(":")
buf.WriteString(dump(data))
buf.WriteString(",")
}
buf.WriteString("}")
return buf.String()
Expand Down
4 changes: 2 additions & 2 deletions example/scalars/generated.go

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

2 changes: 1 addition & 1 deletion example/scalars/schema.graphql
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
type Query {
user(id: ID!): User
search(input: SearchArgs = {location: "37,144"}): [User!]!
search(input: SearchArgs = {location: "37,144", isBanned: false}): [User!]!
}

type User {
Expand Down

0 comments on commit e8c30ac

Please sign in to comment.