diff --git a/config.go b/config.go index 68edc35..83ca874 100644 --- a/config.go +++ b/config.go @@ -2,10 +2,9 @@ package gorql import ( "errors" + "github.com/iancoleman/strcase" "log" "reflect" - "strings" - "unicode" ) const ( @@ -99,27 +98,14 @@ func (c *Config) defaults() error { return nil } -// Column is the default function that converts field name into a database column. -// It used to convert the struct fields into their database names. For example: +// Column is the default function that transform field name into column name. +// It used to convert the struct fields into lower camelcase. For example: // // Username => username -// FullName => full_name -// HTTPCode => http_code +// FullName => fullName +// HTTPCode => httpcode func Column(s string) string { - var b strings.Builder - for i := 0; i < len(s); i++ { - r := rune(s[i]) - // put '.' if it is not a start or end of a word, current letter is an uppercase letter, - // and previous letter is a lowercase letter (cases like: "UserName"), or next letter is - // also a lowercase letter and previous letter is not "_". - if i > 0 && i < len(s)-1 && unicode.IsUpper(r) && - (unicode.IsLower(rune(s[i-1])) || - unicode.IsLower(rune(s[i+1])) && unicode.IsLetter(rune(s[i-1]))) { - b.WriteString("_") - } - b.WriteRune(unicode.ToLower(r)) - } - return b.String() + return strcase.ToLowerCamel(s) } func defaultString(s *string, v string) { diff --git a/go.mod b/go.mod index 3628f61..eccfaf5 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,5 @@ module github.com/douglaslim/gorql go 1.18 + +require github.com/iancoleman/strcase v0.3.0 diff --git a/parser.go b/parser.go index 4ccffbe..970769b 100644 --- a/parser.go +++ b/parser.go @@ -160,9 +160,9 @@ type Parser struct { // field is a configuration of a struct field. type field struct { - // Name of the column. + // Name of the field. Name string - // Has a "replacewith" option in the tag. If present, the name of the column will be converted to this name + // Has a "replacewith" option in the tag. If present, this name will be used as the db column name ReplaceWith string // Has a "sort" option in the tag. Sortable bool