Skip to content

Commit

Permalink
fix: change default column function name to lower camelcase
Browse files Browse the repository at this point in the history
  • Loading branch information
tauslim committed May 24, 2024
1 parent 4863e3e commit 763db9e
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 22 deletions.
26 changes: 6 additions & 20 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ package gorql

import (
"errors"
"github.com/iancoleman/strcase"
"log"
"reflect"
"strings"
"unicode"
)

const (
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module github.com/douglaslim/gorql

go 1.18

require github.com/iancoleman/strcase v0.3.0
4 changes: 2 additions & 2 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 763db9e

Please sign in to comment.