Skip to content

Commit

Permalink
reflectx: use github.com/scylladb/go-reflectx
Browse files Browse the repository at this point in the history
Signed-off-by: Michał Matczuk <[email protected]>
  • Loading branch information
mmatczuk committed Jan 25, 2019
1 parent d20fd5b commit 2348379
Show file tree
Hide file tree
Showing 11 changed files with 7 additions and 1,555 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ test:
@$(GOTEST) .
@$(GOTEST) ./migrate
@$(GOTEST) ./qb
@$(GOTEST) ./reflectx
@$(GOTEST) ./table

.PHONY: bench
bench:
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ module github.com/scylladb/gocqlx
require (
github.com/gocql/gocql v0.0.0-20181124151448-70385f88b28b
github.com/google/go-cmp v0.2.0
github.com/scylladb/go-reflectx v1.0.0
)
2 changes: 1 addition & 1 deletion gocqlx.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"reflect"

"github.com/gocql/gocql"
"github.com/scylladb/gocqlx/reflectx"
"github.com/scylladb/go-reflectx"
)

// structOnlyError returns an error appropriate for type when a non-scannable
Expand Down
2 changes: 1 addition & 1 deletion iterx.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"reflect"

"github.com/gocql/gocql"
"github.com/scylladb/gocqlx/reflectx"
"github.com/scylladb/go-reflectx"
)

// Get is a convenience function for creating iterator and calling Get.
Expand Down
33 changes: 2 additions & 31 deletions mapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,11 @@
package gocqlx

import (
"fmt"
"unicode"

"github.com/scylladb/gocqlx/reflectx"
"github.com/scylladb/go-reflectx"
)

// DefaultMapper uses `db` tag and automatically converts struct field names to
// snake case. It can be set to whatever you want, but it is encouraged to be
// set before gocqlx is used as name-to-field mappings are cached after first
// use on a type.
var DefaultMapper = reflectx.NewMapperFunc("db", snakeCase)

// snakeCase converts camel case to snake case.
func snakeCase(s string) string {
buf := []byte(s)
out := make([]byte, 0, len(buf)+3)

l := len(buf)
for i := 0; i < l; i++ {
if !(allowedBindRune(buf[i]) || buf[i] == '_') {
panic(fmt.Sprint("not allowed name ", s))
}

b := rune(buf[i])

if unicode.IsUpper(b) {
if i > 0 && buf[i-1] != '_' && (unicode.IsLower(rune(buf[i-1])) || (i+1 < l && unicode.IsLower(rune(buf[i+1])))) {
out = append(out, '_')
}
b = unicode.ToLower(b)
}

out = append(out, byte(b))
}

return string(out)
}
var DefaultMapper = reflectx.NewMapperFunc("db", reflectx.CamelToSnakeASCII)
22 changes: 0 additions & 22 deletions mapper_bench_test.go

This file was deleted.

71 changes: 0 additions & 71 deletions mapper_test.go

This file was deleted.

2 changes: 1 addition & 1 deletion queryx.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"strconv"

"github.com/gocql/gocql"
"github.com/scylladb/gocqlx/reflectx"
"github.com/scylladb/go-reflectx"
)

// CompileNamedQuery translates query with named parameters in a form
Expand Down
17 changes: 0 additions & 17 deletions reflectx/README.md

This file was deleted.

Loading

0 comments on commit 2348379

Please sign in to comment.