gorm/schema/utils.go

166 lines
4.3 KiB
Go
Raw Normal View History

2020-01-31 07:22:37 +03:00
package schema
import (
"reflect"
2020-02-01 13:02:19 +03:00
"regexp"
2020-01-31 07:22:37 +03:00
"strings"
2020-05-18 08:07:11 +03:00
"github.com/jinzhu/gorm/utils"
2020-01-31 07:22:37 +03:00
)
2020-02-21 19:02:05 +03:00
func ParseTagSetting(str string, sep string) map[string]string {
settings := map[string]string{}
names := strings.Split(str, sep)
for i := 0; i < len(names); i++ {
j := i
if len(names[j]) > 0 {
for {
if names[j][len(names[j])-1] == '\\' {
i++
names[j] = names[j][0:len(names[j])-1] + sep + names[i]
names[i] = ""
} else {
break
}
2020-01-31 07:22:37 +03:00
}
}
2020-02-21 19:02:05 +03:00
values := strings.Split(names[j], ":")
k := strings.TrimSpace(strings.ToUpper(values[0]))
if len(values) >= 2 {
settings[k] = strings.Join(values[1:], ":")
} else if k != "" {
settings[k] = k
}
2020-01-31 07:22:37 +03:00
}
2020-02-21 19:02:05 +03:00
return settings
2020-01-31 07:22:37 +03:00
}
func toColumns(val string) (results []string) {
if val != "" {
for _, v := range strings.Split(val, ",") {
results = append(results, strings.TrimSpace(v))
}
}
return
}
2020-02-01 13:02:19 +03:00
func removeSettingFromTag(tag reflect.StructTag, name string) reflect.StructTag {
return reflect.StructTag(regexp.MustCompile(`(?i)(gorm:.*?)(`+name+`:.*?)(;|("))`).ReplaceAllString(string(tag), "${1}${4}"))
}
2020-05-18 08:07:11 +03:00
// GetRelationsValues get relations's values from a reflect value
func GetRelationsValues(reflectValue reflect.Value, rels []*Relationship) (reflectResults reflect.Value) {
for _, rel := range rels {
reflectResults = reflect.MakeSlice(reflect.SliceOf(rel.FieldSchema.ModelType), 0, 0)
appendToResults := func(value reflect.Value) {
if _, isZero := rel.Field.ValueOf(value); !isZero {
result := reflect.Indirect(rel.Field.ReflectValueOf(value))
switch result.Kind() {
case reflect.Struct:
reflectResults = reflect.Append(reflectResults, result)
case reflect.Slice, reflect.Array:
for i := 0; i < value.Len(); i++ {
reflectResults = reflect.Append(reflectResults, reflect.Indirect(value.Index(i)))
}
}
}
}
switch reflectValue.Kind() {
case reflect.Struct:
appendToResults(reflectValue)
case reflect.Slice:
for i := 0; i < reflectValue.Len(); i++ {
appendToResults(reflectValue.Index(i))
}
}
reflectValue = reflectResults
}
return
}
// GetIdentityFieldValuesMap get identity map from fields
func GetIdentityFieldValuesMap(reflectValue reflect.Value, fields []*Field) (map[string][]reflect.Value, [][]interface{}) {
var (
2020-05-23 16:03:28 +03:00
results = [][]interface{}{}
dataResults = map[string][]reflect.Value{}
notZero, zero bool
2020-05-18 08:07:11 +03:00
)
switch reflectValue.Kind() {
case reflect.Struct:
results = [][]interface{}{make([]interface{}, len(fields))}
for idx, field := range fields {
2020-05-23 16:03:28 +03:00
results[0][idx], zero = field.ValueOf(reflectValue)
notZero = notZero || !zero
2020-05-18 08:07:11 +03:00
}
2020-05-23 16:03:28 +03:00
if !notZero {
return nil, nil
}
dataResults[utils.ToStringKey(results[0]...)] = []reflect.Value{reflectValue}
2020-05-18 08:07:11 +03:00
case reflect.Slice, reflect.Array:
for i := 0; i < reflectValue.Len(); i++ {
2020-05-23 16:35:12 +03:00
fieldValues := make([]interface{}, len(fields))
2020-05-23 16:03:28 +03:00
notZero = false
2020-05-18 08:07:11 +03:00
for idx, field := range fields {
2020-05-23 16:35:12 +03:00
fieldValues[idx], zero = field.ValueOf(reflectValue.Index(i))
2020-05-23 16:03:28 +03:00
notZero = notZero || !zero
2020-05-18 08:07:11 +03:00
}
2020-05-23 16:03:28 +03:00
if notZero {
dataKey := utils.ToStringKey(fieldValues...)
if _, ok := dataResults[dataKey]; !ok {
results = append(results, fieldValues[:])
dataResults[dataKey] = []reflect.Value{reflectValue.Index(i)}
} else {
dataResults[dataKey] = append(dataResults[dataKey], reflectValue.Index(i))
2020-05-18 08:07:11 +03:00
}
}
}
}
return dataResults, results
}
2020-05-19 21:03:43 +03:00
// GetIdentityFieldValuesMapFromValues get identity map from fields
func GetIdentityFieldValuesMapFromValues(values []interface{}, fields []*Field) (map[string][]reflect.Value, [][]interface{}) {
resultsMap := map[string][]reflect.Value{}
results := [][]interface{}{}
for _, v := range values {
rm, rs := GetIdentityFieldValuesMap(reflect.Indirect(reflect.ValueOf(v)), fields)
for k, v := range rm {
resultsMap[k] = append(resultsMap[k], v...)
}
results = append(results, rs...)
}
return resultsMap, results
}
2020-05-18 08:07:11 +03:00
// ToQueryValues to query values
func ToQueryValues(foreignKeys []string, foreignValues [][]interface{}) (interface{}, []interface{}) {
queryValues := make([]interface{}, len(foreignValues))
if len(foreignKeys) == 1 {
for idx, r := range foreignValues {
queryValues[idx] = r[0]
}
return foreignKeys[0], queryValues
} else {
for idx, r := range foreignValues {
queryValues[idx] = r
}
}
return foreignKeys, queryValues
}