-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmapping_json.go
158 lines (130 loc) · 3.69 KB
/
mapping_json.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
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
package thunder
import (
"errors"
"fmt"
)
type JsonMappingField struct {
FieldType string `json:"_type"`
// Simple field
Column string `json:"column,omitempty"`
Alias string `json:"alias,omitempty"`
// Relation
Name string `json:"name,omitempty"`
Type string `json:"type,omitempty"`
Table string `json:"table,omitempty"`
PrimaryKeys []string `json:"primary_keys,omitempty"`
LocalKey string `json:"local_key,omitempty"`
UsePivotTable bool `json:"use_pivot_table,omitempty"`
PivotTable string `json:"pivot_table,omitempty"`
ForeignPivotKey string `json:"foreign_pivot_key,omitempty"`
LocalPivotKey string `json:"local_pivot_key,omitempty"`
PivotFields []JsonMappingField `json:"pivot_fields,omitempty"`
ForeignKey string `json:"foreign_key,omitempty"`
Mapping JsonMapping `json:"mapping,omitempty"`
}
type JsonMapping []JsonMappingField
func SerializeMapping(m *Mapping) (*JsonMapping, error) {
var jm = JsonMapping{}
for _, field := range m.Fields {
name := ""
if field.Name != nil {
name = *field.Name
}
jm = append(jm, JsonMappingField{
FieldType: "simple",
Column: field.Column,
Name: name,
})
}
for _, rel := range m.Relations {
jsonRel := JsonMappingField{
FieldType: "relation",
Name: rel.Name,
Type: "one-to-one",
Table: rel.Table,
LocalKey: rel.LocalKey,
ForeignKey: rel.ForeignKey,
PrimaryKeys: rel.PrimaryKeys,
}
if rel.Many {
jsonRel.Type = "has-many"
}
if rel.Pivot != nil {
nested, err := SerializeMapping(&Mapping{Fields: rel.Pivot.Fields})
if err != nil {
return nil, err
}
jsonRel.UsePivotTable = true
jsonRel.ForeignPivotKey = rel.Pivot.ForeignKey
jsonRel.LocalPivotKey = rel.Pivot.LocalKey
jsonRel.PivotTable = rel.Pivot.Table
jsonRel.PivotFields = *nested
}
nestedMapping, err := SerializeMapping(&rel.Mapping)
if err != nil {
return nil, err
}
jsonRel.Mapping = *nestedMapping
jm = append(jm, jsonRel)
}
return &jm, nil
}
func UnserializeMapping(jm *JsonMapping, parent *Relation) (*Mapping, error) {
var mapping = Mapping{
Fields: []SimpleField{},
Relations: []Relation{},
}
for _, field := range *jm {
if field.FieldType == "simple" {
var name *string = nil
if field.Name != "" {
name = &field.Name
}
mapping.Fields = append(mapping.Fields, SimpleField{
Column: field.Column,
Name: name,
})
continue
}
if field.FieldType == "relation" {
relation := Relation{
Name: field.Name,
Many: field.Type == "has-many",
Table: field.Table,
PrimaryKeys: field.PrimaryKeys,
LocalKey: field.LocalKey,
ForeignKey: field.ForeignKey,
Children: []*Relation{},
}
// Add pivot if needed
if field.UsePivotTable {
nestedFields, err := UnserializeMapping(&field.Mapping, &relation)
if err != nil {
return nil, err
}
relation.Pivot = &RelationPivot{
Table: field.PivotTable,
LocalKey: field.LocalPivotKey,
ForeignKey: field.ForeignPivotKey,
Fields: nestedFields.Fields, //TODO FRONT
}
}
// Add nested mapping
nestedMapping, err := UnserializeMapping(&field.Mapping, &relation)
if err != nil {
return nil, err
}
relation.Mapping = *nestedMapping
// Set navigation pointer if parent exists
if parent != nil {
relation.Parent = parent
parent.Children = append(parent.Children, &relation)
}
// Inject relation
mapping.Relations = append(mapping.Relations, relation)
continue
}
return nil, errors.New(fmt.Sprintf("Unknown field type: %s", field.FieldType))
}
return &mapping, nil
}