[Bug241] Remove control characters from mysql column comment
This commit is contained in:
parent
dbcf614140
commit
bb22b80984
3 changed files with 104 additions and 3 deletions
|
|
@ -1,5 +1,7 @@
|
||||||
package metadata
|
package metadata
|
||||||
|
|
||||||
|
import "regexp"
|
||||||
|
|
||||||
// Column struct
|
// Column struct
|
||||||
type Column struct {
|
type Column struct {
|
||||||
Name string
|
Name string
|
||||||
|
|
@ -10,6 +12,16 @@ type Column struct {
|
||||||
Comment string
|
Comment string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GoLangComment returns column comment without ascii control characters
|
||||||
|
func (c Column) GoLangComment() string {
|
||||||
|
if c.Comment == "" {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
// remove ascii control characters from string
|
||||||
|
return regexp.MustCompile(`[[:cntrl:]]+`).ReplaceAllString(c.Comment, "")
|
||||||
|
}
|
||||||
|
|
||||||
// DataTypeKind is database type kind(base, enum, user-defined, array)
|
// DataTypeKind is database type kind(base, enum, user-defined, array)
|
||||||
type DataTypeKind string
|
type DataTypeKind string
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -32,7 +32,7 @@ type {{structImplName}} struct {
|
||||||
// Columns
|
// Columns
|
||||||
{{- range $i, $c := .Columns}}
|
{{- range $i, $c := .Columns}}
|
||||||
{{- $field := columnField $c}}
|
{{- $field := columnField $c}}
|
||||||
{{$field.Name}} {{dialect.PackageName}}.Column{{$field.Type}} {{- if $c.Comment }} // {{$c.Comment}} {{end}}
|
{{$field.Name}} {{dialect.PackageName}}.Column{{$field.Type}} {{- if $c.Comment }} // {{$c.GoLangComment}} {{end}}
|
||||||
{{- end}}
|
{{- end}}
|
||||||
|
|
||||||
AllColumns {{dialect.PackageName}}.ColumnList
|
AllColumns {{dialect.PackageName}}.ColumnList
|
||||||
|
|
@ -122,7 +122,7 @@ import (
|
||||||
type {{$modelTableTemplate.TypeName}} struct {
|
type {{$modelTableTemplate.TypeName}} struct {
|
||||||
{{- range .Columns}}
|
{{- range .Columns}}
|
||||||
{{- $field := structField .}}
|
{{- $field := structField .}}
|
||||||
{{$field.Name}} {{$field.Type.Name}} ` + "{{$field.TagsString}}" + ` {{- if .Comment }} // {{.Comment}} {{end}}
|
{{$field.Name}} {{$field.Type.Name}} ` + "{{$field.TagsString}}" + ` {{- if .Comment }} // {{.GoLangComment}} {{end}}
|
||||||
{{- end}}
|
{{- end}}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -191,7 +191,96 @@ type Link struct {
|
||||||
ID int32 `+"`sql:\"primary_key\"`"+` // this is link id
|
ID int32 `+"`sql:\"primary_key\"`"+` // this is link id
|
||||||
URL string // link url
|
URL string // link url
|
||||||
Name string // link name
|
Name string // link name
|
||||||
Description *string // this is link description
|
Description *string // '"\\%\_
|
||||||
|
}
|
||||||
|
`)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSQLBuilderColumnComment(t *testing.T) {
|
||||||
|
testutils.AssertFileContent(t, "./../.gentestdata/mysql/test_sample/table/link.go", `
|
||||||
|
//
|
||||||
|
// Code generated by go-jet DO NOT EDIT.
|
||||||
|
//
|
||||||
|
// WARNING: Changes to this file may cause incorrect behavior
|
||||||
|
// and will be lost if the code is regenerated
|
||||||
|
//
|
||||||
|
|
||||||
|
package table
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/go-jet/jet/v2/mysql"
|
||||||
|
)
|
||||||
|
|
||||||
|
var Link = newLinkTable("test_sample", "link", "")
|
||||||
|
|
||||||
|
type linkTable struct {
|
||||||
|
mysql.Table
|
||||||
|
|
||||||
|
// Columns
|
||||||
|
ID mysql.ColumnInteger // this is link id
|
||||||
|
URL mysql.ColumnString // link url
|
||||||
|
Name mysql.ColumnString // link name
|
||||||
|
Description mysql.ColumnString // '"\\%\_
|
||||||
|
|
||||||
|
AllColumns mysql.ColumnList
|
||||||
|
MutableColumns mysql.ColumnList
|
||||||
|
}
|
||||||
|
|
||||||
|
type LinkTable struct {
|
||||||
|
linkTable
|
||||||
|
|
||||||
|
NEW linkTable
|
||||||
|
}
|
||||||
|
|
||||||
|
// AS creates new LinkTable with assigned alias
|
||||||
|
func (a LinkTable) AS(alias string) *LinkTable {
|
||||||
|
return newLinkTable(a.SchemaName(), a.TableName(), alias)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Schema creates new LinkTable with assigned schema name
|
||||||
|
func (a LinkTable) FromSchema(schemaName string) *LinkTable {
|
||||||
|
return newLinkTable(schemaName, a.TableName(), a.Alias())
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithPrefix creates new LinkTable with assigned table prefix
|
||||||
|
func (a LinkTable) WithPrefix(prefix string) *LinkTable {
|
||||||
|
return newLinkTable(a.SchemaName(), prefix+a.TableName(), a.TableName())
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithSuffix creates new LinkTable with assigned table suffix
|
||||||
|
func (a LinkTable) WithSuffix(suffix string) *LinkTable {
|
||||||
|
return newLinkTable(a.SchemaName(), a.TableName()+suffix, a.TableName())
|
||||||
|
}
|
||||||
|
|
||||||
|
func newLinkTable(schemaName, tableName, alias string) *LinkTable {
|
||||||
|
return &LinkTable{
|
||||||
|
linkTable: newLinkTableImpl(schemaName, tableName, alias),
|
||||||
|
NEW: newLinkTableImpl("", "new", ""),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func newLinkTableImpl(schemaName, tableName, alias string) linkTable {
|
||||||
|
var (
|
||||||
|
IDColumn = mysql.IntegerColumn("id")
|
||||||
|
URLColumn = mysql.StringColumn("url")
|
||||||
|
NameColumn = mysql.StringColumn("name")
|
||||||
|
DescriptionColumn = mysql.StringColumn("description")
|
||||||
|
allColumns = mysql.ColumnList{IDColumn, URLColumn, NameColumn, DescriptionColumn}
|
||||||
|
mutableColumns = mysql.ColumnList{URLColumn, NameColumn, DescriptionColumn}
|
||||||
|
)
|
||||||
|
|
||||||
|
return linkTable{
|
||||||
|
Table: mysql.NewTable(schemaName, tableName, alias, allColumns...),
|
||||||
|
|
||||||
|
//Columns
|
||||||
|
ID: IDColumn,
|
||||||
|
URL: URLColumn,
|
||||||
|
Name: NameColumn,
|
||||||
|
Description: DescriptionColumn,
|
||||||
|
|
||||||
|
AllColumns: allColumns,
|
||||||
|
MutableColumns: mutableColumns,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
`)
|
`)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue