forked from raystack/optimus
-
Notifications
You must be signed in to change notification settings - Fork 1
/
table_spec.go
93 lines (72 loc) · 2.17 KB
/
table_spec.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
package bigquery
import (
"strings"
"github.com/goto/optimus/core/resource"
"github.com/goto/optimus/internal/errors"
)
const (
EntityTable = "resource_table"
)
type Table struct {
Name resource.Name
Description string `mapstructure:"description,omitempty"`
Schema Schema `mapstructure:"schema,omitempty"`
Cluster *Cluster `mapstructure:"cluster,omitempty"`
Partition *Partition `mapstructure:"partition,omitempty"`
ExtraConfig map[string]interface{} `mapstructure:",remain"`
}
func (t *Table) FullName() string {
return t.Name.String()
}
func (t *Table) Validate() error {
if len(t.Schema) == 0 {
return errors.InvalidArgument(EntityTable, "empty schema for table "+t.FullName())
}
if err := t.Schema.Validate(); err != nil {
return errors.AddErrContext(err, EntityTable, "invalid schema for table "+t.FullName())
}
if t.Partition != nil {
if err := t.Partition.Validate(); err != nil {
return errors.AddErrContext(err, EntityTable, "invalid partition for table "+t.FullName())
}
}
if t.Cluster != nil {
if err := t.Cluster.Validate(); err != nil {
return errors.AddErrContext(err, EntityTable, "invalid cluster for table "+t.FullName())
}
}
return nil
}
type Cluster struct {
Using []string `mapstructure:"using,omitempty"`
}
func (c Cluster) Validate() error {
if len(c.Using) == 0 {
return errors.InvalidArgument(EntityTable, "cluster config is empty")
}
for _, clause := range c.Using {
if clause == "" {
return errors.InvalidArgument(EntityTable, "cluster config has invalid value")
}
}
return nil
}
type Partition struct {
Field string `mapstructure:"field,omitempty"`
Type string `mapstructure:"type,omitempty"`
Expiration int64 `mapstructure:"expiration,omitempty"`
Range *Range `mapstructure:"range,omitempty"`
}
func (p Partition) Validate() error {
if strings.EqualFold(p.Type, "range") {
if p.Range == nil {
return errors.InvalidArgument(EntityTable, "partition type range have no range config for "+p.Field)
}
}
return nil
}
type Range struct {
Start int64 `mapstructure:"start,omitempty"`
End int64 `mapstructure:"end,omitempty"`
Interval int64 `mapstructure:"interval,omitempty"`
}