/
schema.go
149 lines (128 loc) · 3.41 KB
/
schema.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
package typecheck
import (
"encoding/json"
"fmt"
"io"
"os"
"strings"
errors2 "github.com/influxdata/influxdb/v2/pkg/errors"
)
type UniqueField struct {
Database string `json:"database"`
Retention string `json:"retention"`
Measurement string `json:"measurement"`
Field string `json:"field"`
}
type FieldTypes map[string]struct{}
type Schema map[string]FieldTypes
func (ft FieldTypes) MarshalText() (text []byte, err error) {
s := make([]string, 0, len(ft))
for f := range ft {
s = append(s, f)
}
return []byte(strings.Join(s, ",")), nil
}
func (ft *FieldTypes) UnmarshalText(text []byte) error {
if *ft == nil {
*ft = make(FieldTypes)
}
for _, ty := range strings.Split(string(text), ",") {
(*ft)[ty] = struct{}{}
}
return nil
}
func NewSchema() Schema {
return make(Schema)
}
func SchemaFromFile(filename string) (Schema, error) {
f, err := os.Open(filename)
if err != nil {
return nil, fmt.Errorf("unable to open schema file %q: %w", filename, err)
}
s := NewSchema()
if err := s.Decode(f); err != nil {
return nil, fmt.Errorf("unable to decode schema file %q: %w", filename, err)
}
return s, nil
}
func (uf *UniqueField) String() string {
return fmt.Sprintf("%q.%q.%q.%q", uf.Database, uf.Retention, uf.Measurement, uf.Field)
}
func (s Schema) AddField(database, retention, measurement, field, dataType string) {
uf := UniqueField{
Database: database,
Retention: retention,
Measurement: measurement,
Field: field,
}
s.AddFormattedField(uf.String(), dataType)
}
func (s Schema) AddFormattedField(field string, dataType string) {
if _, ok := s[field]; !ok {
s[field] = make(map[string]struct{})
}
s[field][dataType] = struct{}{}
}
func (s Schema) Merge(schema Schema) {
for field, types := range schema {
for t := range types {
s.AddFormattedField(field, t)
}
}
}
func (s Schema) Conflicts() Schema {
cs := NewSchema()
for field, t := range s {
if len(t) > 1 {
for ty := range t {
cs.AddFormattedField(field, ty)
}
}
}
return cs
}
func (s Schema) WriteSchemaFile(filename string) error {
if len(s) == 0 {
fmt.Println("No schema file generated: no valid measurements/fields found")
return nil
}
if err := s.encodeSchema(filename); err != nil {
return fmt.Errorf("unable to write schema file to %q: %w", filename, err)
}
fmt.Printf("Schema file written successfully to: %q\n", filename)
return nil
}
func (s Schema) WriteConflictsFile(filename string) error {
conflicts := s.Conflicts()
if len(conflicts) == 0 {
fmt.Println("No conflicts file generated: no conflicts found")
return nil
}
if err := conflicts.encodeSchema(filename); err != nil {
return fmt.Errorf("unable to write conflicts file to %q: %w", filename, err)
}
fmt.Printf("Conflicts file written successfully to: %q\n", filename)
return nil
}
func (s Schema) encodeSchema(filename string) (rErr error) {
schemaFile, err := os.Create(filename)
defer errors2.Capture(&rErr, schemaFile.Close)
if err != nil {
return fmt.Errorf("unable to create schema file: %w", err)
}
return s.Encode(schemaFile)
}
func (s Schema) Encode(w io.Writer) error {
enc := json.NewEncoder(w)
enc.SetIndent("", " ")
if err := enc.Encode(s); err != nil {
return fmt.Errorf("unable to encode schema: %w", err)
}
return nil
}
func (s Schema) Decode(r io.Reader) error {
if err := json.NewDecoder(r).Decode(&s); err != nil {
return fmt.Errorf("unable to decode schema: %w", err)
}
return nil
}