/
topic.go
94 lines (81 loc) · 2.32 KB
/
topic.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
package datahub
import (
"encoding/json"
"fmt"
)
type Field struct {
Name string `json:"name"`
Type FieldType `json:"type"`
AllowNull bool `json:"notnull"`
Comment string `json:"comment"`
}
// RecordSchema
type RecordSchema struct {
Fields []Field `json:"fields"`
}
// NewRecordSchema create a new record schema for tuple record
func NewRecordSchema() *RecordSchema {
return &RecordSchema{
Fields: make([]Field, 0, 0),
}
}
func NewRecordSchemaFromJson(SchemaJson string) (recordSchema *RecordSchema, err error) {
recordSchema = &RecordSchema{}
if err = json.Unmarshal([]byte(SchemaJson), recordSchema); err != nil {
return
}
for _, v := range recordSchema.Fields {
if !validateFieldType(v.Type) {
panic(fmt.Sprintf("field type %q illegal", v.Type))
}
}
return
}
func (rs *RecordSchema) String() string {
type FieldHelper struct {
Name string `json:"name"`
Type FieldType `json:"type"`
NotNull bool `json:"notnull,omitempty"`
Comment string `json:"comment,omitempty"`
}
fields := make([]FieldHelper, 0, rs.Size())
for _, field := range rs.Fields {
tmpField := FieldHelper{field.Name, field.Type, !field.AllowNull, field.Comment}
fields = append(fields, tmpField)
}
tmpSchema := struct {
Fields []FieldHelper `json:"fields"`
}{fields}
buf, _ := json.Marshal(tmpSchema)
return string(buf)
}
// AddField add a field
func (rs *RecordSchema) AddField(f Field) *RecordSchema {
if !validateFieldType(f.Type) {
panic(fmt.Sprintf("field type %q illegal", f.Type))
}
for _, v := range rs.Fields {
if v.Name == f.Name {
panic(fmt.Sprintf("field %q duplicated", f.Name))
}
}
rs.Fields = append(rs.Fields, f)
return rs
}
// GetFieldIndex get index of given field
func (rs *RecordSchema) GetFieldIndex(fname string) int {
for idx, v := range rs.Fields {
if fname == v.Name {
return idx
}
}
return -1
}
// Size get record schema fields size
func (rs *RecordSchema) Size() int {
return len(rs.Fields)
}
type RecordSchemaInfo struct {
VersionId int `json:"VersionId"`
RecordSchema RecordSchema `json:"RecordSchema"`
}