Skip to content

Commit cfa0555

Browse files
committed
Fix build file
1 parent 1b9ef19 commit cfa0555

File tree

6 files changed

+72
-44
lines changed

6 files changed

+72
-44
lines changed

analyze_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2110,7 +2110,7 @@ func TestAnalyze_lua(t *testing.T) {
21102110
err := analyze("nginx.conf", tc.stmt, ";", tc.ctx, &ParseOptions{
21112111
MatchFuncs: []MatchFunc{MatchLua},
21122112
LexOptions: LexOptions{
2113-
ExternalLexers: []ExtLexer{
2113+
ExternalLexers: []ExternalLexer{
21142114
&LuaLexer{},
21152115
},
21162116
},

build.go

Lines changed: 55 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ package crossplane
1010
import (
1111
"bytes"
1212
"io"
13+
"log"
1314
"os"
1415
"path/filepath"
1516
"strings"
@@ -18,15 +19,22 @@ import (
1819
)
1920

2021
type BuildOptions struct {
21-
Indent int
22-
Tabs bool
23-
Header bool
24-
ExternalBuild []ExtBuild // handle specific directives
22+
Indent int
23+
Tabs bool
24+
Header bool
25+
ExternalBuilds []ExternalBuilder // handle specific directives
2526
}
2627

27-
type ExtBuild interface {
28-
ExtDirectives() []string
29-
BuildLuaBlock(sb io.StringWriter, directive string, stmt *Directive)
28+
// ExternalBuilder is the interface that provides an abstraction for implementing builders that
29+
// can handle external and custom NGINXdirectives during the build process of NGINX configurations
30+
// from JSON payloads.
31+
type ExternalBuilder interface {
32+
// RegisterExternalBuilder allows the build system to identify which NGINX directives are supported
33+
// by the external builder and routes the build process of those directives to this builder.
34+
RegisterExternalBuilder() []string
35+
// Builder is responsible for constructing the configuration block for a specific directive.
36+
// It is called during the configuration building process whenever a registered directive is encountered.
37+
Builder(sb io.StringWriter, directive string, stmt *Directive) error
3038
}
3139

3240
const MaxIndent = 100
@@ -45,7 +53,7 @@ const header = `# This config was built from JSON using NGINX crossplane.
4553

4654
type LuaBuild struct{}
4755

48-
func (lb *LuaBuild) ExtDirectives() []string {
56+
func (lb *LuaBuild) RegisterExternalBuilder() []string {
4957
return []string{
5058
"init_by_lua_block",
5159
"init_worker_by_lua_block",
@@ -66,19 +74,41 @@ func (lb *LuaBuild) ExtDirectives() []string {
6674
}
6775
}
6876

69-
func (lb *LuaBuild) BuildLuaBlock(sb io.StringWriter, directive string, stmt *Directive) {
77+
func (lb *LuaBuild) Builder(sb io.StringWriter, directive string, stmt *Directive) error {
78+
// helper function to write to sb and check for an error
79+
writeAndCheck := func(s string) error {
80+
_, err := sb.WriteString(s)
81+
return err
82+
}
7083
// special handling for'set_by_lua_block' directive
7184
if directive == "set_by_lua_block" {
72-
_, _ = sb.WriteString(" ")
73-
_, _ = sb.WriteString(stmt.Args[0]) // argument for return value
74-
_, _ = sb.WriteString(" {")
75-
_, _ = sb.WriteString(stmt.Args[1]) // argument containing block content
76-
_, _ = sb.WriteString("}")
85+
if err := writeAndCheck(" "); err != nil {
86+
return err
87+
}
88+
if err := writeAndCheck(stmt.Args[0]); err != nil { // argument for return value
89+
return err
90+
}
91+
if err := writeAndCheck(" {"); err != nil {
92+
return err
93+
}
94+
if err := writeAndCheck(stmt.Args[1]); err != nil { // argument containing block content
95+
return err
96+
}
97+
if err := writeAndCheck("}"); err != nil {
98+
return err
99+
}
77100
} else {
78-
_, _ = sb.WriteString(" {")
79-
_, _ = sb.WriteString(stmt.Args[0])
80-
_, _ = sb.WriteString("}")
101+
if err := writeAndCheck(" {"); err != nil {
102+
return err
103+
}
104+
if err := writeAndCheck(stmt.Args[0]); err != nil { // argument for return value
105+
return err
106+
}
107+
if err := writeAndCheck("}"); err != nil {
108+
return err
109+
}
81110
}
111+
return nil
82112
}
83113

84114
// BuildFiles builds all of the config files in a crossplane.Payload and
@@ -152,7 +182,7 @@ func Build(w io.Writer, config Config, options *BuildOptions) error {
152182
return err
153183
}
154184

155-
//nolint:funlen,gocognit
185+
//nolint:funlen,gocognit,gocyclo
156186
func buildBlock(sb io.StringWriter, parent *Directive, block Directives, depth int, lastLine int, options *BuildOptions) {
157187
for i, stmt := range block {
158188
// if the this statement is a comment on the same line as the preview, do not emit EOL for this stmt
@@ -176,16 +206,18 @@ func buildBlock(sb io.StringWriter, parent *Directive, block Directives, depth i
176206
directive := Enquote(stmt.Directive)
177207
_, _ = sb.WriteString(directive)
178208

179-
if options.ExternalBuild != nil {
180-
extDirectivesMap := make(map[string]ExtBuild)
181-
for _, ext := range options.ExternalBuild {
182-
directives := ext.ExtDirectives()
209+
if options.ExternalBuilds != nil {
210+
extDirectivesMap := make(map[string]ExternalBuilder)
211+
for _, ext := range options.ExternalBuilds {
212+
directives := ext.RegisterExternalBuilder()
183213
for _, d := range directives {
184214
extDirectivesMap[d] = ext
185215
}
186216

187217
if ext, ok := extDirectivesMap[directive]; ok {
188-
ext.BuildLuaBlock(sb, directive, stmt)
218+
if err := ext.Builder(sb, directive, stmt); err != nil {
219+
log.Printf("Failed to write externaller block: %v", err)
220+
}
189221
}
190222
}
191223
} else {

build_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ var buildFixtures = []buildFixture{
260260
},
261261
{
262262
name: "lua block",
263-
options: BuildOptions{ExternalBuild: []ExtBuild{&LuaBuild{}}},
263+
options: BuildOptions{ExternalBuilds: []ExternalBuilder{&LuaBuild{}}},
264264
parsed: Directives{
265265
{
266266
Directive: "content_by_lua_block",
@@ -273,7 +273,7 @@ var buildFixtures = []buildFixture{
273273
},
274274
{
275275
name: "set_by_lua_block",
276-
options: BuildOptions{ExternalBuild: []ExtBuild{&LuaBuild{}}},
276+
options: BuildOptions{ExternalBuilds: []ExternalBuilder{&LuaBuild{}}},
277277
parsed: Directives{
278278
{
279279
Directive: "set_by_lua_block",

lex.go

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ func SetTokenChanCap(size int) {
4545
tokChanCap = size
4646
}
4747

48-
type ExtLexer interface {
49-
Register(scanner *extScanner) []string
48+
type ExternalLexer interface {
49+
RegisterExternalLexer(scanner *extScanner) []string
5050
Lex(matchedToken string) <-chan NgxToken
5151
}
5252

@@ -55,7 +55,7 @@ type ExtLexer interface {
5555
// external lexers can ensure that these directives are processed separately
5656
// from the general lexical analysis logic.
5757
type LexOptions struct {
58-
ExternalLexers []ExtLexer
58+
ExternalLexers []ExternalLexer
5959
}
6060

6161
func LexWithOptions(r io.Reader, options LexOptions) chan NgxToken {
@@ -111,18 +111,18 @@ func tokenize(reader io.Reader, tokenCh chan NgxToken, options LexOptions) {
111111
lexState = skipSpace
112112
}
113113

114-
var externalLexers map[string]ExtLexer
114+
var externalLexers map[string]ExternalLexer
115115
var externalScanner *extScanner
116116
for _, ext := range options.ExternalLexers {
117117
if externalLexers == nil {
118-
externalLexers = make(map[string]ExtLexer)
118+
externalLexers = make(map[string]ExternalLexer)
119119
}
120120

121121
if externalScanner == nil {
122122
externalScanner = &extScanner{scanner: scanner, tokenLine: tokenLine}
123123
}
124124

125-
for _, d := range ext.Register(externalScanner) {
125+
for _, d := range ext.RegisterExternalLexer(externalScanner) {
126126
if _, ok := externalLexers[d]; ok {
127127
// Handle the duplicate token name, emitting an error token and exit
128128
tokenCh <- NgxToken{Value: "Duplicate token name", Line: tokenLine, IsQuoted: false, Error: errors.New("duplicate token name handled")}
@@ -164,15 +164,11 @@ func tokenize(reader io.Reader, tokenCh chan NgxToken, options LexOptions) {
164164

165165
if token.Len() > 0 {
166166
tokenStr := token.String()
167-
if ext, ok := externalLexers[tokenStr]; ok {
168-
if nextTokenIsDirective {
167+
if nextTokenIsDirective {
168+
if ext, ok := externalLexers[tokenStr]; ok {
169169
// saving lex state before emitting tokenStr to know if we encountered start quote
170170
lastLexState := lexState
171-
if lexState == inQuote {
172-
emit(tokenStartLine, true, nil)
173-
} else {
174-
emit(tokenStartLine, false, nil)
175-
}
171+
emit(tokenStartLine, lexState == inQuote, nil)
176172

177173
externalScanner.tokenLine = tokenLine
178174
extTokenCh := ext.Lex(tokenStr)
@@ -324,7 +320,7 @@ type LuaLexer struct {
324320
s *extScanner
325321
}
326322

327-
func (ll *LuaLexer) Register(s *extScanner) []string {
323+
func (ll *LuaLexer) RegisterExternalLexer(s *extScanner) []string {
328324
ll.s = s
329325
return []string{
330326
"init_by_lua_block",

lex_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,7 @@ func TestLex(t *testing.T) {
430430
}
431431
defer file.Close()
432432
options := LexOptions{
433-
ExternalLexers: []ExtLexer{
433+
ExternalLexers: []ExternalLexer{
434434
&LuaLexer{},
435435
},
436436
}

parse_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1707,7 +1707,7 @@ var parseFixtures = []parseFixture{
17071707
ErrorOnUnknownDirectives: true,
17081708
MatchFuncs: []MatchFunc{MatchLua},
17091709
LexOptions: LexOptions{
1710-
ExternalLexers: []ExtLexer{
1710+
ExternalLexers: []ExternalLexer{
17111711
&LuaLexer{},
17121712
},
17131713
},
@@ -1838,7 +1838,7 @@ var parseFixtures = []parseFixture{
18381838
ErrorOnUnknownDirectives: true,
18391839
MatchFuncs: []MatchFunc{MatchLua},
18401840
LexOptions: LexOptions{
1841-
ExternalLexers: []ExtLexer{
1841+
ExternalLexers: []ExternalLexer{
18421842
&LuaLexer{},
18431843
},
18441844
},
@@ -1932,7 +1932,7 @@ var parseFixtures = []parseFixture{
19321932
ParseComments: true,
19331933
MatchFuncs: []MatchFunc{MatchLua},
19341934
LexOptions: LexOptions{
1935-
ExternalLexers: []ExtLexer{
1935+
ExternalLexers: []ExternalLexer{
19361936
&LuaLexer{},
19371937
},
19381938
},

0 commit comments

Comments
 (0)