forked from maxence-charriere/go-app
/
compo.go
193 lines (161 loc) · 4.35 KB
/
compo.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package app
import (
"encoding/json"
"net/url"
"reflect"
"strconv"
"strings"
)
// Compo is the interface that describes a component.
// Must be implemented on a non empty struct pointer.
type Compo interface {
// Render must return HTML 5.
// It supports standard Go html/template API.
// The pipeline is based on the component struct.
// See https://golang.org/pkg/text/template and
// https://golang.org/pkg/html/template for template usage.
Render() string
}
// Mounter is the interface that wraps OnMount method.
type Mounter interface {
Compo
// OnMount is called when a component is mounted.
// App.Render should not be called inside.
OnMount()
}
// Dismounter is the interface that wraps OnDismount method.
type Dismounter interface {
Compo
// OnDismount is called when a component is dismounted.
// App.Render should not be called inside.
OnDismount()
}
// Navigable is the interface that wraps OnNavigate method.
type Navigable interface {
Compo
// OnNavigate is called when a component is navigated to.
OnNavigate(u *url.URL)
}
// EventSubscriber is the interface that describes a component that subscribes
// to events emitted from messages.
type EventSubscriber interface {
// Subscribe is called when a component is mounted.
// The returned subscriber is used to subscribe to events emitted from
// messages.
// All the event subscribed are automatically unsuscribed when the component
// is dismounted.
Subscribe() *Subscriber
}
// CompoWithExtendedRender is the interface that wraps Funcs method.
type CompoWithExtendedRender interface {
Compo
// Funcs returns a map of funcs to use when rendering a component.
// Funcs named raw, json and time are reserved.
// They handle raw html code, json conversions and time format.
// They can't be overloaded.
// See https://golang.org/pkg/text/template/#Template.Funcs for more details.
Funcs() map[string]interface{}
}
// ZeroCompo is the type to use as base for empty components.
// Every instances of an empty struct is given the same memory address, which
// causes problem for indexing components.
// ZeroCompo have a placeholder field to avoid that.
type ZeroCompo struct {
placeholder byte
}
type compo struct {
ID string
ParentID string
Compo Compo
Events *Subscriber
}
func compoName(c Compo) string {
v := reflect.ValueOf(c)
v = reflect.Indirect(v)
name := strings.ToLower(v.Type().String())
return strings.TrimPrefix(name, "main.")
}
func compoNameFromURLString(rawurl string) string {
u, _ := url.Parse(rawurl)
return compoNameFromURL(u)
}
func compoNameFromURL(u *url.URL) string {
p := u.Path
p = strings.TrimPrefix(p, "/")
path := strings.SplitN(p, "/", 2)
if len(path[0]) == 0 {
return ""
}
names := strings.SplitN(path[0], "?", 2)
name := names[0]
name = strings.ToLower(name)
return strings.TrimPrefix(name, "main.")
}
func mapCompoFields(c Compo, fields map[string]string) error {
v := reflect.ValueOf(c).Elem()
t := v.Type()
for i, numfields := 0, t.NumField(); i < numfields; i++ {
fv := v.Field(i)
ft := t.Field(i)
if ft.Anonymous {
continue
}
// Ignore non exported field.
if len(ft.PkgPath) != 0 {
continue
}
name := strings.ToLower(ft.Name)
value, ok := fields[name]
// Remove not set boolean.
if !ok && fv.Kind() == reflect.Bool {
fv.SetBool(false)
continue
} else if !ok {
continue
}
if err := mapCompoField(fv, value); err != nil {
return err
}
}
return nil
}
func mapCompoField(field reflect.Value, value string) error {
switch field.Kind() {
case reflect.String:
field.SetString(value)
case reflect.Bool:
if len(value) == 0 {
value = "true"
}
b, err := strconv.ParseBool(value)
if err != nil {
return err
}
field.SetBool(b)
case reflect.Int, reflect.Int64, reflect.Int32, reflect.Int16, reflect.Int8:
n, err := strconv.ParseInt(value, 0, 64)
if err != nil {
return err
}
field.SetInt(n)
case reflect.Uint, reflect.Uint64, reflect.Uint32, reflect.Uint16, reflect.Uint8, reflect.Uintptr:
n, err := strconv.ParseUint(value, 0, 64)
if err != nil {
return err
}
field.SetUint(n)
case reflect.Float64, reflect.Float32:
n, err := strconv.ParseFloat(value, 64)
if err != nil {
return err
}
field.SetFloat(n)
default:
addr := field.Addr()
i := addr.Interface()
if err := json.Unmarshal([]byte(value), i); err != nil {
return err
}
}
return nil
}