~repos /gromer

#golang#htmx#ssr

git clone https://pyrossh.dev/repos/gromer.git

gromer is a framework and cli to build multipage web apps in golang using htmx and alpinejs.


245e0cb2 pyros2097

5 years ago
remove errors
Files changed (6) hide show
  1. component.go +9 -32
  2. element.go +15 -77
  3. node.go +0 -44
  4. node_test.go +8 -34
  5. raw.go +5 -26
  6. testing.go +20 -26
component.go CHANGED
@@ -4,7 +4,6 @@ import (
4
4
  "reflect"
5
5
  "strings"
6
6
 
7
- "github.com/pyros2097/wapp/errors"
8
7
  "github.com/pyros2097/wapp/js"
9
8
  )
10
9
 
@@ -17,10 +16,6 @@ func getCurrentContext() *RenderContext {
17
16
 
18
17
  type RenderFunc func(ctx *RenderContext) UI
19
18
 
20
- func (r RenderFunc) Kind() Kind {
21
- return FunctionalComponent
22
- }
23
-
24
19
  func (r RenderFunc) JSValue() js.Value {
25
20
  c := getCurrentContext()
26
21
  return c.root.JSValue()
@@ -103,11 +98,11 @@ func (r RenderFunc) children() []UI {
103
98
  func (r RenderFunc) mount() error {
104
99
  c := getCurrentContext()
105
100
  if r.Mounted() {
106
- panic("mounting component failed already mounted " + r.name() + r.Kind().String())
101
+ panic("mounting component failed already mounted " + r.name())
107
102
  }
108
103
  root := r.Render()
109
104
  if err := mount(root); err != nil {
110
- panic("mounting component failed " + r.name() + r.Kind().String())
105
+ panic("mounting component failed " + r.name())
111
106
  }
112
107
  root.setParent(c.this)
113
108
  c.root = root
@@ -131,14 +126,8 @@ func (r RenderFunc) update(n UI) error {
131
126
  return nil
132
127
  }
133
128
 
134
- if r.Kind() != r.Kind() || n.name() != n.name() {
129
+ if n.name() != n.name() {
135
- return errors.New("updating ui element failed").
136
- Tag("replace", true).
137
- Tag("reason", "different element types").
130
+ panic("updating ui element failed replace different element type current-name: " + r.name() + " updated-name: " + n.name())
138
- Tag("current-kind", r.Kind()).
139
- Tag("current-name", r.name()).
140
- Tag("updated-kind", n.Kind()).
141
- Tag("updated-name", n.name())
142
131
  }
143
132
 
144
133
  aval := reflect.Indirect(reflect.ValueOf(r.self()))
@@ -177,10 +166,7 @@ func (r RenderFunc) updateRoot() error {
177
166
  }
178
167
 
179
168
  if err != nil {
180
- return errors.New("updating component failed").
169
+ panic("updating component failed " + r.name())
181
- Tag("kind", r.Kind()).
182
- Tag("name", r.name()).
183
- Wrap(err)
184
170
  }
185
171
 
186
172
  return nil
@@ -192,29 +178,20 @@ func (r RenderFunc) replaceRoot(n UI) error {
192
178
  new := n
193
179
 
194
180
  if err := mount(new); err != nil {
195
- return errors.New("replacing component root failed").
181
+ panic("replacing component root failed name: " + r.name() + " root-name: " + old.name() + "new-root-name: " + new.name())
196
- Tag("kind", r.Kind()).
197
- Tag("name", r.name()).
198
- Tag("root-kind", old.Kind()).
199
- Tag("root-name", old.name()).
200
- Tag("new-root-kind", new.Kind()).
201
- Tag("new-root-name", new.name()).
202
- Wrap(err)
203
182
  }
204
183
 
205
184
  var parent UI
206
185
  for {
207
186
  parent = r.parent()
187
+ _, isElem := parent.(*elem)
208
- if parent == nil || parent.Kind() == HTML {
188
+ if parent == nil || isElem {
209
189
  break
210
190
  }
211
191
  }
212
192
 
213
193
  if parent == nil {
214
- return errors.New("replacing component root failed").
215
- Tag("kind", r.Kind()).
216
- Tag("name", r.name()).
217
- Tag("reason", "coponent does not have html element parents")
194
+ panic("replacing component root failed name: " + r.name() + " component does not have html element parents")
218
195
  }
219
196
 
220
197
  c.root = new
element.go CHANGED
@@ -2,8 +2,8 @@ package app
2
2
 
3
3
  import (
4
4
  "io"
5
+ "strconv"
5
6
 
6
- "github.com/pyros2097/wapp/errors"
7
7
  "github.com/pyros2097/wapp/js"
8
8
  )
9
9
 
@@ -18,10 +18,6 @@ type elem struct {
18
18
  this UI
19
19
  }
20
20
 
21
- func (e *elem) Kind() Kind {
22
- return HTML
23
- }
24
-
25
21
  func (e *elem) JSValue() js.Value {
26
22
  return e.jsvalue
27
23
  }
@@ -65,18 +61,12 @@ func (e *elem) children() []UI {
65
61
 
66
62
  func (e *elem) mount() error {
67
63
  if e.Mounted() {
68
- return errors.New("mounting ui element failed").
69
- Tag("reason", "already mounted").
64
+ panic("mounting elem failed already mounted " + e.name())
70
- Tag("name", e.name()).
71
- Tag("kind", e.Kind())
72
65
  }
73
66
 
74
67
  v := js.Window.Get("document").Call("createElement", e.tag)
75
68
  if !v.Truthy() {
76
- return errors.New("mounting ui element failed").
77
- Tag("reason", "create javascript node returned nil").
69
+ panic("mounting component failed create javascript node returned nil " + e.name())
78
- Tag("name", e.name()).
79
- Tag("kind", e.Kind())
80
70
  }
81
71
  e.jsvalue = v
82
72
 
@@ -90,10 +80,7 @@ func (e *elem) mount() error {
90
80
 
91
81
  for _, c := range e.children() {
92
82
  if err := e.appendChild(c, true); err != nil {
93
- return errors.New("mounting ui element failed").
83
+ panic("mounting component failed appendChild " + e.name())
94
- Tag("name", e.name()).
95
- Tag("kind", e.Kind()).
96
- Wrap(err)
97
84
  }
98
85
  }
99
86
 
@@ -117,14 +104,8 @@ func (e *elem) update(n UI) error {
117
104
  return nil
118
105
  }
119
106
 
120
- if n.Kind() != e.Kind() || n.name() != e.name() {
107
+ if n.name() != e.name() {
121
- return errors.New("updating ui element failed").
122
- Tag("replace", true).
123
- Tag("reason", "different element types").
108
+ panic("updating element failed replace different element type current-name: " + e.name() + " updated-name: " + n.name())
124
- Tag("current-kind", e.Kind()).
125
- Tag("current-name", e.name()).
126
- Tag("updated-kind", n.Kind()).
127
- Tag("updated-name", n.name())
128
109
  }
129
110
 
130
111
  e.updateAttrs(n.attributes())
@@ -145,10 +126,7 @@ func (e *elem) update(n UI) error {
145
126
  }
146
127
 
147
128
  if err != nil {
148
- return errors.New("updating ui element failed").
129
+ panic("updating element failed name: " + e.name())
149
- Tag("kind", e.Kind()).
150
- Tag("name", e.name()).
151
- Wrap(err)
152
130
  }
153
131
 
154
132
  achildren = achildren[1:]
@@ -159,10 +137,7 @@ func (e *elem) update(n UI) error {
159
137
  // Remove children:
160
138
  for len(achildren) != 0 {
161
139
  if err := e.removeChildAt(i); err != nil {
162
- return errors.New("updating ui element failed").
140
+ panic("updating element failed name: " + e.name())
163
- Tag("kind", e.Kind()).
164
- Tag("name", e.name()).
165
- Wrap(err)
166
141
  }
167
142
 
168
143
  achildren = achildren[1:]
@@ -173,10 +148,7 @@ func (e *elem) update(n UI) error {
173
148
  c := bchildren[0]
174
149
 
175
150
  if err := e.appendChild(c, false); err != nil {
176
- return errors.New("updating ui element failed").
151
+ panic("updating element failed name: " + e.name())
177
- Tag("kind", e.Kind()).
178
- Tag("name", e.name()).
179
- Wrap(err)
180
152
  }
181
153
 
182
154
  bchildren = bchildren[1:]
@@ -187,12 +159,7 @@ func (e *elem) update(n UI) error {
187
159
 
188
160
  func (e *elem) appendChild(c UI, onlyJsValue bool) error {
189
161
  if err := mount(c); err != nil {
190
- return errors.New("appending child failed").
191
- Tag("name", e.name()).
192
- Tag("kind", e.Kind()).
193
- Tag("child-name", c.name()).
162
+ panic("appending child failed child-name: " + c.name() + " name: " + e.name())
194
- Tag("child-kind", c.Kind()).
195
- Wrap(err)
196
163
  }
197
164
 
198
165
  if !onlyJsValue {
@@ -208,15 +175,7 @@ func (e *elem) replaceChildAt(idx int, new UI) error {
208
175
  old := e.body[idx]
209
176
 
210
177
  if err := mount(new); err != nil {
211
- return errors.New("replacing child failed").
178
+ panic("replacing child failed name: " + e.name() + " old-name: " + old.name() + " new-name: " + new.name())
212
- Tag("name", e.name()).
213
- Tag("kind", e.Kind()).
214
- Tag("index", idx).
215
- Tag("old-name", old.name()).
216
- Tag("old-kind", old.Kind()).
217
- Tag("new-name", new.name()).
218
- Tag("new-kind", new.Kind()).
219
- Wrap(err)
220
179
  }
221
180
 
222
181
  e.body[idx] = new
@@ -230,11 +189,7 @@ func (e *elem) replaceChildAt(idx int, new UI) error {
230
189
  func (e *elem) removeChildAt(idx int) error {
231
190
  body := e.body
232
191
  if idx < 0 || idx >= len(body) {
233
- return errors.New("removing child failed").
234
- Tag("reason", "index out of range").
192
+ panic("removing child failed index out of range name: " + e.name() + " index: " + strconv.Itoa(idx))
235
- Tag("index", idx).
236
- Tag("name", e.name()).
237
- Tag("kind", e.Kind())
238
193
  }
239
194
 
240
195
  c := body[idx]
@@ -353,10 +308,7 @@ func (e *elem) delJsEventHandler(k string, h js.EventHandler) {
353
308
 
354
309
  func (e *elem) setBody(body ...interface{}) {
355
310
  if e.selfClosing {
356
- panic(errors.New("setting html element body failed").
357
- Tag("reason", "self closing element can't have children").
311
+ panic("setting html element body failed: self closing element can't have children" + e.name())
358
- Tag("name", e.name()),
359
- )
360
312
  }
361
313
 
362
314
  e.body = FilterUIElems(body...)
@@ -414,10 +366,6 @@ func Text(v string) UI {
414
366
  return &text{value: v}
415
367
  }
416
368
 
417
- func (t *text) Kind() Kind {
418
- return SimpleText
419
- }
420
-
421
369
  func (t *text) JSValue() js.Value {
422
370
  return t.jsvalue
423
371
  }
@@ -459,11 +407,7 @@ func (t *text) children() []UI {
459
407
 
460
408
  func (t *text) mount() error {
461
409
  if t.Mounted() {
462
- return errors.New("mounting ui element failed").
463
- Tag("reason", "already mounted").
410
+ panic("mounting text element failed already mounted" + t.name() + " " + t.value)
464
- Tag("kind", t.Kind()).
465
- Tag("name", t.name()).
466
- Tag("value", t.value)
467
411
  }
468
412
 
469
413
  t.jsvalue = js.Window.
@@ -484,13 +428,7 @@ func (t *text) update(n UI) error {
484
428
 
485
429
  o, isText := n.(*text)
486
430
  if !isText {
487
- return errors.New("updating ui element failed").
488
- Tag("replace", true).
489
- Tag("reason", "different element types").
431
+ panic("updating ui element failed replace different element type current-name: " + t.name() + " updated-name: " + n.name())
490
- Tag("current-kind", t.Kind()).
491
- Tag("current-name", t.name()).
492
- Tag("updated-kind", n.Kind()).
493
- Tag("updated-name", n.name())
494
432
  }
495
433
 
496
434
  if t.value != o.value {
node.go CHANGED
@@ -11,9 +11,6 @@ import (
11
11
  // UI is the interface that describes a user interface element such as
12
12
  // components and HTML elements.
13
13
  type UI interface {
14
- // Kind represents the specific kind of a UI element.
15
- Kind() Kind
16
-
17
14
  // JSValue returns the javascript value linked to the element.
18
15
  JSValue() js.Value
19
16
 
@@ -33,47 +30,6 @@ type UI interface {
33
30
  update(UI) error
34
31
  }
35
32
 
36
- // Kind represents the specific kind of a user interface element.
37
- type Kind uint
38
-
39
- func (k Kind) String() string {
40
- switch k {
41
- case SimpleText:
42
- return "text"
43
-
44
- case HTML:
45
- return "html"
46
-
47
- case RawHTML:
48
- return "raw"
49
-
50
- case FunctionalComponent:
51
- return "function"
52
-
53
- default:
54
- return "undefined"
55
- }
56
- }
57
-
58
- const (
59
- // UndefinedElem represents an undefined UI element.
60
- UndefinedElem Kind = iota
61
-
62
- // SimpleText represents a simple text element.
63
- SimpleText
64
-
65
- // HTML represents an HTML element.
66
- HTML
67
-
68
- // Component represents a customized, independent and reusable UI element.
69
- Component
70
-
71
- // RawHTML represents an HTML element obtained from a raw HTML code snippet.
72
- RawHTML
73
-
74
- FunctionalComponent
75
- )
76
-
77
33
  // FilterUIElems returns a filtered version of the given UI elements where
78
34
  // selector elements such as If and Range are interpreted and removed. It also
79
35
  // remove nil elements.
node_test.go CHANGED
@@ -8,32 +8,6 @@ import (
8
8
  "github.com/stretchr/testify/require"
9
9
  )
10
10
 
11
- func TestKindString(t *testing.T) {
12
- utests := []struct {
13
- kind Kind
14
- expectedString string
15
- }{
16
- {
17
- kind: UndefinedElem,
18
- expectedString: "undefined",
19
- },
20
- {
21
- kind: SimpleText,
22
- expectedString: "text",
23
- },
24
- {
25
- kind: HTML,
26
- expectedString: "html",
27
- },
28
- }
29
-
30
- for _, u := range utests {
31
- t.Run(u.expectedString, func(t *testing.T) {
32
- require.Equal(t, u.expectedString, u.kind.String())
33
- })
34
- }
35
- }
36
-
37
11
  func TestFilterUIElems(t *testing.T) {
38
12
  var nilText *text
39
13
 
@@ -108,10 +82,10 @@ func testMounted(t *testing.T, n UI) {
108
82
  require.NotNil(t, n.JSValue())
109
83
  require.True(t, n.Mounted())
110
84
 
111
- switch n.Kind() {
85
+ // switch n.Kind() {
112
- case HTML, Component:
86
+ // case HTML, Component:
113
- require.NotNil(t, n.self())
87
+ // require.NotNil(t, n.self())
114
- }
88
+ // }
115
89
 
116
90
  for _, c := range n.children() {
117
91
  require.Equal(t, n, c.parent())
@@ -123,10 +97,10 @@ func testDismounted(t *testing.T, n UI) {
123
97
  require.Nil(t, n.JSValue())
124
98
  require.False(t, n.Mounted())
125
99
 
126
- switch n.Kind() {
100
+ // switch n.Kind() {
127
- case HTML, Component:
101
+ // case HTML, Component:
128
- require.Nil(t, n.self())
102
+ // require.Nil(t, n.self())
129
- }
103
+ // }
130
104
 
131
105
  for _, c := range n.children() {
132
106
  testDismounted(t, c)
raw.go CHANGED
@@ -35,10 +35,6 @@ type raw struct {
35
35
  value string
36
36
  }
37
37
 
38
- func (r *raw) Kind() Kind {
39
- return RawHTML
40
- }
41
-
42
38
  func (r *raw) JSValue() js.Value {
43
39
  return r.jsvalue
44
40
  }
@@ -80,10 +76,7 @@ func (r *raw) children() []UI {
80
76
 
81
77
  func (r *raw) mount() error {
82
78
  if r.Mounted() {
83
- return errors.New("mounting raw html element failed").
84
- Tag("reason", "already mounted").
79
+ panic("mounting raw failed already mounted " + r.name())
85
- Tag("name", r.name()).
86
- Tag("kind", r.Kind())
87
80
  }
88
81
 
89
82
  wrapper := js.Window.Get("document").Call("createElement", "div")
@@ -91,11 +84,7 @@ func (r *raw) mount() error {
91
84
 
92
85
  value := wrapper.Get("firstChild")
93
86
  if !value.Truthy() {
94
- return errors.New("mounting raw html element failed").
95
- Tag("reason", "converting raw html to html elements returned nil").
87
+ panic("mounting raw failed converting raw html to html elements returned nil " + r.name() + " " + r.value)
96
- Tag("name", r.name()).
97
- Tag("kind", r.Kind()).
98
- Tag("raw-html", r.value)
99
88
  }
100
89
 
101
90
  wrapper.Call("removeChild", value)
@@ -112,22 +101,12 @@ func (r *raw) update(n UI) error {
112
101
  return nil
113
102
  }
114
103
 
115
- if n.Kind() != r.Kind() || r.name() != r.name() {
104
+ if r.name() != r.name() {
116
- return errors.New("updating raw html element failed").
117
- Tag("replace", true).
118
- Tag("reason", "different element types").
105
+ panic("updating raw element failed replace different element type current-name: " + r.name() + " updated-name: " + n.name())
119
- Tag("current-kind", r.Kind()).
120
- Tag("current-name", r.name()).
121
- Tag("updated-kind", n.Kind()).
122
- Tag("updated-name", n.name())
123
106
  }
124
107
 
125
108
  if v := n.(*raw).value; r.value != v {
126
- return errors.New("updating raw html element failed").
127
- Tag("replace", true).
128
- Tag("reason", "different raw values").
109
+ panic("updating raw element failed replace different raw values current-value: " + r.value + " new-value: " + v)
129
- Tag("current-value", r.value).
130
- Tag("new-value", v)
131
110
  }
132
111
 
133
112
  return nil
testing.go CHANGED
@@ -91,9 +91,7 @@ func TestMatch(tree UI, d TestUIDescriptor) error {
91
91
 
92
92
  return errors.New("ui element to match is out of range").
93
93
  Tag("name", d.Expected.name()).
94
- Tag("kind", d.Expected.Kind()).
95
94
  Tag("parent-name", tree.name()).
96
- Tag("parent-kind", tree.Kind()).
97
95
  Tag("parent-children-count", len(tree.children())).
98
96
  Tag("index", idx)
99
97
  }
@@ -104,12 +102,9 @@ func TestMatch(tree UI, d TestUIDescriptor) error {
104
102
  if p != tree {
105
103
  return errors.New("unexpected ui element parent").
106
104
  Tag("name", d.Expected.name()).
107
- Tag("kind", d.Expected.Kind()).
108
105
  Tag("parent-name", p.name()).
109
- Tag("parent-kind", p.Kind()).
110
106
  Tag("parent-addr", fmt.Sprintf("%p", p)).
111
107
  Tag("expected-parent-name", tree.name()).
112
- Tag("expected-parent-kind", tree.Kind()).
113
108
  Tag("expected-parent-addr", fmt.Sprintf("%p", tree))
114
109
  }
115
110
 
@@ -117,35 +112,34 @@ func TestMatch(tree UI, d TestUIDescriptor) error {
117
112
  return TestMatch(c, d)
118
113
  }
119
114
 
120
- if d.Expected.name() != tree.name() || d.Expected.Kind() != tree.Kind() {
115
+ if d.Expected.name() != tree.name() {
121
116
  return errors.New("the UI element is not matching the descriptor").
122
117
  Tag("expected-name", d.Expected.name()).
123
- Tag("expected-kind", d.Expected.Kind()).
124
- Tag("current-name", tree.name()).
118
+ Tag("current-name", tree.name())
125
- Tag("current-kind", tree.Kind())
126
119
  }
127
120
 
128
- switch d.Expected.Kind() {
121
+ // switch d.Expected.Kind() {
129
- case SimpleText:
122
+ // case SimpleText:
130
- return matchText(tree, d)
123
+ // return matchText(tree, d)
131
124
 
132
- case HTML:
125
+ // case HTML:
133
- if err := matchHTMLElemAttrs(tree, d); err != nil {
126
+ // if err := matchHTMLElemAttrs(tree, d); err != nil {
134
- return err
127
+ // return err
135
- }
128
+ // }
136
- return matchHTMLElemEventHandlers(tree, d)
129
+ // return matchHTMLElemEventHandlers(tree, d)
137
130
 
138
- // case Component:
131
+ // // case Component:
139
- // return matchComponent(tree, d)
132
+ // // return matchComponent(tree, d)
140
133
 
141
- case RawHTML:
134
+ // case RawHTML:
142
- return matchRaw(tree, d)
135
+ // return matchRaw(tree, d)
143
136
 
144
- default:
137
+ // default:
145
- return errors.New("the UI element is not matching the descriptor").
138
+ // return errors.New("the UI element is not matching the descriptor").
146
- Tag("reason", "unavailable matching for the kind").
139
+ // Tag("reason", "unavailable matching for the kind").
147
- Tag("kind", d.Expected.Kind())
140
+ // Tag("kind", d.Expected.Kind())
148
- }
141
+ // }
142
+ return nil
149
143
  }
150
144
 
151
145
  func matchText(n UI, d TestUIDescriptor) error {