good morning!!!!

Skip to content
Snippets Groups Projects
Commit 642dbdb3 authored by José Carlos Nieto's avatar José Carlos Nieto
Browse files

Merging omitempty and inline tag options into the 'db' tag. Fixing #12.

parent 3ad6c1d5
Branches
Tags v0.4.3 v0.4.4
No related merge requests found
...@@ -43,6 +43,25 @@ type C struct { ...@@ -43,6 +43,25 @@ type C struct {
SetName string SetName string
} }
type tagOptions map[string]bool
func parseTagOptions(s string) tagOptions {
opts := make(tagOptions)
chunks := strings.Split(s, ",")
for _, chunk := range chunks {
opts[strings.TrimSpace(chunk)] = true
}
return opts
}
// Based on http://golang.org/src/pkg/encoding/json/tags.go
func ParseTag(tag string) (string, tagOptions) {
if i := strings.Index(tag, ","); i != -1 {
return tag[:i], parseTagOptions(tag[i+1:])
}
return tag, parseTagOptions("")
}
func columnCompare(s string) string { func columnCompare(s string) string {
return strings.ToLower(columnCompareExclude.ReplaceAllString(s, "")) return strings.ToLower(columnCompareExclude.ReplaceAllString(s, ""))
} }
...@@ -56,35 +75,46 @@ func GetStructFieldIndex(t reflect.Type, columnName string) []int { ...@@ -56,35 +75,46 @@ func GetStructFieldIndex(t reflect.Type, columnName string) []int {
n := t.NumField() n := t.NumField()
columnNameLower := columnCompare(columnName)
for i := 0; i < n; i++ { for i := 0; i < n; i++ {
field := t.Field(i) field := t.Field(i)
// Field is exported. if field.PkgPath != "" {
if field.PkgPath == "" { // Field is unexported.
continue
}
tag := field.Tag // Attempt to use db:"column_name"
fieldName, fieldOptions := ParseTag(field.Tag.Get("db"))
// Tag: field:"columnName" // Deprecated "field" tag.
fieldName := tag.Get("field") if deprecatedField := field.Tag.Get("field"); deprecatedField != "" {
fieldName = deprecatedField
}
if fieldName != "" { // Deprecated "inline" tag.
if fieldName == columnName { if deprecatedInline := field.Tag.Get("inline"); deprecatedInline != "" {
return []int{i} fieldOptions["inline"] = true
} }
// Matching fieldName
if fieldName == "-" {
continue
} }
// Simply matching column to name. // Attempt to match field name.
fieldNameLower := columnCompare(field.Name) if fieldName == columnName {
return []int{i}
}
if fieldNameLower == columnNameLower { if fieldName == "" {
if columnCompare(field.Name) == columnCompare(columnName) {
return []int{i} return []int{i}
} }
}
// Tag: inline:bool // Inline option.
if tag.Get("inline") == "true" { if fieldOptions["inline"] == true {
index := GetStructFieldIndex(field.Type, columnName) index := GetStructFieldIndex(field.Type, columnName)
if index != nil { if index != nil {
res := append([]int{i}, index...) res := append([]int{i}, index...)
...@@ -94,8 +124,6 @@ func GetStructFieldIndex(t reflect.Type, columnName string) []int { ...@@ -94,8 +124,6 @@ func GetStructFieldIndex(t reflect.Type, columnName string) []int {
} }
}
// No match. // No match.
return nil return nil
} }
......
...@@ -270,6 +270,7 @@ func (self *T) FieldValues(item interface{}, convertFn func(interface{}) interfa ...@@ -270,6 +270,7 @@ func (self *T) FieldValues(item interface{}, convertFn func(interface{}) interfa
switch item_t.Kind() { switch item_t.Kind() {
case reflect.Struct: case reflect.Struct:
nfields := item_v.NumField() nfields := item_v.NumField()
values = make([]interface{}, 0, nfields) values = make([]interface{}, 0, nfields)
...@@ -279,26 +280,31 @@ func (self *T) FieldValues(item interface{}, convertFn func(interface{}) interfa ...@@ -279,26 +280,31 @@ func (self *T) FieldValues(item interface{}, convertFn func(interface{}) interfa
field := item_t.Field(i) field := item_t.Field(i)
if field.PkgPath == "" { if field.PkgPath != "" {
// Field is unexported.
value := item_v.Field(i).Interface() continue
}
// Struct tags // Field options.
tag := field.Tag fieldName, fieldOptions := util.ParseTag(field.Tag.Get("db"))
// omitempty:bool // Deprecated "field" tag.
if tag.Get("omitempty") == "true" { if deprecatedField := field.Tag.Get("field"); deprecatedField != "" {
zero := reflect.Zero(reflect.TypeOf(value)).Interface() fieldName = deprecatedField
if value == zero {
continue
} }
// Deprecated "omitempty" tag.
if deprecatedOmitEmpty := field.Tag.Get("omitempty"); deprecatedOmitEmpty != "" {
fieldOptions["omitempty"] = true
} }
// field:string // Deprecated "inline" tag.
fieldName := tag.Get("field") if deprecatedInline := field.Tag.Get("inline"); deprecatedInline != "" {
fieldOptions["inline"] = true
}
// Processing field name.
if fieldName == "-" { if fieldName == "-" {
// Skip the field if its tag's value is -
continue continue
} }
...@@ -306,8 +312,17 @@ func (self *T) FieldValues(item interface{}, convertFn func(interface{}) interfa ...@@ -306,8 +312,17 @@ func (self *T) FieldValues(item interface{}, convertFn func(interface{}) interfa
fieldName = self.ColumnLike(field.Name) fieldName = self.ColumnLike(field.Name)
} }
// inline:bool // Processing tag options.
if tag.Get("inline") == "true" { value := item_v.Field(i).Interface()
if fieldOptions["omitempty"] == true {
zero := reflect.Zero(reflect.TypeOf(value)).Interface()
if value == zero {
continue
}
}
if fieldOptions["inline"] == true {
infields, invalues, inerr := self.FieldValues(value, convertFn) infields, invalues, inerr := self.FieldValues(value, convertFn)
if inerr != nil { if inerr != nil {
return nil, nil, inerr return nil, nil, inerr
...@@ -320,7 +335,6 @@ func (self *T) FieldValues(item interface{}, convertFn func(interface{}) interfa ...@@ -320,7 +335,6 @@ func (self *T) FieldValues(item interface{}, convertFn func(interface{}) interfa
} }
} }
}
case reflect.Map: case reflect.Map:
nfields := item_v.Len() nfields := item_v.Len()
values = make([]interface{}, nfields) values = make([]interface{}, nfields)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment