forked from uadmin/uadmin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
representation.go
122 lines (112 loc) · 2.99 KB
/
representation.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package uadmin
import (
"fmt"
"reflect"
"strings"
)
// GetID !
func GetID(m reflect.Value) uint {
if m.Kind() == reflect.Ptr {
return uint(m.Elem().FieldByName("ID").Uint())
}
return uint(m.FieldByName("ID").Uint())
}
// GetString returns string representation on an instance of
// a model
func GetString(a interface{}) string {
str, ok := a.(fmt.Stringer)
if ok {
return str.String()
}
t := reflect.TypeOf(a)
v := reflect.ValueOf(a)
if t.Kind() != reflect.Ptr && t.Kind() == reflect.Struct {
v = reflect.Indirect(reflect.New(t))
v.Set(reflect.ValueOf(a))
sp := v.Addr().Interface()
str, ok := sp.(fmt.Stringer)
if ok {
return str.String()
}
if _, ok := t.FieldByName("Name"); ok {
return v.FieldByName("Name").String()
}
} else if t.Kind() == reflect.Ptr && t.Elem().Kind() == reflect.Struct {
// Check if nil
if v.IsNil() {
return ""
}
if _, ok := t.Elem().FieldByName("Name"); ok {
return v.Elem().FieldByName("Name").String()
}
} else if t.Kind() == reflect.Int && t.Name() != "int" {
val := v.Int()
// This is a static list type
for i := 0; i < v.NumMethod(); i++ {
ret := v.Method(i).Call([]reflect.Value{})
if len(ret) > 0 {
if ret[0].Int() == val {
return t.Method(i).Name
}
}
}
}
return fmt.Sprint(a)
}
// getChoices return a list of choices
func getChoices(ModelName string) []Choice {
choices := []Choice{
{" - ", 0, false},
}
m, ok := NewModelArray(strings.ToLower(ModelName), false)
// If no model exists, return an empty choices list
if !ok {
return choices
}
//TODO: implement limit choices to
// Get all choices
All(m.Addr().Interface())
for i := 0; i < m.Len(); i++ {
id := GetID(m.Index(i))
choices = append(choices, Choice{GetString(m.Index(i).Interface()), uint(id), false})
}
return choices
}
// getModelName returns the name of a model
func getModelName(a interface{}) string {
if val, ok := a.(reflect.Value); ok {
return getModelName(val.Interface())
}
if val, ok := a.(*reflect.Value); ok {
return getModelName(val.Elem().Interface())
}
if reflect.TypeOf(a).Kind() == reflect.Ptr {
return getModelName(reflect.ValueOf(a).Elem().Interface())
}
if reflect.TypeOf(a).Kind() == reflect.Slice {
return getModelName(reflect.New(reflect.TypeOf(a).Elem()))
}
//if val, ok := a.(reflect.Type); ok {
// return strings.ToLower(val.Name())
//}
return strings.ToLower(reflect.TypeOf(a).Name())
}
// getModelName returns the name of a model
func getModelNameNorm(a interface{}) string {
if val, ok := a.(reflect.Value); ok {
return getModelNameNorm(val.Interface())
}
if val, ok := a.(*reflect.Value); ok {
return getModelNameNorm(val.Elem().Interface())
}
if reflect.TypeOf(a).Kind() == reflect.Ptr {
return getModelNameNorm(reflect.ValueOf(a).Elem().Interface())
}
if reflect.TypeOf(a).Kind() == reflect.Slice {
return getModelNameNorm(reflect.New(reflect.TypeOf(a).Elem()))
}
//if val, ok := a.(reflect.Type); ok {
// return strings.ToLower(val.Name())
//}
return reflect.TypeOf(a).Name()
}