@@ -65,6 +65,7 @@ type Mapper struct {
6565 tagName string
6666 tagMapFunc func (string ) string
6767 mapFunc func (string ) string
68+ colMapFunc func (string ) string
6869 mutex sync.Mutex
6970}
7071
@@ -89,6 +90,19 @@ func NewMapperTagFunc(tagName string, mapFunc, tagMapFunc func(string) string) *
8990 }
9091}
9192
93+ // NewMapperTagColFunc returns a new mapper which contains a mapper for field names
94+ // AND a mapper for tag values AND sql column names. This is useful for tags like json which can
95+ // have values like "name,omitempty", or on databases like Sqlite where column casing in queries cause problems.
96+ func NewMapperTagColFunc (tagName string , mapFunc , tagMapFunc , colMapFunc func (string ) string ) * Mapper {
97+ return & Mapper {
98+ cache : make (map [reflect.Type ]* StructMap ),
99+ tagName : tagName ,
100+ mapFunc : mapFunc ,
101+ tagMapFunc : tagMapFunc ,
102+ colMapFunc : colMapFunc ,
103+ }
104+ }
105+
92106// NewMapperFunc returns a new mapper which optionally obeys a field tag and
93107// a struct field name mapper func given by f. Tags will take precedence, but
94108// for any other field, the mapped name will be f(field.Name)
@@ -189,6 +203,9 @@ func (m *Mapper) TraversalsByNameFunc(t reflect.Type, names []string, fn func(in
189203 nameCounter := make (map [string ]int , len (names ))
190204
191205 for i , name := range names {
206+ if m .colMapFunc != nil {
207+ name = m .colMapFunc (name )
208+ }
192209 fi , ok := tm .Names [name ]
193210 if ! ok {
194211 if leafs , lok := tm .Leafs [name ]; lok {
0 commit comments