-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.go
83 lines (68 loc) · 1.23 KB
/
model.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
package schema
//go:generate go run ../../cmd/mkunion/main.go
//go:tag mkunion:"Schema"
type (
None struct{}
Bool bool
Number float64
String string
Binary []byte
List []Schema
Map map[string]Schema
)
//go:tag serde:"json"
type Field struct {
Name string
Value Schema
}
var none = &None{}
func MkNone() *None {
return none
}
func IsNone(x Schema) bool {
_, ok := x.(*None)
return ok
}
func MkBool(b bool) *Bool {
return (*Bool)(&b)
}
func MkInt(x int64) *Number {
v := float64(x)
return (*Number)(&v)
}
func MkUint(x uint64) *Number {
v := float64(x)
return (*Number)(&v)
}
func MkFloat(x float64) *Number {
return (*Number)(&x)
}
func MkBinary(b []byte) *Binary {
v := Binary(b)
return &v
}
func MkString(s string) *String {
return (*String)(&s)
}
func MkList(items ...Schema) *List {
result := make(List, len(items))
copy(result, items)
return &result
}
func MkMap(fields ...Field) *Map {
var result = make(Map)
for _, field := range fields {
result[field.Name] = field.Value
}
return &result
}
func MkField(name string, value Schema) Field {
return Field{
Name: name,
Value: value,
}
}
func AppendList(list *List, items ...Schema) *List {
result := append(*list, items...)
return &result
}