-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfiguration.go
More file actions
119 lines (103 loc) · 3.44 KB
/
Copy pathconfiguration.go
File metadata and controls
119 lines (103 loc) · 3.44 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
package morph
// Configuration represents the configuration used to construct
// the table and column mappings.
type Configuration struct {
Tables []TableConfiguration `json:"tables" yaml:"tables"`
}
// AsMetadata converts the configuration to metadata mappings.
func (c Configuration) AsMetadata() []Table {
var tables []Table
references := map[string][]ReferenceConfiguration{}
tablesByName := map[string]*Table{}
columnsByName := map[string]*Column{}
for _, t := range c.Tables {
table := t.AsTable()
for _, column := range table.Columns() {
columnsByName[column.Name()] = &column
}
tablesByName[table.Name()] = &table
if len(t.References) > 0 {
if _, ok := references[table.Name()]; !ok {
references[table.Name()] = []ReferenceConfiguration{}
}
references[table.Name()] = append(references[table.Name()], t.References...)
}
}
for childTableName, refs := range references {
child, ok := tablesByName[childTableName]
if !ok {
continue
}
for _, ref := range refs {
parent, ok := tablesByName[ref.TableName]
if !ok {
continue
}
key := []Column{}
for _, fk := range ref.ForeignKey {
if column, ok := columnsByName[fk.ColumnName]; ok {
key = append(key, *column)
}
}
if _, err := child.References(parent, key); err != nil {
continue
}
}
}
for _, table := range tablesByName {
tables = append(tables, *table)
}
return tables
}
// TableConfiguration represents the configuration used to construct
// a single table mapping.
type TableConfiguration struct {
TypeName string `json:"typeName" yaml:"typeName"`
Name string `json:"name" yaml:"name"`
Alias string `json:"alias" yaml:"alias"`
Columns []ColumnConfiguration `json:"columns" yaml:"columns"`
References []ReferenceConfiguration `json:"references" yaml:"references"`
}
// AsTable converts the configuration to a table mapping.
func (t TableConfiguration) AsTable() Table {
var table Table
table.SetTypeName(t.TypeName)
table.SetName(t.Name)
table.SetAlias(t.Alias)
for _, c := range t.Columns {
if err := table.AddColumn(c.AsColumn()); err != nil {
continue
}
}
return table
}
// ReferenceConfiguration represents the configuration used to construct
// a single reference between two tables.
type ReferenceConfiguration struct {
TableName string `json:"tableName" yaml:"tableName"`
ForeignKey []ForeignKeyConfiguration `json:"foreignKey" yaml:"foreignKey"`
}
// ForeignKeyConfiguration represents the configuration used to communicate
// which columns comprise the foreign key for a reference.
type ForeignKeyConfiguration struct {
ColumnName string `json:"columnName" yaml:"columnName"`
}
// ColumnConfiguration represents the configuration used to construct
// a single column mapping.
type ColumnConfiguration struct {
Name string `json:"name" yaml:"name"`
Field string `json:"field" yaml:"field"`
FieldType string `json:"fieldType" yaml:"fieldType"`
FieldStrategy FieldStrategy `json:"fieldStrategy" yaml:"fieldStrategy"`
PrimaryKey bool `json:"primaryKey" yaml:"primaryKey"`
}
// AsColumn converts the configuration to a column mapping.
func (c ColumnConfiguration) AsColumn() Column {
var column Column
column.SetField(c.Field)
column.SetName(c.Name)
column.SetFieldType(c.FieldType)
column.SetStrategy(c.FieldStrategy)
column.SetPrimaryKey(c.PrimaryKey)
return column
}