-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema.go
149 lines (116 loc) · 2.99 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 model
import (
"bytes"
"fmt"
"io"
"path"
"strings"
"text/tabwriter"
"github.com/rjeczalik/bigstruct/big"
)
type Schema struct {
Model `yaml:",inline"`
Overlay *Overlay `yaml:"-" json:"-"`
OverlayID uint64 `gorm:"column:overlay_id;type:bigint;not null;index" yaml:"overlay_id,omitempty" json:"overlay_id,omitempty"`
OverlayProperty string `gorm:"column:overlay_property;type:tinytext;not null" yaml:"overlay_property,omitempty" json:"overlay_property,omitempty"`
Key string `gorm:"column:key;type:text;not null" yaml:"key,omitempty" json:"key,omitempty"`
Type string `gorm:"column:type;type:tinytext;not null" yaml:"type,omitempty" json:"type,omitempty"`
Encoding string `gorm:"column:encoding;type:tinytext;not null" yaml:"encoding,omitempty" json:"encoding,omitempty"`
Metadata Object `gorm:"column:metadata;type:text" yaml:"metadata,omitempty" json:"metadata,omitempty"`
}
func (*Schema) TableName() string {
return TablePrefix + "_schema"
}
func (s *Schema) Codec() string {
if s.Encoding != "" {
return path.Join(s.Type, s.Encoding)
}
return s.Type
}
type Schemas []*Schema
func MakeSchemas(o *Overlay, f big.Fields) Schemas {
schemas := make(Schemas, 0, len(f))
for _, f := range f {
if f.Type == "" {
continue // skip empty entries, they will get recreated from the tree either way
}
s := &Schema{
Key: f.Key,
Type: f.Type,
}
if o != nil {
s.Overlay = o
s.OverlayID = o.ID
s.OverlayProperty = o.Property
}
if i := strings.IndexRune(f.Type, '/'); i != -1 {
s.Type = f.Type[:i]
s.Encoding = f.Type[i+1:]
}
schemas = append(schemas, s)
}
return schemas
}
func (s Schemas) SetOverlay(o *Overlay) {
for _, s := range s {
s.Overlay = o
s.OverlayID = o.ID
s.OverlayProperty = o.Property
}
}
func (s Schemas) SetMeta(meta Object) {
for _, s := range s {
s.Metadata = meta
}
}
func (s Schemas) Fields() big.Fields {
f := make(big.Fields, 0, len(s))
for _, s := range s {
f = append(f, big.Field{
Key: s.Key,
Type: path.Join(s.Type, s.Encoding),
})
}
return f
}
func (s Schemas) WriteTab(w io.Writer) (int64, error) {
var n int64
m, err := fmt.Fprint(w, "ID\tOVERLAY\tKEY\tTYPE\tENCODING\tMETADATA\n")
if err != nil {
return int64(m), err
}
n += int64(m)
for _, s := range s {
m, err = fmt.Fprintf(w, "%v\t%s\t%s\t%s\t%s\t%s\n",
s.ID,
nonempty(s.Overlay.Ref(), "-"),
s.Key,
s.Type,
nonempty(s.Encoding, "-"),
nonempty(s.Metadata.String(), "-"),
)
n += int64(m)
if err != nil {
return n, err
}
}
return n, nil
}
func (s Schemas) WriteTo(w io.Writer) (int64, error) {
tw := tabwriter.NewWriter(w, 2, 0, 2, ' ', 0)
n, err := s.WriteTab(tw)
if err != nil {
return n, err
}
if err := tw.Flush(); err != nil {
return n, err
}
return n, err
}
func (s Schemas) String() string {
var buf bytes.Buffer
if _, err := s.WriteTo(&buf); err != nil {
panic("unexpected error: " + err.Error())
}
return buf.String()
}