Skip to content

Commit

Permalink
Merge pull request #320 from andrioid/master
Browse files Browse the repository at this point in the history
Description added to generated Model code
  • Loading branch information
vektah authored Aug 28, 2018
2 parents 988b367 + 7c04004 commit 8c17eea
Show file tree
Hide file tree
Showing 14 changed files with 34 additions and 10 deletions.
4 changes: 2 additions & 2 deletions codegen/enum.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package codegen

type Enum struct {
*NamedType

Values []EnumValue
Description string
Values []EnumValue
}

type EnumValue struct {
Expand Down
5 changes: 3 additions & 2 deletions codegen/enum_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ func (cfg *Config) buildEnums(types NamedTypes) []Enum {
}

enum := Enum{
NamedType: namedType,
Values: values,
NamedType: namedType,
Values: values,
Description: typ.Description,
}
enum.GoType = templates.ToCamel(enum.GQLType)
enums = append(enums, enum)
Expand Down
5 changes: 3 additions & 2 deletions codegen/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package codegen

type Model struct {
*NamedType

Fields []ModelField
Description string
Fields []ModelField
}

type ModelField struct {
Expand All @@ -12,4 +12,5 @@ type ModelField struct {
GoFieldName string
GoFKName string
GoFKType string
Description string
}
1 change: 1 addition & 0 deletions codegen/models_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func (cfg *Config) buildModels(types NamedTypes, prog *loader.Program, imports *
default:
continue
}
model.Description = typ.Description // It's this or change both obj2Model and buildObject

models = append(models, model)
}
Expand Down
2 changes: 1 addition & 1 deletion codegen/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type Object struct {

type Field struct {
*Type

Description string // Description of a field
GQLName string // The name of the field in graphql
GoFieldType GoFieldType // The field type in go, if any
GoReceiverName string // The name of method & var receiver in go, if any
Expand Down
1 change: 1 addition & 0 deletions codegen/object_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ func (cfg *Config) buildObject(types NamedTypes, typ *ast.Definition, imports *I
GoReceiverName: "ec",
GoFieldName: "introspectSchema",
Object: obj,
Description: field.Description,
})
continue
}
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.

11 changes: 9 additions & 2 deletions codegen/templates/models.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,15 @@ import (
)

{{ range $model := .Models }}
{{with .Description}} {{.|prefixLines "// "}} {{end}}
{{- if .IsInterface }}
type {{.GoType}} interface {}
{{- else }}
type {{.GoType}} struct {
{{- range $field := .Fields }}
{{- with .Description}}
{{.|prefixLines "// "}}
{{- end}}
{{- if $field.GoFieldName }}
{{ $field.GoFieldName }} {{$field.Signature}} `json:"{{$field.GQLName}}"`
{{- else }}
Expand All @@ -25,10 +29,13 @@ import (
{{- end}}

{{ range $enum := .Enums }}
{{with .Description}}{{.|prefixLines "// "}} {{end}}
type {{.GoType}} string
const (
{{ range $value := .Values -}}
{{with .Description}} {{.|prefixLines "// "}} {{end}}
{{- range $value := .Values}}
{{- with .Description}}
{{.|prefixLines "// "}}
{{- end}}
{{$enum.GoType}}{{ .Name|toCamel }} {{$enum.GoType}} = {{.Name|quote}}
{{- end }}
)
Expand Down
4 changes: 4 additions & 0 deletions codegen/testserver/models-gen.go

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

1 change: 1 addition & 0 deletions example/config/models_gen.go

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

1 change: 1 addition & 0 deletions example/dataloader/models_gen.go

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

2 changes: 2 additions & 0 deletions example/selection/models_gen.go

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

4 changes: 4 additions & 0 deletions example/starwars/models_gen.go

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

1 change: 1 addition & 0 deletions example/todo/models_gen.go

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

0 comments on commit 8c17eea

Please sign in to comment.