diff --git a/.gitignore b/.gitignore index 37ab1486812880140901b2abcd6bf783418cb2c5..c72d686347dc19af2a5f4a896730f98eadbf36e0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ *.swp *.db +*.tmp diff --git a/main_test.go b/main_test.go index 461d52576c1fecc3a55bc7f302b990ba13b25d3e..0ce508d4b3a270f8c3205ca46d411616b5ce54be 100644 --- a/main_test.go +++ b/main_test.go @@ -309,19 +309,23 @@ var setupFn = map[string]func(driver interface{}) error{ type Birthday struct { Name string // `db:"name"` // Must match by name. Born time.Time // `db:"born"` // Must match by name. - OmitMe bool `db:"-" bson:"-"` + OmitMe bool `json:"omit_me" db:"-" bson:"-"` } type Fibonacci struct { Input uint64 `db:"input"` Output uint64 `db:"output"` - OmitMe bool `db:"omit_me,omitempty" bson:"omit_me,omitempty"` + // Test for BSON option. + OmitMe bool `json:"omitme" db:",bson,omitempty" bson:"omit_me,omitempty"` } type OddEven struct { - Input int `db:"input"` - IsEven bool `db:"is_even" bson:"is_even"` // The "bson" tag is required by mgo. - OmitMe bool `db:"-,omitempty" bson:"-,omitempty"` + // Test for JSON option. + Input int `json:"input"` + // Test for JSON option. + // The "bson" tag is required by mgo. + IsEven bool `json:"is_even" db:",json" bson:"is_even"` + OmitMe bool `json:"omit_me" db:"-" bson:"-"` } func even(i int) bool { diff --git a/util/main.go b/util/main.go index a077bcfb04ca8106121aa35912d293a17af97c49..147b8fe8be680a87ce3f255aa47994061491627a 100644 --- a/util/main.go +++ b/util/main.go @@ -67,51 +67,73 @@ func GetStructFieldIndex(t reflect.Type, columnName string) []int { field := t.Field(i) - if field.PkgPath != "" { + if field.PkgPath != `` { // Field is unexported. continue } - // Attempt to use db:"column_name" - fieldName, fieldOptions := ParseTag(field.Tag.Get("db")) + // Attempt to use db:`column_name` + fieldName, fieldOptions := ParseTag(field.Tag.Get(`db`)) - // Deprecated "field" tag. - if deprecatedField := field.Tag.Get("field"); deprecatedField != "" { + // Deprecated `field` tag. + if deprecatedField := field.Tag.Get(`field`); deprecatedField != `` { fieldName = deprecatedField } - // Deprecated "inline" tag. - if deprecatedInline := field.Tag.Get("inline"); deprecatedInline != "" { - fieldOptions["inline"] = true + // Deprecated `inline` tag. + if deprecatedInline := field.Tag.Get(`inline`); deprecatedInline != `` { + fieldOptions[`inline`] = true } - // Matching fieldName - if fieldName == "-" { + // Skipping field + if fieldName == `-` { continue } + // Trying to match field name. + + // Explicit JSON or BSON options. + if fieldName == `` && fieldOptions[`bson`] { + // Using name from the BSON tag. + fieldName, _ = ParseTag(field.Tag.Get(`bson`)) + } + + if fieldName == `` && fieldOptions[`bson`] { + // Using name from the JSON tag. + fieldName, _ = ParseTag(field.Tag.Get(`bson`)) + } + + // Still don't have a match? try to match againt JSON. + if fieldName == `` { + fieldName, _ = ParseTag(field.Tag.Get(`json`)) + } + + // Still don't have a match? try to match againt BSON. + if fieldName == `` { + fieldName, _ = ParseTag(field.Tag.Get(`bson`)) + } + // Attempt to match field name. if fieldName == columnName { return []int{i} } - if fieldName == "" { + // Nothing works, trying to match by name. + if fieldName == `` { if NormalizeColumn(field.Name) == NormalizeColumn(columnName) { return []int{i} } } // Inline option. - if fieldOptions["inline"] == true { + if fieldOptions[`inline`] == true { index := GetStructFieldIndex(field.Type, columnName) if index != nil { res := append([]int{i}, index...) return res } } - } - // No match. return nil } diff --git a/util/sqlutil/main.go b/util/sqlutil/main.go index 0ec5636e91e13b791010c1b34613278133c457a3..76054607ab9472832c0f85c3bd4b06e37bf7a8d2 100644 --- a/util/sqlutil/main.go +++ b/util/sqlutil/main.go @@ -365,11 +365,35 @@ func (self *T) FieldValues(item interface{}, convertFn func(interface{}) interfa fieldOptions[`inline`] = true } - // Processing field name. + // Skipping field if fieldName == `-` { continue } + // Trying to match field name. + + // Explicit JSON or BSON options. + if fieldName == `` && fieldOptions[`bson`] { + // Using name from the BSON tag. + fieldName, _ = util.ParseTag(field.Tag.Get(`bson`)) + } + + if fieldName == `` && fieldOptions[`bson`] { + // Using name from the JSON tag. + fieldName, _ = util.ParseTag(field.Tag.Get(`bson`)) + } + + // Still don't have a match? try to match againt JSON. + if fieldName == `` { + fieldName, _ = util.ParseTag(field.Tag.Get(`json`)) + } + + // Still don't have a match? try to match againt BSON. + if fieldName == `` { + fieldName, _ = util.ParseTag(field.Tag.Get(`bson`)) + } + + // Nothing works, trying to match by name. if fieldName == `` { fieldName = self.columnLike(field.Name) }