-
Notifications
You must be signed in to change notification settings - Fork 20
/
range.go
104 lines (87 loc) · 2.5 KB
/
range.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
package tosca_v2_0
import (
"math"
"github.com/tliron/kutil/ard"
"github.com/tliron/puccini/tosca"
)
//
// Range
//
// [TOSCA-v2.0] @ ?
// [TOSCA-Simple-Profile-YAML-v1.3] @ 3.3.3
// [TOSCA-Simple-Profile-YAML-v1.2] @ 3.3.3
// [TOSCA-Simple-Profile-YAML-v1.1] @ 3.2.3
// [TOSCA-Simple-Profile-YAML-v1.0] @ 3.2.3
//
type Range struct {
Lower uint64 `json:"lower" yaml:"lower"`
Upper uint64 `json:"upper" yaml:"upper"`
}
// tosca.Reader signature
func ReadRange(context *tosca.Context) tosca.EntityPtr {
var self Range
if !context.ValidateType(ard.TypeList) {
return &self
}
list := context.Data.(ard.List)
if len(list) != 2 {
context.ReportValueMalformed("range", "list length not 2")
return &self
}
lowerContext := context.ListChild(0, list[0])
lowerOk := false
if lowerContext.ValidateType(ard.TypeInteger) {
lowerInt := *lowerContext.ReadInteger()
if lowerInt < 0 {
context.ReportValueMalformed("range", "lower bound negative")
} else {
self.Lower = uint64(lowerInt)
lowerOk = true
}
}
upperContext := context.ListChild(1, list[1])
upperOk := false
if upperContext.ValidateType(ard.TypeInteger, ard.TypeString) {
if upperContext.Is(ard.TypeInteger) {
upperInt := *upperContext.ReadInteger()
if upperInt < 0 {
context.ReportValueMalformed("range", "upper bound negative")
} else {
self.Upper = uint64(upperInt)
upperOk = true
}
} else if upperContext.Data.(string) == "UNBOUNDED" {
self.Upper = math.MaxUint64
upperOk = true
} else {
context.ReportValueMalformed("range", "upper bound string not UNBOUNDED")
}
}
// The TOSCA spec in 1.0 to 1.2 says that the upper bound *must* be greater than the lower
// bound but that makes no sense to us (it would mean than ranges must include at least two
// numbers) so we are ignoring that and assuming that upper must be >= lower
// (The spec was fixed in 1.3)
if upperOk && lowerOk && (self.Upper < self.Lower) {
context.ReportValueMalformed("range", "upper bound lower than lower bound")
}
return &self
}
func (self *Range) InRange(number uint64) bool {
return (number >= self.Lower) && (number <= self.Upper)
}
//
// RangeEntity
//
type RangeEntity struct {
*Entity `name:"range"`
Range *Range `traverse:"ignore"`
}
func NewRangeEntity(context *tosca.Context) *RangeEntity {
return &RangeEntity{Entity: NewEntity(context)}
}
// tosca.Reader signature
func ReadRangeEntity(context *tosca.Context) tosca.EntityPtr {
self := NewRangeEntity(context)
self.Range = ReadRange(context).(*Range)
return self
}