2013-10-25 14:04:48 +04:00
|
|
|
package gorm
|
2013-10-26 05:49:40 +04:00
|
|
|
|
|
|
|
import (
|
2013-10-26 10:23:02 +04:00
|
|
|
"bytes"
|
2013-11-10 19:07:09 +04:00
|
|
|
"errors"
|
|
|
|
"strconv"
|
2013-10-26 12:33:59 +04:00
|
|
|
|
2013-11-10 14:33:37 +04:00
|
|
|
"fmt"
|
2013-10-26 05:49:40 +04:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2013-10-26 10:23:02 +04:00
|
|
|
func toSnake(s string) string {
|
|
|
|
buf := bytes.NewBufferString("")
|
|
|
|
for i, v := range s {
|
|
|
|
if i > 0 && v >= 'A' && v <= 'Z' {
|
|
|
|
buf.WriteRune('_')
|
|
|
|
}
|
|
|
|
buf.WriteRune(v)
|
|
|
|
}
|
|
|
|
return strings.ToLower(buf.String())
|
|
|
|
}
|
|
|
|
|
2013-10-26 10:40:37 +04:00
|
|
|
func snakeToUpperCamel(s string) string {
|
|
|
|
buf := bytes.NewBufferString("")
|
|
|
|
for _, v := range strings.Split(s, "_") {
|
|
|
|
if len(v) > 0 {
|
|
|
|
buf.WriteString(strings.ToUpper(v[:1]))
|
|
|
|
buf.WriteString(v[1:])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return buf.String()
|
|
|
|
}
|
|
|
|
|
2013-10-31 08:59:04 +04:00
|
|
|
func toSearchableMap(attrs ...interface{}) (result interface{}) {
|
|
|
|
if len(attrs) > 1 {
|
|
|
|
if str, ok := attrs[0].(string); ok {
|
|
|
|
result = map[string]interface{}{str: attrs[1]}
|
|
|
|
}
|
|
|
|
} else if len(attrs) == 1 {
|
|
|
|
if attr, ok := attrs[0].(map[string]interface{}); ok {
|
|
|
|
result = attr
|
|
|
|
}
|
|
|
|
|
|
|
|
if attr, ok := attrs[0].(interface{}); ok {
|
|
|
|
result = attr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-11-10 19:07:09 +04:00
|
|
|
func getInterfaceAsString(value interface{}) (str string, err error) {
|
|
|
|
switch value := value.(type) {
|
|
|
|
case string:
|
|
|
|
str = value
|
|
|
|
case int:
|
|
|
|
if value < 0 {
|
|
|
|
str = ""
|
|
|
|
} else {
|
|
|
|
str = strconv.Itoa(value)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
err = errors.New(fmt.Sprintf("Can't understand %v", value))
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|