~repos /gromer
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.
4391b748
—
Peter John 3 years ago
fix parser issues
- gsx/context.go +4 -1
- gsx/gsx.go +22 -11
- gsx/parser.go +2 -1
- gsx/parser_test.go +2 -2
- gsx/twx.go +1 -1
gsx/context.go
CHANGED
|
@@ -66,7 +66,10 @@ func (c *Context) Data(data M) {
|
|
|
66
66
|
}
|
|
67
67
|
|
|
68
68
|
func (c *Context) Render(tpl string) []*Tag {
|
|
69
|
-
name,
|
|
69
|
+
name, ok := c.Get("funcName").(string)
|
|
70
|
+
if !ok {
|
|
71
|
+
panic("funcName is required")
|
|
72
|
+
}
|
|
70
73
|
tags := parse(name, tpl)
|
|
71
74
|
return populate(c, tags)
|
|
72
75
|
}
|
gsx/gsx.go
CHANGED
|
@@ -72,25 +72,35 @@ func (comp ComponentFunc) Render(c *Context, tag *Tag) []*Tag {
|
|
|
72
72
|
})
|
|
73
73
|
var data interface{}
|
|
74
74
|
if v.Value.Ref != nil {
|
|
75
|
-
data = c
|
|
75
|
+
data = getRefValue(c, *v.Value.Ref)
|
|
76
76
|
} else if v.Value.Str != nil {
|
|
77
77
|
data = *v.Value.Str
|
|
78
78
|
}
|
|
79
79
|
switch t.Kind() {
|
|
80
80
|
case reflect.Int:
|
|
81
|
+
var value int
|
|
82
|
+
if v, ok := data.(int); ok {
|
|
83
|
+
value = v
|
|
84
|
+
} else {
|
|
81
|
-
|
|
85
|
+
s, ok := data.(string)
|
|
82
|
-
|
|
86
|
+
if !ok {
|
|
83
|
-
|
|
87
|
+
panic(fmt.Errorf("expected component %s: prop %s to be of type string but got %+v ", comp.Name, arg, data))
|
|
88
|
+
}
|
|
89
|
+
value, _ = strconv.Atoi(s)
|
|
84
90
|
}
|
|
85
|
-
value, _ := strconv.Atoi(s)
|
|
86
91
|
c.Set(arg, value)
|
|
87
92
|
args = append(args, reflect.ValueOf(value))
|
|
88
93
|
case reflect.Bool:
|
|
94
|
+
var value bool
|
|
95
|
+
if v, ok := data.(bool); ok {
|
|
96
|
+
value = v
|
|
97
|
+
} else {
|
|
89
|
-
|
|
98
|
+
s, ok := data.(string)
|
|
90
|
-
|
|
99
|
+
if !ok {
|
|
91
|
-
|
|
100
|
+
panic(fmt.Errorf("expected component %s: prop %s to be of type string but got %+v ", comp.Name, arg, data))
|
|
101
|
+
}
|
|
102
|
+
value, _ = strconv.ParseBool(s)
|
|
92
103
|
}
|
|
93
|
-
value, _ := strconv.ParseBool(s)
|
|
94
104
|
c.Set(arg, value)
|
|
95
105
|
args = append(args, reflect.ValueOf(value))
|
|
96
106
|
default:
|
|
@@ -240,7 +250,7 @@ func populateTag(c *Context, tag *Tag) {
|
|
|
240
250
|
}
|
|
241
251
|
} else if loop := tag.Text.For; loop != nil {
|
|
242
252
|
tag.Name = "fragment"
|
|
243
|
-
data := c
|
|
253
|
+
data := getRefValue(c, loop.Reference)
|
|
244
254
|
statement := loop.Statements[0].ReturnStatement
|
|
245
255
|
switch reflect.TypeOf(data).Kind() {
|
|
246
256
|
case reflect.Slice:
|
|
@@ -276,7 +286,8 @@ func populateTag(c *Context, tag *Tag) {
|
|
|
276
286
|
*a.Value.Str = removeQuotes(*a.Value.Str)
|
|
277
287
|
}
|
|
278
288
|
} else if a.Value.Ref != nil {
|
|
279
|
-
|
|
289
|
+
value := getRefValue(c, *a.Value.Ref)
|
|
290
|
+
subs := fmt.Sprintf("%v", value)
|
|
280
291
|
a.Value = &Literal{Str: &subs}
|
|
281
292
|
} else if a.Key == "class" && a.Value.KV != nil {
|
|
282
293
|
classes := []string{}
|
gsx/parser.go
CHANGED
|
@@ -66,7 +66,7 @@ type Literal struct {
|
|
|
66
66
|
Pos lexer.Position
|
|
67
67
|
Str *string `@String`
|
|
68
68
|
Ref *string `| "{" @Ident ( @"." @Ident )* "}"`
|
|
69
|
-
KV []*KV `| "{" @@
|
|
69
|
+
KV []*KV `| "{" [ @@ { "," @@ } ] "}"`
|
|
70
70
|
For *ForStatement `| @@`
|
|
71
71
|
}
|
|
72
72
|
|
|
@@ -232,6 +232,7 @@ func processTree(nodes []*AstNode) []*Tag {
|
|
|
232
232
|
func parse(name, s string) []*Tag {
|
|
233
233
|
ast, err := htmlParser.ParseString(name, s)
|
|
234
234
|
if err != nil {
|
|
235
|
+
println("name", name)
|
|
235
236
|
panic(err)
|
|
236
237
|
}
|
|
237
238
|
return processTree(ast.Nodes)
|
gsx/parser_test.go
CHANGED
|
@@ -10,7 +10,7 @@ import (
|
|
|
10
10
|
func TestParse(t *testing.T) {
|
|
11
11
|
r := require.New(t)
|
|
12
12
|
actual := RenderString(parse("test", `
|
|
13
|
-
<ul id="todo-list" class="relative">
|
|
13
|
+
<ul id="todo-list" class={"relative": true, "completed": false }>
|
|
14
14
|
<Todo todo={v}>
|
|
15
15
|
<div>"Todo123"</div>
|
|
16
16
|
</Todo>
|
|
@@ -24,7 +24,7 @@ func TestParse(t *testing.T) {
|
|
|
24
24
|
</div>
|
|
25
25
|
`))
|
|
26
26
|
expected := strings.TrimLeft(`
|
|
27
|
-
<ul id=""todo-list""
|
|
27
|
+
<ul id=""todo-list"">
|
|
28
28
|
<Todo>
|
|
29
29
|
<div>
|
|
30
30
|
Todo123
|
gsx/twx.go
CHANGED
|
@@ -554,7 +554,7 @@ func computeCss(classMap M, parent string) string {
|
|
|
554
554
|
p += " " + s + "\n"
|
|
555
555
|
}
|
|
556
556
|
p += "}\n"
|
|
557
|
-
} else if prefix == "
|
|
557
|
+
} else if prefix == "hover" {
|
|
558
558
|
p += "\n" + className + ":hover" + " {\n"
|
|
559
559
|
if s, ok := twClassLookup[class]; ok {
|
|
560
560
|
p += " " + s + "\n"
|