Extract column indexing into reflect2 package

This commit is contained in:
2018-07-03 19:47:23 +02:00
parent 4aae29e4bf
commit 406888cdd5
2 changed files with 33 additions and 21 deletions
+27
View File
@@ -0,0 +1,27 @@
package reflect2
import "reflect"
// TagIndex returns a map that associates tag values with field indices.
func TagIndex(typ reflect.Type, tag string) map[string]int {
tagIndex := map[string]int{}
for i := 0; i < typ.NumField(); i++ {
tag := typ.Field(i).Tag.Get(tag)
if tag != "" {
tagIndex[tag] = i
}
}
return tagIndex
}
// AssociateColumns returns a map that associates column indices with fields.
func AssociateColumns(typ reflect.Type, tag string, cols []string) map[int]int {
tagIndex := TagIndex(typ, tag)
colFields := map[int]int{}
for i, col := range cols {
if fi, ok := tagIndex[col]; ok {
colFields[i] = fi
}
}
return colFields
}