-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathschemaregistry_mock.go
144 lines (129 loc) · 3.95 KB
/
schemaregistry_mock.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
package avro
import "fmt"
// NewNOOPCodecRegistry returns a CodecRegistry that uses the NOOP
// schema registry
func NewNOOPCodecRegistry(subject string) *CodecRegistry {
return &CodecRegistry{
codecByID: make(map[SchemaID]*Codec),
Registry: NewNOOPClient(),
subject: subject,
}
}
// Retrieved from https://github.com/Landoop/schema-registry
type mockSchemaRegistry struct {
SubjectsFn func() (subjects []string, err error)
VersionsFn func(subject string) (versions []int, err error)
RegisterNewSchemaFn func(subject, schema string) (int, error)
IsRegisteredFn func(subject, schema string) (bool, Schema, error)
GetSchemaByIDFn func(id int) (string, error)
GetSchemaBySubjectFn func(subject string, ver int) (Schema, error)
GetLatestSchemaFn func(subject string) (Schema, error)
DeleteSubjectFn func(subject string) (versions []int, err error)
}
func (c *mockSchemaRegistry) Subjects() (subjects []string, err error) {
return c.SubjectsFn()
}
func (c *mockSchemaRegistry) Versions(subject string) (versions []int, err error) {
return c.VersionsFn(subject)
}
func (c *mockSchemaRegistry) RegisterNewSchema(subject, schema string) (int, error) {
return c.RegisterNewSchemaFn(subject, schema)
}
func (c *mockSchemaRegistry) IsRegistered(subject, schema string) (bool, Schema, error) {
return c.IsRegisteredFn(subject, schema)
}
func (c *mockSchemaRegistry) GetSchemaByID(id int) (string, error) {
return c.GetSchemaByIDFn(id)
}
func (c *mockSchemaRegistry) GetSchemaBySubject(subject string, ver int) (Schema, error) {
return c.GetSchemaBySubjectFn(subject, ver)
}
func (c *mockSchemaRegistry) GetLatestSchema(subject string) (Schema, error) {
return c.GetLatestSchemaFn(subject)
}
func (c *mockSchemaRegistry) DeleteSubject(subject string) ([]int, error) {
return c.DeleteSubjectFn(subject)
}
// NewNOOPClient is a mock schema registry which can be used for testing purposes
// nolint
func NewNOOPClient() SchemaRegistry {
var newID int
ptrNewID := &newID
store := make(map[string][]Schema)
return &mockSchemaRegistry{
SubjectsFn: func() (subjects []string, err error) {
var keys []string
for key := range store {
keys = append(keys, key)
}
return keys, nil
},
VersionsFn: func(subject string) (versions []int, err error) {
for _, schemas := range store {
for i := range schemas {
versions = append(versions, i)
}
}
return versions, nil
},
RegisterNewSchemaFn: func(subject, rawSchema string) (int, error) {
var schema Schema
schema.Schema = rawSchema
schema.Subject = subject
schemas, ok := store[subject]
if !ok {
schemas = []Schema{}
}
schema.ID = *ptrNewID
*ptrNewID++
schema.Version = len(schemas)
schemas = append(schemas, schema)
store[subject] = schemas
return schema.ID, nil
},
IsRegisteredFn: func(subject, schema string) (bool, Schema, error) {
schemas, ok := store[subject]
if !ok {
return false, Schema{}, nil
}
for _, s := range schemas {
if schema == s.Schema {
return true, s, nil
}
}
return false, Schema{}, nil
},
GetSchemaByIDFn: func(id int) (string, error) {
for _, schemas := range store {
for _, schema := range schemas {
if schema.ID == id {
return schema.Schema, nil
}
}
}
return "", fmt.Errorf("schema not found")
},
GetSchemaBySubjectFn: func(subject string, ver int) (Schema, error) {
return Schema{}, nil
},
GetLatestSchemaFn: func(subject string) (Schema, error) {
schemas, ok := store[subject]
if !ok {
return Schema{}, fmt.Errorf("subject not found")
}
if len(schemas) == 0 {
return Schema{}, fmt.Errorf("no schema found in this subject")
}
return schemas[len(schemas)-1], nil
},
DeleteSubjectFn: func(subject string) (versions []int, err error) {
for _, schemas := range store {
for i := range schemas {
versions = append(versions, i)
}
}
delete(store, subject)
return versions, nil
},
}
}