/
value.go
72 lines (61 loc) · 1.28 KB
/
value.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
package core
import (
"encoding/json"
)
//revive:disable-next-line redefines-builtin-id
type Type int64
const (
NoneType Type = 0
BooleanType Type = 1
IntType Type = 2
FloatType Type = 3
StringType Type = 4
DateTimeType Type = 5
ArrayType Type = 6
ObjectType Type = 7
HtmlElementType Type = 8
HtmlDocumentType Type = 9
BinaryType Type = 10
)
var typestr = map[Type]string{
NoneType: "none",
BooleanType: "boolean",
IntType: "int",
FloatType: "float",
StringType: "string",
DateTimeType: "datetime",
ArrayType: "array",
ObjectType: "object",
HtmlElementType: "HTMLElement",
HtmlDocumentType: "HTMLDocument",
BinaryType: "BinaryType",
}
func (t Type) String() string {
return typestr[t]
}
type Value interface {
json.Marshaler
Type() Type
String() string
Compare(other Value) int
Unwrap() interface{}
Hash() int
Clone() Value
}
func IsTypeOf(value Value, check Type) bool {
return value.Type() == check
}
func ValidateType(value Value, required ...Type) error {
var valid bool
ct := value.Type()
for _, t := range required {
if ct == t {
valid = true
break
}
}
if !valid {
return TypeError(value.Type(), required...)
}
return nil
}