-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoken.go
371 lines (307 loc) · 7.34 KB
/
token.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
package petri
import (
"errors"
"fmt"
)
var _ Object = (*TokenSchema)(nil)
var _ Input = (*TokenSchemaInput)(nil)
var _ Update = (*TokenUpdate)(nil)
var _ Filter = (*TokenFilter)(nil)
type TokenType string
type PropertiesInput map[string]Properties
func (p PropertiesInput) Properties() *Properties {
properties := make(map[string]Properties)
for key, value := range p {
properties[key] = value
}
return &Properties{
Type: Obj,
Properties: properties,
}
}
type TokenSchemaInput struct {
Name string
Type TokenType
Properties PropertiesInput
}
func (t *TokenSchemaInput) Object() Object {
obj := &TokenSchema{
ID: ID(),
Name: t.Name,
Type: t.Type,
Properties: t.Properties,
}
return obj
}
func (t *TokenSchemaInput) Kind() Kind {
return TokenObject
}
type TokenUpdate struct {
Name string
Type string
Properties PropertiesInput
Mask TokenMask
}
type TokenMask struct {
Name bool
Type bool
}
type TokenFilter struct {
ID *StringSelector `json:"_id,omitempty"`
Name *StringSelector `json:"name,omitempty"`
Type *StringSelector `json:"type,omitempty"`
Properties *Selector[Properties] `json:"properties,omitempty"`
}
type FloatType struct {
min *float64
max *float64
}
func (f *FloatType) Properties() Properties {
return Properties{
Type: Float,
}
}
func (f *FloatType) String() string {
return "float"
}
func (f *FloatType) IsValid(value interface{}) bool {
_, ok := value.(float64)
return ok
}
type IntegerType struct{}
func (i *IntegerType) Properties() Properties {
return Properties{
Type: Int,
}
}
func (i *IntegerType) String() string {
return "integer"
}
func (i *IntegerType) IsValid(value interface{}) bool {
_, ok := value.(int64)
return ok
}
type StringType struct{}
func (s *StringType) Properties() Properties {
return Properties{
Type: String,
}
}
func (s *StringType) String() string {
return "string"
}
func (s *StringType) IsValid(value interface{}) bool {
_, ok := value.(string)
return ok
}
type BooleanType struct{}
func (b *BooleanType) Properties() Properties {
return Properties{
Type: Boolean,
}
}
func (b *BooleanType) String() string {
return "boolean"
}
func (b *BooleanType) IsValid(value interface{}) bool {
_, ok := value.(bool)
return ok
}
type Properties struct {
Type TokenType `json:"type"`
Properties map[string]Properties `json:"properties,omitempty"`
}
type ObjectType struct {
Props map[string]Properties
}
func (o *ObjectType) Properties() Properties {
return Properties{
Type: Obj,
Properties: o.Props,
}
}
func (o *ObjectType) String() string {
return "object"
}
var (
Float = TokenType("float")
Int = TokenType("integer")
String = TokenType("string")
Boolean = TokenType("boolean")
Obj = TokenType("object")
)
// TokenSchema is a simple struct that describes a token in a Petri net. Petri net operations are
// performed on tokens, and tokens are the only objects that can be placed in a Petri net.
type TokenSchema struct {
// ID is the unique identifier of the token schema.
ID string `json:"_id"`
// Name is the name of the token schema.
Name string `json:"name"`
// Type is the type of the token schema.
Type TokenType `json:"type"`
Properties map[string]Properties `json:"properties,omitempty"`
}
func (t *TokenSchema) PostInit() error {
return nil
}
func (t *TokenSchema) PropertiesJSON() map[string]interface{} {
if t.Properties != nil {
ret := make(map[string]interface{})
for key, value := range t.Properties {
ret[key] = value
}
return ret
}
return nil
}
func (t *TokenSchema) Document() Document {
if t.Properties != nil {
return Document{
"_id": t.ID,
"name": t.Name,
"type": t.Type,
"properties": t.Properties,
}
}
return Document{
"_id": t.ID,
"name": t.Name,
"type": t.Type,
}
}
func (t *TokenSchema) String() string {
return t.Name
}
// Token is an instance of a TokenSchema.
type Token[T interface{}] struct {
// ID is the unique identifier of the token.
ID string `json:"_id"`
// Schema is the schema of the token.
Schema *TokenSchema `json:"schema"`
// Value is the value of the token.
Value T `json:"value"`
}
func (t *Token[T]) String() string {
return fmt.Sprintf("%s(%v)", t.Schema.Name, t.Value)
}
func (t *TokenSchema) Kind() Kind {
return TokenObject
}
func (t *TokenSchema) Identifier() string {
return t.ID
}
func parseTokenType(t string) (TokenType, error) {
switch t {
case "float":
return Float, nil
case "integer":
return Int, nil
case "string":
return String, nil
case "boolean":
return Boolean, nil
case "object":
return Obj, nil
default:
return "", errors.New("invalid token type")
}
}
func (t *TokenSchema) Update(update Update) error {
upd, ok := update.(*TokenUpdate)
if !ok {
return errors.New("invalid update type")
}
if upd.Mask.Type {
tokType, err := parseTokenType(upd.Type)
if err != nil {
return err
}
t.Type = tokType
}
if upd.Mask.Name {
t.Name = upd.Name
}
return nil
}
type InvalidTokenValueError struct {
TokenSchema *TokenSchema
Value interface{}
}
func (e *InvalidTokenValueError) Error() string {
return fmt.Sprintf("invalid value for token %s: %v", e.TokenSchema.Name, e.Value)
}
// NewToken creates a new token from the schema.
func (t *TokenSchema) NewToken(value interface{}) (*Token[interface{}], error) {
return &Token[interface{}]{
ID: ID(),
Schema: t,
Value: value,
}, nil
}
type Handler interface {
Handle(token ...*Token[interface{}]) ([]*Token[interface{}], error)
}
type Generator interface {
Handler
Generate(value ...interface{}) ([]*Token[interface{}], error)
}
type Transformer interface {
Handler
Transform(token ...*Token[interface{}]) ([]*Token[interface{}], error)
}
type Consumer interface {
Handler
Consume(token ...*Token[interface{}]) error
}
type generator struct {
f func(value ...interface{}) ([]*Token[interface{}], error)
}
func (g *generator) Generate(values ...interface{}) ([]*Token[interface{}], error) {
return g.f(values...)
}
func (g *generator) Handle(tokens ...*Token[interface{}]) ([]*Token[interface{}], error) {
return g.Generate()
}
func NewGenerator(f func(value ...interface{}) ([]*Token[interface{}], error)) Generator {
return &generator{
f: f,
}
}
type transformer struct {
f func(token ...*Token[interface{}]) ([]*Token[interface{}], error)
}
func (t *transformer) Handle(token ...*Token[interface{}]) ([]*Token[interface{}], error) {
return t.f(token...)
}
func (t *transformer) Transform(tokens ...*Token[interface{}]) ([]*Token[interface{}], error) {
return t.f(tokens...)
}
func NewTransformer(f func(tokens ...*Token[interface{}]) ([]*Token[interface{}], error)) Transformer {
return &transformer{
f: f,
}
}
type consumer struct {
f func(token ...*Token[interface{}]) error
}
func (c *consumer) Consume(token ...*Token[interface{}]) error {
return c.f(token...)
}
func (c *consumer) Handle(tokens ...*Token[interface{}]) ([]*Token[interface{}], error) {
return nil, c.f(tokens...)
}
func NewConsumer(f func(token ...*Token[interface{}]) error) Consumer {
return &consumer{
f: f,
}
}
func Signal() *TokenSchema {
return &TokenSchema{
ID: ID(),
Name: "Signal",
Type: Int,
}
}
func (t *TokenSchemaInput) IsInput() {}
func (t *TokenUpdate) IsUpdate() {}
func (t *TokenFilter) IsFilter() {}