Skip to content

Commit

Permalink
feat: allow naming convention by using env value
Browse files Browse the repository at this point in the history
  • Loading branch information
si3nloong committed Jul 3, 2023
1 parent 4406ceb commit ae491d7
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions x/reflext/cache.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package reflext

import (
"os"
"reflect"
"runtime"
"strings"

"github.com/si3nloong/sqlike/v2/internal/lrucache"
"github.com/si3nloong/sqlike/v2/internal/strfmt"
)

var defaultMapper = NewMapperFunc(500, []string{"sqlike", "db", "sql"})
Expand Down Expand Up @@ -43,6 +46,15 @@ func NewMapperFunc(size int, tags []string, formatter ...FormatFunc) StructMappe
}

Check warning on line 46 in x/reflext/cache.go

View check run for this annotation

Codecov / codecov/patch

x/reflext/cache.go#L45-L46

Added lines #L45 - L46 were not covered by tests
if len(formatter) > 0 {
fmtFunc = formatter[0]
} else {
switch strings.ToLower(os.Getenv("SQL_NAMING_CONVENTION")) {
case "camelcase":
fmtFunc = strfmt.ToCamelCase
case "snakecase":
fmtFunc = strfmt.ToSnakeCase
case "pascalcase":
fmtFunc = strfmt.ToPascalCase

Check warning on line 56 in x/reflext/cache.go

View check run for this annotation

Codecov / codecov/patch

x/reflext/cache.go#L51-L56

Added lines #L51 - L56 were not covered by tests
}
}
return &mapper{
cache: lrucache.New[reflect.Type, *Struct](size),
Expand All @@ -55,13 +67,6 @@ func NewMapperFunc(size int, tags []string, formatter ...FormatFunc) StructMappe
func (m *mapper) CodecByType(t reflect.Type) StructInfo {
mapping, ok := m.cache.Get(t)
if !ok {
// m.mutex.Lock()
// mapping = getCodec(t, m.tags, m.fmtFunc)
// _, ok = m.cache[t]
// if !ok {
// m.cache[t] = mapping
// }
// m.mutex.Unlock()
mapping = getCodec(t, m.tags, m.fmtFunc)
m.cache.Set(t, mapping)
}
Expand Down

0 comments on commit ae491d7

Please sign in to comment.