/
dynamicvalue.go
81 lines (67 loc) · 1.83 KB
/
dynamicvalue.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
package model
import (
"reflect"
"labix.org/v2/mgo/bson"
)
var DynamicValueType = reflect.TypeOf(DynamicValue{})
// DynamicValue can be used to build a model dynamically
// instead of using a static typed struct.
// See DynamicValues
type DynamicValue struct {
Name string
Value
Attribs map[string]map[string]string
}
// Implements reflection.DontVisitStruct
func (DynamicValue) DontVisitStruct() {}
func (self *DynamicValue) Attrib(tagKey, name string) string {
if attribs, ok := self.Attribs[tagKey]; ok {
if attrib, ok := attribs[name]; ok {
return attrib
}
}
return ""
}
func (self *DynamicValue) SetAttrib(tagKey, name, value string) {
if self.Attribs == nil {
self.Attribs = make(map[string]map[string]string)
}
self.Attribs[tagKey] = map[string]string{name: value}
}
// func (self *DynamicValue) String() *String {
// return self.Value.(*String)
// }
func (self *DynamicValue) GetBSON() (interface{}, error) {
if self.Value == nil {
return nil, nil
}
v := reflect.ValueOf(self.Value).Elem()
if v.Kind() == reflect.Ptr {
v = v.Elem()
}
return v.Interface(), nil
}
func (self *DynamicValue) SetBSON(raw bson.Raw) error {
if self.Value == nil {
panic("Can't SetBSON for nil model.Value")
}
return raw.Unmarshal(reflect.ValueOf(self.Value).Elem().Interface())
}
///////////////////////////////////////////////////////////////////////////////
// DynamicValues is used to create a model dynamically instead
// of using a static typed struct.
type DynamicValues []DynamicValue
func (self DynamicValues) Get(name string) (value *DynamicValue, found bool) {
for i := range self {
if self[i].Name == name {
return &self[i], true
}
}
return nil, false
}
func (self DynamicValues) GetValue(name string) (value Value, found bool) {
if v, found := self.Get(name); found {
return v.Value, true
}
return nil, false
}