Skip to content

Commit 3345274

Browse files
committed
Add lua block check for builder
1 parent 509b109 commit 3345274

File tree

2 files changed

+76
-23
lines changed

2 files changed

+76
-23
lines changed

build.go

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -131,37 +131,52 @@ func buildBlock(sb io.StringWriter, parent *Directive, block Directives, depth i
131131
directive := Enquote(stmt.Directive)
132132
_, _ = sb.WriteString(directive)
133133

134-
// special handling for if statements
135-
if directive == "if" {
136-
_, _ = sb.WriteString(" (")
137-
for i, arg := range stmt.Args {
138-
if i > 0 {
134+
// special handling for'set_by_lua_block' directive
135+
if directive == "set_by_lua_block" {
136+
_, _ = sb.WriteString(" ")
137+
_, _ = sb.WriteString(stmt.Args[0]) // argument for return value
138+
_, _ = sb.WriteString(" {")
139+
_, _ = sb.WriteString(stmt.Args[1]) // argument containing block content
140+
_, _ = sb.WriteString("}")
141+
// handling other lua block directives
142+
} else if strings.Contains(directive, "_by_lua_block") {
143+
_, _ = sb.WriteString(" {")
144+
_, _ = sb.WriteString(stmt.Args[0])
145+
_, _ = sb.WriteString("}")
146+
} else {
147+
// special handling for if statements
148+
if directive == "if" {
149+
_, _ = sb.WriteString(" (")
150+
for i, arg := range stmt.Args {
151+
if i > 0 {
152+
_, _ = sb.WriteString(" ")
153+
}
154+
_, _ = sb.WriteString(Enquote(arg))
155+
}
156+
_, _ = sb.WriteString(")")
157+
} else {
158+
for _, arg := range stmt.Args {
139159
_, _ = sb.WriteString(" ")
160+
_, _ = sb.WriteString(Enquote(arg))
140161
}
141-
_, _ = sb.WriteString(Enquote(arg))
142-
}
143-
_, _ = sb.WriteString(")")
144-
} else {
145-
for _, arg := range stmt.Args {
146-
_, _ = sb.WriteString(" ")
147-
_, _ = sb.WriteString(Enquote(arg))
148162
}
149-
}
150163

151-
if !stmt.IsBlock() {
152-
_, _ = sb.WriteString(";")
153-
} else {
154-
_, _ = sb.WriteString(" {")
155-
stmt := stmt
156-
buildBlock(sb, stmt, stmt.Block, depth+1, stmt.Line, options)
157-
_, _ = sb.WriteString("\n")
158-
_, _ = sb.WriteString(margin(options, depth))
159-
_, _ = sb.WriteString("}")
164+
if !stmt.IsBlock() {
165+
_, _ = sb.WriteString(";")
166+
} else {
167+
_, _ = sb.WriteString(" {")
168+
stmt := stmt
169+
buildBlock(sb, stmt, stmt.Block, depth+1, stmt.Line, options)
170+
_, _ = sb.WriteString("\n")
171+
_, _ = sb.WriteString(margin(options, depth))
172+
_, _ = sb.WriteString("}")
173+
}
160174
}
161175
}
162176
lastLine = stmt.Line
163177
}
164178
}
179+
165180
func margin(options *BuildOptions, depth int) string {
166181
indent := depth * options.Indent
167182
if indent < MaxIndent {

build_test.go

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ type compareFixture struct {
3535
options ParseOptions
3636
}
3737

38-
//nolint:gochecknoglobals
38+
//nolint:gochecknoglobals, goconst
3939
var buildFixtures = []buildFixture{
4040
{
4141
name: "nested-and-multiple-args",
@@ -258,6 +258,44 @@ var buildFixtures = []buildFixture{
258258
},
259259
expected: "#comment1\nuser root; #comment2 #comment3",
260260
},
261+
{
262+
name: "lua block",
263+
options: BuildOptions{},
264+
parsed: Directives{
265+
{
266+
Directive: "content_by_lua_block",
267+
Line: 1,
268+
Args: []string{"\n ngx.say(\"I need no extra escaping here, for example: \\r\\nblah\")\n "},
269+
},
270+
},
271+
272+
expected: "content_by_lua_block {\n ngx.say(\"I need no extra escaping here, for example: \\r\\nblah\")\n }",
273+
},
274+
{
275+
name: "set_by_lua_block",
276+
options: BuildOptions{},
277+
parsed: Directives{
278+
{
279+
Directive: "set_by_lua_block",
280+
Line: 1,
281+
Args: []string{"$res", " -- irregular lua block directive" +
282+
"\n local a = 32" +
283+
"\n local b = 56" +
284+
"\n" +
285+
"\n ngx.var.diff = a - b; -- write to $diff directly" +
286+
"\n return a + b; -- return the $sum value normally" +
287+
"\n "},
288+
},
289+
},
290+
291+
expected: "set_by_lua_block $res { -- irregular lua block directive" +
292+
"\n local a = 32" +
293+
"\n local b = 56" +
294+
"\n" +
295+
"\n ngx.var.diff = a - b; -- write to $diff directly" +
296+
"\n return a + b; -- return the $sum value normally" +
297+
"\n }",
298+
},
261299
}
262300

263301
func TestBuild(t *testing.T) {

0 commit comments

Comments
 (0)