forked from Helvethink/go-odoo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconversion.go
More file actions
173 lines (163 loc) · 4.73 KB
/
conversion.go
File metadata and controls
173 lines (163 loc) · 4.73 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package odoo
import (
"errors"
"fmt"
"reflect"
"strings"
"time"
)
const (
dateFormat = "2006-01-02"
datetimeFormat = "2006-01-02 15:04:05"
tagName = "xmlrpc"
)
func convertFromStaticToDynamic(static interface{}) map[string]interface{} {
var dynamic = make(map[string]interface{})
sv := reflect.ValueOf(static).Elem()
st := reflect.TypeOf(static).Elem()
for i := 0; i < sv.NumField(); i++ {
field := sv.Field(i)
if field.IsNil() {
continue
}
key, _ := st.Field(i).Tag.Lookup(tagName)
if dynamicValue := convertFromStaticToDynamicValue(field.Interface()); dynamicValue != nil {
dynamic[strings.Split(key, ",")[0]] = dynamicValue
}
}
return dynamic
}
func convertFromStaticToDynamicValue(staticValue interface{}) interface{} {
var v interface{}
switch sv := staticValue.(type) {
case *String:
v = sv.v
case *Int:
v = sv.v
case *Bool:
v = sv.v
case *Selection:
v = sv.v
case *Time:
v = sv.v.Format(datetimeFormat)
case *Float:
v = sv.v
case *Many2One:
if sv.ID == 0 {
v = false
} else {
v = sv.ID
}
case *Relation:
v = sv.v
default:
v = staticValue
}
return v
}
func convertFromDynamicToStatic(dynamic interface{}, static interface{}) error {
model := reflect.TypeOf(static).Elem()
var sv reflect.Value
switch d := dynamic.(type) {
case []interface{}:
if model.Kind() != reflect.Slice {
return fmt.Errorf("cannot convert dynamic model to static model %s", model.Name())
}
sv = convertFromDynamicToStaticSlice(d, model)
case map[string]interface{}:
if model.Kind() == reflect.Slice {
return fmt.Errorf("cannot convert dynamic model to static model %s", model.Name())
}
sv = convertFromDynamicToStaticOne(d, model)
default:
return errors.New("cannot convert dynamic of this type")
}
reflect.ValueOf(static).Elem().Set(sv)
return nil
}
func convertFromDynamicToStaticSlice(dynamic []interface{}, sliceModel reflect.Type) reflect.Value {
lenSlice := len(dynamic)
ss := reflect.MakeSlice(sliceModel, lenSlice, lenSlice)
for i := 0; i < lenSlice; i++ {
ss.Index(i).Set(convertFromDynamicToStaticOne(dynamic[i].(map[string]interface{}), sliceModel.Elem()))
}
return ss
}
func convertFromDynamicToStaticOne(dynamic map[string]interface{}, oneModel reflect.Type) reflect.Value {
s := reflect.New(oneModel).Elem()
staticValues := scanStaticModelValues(oneModel, s)
for key, dynamicValue := range dynamic {
if _, ok := staticValues[key]; ok {
staticField := staticValues[key]
staticValue := convertFromDynamicToStaticValue(staticField.Type(), dynamicValue)
if staticValue != nil {
staticField.Set(reflect.ValueOf(staticValue))
}
}
}
return s
}
func convertFromDynamicToStaticValue(staticType reflect.Type, dynamicValue interface{}) interface{} {
var staticValue interface{}
if staticType.Kind() == reflect.Ptr {
staticType = staticType.Elem()
}
typeName := staticType.Name()
if !(dynamicValue == nil || (reflect.ValueOf(dynamicValue).Kind() == reflect.Bool && typeName != "Bool")) {
switch typeName {
case "String":
if strVal, ok := dynamicValue.(string); ok {
staticValue = NewString(strVal)
} else {
// We use "String" for Odoo Binary field type also, which is used to store binary data.
// However, in rare cases (compute fields), this field might return "[]interface{}" instead of "[]byte".
// @TODO: It's important to handle this scenario as well.
}
case "Int":
staticValue = NewInt(dynamicValue.(int64))
case "Selection":
staticValue = NewSelection(dynamicValue)
case "Float":
staticValue = NewFloat(dynamicValue.(float64))
case "Time":
format := dateFormat
if len(dynamicValue.(string)) > 10 {
format = datetimeFormat
}
t, _ := time.Parse(format, dynamicValue.(string))
staticValue = NewTime(t)
case "Many2One":
if intVal, ok := dynamicValue.(int64); ok {
// for many2one_reference field type
staticValue = NewMany2One(intVal, "")
} else {
name, _ := dynamicValue.([]interface{})[1].(string)
staticValue = NewMany2One(dynamicValue.([]interface{})[0].(int64), name)
}
case "Relation":
staticValue = NewRelation()
staticValue.(*Relation).ids = sliceInterfaceToInt64Slice(dynamicValue.([]interface{}))
case "Bool":
staticValue = NewBool(dynamicValue.(bool))
default:
staticValue = dynamicValue
}
}
return staticValue
}
func scanStaticModelValues(typ reflect.Type, s reflect.Value) map[string]reflect.Value {
fields := make(map[string]reflect.Value)
for i := 0; i < s.NumField(); i++ {
field := s.Field(i)
key, _ := typ.Field(i).Tag.Lookup(tagName)
fields[strings.Split(key, ",")[0]] = field
}
return fields
}
func sliceInterfaceToInt64Slice(s []interface{}) []int64 {
i64 := make([]int64, len(s))
for i := 0; i < len(s); i++ {
i64[i] = s[i].(int64)
}
return i64
}