forked from contiv/libovsdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema.go
63 lines (57 loc) · 1.49 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
package libovsdb
import (
"fmt"
"io"
)
type DatabaseSchema struct {
Name string `json:"name"`
Version string `json:"version"`
Tables map[string]TableSchema `json:"tables"`
}
type TableSchema struct {
Columns map[string]ColumnSchema `json:"columns"`
Indexes [][]string `json:"indexes,omitempty"`
}
type ColumnSchema struct {
Name string `json:"name"`
Type interface{} `json:"type"`
Ephemeral bool `json:"ephemeral,omitempty"`
Mutable bool `json:"mutable,omitempty"`
}
func (schema DatabaseSchema) Print(w io.Writer) {
fmt.Fprintf(w, "%s, (%s)\n", schema.Name, schema.Version)
for table, tableSchema := range schema.Tables {
fmt.Fprintf(w, "\t %s\n", table)
for column, columnSchema := range tableSchema.Columns {
fmt.Fprintf(w, "\t\t %s => %v\n", column, columnSchema)
}
}
}
// Basic validation for operations against Database Schema
func (schema DatabaseSchema) validateOperations(operations ...Operation) bool {
for _, op := range operations {
table, ok := schema.Tables[op.Table]
if ok {
for column, _ := range op.Row {
if _, ok := table.Columns[column]; !ok {
return false
}
}
for _, row := range op.Rows {
for column, _ := range row {
if _, ok := table.Columns[column]; !ok {
return false
}
}
}
for _, column := range op.Columns {
if _, ok := table.Columns[column]; !ok {
return false
}
}
} else {
return false
}
}
return true
}