-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathschema.go
154 lines (124 loc) · 3.62 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
150
151
152
153
154
package memphis
import (
"encoding/json"
"errors"
"fmt"
"os"
"regexp"
"strings"
)
const (
schemaObjectName = "Schema"
)
type Schema struct {
Name string `json:"name"`
Type string `json:"type"`
CreatedByUsername string `json:"created_by_username"`
SchemaContent string `json:"schema_content"`
MessageStructName string `json:"message_struct_name"`
}
type createSchemaReq struct {
Name string `json:"name"`
Type string `json:"type"`
CreatedByUsername string `json:"created_by_username"`
SchemaContent string `json:"schema_content"`
MessageStructName string `json:"message_struct_name"`
}
type createSchemaResp struct {
Err string `json:"error"`
}
type removeSchemaReq struct {
Name string `json:"name"`
}
func (s *Schema) getCreationSubject() string {
return "$memphis_schema_creations"
}
func (s *Schema) getDestructionSubject() string {
return ""
}
func (s *Schema) getCreationReq() any {
return createSchemaReq{
Name: s.Name,
Type: s.Type,
CreatedByUsername: s.CreatedByUsername,
SchemaContent: s.SchemaContent,
}
}
func (s *Schema) handleCreationResp(resp []byte) error {
cr := &createSchemaResp{}
err := json.Unmarshal(resp, cr)
if err != nil {
return defaultHandleCreationResp(resp)
}
if cr.Err != "" {
return memphisError(errors.New(cr.Err))
}
return nil
}
func (s *Schema) getDestructionReq() any {
return nil
}
// CreateSchema - validates and uploads a new schema to the Broker. In case schema is already exist a new version will be created
func (c *Conn) CreateSchema(name, schemaType, path string, options ...RequestOpt) error {
data, err := os.ReadFile(path)
if err != nil {
return memphisError(err)
}
schemaContent := string(data)
err = validateSchemaName(name)
if err != nil {
return memphisError(err)
}
err = validateSchemaType(schemaType)
if err != nil {
return memphisError(err)
}
s := Schema{
Name: name,
Type: schemaType,
CreatedByUsername: c.username,
SchemaContent: schemaContent,
MessageStructName: "",
}
if err = c.create(&s, options...); err != nil && !strings.Contains(err.Error(), "already exists") {
return memphisError(err)
}
return nil
}
func validateSchemaName(schemaName string) error {
return validateName(schemaName, schemaObjectName)
}
func validateName(name, objectType string) error {
emptyErrStr := fmt.Sprintf("%v name can not be empty", objectType)
tooLongErrStr := fmt.Sprintf("%v should be under 128 characters", objectType)
invalidCharErrStr := fmt.Sprintf("Only alphanumeric and the '_', '-', '.' characters are allowed in %v", objectType)
firstLetterErrStr := fmt.Sprintf("%v name can not start or end with non alphanumeric character", objectType)
emptyErr := errors.New(emptyErrStr)
tooLongErr := errors.New(tooLongErrStr)
invalidCharErr := errors.New(invalidCharErrStr)
firstLetterErr := errors.New(firstLetterErrStr)
if len(name) == 0 {
return emptyErr
}
if len(name) > 128 {
return tooLongErr
}
re := regexp.MustCompile("^[a-z0-9_.-]*$")
validName := re.MatchString(name)
if !validName {
return invalidCharErr
}
if name[0:1] == "." || name[0:1] == "-" || name[0:1] == "_" || name[len(name)-1:] == "." || name[len(name)-1:] == "-" || name[len(name)-1:] == "_" {
return firstLetterErr
}
return nil
}
func validateSchemaType(schemaType string) error {
invalidTypeErrStr := "unsupported schema type"
invalidTypeErr := errors.New(invalidTypeErrStr)
if schemaType == "protobuf" || schemaType == "json" || schemaType == "graphql" || schemaType == "avro" {
return nil
} else {
return invalidTypeErr
}
}