Replace invalid character from the go identifiers with description string.
This commit is contained in:
parent
255f4a8eaf
commit
6b098b8e41
3 changed files with 135 additions and 7 deletions
|
|
@ -3,6 +3,7 @@ package dbidentifier
|
|||
import (
|
||||
"github.com/go-jet/jet/v2/internal/3rdparty/snaker"
|
||||
"strings"
|
||||
"unicode"
|
||||
)
|
||||
|
||||
// ToGoIdentifier converts database identifier to Go identifier.
|
||||
|
|
@ -15,10 +16,92 @@ func ToGoFileName(databaseIdentifier string) string {
|
|||
return strings.ToLower(replaceInvalidChars(databaseIdentifier))
|
||||
}
|
||||
|
||||
func replaceInvalidChars(str string) string {
|
||||
str = strings.Replace(str, " ", "_", -1)
|
||||
str = strings.Replace(str, "-", "_", -1)
|
||||
str = strings.Replace(str, ".", "_", -1)
|
||||
func replaceInvalidChars(identifier string) string {
|
||||
increase, needs := needsCharReplacement(identifier)
|
||||
|
||||
return str
|
||||
if !needs {
|
||||
return identifier
|
||||
}
|
||||
|
||||
var b strings.Builder
|
||||
|
||||
b.Grow(len(identifier) + increase)
|
||||
|
||||
for _, c := range identifier {
|
||||
switch {
|
||||
case unicode.IsSpace(c):
|
||||
b.WriteByte('_')
|
||||
case unicode.IsControl(c):
|
||||
continue
|
||||
default:
|
||||
replacement, ok := asciiCharacterReplacement[c]
|
||||
|
||||
if ok {
|
||||
b.WriteByte('_')
|
||||
b.WriteString(replacement)
|
||||
b.WriteByte('_')
|
||||
} else {
|
||||
b.WriteRune(c)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return b.String()
|
||||
}
|
||||
|
||||
func needsCharReplacement(identifier string) (increase int, needs bool) {
|
||||
for _, c := range identifier {
|
||||
switch {
|
||||
case unicode.IsSpace(c):
|
||||
needs = true
|
||||
case unicode.IsControl(c):
|
||||
increase += -1
|
||||
needs = true
|
||||
continue
|
||||
default:
|
||||
replacement, ok := asciiCharacterReplacement[c]
|
||||
|
||||
if ok {
|
||||
increase += len(replacement) + 1
|
||||
needs = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return increase, needs
|
||||
}
|
||||
|
||||
var asciiCharacterReplacement = map[rune]string{
|
||||
'!': "exclamation",
|
||||
'"': "quotation",
|
||||
'#': "number",
|
||||
'$': "dollar",
|
||||
'%': "percent",
|
||||
'&': "ampersand",
|
||||
'\'': "apostrophe",
|
||||
'(': "opening_parentheses",
|
||||
')': "closing_parentheses",
|
||||
'*': "asterisk",
|
||||
'+': "plus",
|
||||
',': "comma",
|
||||
'-': "_",
|
||||
'.': "_",
|
||||
'/': "slash",
|
||||
':': "colon",
|
||||
';': "semicolon",
|
||||
'<': "less",
|
||||
'=': "equal",
|
||||
'>': "greater",
|
||||
'?': "question",
|
||||
'@': "at",
|
||||
'[': "opening_bracket",
|
||||
'\\': "backslash",
|
||||
']': "closing_bracket",
|
||||
'^': "caret",
|
||||
'`': "accent",
|
||||
'{': "opening_braces",
|
||||
'|': "vertical_bar",
|
||||
'}': "closing_braces",
|
||||
'~': "tilde",
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue