-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.go
115 lines (101 loc) · 2.65 KB
/
database.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
package main
import (
"fmt"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/gohcl"
"github.com/heimdalr/dag"
"github.com/zclconf/go-cty/cty"
)
type Database struct {
Name string
Tables map[string]*Table
DAG *dag.DAG
Block *hcl.Block
Config *Config
Destination string `hcl:"destination_database,optional"`
Remain hcl.Body `hcl:",remain"`
}
var databaseSchema = &hcl.BodySchema{
Blocks: []hcl.BlockHeaderSchema{
{
Type: "table",
LabelNames: []string{"name"},
},
},
}
func NewDatabase(name string, block *hcl.Block, config *Config) (database *Database, diags hcl.Diagnostics) {
database = &Database{
Name: name,
Block: block,
Tables: make(map[string]*Table),
DAG: dag.NewDAG(),
Config: config,
}
// allows extra content to be parsed later in Body.PartialContent
moreDiags := gohcl.DecodeBody(block.Body, nil, database)
diags = append(diags, moreDiags...)
if moreDiags.HasErrors() {
return
}
if len(database.Destination) == 0 {
database.Destination = name
}
// partial because some of the attributes may be consumed with DecodeBody
content, _, diags := database.Block.Body.PartialContent(databaseSchema)
if diags.HasErrors() {
return
}
for _, tableBlock := range content.Blocks {
_, moreDiags := database.AddTable(tableBlock.Labels[0], tableBlock)
if diags = append(diags, moreDiags...); moreDiags.HasErrors() {
continue
}
}
return
}
func (d *Database) AddTable(name string, block *hcl.Block) (table *Table, diags hcl.Diagnostics) {
if _, ok := d.Tables[name]; ok {
return nil, diags.Append(&hcl.Diagnostic{
Summary: fmt.Sprintf("cannot add duplicate table '%s'", name),
Subject: &block.LabelRanges[0],
Severity: hcl.DiagError,
})
}
table, diags = NewTable(d, name, block)
if diags.HasErrors() {
return
}
d.Tables[name] = table
d.DAG.AddVertexByID(table.Name, table)
return
}
func (d *Database) ReadSchema() (diags hcl.Diagnostics) {
for _, table := range d.Tables {
moreDiags := table.ReadSchema()
if diags = append(diags, moreDiags...); diags.HasErrors() {
return
}
}
return
}
func (d *Database) ReadDynamicConfig() (diags hcl.Diagnostics) {
for _, table := range d.Tables {
moreDiags := table.ReadDynamicConfig()
if diags = append(diags, moreDiags...); diags.HasErrors() {
continue
}
}
return
}
func (d *Database) ContextVariables() map[string]cty.Value {
vars := make(map[string]cty.Value)
for _, table := range d.Tables {
vars[table.Name] = cty.MapVal(table.ContextVariables(true))
}
return vars
}
func (d *Database) EvalContext() *hcl.EvalContext {
return &hcl.EvalContext{
Variables: d.ContextVariables(),
}
}