From dba8eab25e12a75f458ca590a5649a4c579d6db1 Mon Sep 17 00:00:00 2001 From: Ariel Mashraki <7413593+a8m@users.noreply.github.com> Date: Wed, 16 Mar 2022 13:41:19 +0200 Subject: [PATCH] entc/gen: move column default quoting to template (#2406) --- entc/gen/func.go | 10 +++++++++- entc/gen/template/migrate/schema.tmpl | 4 ++-- entc/gen/type.go | 9 ++++----- 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/entc/gen/func.go b/entc/gen/func.go index dccbeec127..a85fec5485 100644 --- a/entc/gen/func.go +++ b/entc/gen/func.go @@ -41,7 +41,7 @@ var ( "aggregate": aggregate, "primitives": primitives, "singular": rules.Singularize, - "quote": strconv.Quote, + "quote": quote, "base": filepath.Base, "keys": keys, "join": join, @@ -73,6 +73,14 @@ var ( acronyms = make(map[string]struct{}) ) +// quote only strings. +func quote(v interface{}) interface{} { + if s, ok := v.(string); ok { + return strconv.Quote(s) + } + return v +} + // fieldOps returns all predicate operations for a given field. func fieldOps(f *Field) (ops []Op) { switch t := f.Type.Type; { diff --git a/entc/gen/template/migrate/schema.tmpl b/entc/gen/template/migrate/schema.tmpl index f0b16864df..ad3b9c5985 100644 --- a/entc/gen/template/migrate/schema.tmpl +++ b/entc/gen/template/migrate/schema.tmpl @@ -38,8 +38,8 @@ var ( {{- with $c.Size }} Size: {{ . }},{{ end }} {{- with $c.Attr }} Attr: "{{ . }}",{{ end }} {{- with $c.Enums }} Enums: []string{ {{ range $e := . }}"{{ $e }}",{{ end }} },{{ end }} - {{- if not (isNil $c.Default) }} Default: {{ $c.Default }},{{ end }} - {{- if ne (len $c.Collation) 0 }} Collation: {{ $c.Collation }},{{ end }} + {{- if not (isNil $c.Default) }} Default: {{ quote $c.Default }},{{ end }} + {{- if $c.Collation }} Collation: "{{ $c.Collation }}",{{ end }} {{- with $c.SchemaType }} SchemaType: map[string]string{ {{ range $k, $v := . }}"{{ $k }}": "{{ $v }}",{{ end }}}{{ end }}}, {{- end }} } diff --git a/entc/gen/type.go b/entc/gen/type.go index 0053186a0b..dbbba2be97 100644 --- a/entc/gen/type.go +++ b/entc/gen/type.go @@ -13,7 +13,6 @@ import ( "path" "reflect" "sort" - "strconv" "strings" "unicode" @@ -1219,18 +1218,18 @@ func (f Field) Column() *schema.Column { c.Default = f.DefaultValue() case f.Default && (f.IsString() || f.IsEnum()): if s, ok := f.DefaultValue().(string); ok { - c.Default = strconv.Quote(s) + c.Default = s } } // Override the default-value defined in the // schema if it was provided by an annotation. if ant := f.EntSQL(); ant != nil && ant.Default != "" { - c.Default = strconv.Quote(ant.Default) + c.Default = ant.Default } // Override the collation defined in the // schema if it was provided by an annotation. if ant := f.EntSQL(); ant != nil && ant.Collation != "" { - c.Collation = strconv.Quote(ant.Collation) + c.Collation = ant.Collation } if f.def != nil { c.SchemaType = f.def.SchemaType @@ -1278,7 +1277,7 @@ func (f Field) PK() *schema.Column { // Override the default-value defined in the // schema if it was provided by an annotation. if ant := f.EntSQL(); ant != nil && ant.Default != "" { - c.Default = strconv.Quote(ant.Default) + c.Default = ant.Default } if f.def != nil { c.SchemaType = f.def.SchemaType