/
optional.go
57 lines (49 loc) · 1.21 KB
/
optional.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
package monolith
type SemanticIntConsumerOptionalPart struct {
Name string
Condition Condition
Item BytesPart
Cached *BytesPart
}
func (part *SemanticIntConsumerOptionalPart) Fix(n int) *BytesPart {
if part.Condition.Evaluate(n) {
return &part.Item
} else {
return nil
}
}
func (part *SemanticIntConsumerOptionalPart) Parse(buffer *Buffer, args *Args, context *Context) {
if buffer.Empty() {
return
}
if n, ok := context.GetInt(part.Name); ok {
part.Cached = part.Fix(n)
part.Cached.Parse(buffer, args, context)
}
}
func (part *SemanticIntConsumerOptionalPart) Validate(buffer *Buffer, context *Context) Validity {
if buffer.Empty() {
return Invalid
}
if n, ok := context.GetInt(part.Name); ok {
part.Cached = part.Fix(n)
return part.Cached.Validate(buffer, context)
} else {
return Invalid
}
}
func (part *SemanticIntConsumerOptionalPart) MessageFromArgs(args *Args, context *Context) Message {
if n, ok := context.GetInt(part.Name); ok {
part.Cached = part.Fix(n)
return part.Cached.MessageFromArgs(args, context)
} else {
return nil
}
}
func (part *SemanticIntConsumerOptionalPart) Count() int {
if part.Cached != nil {
return part.Cached.Count()
} else {
return 0
}
}