entc/gen: move migrate annotation creation to init (#1165)

* fix issue with pointer values in Annotation

* entc/gen: move migrate annotation creation to init

Co-authored-by: Ruben de Vries <ruben@rubensayshi.com>
This commit is contained in:
Ariel Mashraki
2021-01-13 12:00:30 +02:00
committed by GitHub
parent 63f8ad6fa4
commit 76c5e48ef8
6 changed files with 55 additions and 9 deletions

View File

@@ -418,7 +418,12 @@ func dumpFields(v interface{}) string {
}
fv := rv.Field(i)
if !fv.IsZero() {
fields = append(fields, fmt.Sprintf("%s: %#v", f.Name, fv.Interface()))
if fv.Kind() == reflect.Ptr {
fv = reflect.Indirect(fv)
fields = append(fields, fmt.Sprintf("%s: &[]%s{%#v}[0]", f.Name, fv.Type(), fv.Interface()))
} else {
fields = append(fields, fmt.Sprintf("%s: %#v", f.Name, fv.Interface()))
}
}
}
return strings.Join(fields, ", ")

File diff suppressed because one or more lines are too long

View File

@@ -97,9 +97,6 @@ var (
{{- end }}
},
{{- end }}
{{- with $ant := $t.Annotation }}
Annotation: &entsql.Annotation{ {{ dumpFields $ant }} },
{{- end }}
}
{{- end }}
// Tables holds all the tables in the schema.
@@ -111,11 +108,31 @@ var (
)
func init() {
{{- range $_, $t := $.Tables }}
{{- range $t := $.Tables }}
{{- $table := pascal $t.Name | printf "%sTable" }}
{{- range $i, $fk := $t.ForeignKeys }}
{{ $table }}.ForeignKeys[{{ $i }}].RefTable = {{ pascal $fk.RefTable.Name | printf "%sTable" }}
{{- end }}
{{- with $ant := $t.Annotation }}
{{ $table }}.Annotation = &entsql.Annotation{
{{- with $ant.Table }}
Table: "{{ . }}",
{{- end }}
{{- with $ant.Charset }}
Charset: "{{ . }}",
{{- end }}
{{- with $ant.Collation }}
Collation: "{{ . }}",
{{- end }}
{{- with $ant.Options }}
Options: "{{ . }}",
{{- end }}
}
{{- with $ant.Incremental }}
{{ $table }}.Annotation.Incremental = new(bool)
*{{ $table }}.Annotation.Incremental = {{ with indirect . }}true{{ else }}false{{ end }}
{{- end }}
{{- end }}
{{- end }}
}

View File

@@ -273,6 +273,23 @@ func TestField_DefaultName(t *testing.T) {
}
}
func TestField_incremental(t *testing.T) {
tests := []struct {
annotations map[string]interface{}
def bool
expected bool
}{
{dict("EntSQL", nil), false, false},
{dict("EntSQL", nil), true, true},
{dict("EntSQL", dict("incremental", true)), false, true},
{dict("EntSQL", dict("incremental", false)), true, false},
}
for _, tt := range tests {
typ := &Field{Annotations: tt.annotations}
require.Equal(t, tt.expected, typ.incremental(tt.def))
}
}
func TestBuilderField(t *testing.T) {
tests := []struct {
name string

View File

@@ -24,7 +24,6 @@ var (
Columns: UsersColumns,
PrimaryKey: []*schema.Column{UsersColumns[0]},
ForeignKeys: []*schema.ForeignKey{},
Annotation: &entsql.Annotation{Table: "Users", Charset: "utf8mb4"},
}
// Tables holds all the tables in the schema.
Tables = []*schema.Table{
@@ -33,4 +32,10 @@ var (
)
func init() {
UsersTable.Annotation = &entsql.Annotation{
Table: "Users",
Charset: "utf8mb4",
}
UsersTable.Annotation.Incremental = new(bool)
*UsersTable.Annotation.Incremental = false
}

View File

@@ -57,9 +57,11 @@ func (User) Mixin() []ent.Mixin {
// Annotations of the User schema.
func (User) Annotations() []schema.Annotation {
incremental := false
return []schema.Annotation{
entsql.Annotation{
Table: "Users",
Table: "Users",
Incremental: &incremental,
},
}
}