~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.


38d18fe4 Peter John

3 years ago
imrpove parser
Files changed (5) hide show
  1. go.mod +2 -0
  2. go.sum +4 -0
  3. gsx/gsx.go +5 -1
  4. gsx/gsx_test.go +31 -19
  5. 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
go.sum CHANGED
@@ -363,6 +363,8 @@ github.com/rs/zerolog v1.26.1 h1:/ihwxqH+4z8UxyI70wM1z9yCvkWcfz/a3mj48k/Zngc=
363
363
  github.com/rs/zerolog v1.26.1/go.mod h1:/wSSJWX7lVrsOwlbyTRSOJvqRlc+WjWlfes+CiJ+tmc=
364
364
  github.com/russross/blackfriday v1.5.2 h1:HyvC0ARfnZBqnXwABFeSZHpKvJHJJfPz81GNueLj0oo=
365
365
  github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
366
+ github.com/samber/lo v1.21.0 h1:FSby8pJQtX4KmyddTCCGhc3JvnnIVrDA+NW37rG+7G8=
367
+ github.com/samber/lo v1.21.0/go.mod h1:2I7tgIv8Q1SG2xEIkRq0F2i2zgxVpnyPOP0d3Gj2r+A=
366
368
  github.com/segmentio/go-camelcase v0.0.0-20160726192923-7085f1e3c734 h1:Cpx2WLIv6fuPvaJAHNhYOgYzk/8RcJXu/8+mOrxf2KM=
367
369
  github.com/segmentio/go-camelcase v0.0.0-20160726192923-7085f1e3c734/go.mod h1:hqVOMAwu+ekffC3Tvq5N1ljnXRrFKcaSjbCmQ8JgYaI=
368
370
  github.com/sergi/go-diff v1.2.0 h1:XU+rvMAioB0UC3q1MFrIQy4Vo5/4VsRDQQXHsEya6xQ=
@@ -446,6 +448,8 @@ golang.org/x/exp v0.0.0-20191227195350-da58074b4299/go.mod h1:2RIsYlXP63K8oxa1u0
446
448
  golang.org/x/exp v0.0.0-20200119233911-0405dc783f0a/go.mod h1:2RIsYlXP63K8oxa1u096TMicItID8zy7Y6sNkU49FU4=
447
449
  golang.org/x/exp v0.0.0-20200207192155-f17229e696bd/go.mod h1:J/WKrq2StrnmMY6+EHIKF9dgMWnmCNThgcyBT1FY9mM=
448
450
  golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6/go.mod h1:3jZMyOhIsHpP37uCMkUooju7aAi5cS1Q23tOzKc+0MU=
451
+ golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17 h1:3MTrJm4PyNL9NBqvYDSj3DHl46qQakyfqfWo4jgfaEM=
452
+ golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17/go.mod h1:lgLbSvA5ygNOMpwM/9anMpWVlVJ7Z+cHWq/eFuinpGE=
449
453
  golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
450
454
  golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
451
455
  golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
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