-
Notifications
You must be signed in to change notification settings - Fork 5
/
spec.go
103 lines (81 loc) · 1.56 KB
/
spec.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
package domui
import (
"reflect"
)
type Spec interface {
IsSpec()
}
// combinators
type Specs []Spec
func (_ Specs) IsSpec() {}
type Lazy func() Spec
func (_ Lazy) IsSpec() {}
func If(cond bool, specs ...Spec) Spec {
if cond {
return Specs(specs)
}
return nil
}
func Alt(cond bool, spec1 Spec, spec2 Spec) Spec {
if cond {
return spec1
}
return spec2
}
func For(slice any, fn any) Specs {
sliceValue := reflect.ValueOf(slice)
fnValue := reflect.ValueOf(fn)
var specs Specs
for i := 0; i < sliceValue.Len(); i++ {
elem := sliceValue.Index(i)
ret := fnValue.Call([]reflect.Value{elem})
s := ret[0].Interface()
if s == nil {
continue
}
specs = append(specs, s.(Spec))
}
return specs
}
func Range(slice any, fn any) Specs {
sliceValue := reflect.ValueOf(slice)
fnValue := reflect.ValueOf(fn)
var specs Specs
for i := 0; i < sliceValue.Len(); i++ {
elem := sliceValue.Index(i)
ret := fnValue.Call([]reflect.Value{reflect.ValueOf(i), elem})
s := ret[0].Interface()
if s == nil {
continue
}
specs = append(specs, s.(Spec))
}
return specs
}
// elements
type IDSpec struct {
Value string
}
func (_ IDSpec) IsSpec() {}
func ID(id string) IDSpec {
return IDSpec{
Value: id,
}
}
type ClassesSpec struct {
Classes map[string]bool
}
func (_ ClassesSpec) IsSpec() {}
func Classes(names ...string) ClassesSpec {
m := make(map[string]bool)
for _, name := range names {
m[name] = true
}
return ClassesSpec{
Classes: m,
}
}
var Class = Classes
type FocusSpec struct{}
func (_ FocusSpec) IsSpec() {}
var Focus = FocusSpec{}