-
-
Notifications
You must be signed in to change notification settings - Fork 366
/
range.go
176 lines (137 loc) · 3.38 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package app
import (
"context"
"io"
"net/url"
"reflect"
"sort"
"github.com/maxence-charriere/go-app/v9/pkg/errors"
)
// RangeLoop represents a control structure that iterates within a slice, an
// array or a map.
type RangeLoop interface {
UI
// Slice sets the loop content by repeating the given function for the
// number of elements in the source.
//
// It panics if the range source is not a slice or an array.
Slice(f func(int) UI) RangeLoop
// Map sets the loop content by repeating the given function for the number
// of elements in the source. Elements are ordered by keys.
//
// It panics if the range source is not a map or if map keys are not strings.
Map(f func(string) UI) RangeLoop
}
// Range returns a range loop that iterates within the given source. Source must
// be a slice, an array or a map with strings as keys.
func Range(src interface{}) RangeLoop {
return rangeLoop{source: src}
}
type rangeLoop struct {
body []UI
source interface{}
}
func (r rangeLoop) Slice(f func(int) UI) RangeLoop {
src := reflect.ValueOf(r.source)
if src.Kind() != reflect.Slice && src.Kind() != reflect.Array {
panic(errors.New("range loop source is not a slice or array").
Tag("src-type", src.Type),
)
}
body := make([]UI, 0, src.Len())
for i := 0; i < src.Len(); i++ {
body = append(body, FilterUIElems(f(i))...)
}
r.body = body
return r
}
func (r rangeLoop) Map(f func(string) UI) RangeLoop {
src := reflect.ValueOf(r.source)
if src.Kind() != reflect.Map {
panic(errors.New("range loop source is not a map").
Tag("src-type", src.Type),
)
}
if keyType := src.Type().Key(); keyType.Kind() != reflect.String {
panic(errors.New("range loop source keys are not strings").
Tag("src-type", src.Type).
Tag("key-type", keyType),
)
}
body := make([]UI, 0, src.Len())
keys := make([]string, 0, src.Len())
for _, k := range src.MapKeys() {
keys = append(keys, k.String())
}
sort.Strings(keys)
for _, k := range keys {
body = append(body, FilterUIElems(f(k))...)
}
r.body = body
return r
}
func (r rangeLoop) Kind() Kind {
return Selector
}
func (r rangeLoop) JSValue() Value {
return nil
}
func (r rangeLoop) Mounted() bool {
return false
}
func (r rangeLoop) name() string {
return "range"
}
func (r rangeLoop) self() UI {
return r
}
func (r rangeLoop) setSelf(UI) {
}
func (r rangeLoop) context() context.Context {
return nil
}
func (r rangeLoop) dispatcher() Dispatcher {
return nil
}
func (r rangeLoop) attributes() map[string]string {
return nil
}
func (r rangeLoop) eventHandlers() map[string]eventHandler {
return nil
}
func (r rangeLoop) parent() UI {
return nil
}
func (r rangeLoop) setParent(UI) {
}
func (r rangeLoop) children() []UI {
return r.body
}
func (r rangeLoop) mount(Dispatcher) error {
return errors.New("range loop is not mountable").
Tag("name", r.name()).
Tag("kind", r.Kind())
}
func (r rangeLoop) dismount() {
}
func (r rangeLoop) update(UI) error {
return errors.New("range loop cannot be updated").
Tag("name", r.name()).
Tag("kind", r.Kind())
}
func (r rangeLoop) onNav(*url.URL) {
}
func (r rangeLoop) onAppUpdate() {
}
func (r rangeLoop) onAppInstallChange() {
}
func (r rangeLoop) onResize() {
}
func (r rangeLoop) preRender(Page) {
}
func (r rangeLoop) html(w io.Writer) {
panic("should not be called")
}
func (r rangeLoop) htmlWithIndent(w io.Writer, indent int) {
panic("should not be called")
}