gromer

#golang#htmx#ssr

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

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


c24dcb4 — Peter John 2022-07-02T16:32:06+05:30
imrpove parser

Generated files not shown: go.sum

Files changed (4) hide show
  1. go.mod +2 -0
  2. gsx/gsx.go +5 -1
  3. gsx/gsx_test.go +31 -19
  4. gsx/parser.go +0 -18
go.mod CHANGED
@@ -46,6 +46,7 @@ require (
46
46
  github.com/pkg/errors v0.9.1 // indirect
47
47
  github.com/pmezard/go-difflib v1.0.0 // indirect
48
48
  github.com/russross/blackfriday v1.5.2 // indirect
49
+ github.com/samber/lo v1.21.0 // indirect
49
50
  github.com/sergi/go-diff v1.2.0 // indirect
50
51
  github.com/shurcooL/github_flavored_markdown v0.0.0-20210228213109-c3a9aa474629 // indirect
51
52
  github.com/shurcooL/go v0.0.0-20200502201357-93f07166e636 // indirect
@@ -58,6 +59,7 @@ require (
58
59
  github.com/sourcegraph/syntaxhighlight v0.0.0-20170531221838-bd320f5d308e // indirect
59
60
  go.opencensus.io v0.23.0 // indirect
60
61
  golang.org/x/crypto v0.0.0-20211215165025-cf75a172585e // indirect
62
+ golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17 // indirect
61
63
  golang.org/x/net v0.0.0-20211216030914-fe4d6282115f // indirect
62
64
  golang.org/x/oauth2 v0.0.0-20210819190943-2bc19b11175f // indirect
63
65
  golang.org/x/sys v0.0.0-20211019181941-9d821ace8654 // indirect
gsx/gsx.go CHANGED
@@ -8,6 +8,8 @@ import (
8
8
  "runtime"
9
9
  "strconv"
10
10
  "strings"
11
+
12
+ "github.com/samber/lo"
11
13
  )
12
14
 
13
15
  var (
@@ -62,7 +64,9 @@ func (comp ComponentFunc) Render(c *Context, tag *Tag) []*Tag {
62
64
  if v, ok := c.data[arg]; ok {
63
65
  args = append(args, reflect.ValueOf(v))
64
66
  } else {
65
- v := findAttribute(tag.Attributes, arg)
67
+ v, _ := lo.Find(tag.Attributes, func(a *Attribute) bool {
68
+ return a.Key == arg
69
+ })
66
70
  t := funcType.In(i + 1)
67
71
  switch t.Kind() {
68
72
  case reflect.Int:
gsx/gsx_test.go CHANGED
@@ -1,6 +1,7 @@
1
1
  package gsx
2
2
 
3
3
  import (
4
+ "strings"
4
5
  "testing"
5
6
 
6
7
  "github.com/stretchr/testify/require"
@@ -58,27 +59,38 @@ func TestComponent(t *testing.T) {
58
59
  },
59
60
  }
60
61
  actual := renderString(h.Render(`
61
- <div>
62
- <Todo>
62
+ <Todo>
63
- <div class="todo-panel">
63
+ <div class="todo-panel">
64
- <span>{todo.Text}</span>
64
+ <span>{todo.Text}</span>
65
- <span>{todo.Completed}</span>
65
+ <span>{todo.Completed}</span>
66
- </div>
66
+ </div>
67
- </Todo>
67
+ </Todo>
68
- <Todo />
68
+ <Todo />
69
- </div>
70
69
  `))
71
- expected := `
70
+ expected := strings.TrimLeft(`
72
- <div>
73
- <todo>
71
+ <Todo>
74
- <li id="todo-4" class="">
72
+ <li id="todo-{todo.ID}"class="{ completed: todo.Completed }">
75
- <div class="view"><span>My fourth todo</span><span>My fourth todo</span></div>
76
- <div class="todo-panel"><span>My fourth todo</span><span>false</span></div>
77
- <div class="count"><span>false</span><span>false</span></div>
73
+ <div class="upper">
74
+ <span>
75
+ {todo.Text}
76
+ </span>
77
+ <span>
78
+ {todo.Text}
79
+ </span>
78
- </li>
80
+ </div>
81
+ {children}
82
+ <div class="bottom">
83
+ <span>
84
+ {todo.Completed}
79
- </todo>
85
+ </span>
86
+ <span>
87
+ {todo.Completed}
88
+ </span>
80
89
  </div>
90
+ </li>
91
+ </Todo>
92
+ <Todo />
81
- `
93
+ `, "\n")
82
94
  r.Equal(expected, actual)
83
95
  }
84
96
 
gsx/parser.go CHANGED
@@ -103,24 +103,6 @@ func renderTagString(x *Tag, space string) string {
103
103
  return s
104
104
  }
105
105
 
106
- func findAttribute(items []*Attribute, key string) *Attribute {
107
- for _, a := range items {
108
- if a.Key == key {
109
- return a
110
- }
111
- }
112
- return nil
113
- }
114
-
115
- func sliceContains(slice []string, s string) bool {
116
- for _, v := range slice {
117
- if s == v {
118
- return true
119
- }
120
- }
121
- return false
122
- }
123
-
124
106
  func processTree(module *Module) []*Tag {
125
107
  tags := []*Tag{}
126
108
  var prevTag *Tag