gorm/schema/utils.go

162 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 (
fieldValues = make([]reflect.Value, len(fields))
results = [][]interface{}{}
dataResults = map[string][]reflect.Value{}
)
switch reflectValue.Kind() {
case reflect.Struct:
results = [][]interface{}{make([]interface{}, len(fields))}
for idx, field := range fields {
fieldValues[idx] = field.ReflectValueOf(reflectValue)
results[0][idx] = fieldValues[idx].Interface()
}
dataResults[utils.ToStringKey(fieldValues...)] = []reflect.Value{reflectValue}
case reflect.Slice, reflect.Array:
for i := 0; i < reflectValue.Len(); i++ {
for idx, field := range fields {
fieldValues[idx] = field.ReflectValueOf(reflectValue.Index(i))
}
dataKey := utils.ToStringKey(fieldValues...)
if _, ok := dataResults[dataKey]; !ok {
result := make([]interface{}, len(fieldValues))
for idx, fieldValue := range fieldValues {
result[idx] = fieldValue.Interface()
}
results = append(results, result)
dataResults[dataKey] = []reflect.Value{reflectValue.Index(i)}
} else {
dataResults[dataKey] = append(dataResults[dataKey], reflectValue.Index(i))
}
}
}
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
}