Export method ToSnake and SnakeToUpperCamel

This commit is contained in:
Jinzhu 2014-07-21 14:06:54 +08:00
parent 11bfaba497
commit 40c16f3c29
4 changed files with 13 additions and 13 deletions

View File

@ -52,7 +52,7 @@ func Query(scope *Scope) {
columns, _ := rows.Columns() columns, _ := rows.Columns()
var values []interface{} var values []interface{}
for _, value := range columns { for _, value := range columns {
field := elem.FieldByName(snakeToUpperCamel(strings.ToLower(value))) field := elem.FieldByName(SnakeToUpperCamel(strings.ToLower(value)))
if field.IsValid() { if field.IsValid() {
values = append(values, field.Addr().Interface()) values = append(values, field.Addr().Interface())
} else { } else {

View File

@ -96,7 +96,7 @@ func (scope *Scope) PrimaryKeyValue() interface{} {
data := reflect.Indirect(reflect.ValueOf(scope.Value)) data := reflect.Indirect(reflect.ValueOf(scope.Value))
if data.Kind() == reflect.Struct { if data.Kind() == reflect.Struct {
if field := data.FieldByName(snakeToUpperCamel(scope.PrimaryKey())); field.IsValid() { if field := data.FieldByName(SnakeToUpperCamel(scope.PrimaryKey())); field.IsValid() {
return field.Interface() return field.Interface()
} }
} }
@ -121,7 +121,7 @@ func (scope *Scope) SetColumn(column string, value interface{}) {
} }
data := reflect.Indirect(reflect.ValueOf(scope.Value)) data := reflect.Indirect(reflect.ValueOf(scope.Value))
setFieldValue(data.FieldByName(snakeToUpperCamel(column)), value) setFieldValue(data.FieldByName(SnakeToUpperCamel(column)), value)
} }
// CallMethod invoke method with necessary argument // CallMethod invoke method with necessary argument
@ -196,7 +196,7 @@ func (scope *Scope) TableName() string {
} }
} }
str := toSnake(data.Type().Name()) str := ToSnake(data.Type().Name())
if !scope.db.parent.singularTable { if !scope.db.parent.singularTable {
for index, reg := range pluralMapKeys { for index, reg := range pluralMapKeys {
@ -246,7 +246,7 @@ func (scope *Scope) Fields() []*Field {
var field Field var field Field
field.Name = fieldStruct.Name field.Name = fieldStruct.Name
field.DBName = toSnake(fieldStruct.Name) field.DBName = ToSnake(fieldStruct.Name)
value := indirectValue.FieldByName(fieldStruct.Name) value := indirectValue.FieldByName(fieldStruct.Name)
field.Value = value.Interface() field.Value = value.Interface()

View File

@ -266,7 +266,7 @@ func (scope *Scope) updatedAttrsWithValues(values map[string]interface{}, ignore
} }
for key, value := range values { for key, value := range values {
if field := data.FieldByName(snakeToUpperCamel(key)); field.IsValid() { if field := data.FieldByName(SnakeToUpperCamel(key)); field.IsValid() {
func() { func() {
defer func() { defer func() {
if err := recover(); err != nil { if err := recover(); err != nil {
@ -400,7 +400,7 @@ func (scope *Scope) related(value interface{}, foreignKeys ...string) *Scope {
if foreignValue, ok := scope.FieldByName(foreignKey); ok { if foreignValue, ok := scope.FieldByName(foreignKey); ok {
return toScope.inlineCondition(foreignValue).callCallbacks(scope.db.parent.callback.queries) return toScope.inlineCondition(foreignValue).callCallbacks(scope.db.parent.callback.queries)
} else if toScope.HasColumn(foreignKey) { } else if toScope.HasColumn(foreignKey) {
sql := fmt.Sprintf("%v = ?", scope.Quote(toSnake(foreignKey))) sql := fmt.Sprintf("%v = ?", scope.Quote(ToSnake(foreignKey)))
return toScope.inlineCondition(sql, scope.PrimaryKeyValue()).callCallbacks(scope.db.parent.callback.queries) return toScope.inlineCondition(sql, scope.PrimaryKeyValue()).callCallbacks(scope.db.parent.callback.queries)
} }
} }
@ -489,7 +489,7 @@ func (scope *Scope) getPrimaryKey() string {
// if primaryKey tag found, return column name // if primaryKey tag found, return column name
if fieldStruct.Tag.Get("primaryKey") != "" { if fieldStruct.Tag.Get("primaryKey") != "" {
return toSnake(fieldStruct.Name) return ToSnake(fieldStruct.Name)
} }
} }

View File

@ -32,7 +32,7 @@ func (s *safeMap) Get(key string) string {
func FieldByName(name string, value interface{}, withAddr ...bool) (interface{}, bool) { func FieldByName(name string, value interface{}, withAddr ...bool) (interface{}, bool) {
data := reflect.Indirect(reflect.ValueOf(value)) data := reflect.Indirect(reflect.ValueOf(value))
name = snakeToUpperCamel(name) name = SnakeToUpperCamel(name)
if data.Kind() == reflect.Struct { if data.Kind() == reflect.Struct {
if field := data.FieldByName(name); field.IsValid() { if field := data.FieldByName(name); field.IsValid() {
@ -60,7 +60,7 @@ func newSafeMap() *safeMap {
var smap = newSafeMap() var smap = newSafeMap()
var umap = newSafeMap() var umap = newSafeMap()
func toSnake(u string) string { func ToSnake(u string) string {
if v := smap.Get(u); v != "" { if v := smap.Get(u); v != "" {
return v return v
} }
@ -78,7 +78,7 @@ func toSnake(u string) string {
return s return s
} }
func snakeToUpperCamel(s string) string { func SnakeToUpperCamel(s string) string {
if v := umap.Get(s); v != "" { if v := umap.Get(s); v != "" {
return v return v
} }
@ -154,7 +154,7 @@ func convertInterfaceToMap(values interface{}) map[string]interface{} {
switch value := values.(type) { switch value := values.(type) {
case map[string]interface{}: case map[string]interface{}:
for k, v := range value { for k, v := range value {
attrs[toSnake(k)] = v attrs[ToSnake(k)] = v
} }
case []interface{}: case []interface{}:
for _, v := range value { for _, v := range value {
@ -168,7 +168,7 @@ func convertInterfaceToMap(values interface{}) map[string]interface{} {
switch reflectValue.Kind() { switch reflectValue.Kind() {
case reflect.Map: case reflect.Map:
for _, key := range reflectValue.MapKeys() { for _, key := range reflectValue.MapKeys() {
attrs[toSnake(key.Interface().(string))] = reflectValue.MapIndex(key).Interface() attrs[ToSnake(key.Interface().(string))] = reflectValue.MapIndex(key).Interface()
} }
default: default:
scope := Scope{Value: values} scope := Scope{Value: values}