Skip to content

Commit

Permalink
ToSnakeCase = strutil.ToSnakeCase
Browse files Browse the repository at this point in the history
  • Loading branch information
ungerik committed Aug 29, 2023
1 parent b006361 commit da81308
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 23 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.21

require (
github.com/domonda/go-errs v0.0.0-20230810132956-1b6272f9fc8f
github.com/domonda/go-types v0.0.0-20230810134814-bd15ee23faf5
github.com/domonda/go-types v0.0.0-20230829145420-30f9974e0bc7
github.com/go-sql-driver/mysql v1.7.1
github.com/lib/pq v1.10.9
github.com/stretchr/testify v1.8.4
Expand Down
8 changes: 2 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ github.com/domonda/go-errs v0.0.0-20230810132956-1b6272f9fc8f h1:OQaXlKXZc52Vsz7
github.com/domonda/go-errs v0.0.0-20230810132956-1b6272f9fc8f/go.mod h1:DYkFE3rxUGhTCMmR5MpQ2NTtoCPiORdjBATGkIEeGKM=
github.com/domonda/go-pretty v0.0.0-20230810130018-8920f571470a h1:b3a6MwwMrHR9dw6585e3Ky51T50OKuD3fRuLyh8ziEw=
github.com/domonda/go-pretty v0.0.0-20230810130018-8920f571470a/go.mod h1:3QkM8UJdyJMeKZiIo7hYzSkQBpRS3k0gOHw4ysyEIB4=
github.com/domonda/go-types v0.0.0-20230810133443-7fcb0a5a218b h1:owt4+9DbUZvPYKjUfUnE+unOlVmJ35CItJTM2Sr8iwk=
github.com/domonda/go-types v0.0.0-20230810133443-7fcb0a5a218b/go.mod h1:qMSeU/23ZUopt+1kY0pJ27iqNRtsY1jATQklyCyLRAU=
github.com/domonda/go-types v0.0.0-20230810134814-bd15ee23faf5 h1:hILmOg7w0+ozEd/AXCiGlCt0MsnD7cp5drmrq2D3xtc=
github.com/domonda/go-types v0.0.0-20230810134814-bd15ee23faf5/go.mod h1:qMSeU/23ZUopt+1kY0pJ27iqNRtsY1jATQklyCyLRAU=
github.com/domonda/go-types v0.0.0-20230829145420-30f9974e0bc7 h1:riEK9SQ1O0ADGI66P1Rz2zqB+g3qlREm/wF7DINj7RI=
github.com/domonda/go-types v0.0.0-20230829145420-30f9974e0bc7/go.mod h1:qMSeU/23ZUopt+1kY0pJ27iqNRtsY1jATQklyCyLRAU=
github.com/go-sql-driver/mysql v1.7.1 h1:lUIinVbN1DY0xBg0eMOzmmtGoHwWBbvnWubQUrtU8EI=
github.com/go-sql-driver/mysql v1.7.1/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
Expand All @@ -23,8 +21,6 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/ungerik/go-reflection v0.0.0-20220113085621-6c5fc1f2694a h1:9vfYtqoyrPw08TbSLxkSXEflp6iXa3RL86Qjs+DrVas=
github.com/ungerik/go-reflection v0.0.0-20220113085621-6c5fc1f2694a/go.mod h1:6Hnd2/4g3Tpt6TjvxHx8wXOZziwApVxRdIGkr7vNpXs=
github.com/ungerik/go-reflection v0.0.0-20230810134712-a63435f6bc7e h1:BPksMeVdgSD8L4yXHYSY3HpdJ/5z2Ok5lF6PxHIVgEQ=
github.com/ungerik/go-reflection v0.0.0-20230810134712-a63435f6bc7e/go.mod h1:1Q14POg/xa/P6/hWKfnUexqUhW1X6jgw+6gG7lOne1E=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
24 changes: 8 additions & 16 deletions structfieldmapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import (
"fmt"
"reflect"
"strings"
"unicode"

"github.com/domonda/go-types/strutil"
)

// FieldFlag is a bitmask for special properties
Expand Down Expand Up @@ -59,6 +60,9 @@ func NewTaggedStructFieldMapping() *TaggedStructFieldMapping {
// Implements StructFieldMapper.
var DefaultStructFieldMapping = NewTaggedStructFieldMapping()

// TaggedStructFieldMapping implements StructFieldMapper
var _ StructFieldMapper = new(TaggedStructFieldMapping)

// TaggedStructFieldMapping implements StructFieldMapper with a struct field NameTag
// to be used for naming and a UntaggedNameFunc in case the NameTag is not set.
type TaggedStructFieldMapping struct {
Expand Down Expand Up @@ -141,18 +145,6 @@ func IgnoreStructField(string) string { return "" }
// ToSnakeCase converts s to snake case
// by lower casing everything and inserting '_'
// before every new upper case character in s.
func ToSnakeCase(s string) string {
var b strings.Builder
b.Grow(len(s) + 2)
lastWasUpper := true
for _, r := range s {
lr := unicode.ToLower(r)
isUpper := lr != r
if isUpper && !lastWasUpper {
b.WriteByte('_')
}
b.WriteRune(lr)
lastWasUpper = isUpper
}
return b.String()
}
// Whitespace, symbol, and punctuation characters
// will be replace by '_'.
var ToSnakeCase = strutil.ToSnakeCase

0 comments on commit da81308

Please sign in to comment.