mapper: moved mapper to separate file, added snake case mapper

Performance is comparable to sting.ToLower

BenchmarkSnakeCase-4              200000              5922 ns/op            1216 B/op         97 allocs/op
BenchmarkToLower-4                300000              5418 ns/op             704 B/op         74 allocs/op
This commit is contained in:
Michał Matczuk
2017-07-26 10:46:06 +02:00
parent 871a3693ad
commit 08b131c5ee
4 changed files with 131 additions and 12 deletions

View File

@@ -4,18 +4,11 @@ import (
"errors"
"fmt"
"reflect"
"strings"
"github.com/gocql/gocql"
"github.com/jmoiron/sqlx/reflectx"
)
// DefaultMapper uses `db` tag and strings.ToLower to lowercase struct field
// names. 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", strings.ToLower)
// structOnlyError returns an error appropriate for type when a non-scannable
// struct is expected but something else is given
func structOnlyError(t reflect.Type) error {