entc/gen: make custom field comment render as additional line(s) (#1234)

* Make custom field comment render as additional line(s)

* Make the test for the comment aware of the new format
This commit is contained in:
zzwx
2021-02-03 15:23:34 -05:00
committed by GitHub
parent a5c90ea4ca
commit 0f6c71d46b
9 changed files with 16 additions and 5 deletions

File diff suppressed because one or more lines are too long

View File

@@ -21,15 +21,19 @@ import (
type {{ $.Name }} struct {
config {{ template "model/omittags" $ }}
// ID of the ent.
{{- if $.ID.Comment }}
{{- range $line := split $.ID.Comment "\n" }}
// {{ $line }}
{{- end }}
{{- end }}
ID {{ $.ID.Type }} {{ with $.Annotations.Fields.StructTag.id }}`{{ . }}`{{ else }}`{{ $.ID.StructTag }}`{{ end }}
{{- range $f := $.Fields }}
{{- $tag := $f.StructTag }}{{ with $tags := $.Annotations.Fields.StructTag }}{{ with index $tags $f.Name }}{{ $tag = . }}{{ end }}{{ end }}
// {{ $f.StructField }} holds the value of the "{{ $f.Name }}" field.
{{- if $f.Comment }}
{{- range $line := split $f.Comment "\n" }}
// {{ $line }}
{{- end }}
{{- else }}
// {{ $f.StructField }} holds the value of the "{{ $f.Name }}" field.
{{- end }}
{{ $f.StructField }} {{ if $f.Nillable }}*{{ end }}{{ $f.Type }} {{ if not $f.Sensitive }}`{{ $tag }}`{{ else }}{{ template "model/omittags" $ }}{{ end }}
{{- end }}

View File

@@ -59,7 +59,7 @@ func TestSchemaConfig(t *testing.T) {
require.NoError(t, err)
ast.Inspect(f, func(n ast.Node) bool {
if f, ok := n.(*ast.Field); ok && len(f.Names) > 0 && f.Names[0].Name == fd.Name {
require.Equal(t, fd.Comment, f.Doc.Text())
require.Contains(t, fd.Comment, f.Doc.Text())
return false
}
return true

View File

@@ -19,6 +19,7 @@ type User struct {
config `json:"-"`
// ID of the ent.
ID int `json:"id,omitempty"`
// Name holds the value of the "name" field.
// Comment line1
// Comment line2
Name string `json:"name,omitempty"`

View File

@@ -27,6 +27,7 @@ type Card struct {
UpdateTime time.Time `json:"update_time,omitempty"`
// Number holds the value of the "number" field.
Number string `json:"-"`
// Name holds the value of the "name" field.
// Exact name written on card
Name string `json:"name,omitempty"`
// Edges holds the relations/edges for other nodes in the graph.

View File

@@ -29,6 +29,7 @@ type Group struct {
Type *string `json:"type,omitempty"`
// MaxUsers holds the value of the "max_users" field.
MaxUsers int `json:"max_users,omitempty"`
// Name holds the value of the "name" field.
// field with multiple validators
Name string `json:"name,omitempty"`
// Edges holds the relations/edges for other nodes in the graph.

View File

@@ -26,6 +26,7 @@ type Card struct {
UpdateTime time.Time `json:"update_time,omitempty"`
// Number holds the value of the "number" field.
Number string `json:"-"`
// Name holds the value of the "name" field.
// Exact name written on card
Name string `json:"name,omitempty"`
// Edges holds the relations/edges for other nodes in the graph.

View File

@@ -28,6 +28,7 @@ type Group struct {
Type *string `json:"type,omitempty"`
// MaxUsers holds the value of the "max_users" field.
MaxUsers int `json:"max_users,omitempty"`
// Name holds the value of the "name" field.
// field with multiple validators
Name string `json:"name,omitempty"`
// Edges holds the relations/edges for other nodes in the graph.

View File

@@ -23,10 +23,12 @@ type Card struct {
ID int `json:"id,omitempty"`
// Number holds the value of the "number" field.
Number string `json:"number,omitempty"`
// Name holds the value of the "name" field.
// Exact name written on card
Name string `json:"name,omitempty"`
// CreatedAt holds the value of the "created_at" field.
CreatedAt time.Time `json:"created_at,omitempty"`
// InHook holds the value of the "in_hook" field.
// A mandatory field that is set by the hook
InHook string `json:"in_hook,omitempty"`
// Edges holds the relations/edges for other nodes in the graph.