Skip to content

Commit 44ab750

Browse files
committed
rename the interface
1 parent fbab1da commit 44ab750

File tree

7 files changed

+37
-43
lines changed

7 files changed

+37
-43
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: []ExternalLexer{
2113+
ExternalLexers: []Lexer{
21142114
&Lua{},
21152115
},
21162116
},

build.go

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ package crossplane
1010
import (
1111
"bytes"
1212
"io"
13-
"log"
1413
"os"
1514
"path/filepath"
1615
"strings"
@@ -22,19 +21,21 @@ type BuildOptions struct {
2221
Indent int
2322
Tabs bool
2423
Header bool
25-
ExternalBuilds []ExternalBuilder // handle specific directives
24+
ExternalBuilds []Builder // handle specific directives
2625
}
2726

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-
// Build 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-
Build(sb io.StringWriter, stmt *Directive) error
27+
// Builder is the interface implemented by types that can render a Directive
28+
// as it appears in NGINX configuration files.
29+
//
30+
// RegisterBuilder returns the names of the directives for which the builder can
31+
// build NGINX configuration.
32+
//
33+
// Build writes the strings that represent the Directive and it's Block to the
34+
// io.StringWriter returning any error encountered that caused the write to stop
35+
// early. Build must not modify the Directive.
36+
type Builder interface {
37+
RegisterBuilder() []string
38+
Build(stmt *Directive) string
3839
}
3940

4041
const MaxIndent = 100
@@ -122,7 +123,7 @@ func Build(w io.Writer, config Config, options *BuildOptions) error {
122123
return err
123124
}
124125

125-
//nolint:funlen,gocognit,gocyclo
126+
//nolint:funlen,gocognit
126127
func buildBlock(sb io.StringWriter, parent *Directive, block Directives, depth int, lastLine int, options *BuildOptions) {
127128
for i, stmt := range block {
128129
// if the this statement is a comment on the same line as the preview, do not emit EOL for this stmt
@@ -147,18 +148,16 @@ func buildBlock(sb io.StringWriter, parent *Directive, block Directives, depth i
147148
_, _ = sb.WriteString(directive)
148149

149150
if options.ExternalBuilds != nil {
150-
extDirectivesMap := make(map[string]ExternalBuilder)
151+
extDirectivesMap := make(map[string]Builder)
151152
for _, ext := range options.ExternalBuilds {
152-
directives := ext.RegisterExternalBuilder()
153+
directives := ext.RegisterBuilder()
153154
for _, d := range directives {
154155
extDirectivesMap[d] = ext
155156
}
156157

157158
if ext, ok := extDirectivesMap[directive]; ok {
158159
_, _ = sb.WriteString(" ") // space between directives and arguments
159-
if err := ext.Build(sb, stmt); err != nil {
160-
log.Printf("Failed to write externaller block: %v", err)
161-
}
160+
_, _ = sb.WriteString(ext.Build(stmt))
162161
}
163162
}
164163
} 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{ExternalBuilds: []ExternalBuilder{&Lua{}}},
263+
options: BuildOptions{ExternalBuilds: []Builder{&Lua{}}},
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{ExternalBuilds: []ExternalBuilder{&Lua{}}},
276+
options: BuildOptions{ExternalBuilds: []Builder{&Lua{}}},
277277
parsed: Directives{
278278
{
279279
Directive: "set_by_lua_block",

lex.go

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

47-
// ExternalLexer is an interface for implementing lexers that handle external NGINX tokens during the lexical analysis phase.
48-
type ExternalLexer interface {
49-
// RegisterExternalLexer registers an external lexer with a given sub-scanner.
47+
// Lexer is an interface for implementing lexers that handle external NGINX tokens during the lexical analysis phase.
48+
type Lexer interface {
49+
// RegisterLexer registers an external lexer with a given SubScanner.
5050
// This method integrates the external lexer into the lexical analysis process,
5151
// enabling it to handle external token scanning. It returns a slice of strings
5252
// representing the tokens that the external lexer can recognize.
53-
RegisterExternalLexer(scanner *SubScanner) []string
53+
RegisterLexer(scanner *SubScanner) []string
5454
// Lex processes a matched token and returns a channel of NgxToken objects.
5555
// This method performs lexical analysis on the matched token and produces a stream of tokens for the parser to consume.
5656
// The external lexer should close the channel once it has completed lexing the input to signal the end of tokens.
@@ -63,7 +63,7 @@ type ExternalLexer interface {
6363
// external lexers can ensure that these directives are processed separately
6464
// from the general lexical analysis logic.
6565
type LexOptions struct {
66-
ExternalLexers []ExternalLexer
66+
ExternalLexers []Lexer
6767
}
6868

6969
func LexWithOptions(r io.Reader, options LexOptions) chan NgxToken {
@@ -119,18 +119,18 @@ func tokenize(reader io.Reader, tokenCh chan NgxToken, options LexOptions) {
119119
lexState = skipSpace
120120
}
121121

122-
var externalLexers map[string]ExternalLexer
122+
var externalLexers map[string]Lexer
123123
var externalScanner *SubScanner
124124
for _, ext := range options.ExternalLexers {
125125
if externalLexers == nil {
126-
externalLexers = make(map[string]ExternalLexer)
126+
externalLexers = make(map[string]Lexer)
127127
}
128128

129129
if externalScanner == nil {
130130
externalScanner = &SubScanner{scanner: scanner, tokenLine: tokenLine}
131131
}
132132

133-
for _, d := range ext.RegisterExternalLexer(externalScanner) {
133+
for _, d := range ext.RegisterLexer(externalScanner) {
134134
externalLexers[d] = ext
135135
}
136136
}

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: []ExternalLexer{
433+
ExternalLexers: []Lexer{
434434
&Lua{},
435435
},
436436
}

lua.go

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package crossplane
22

33
import (
44
"fmt"
5-
"io"
65
"strings"
76
)
87

@@ -31,7 +30,7 @@ func (l *Lua) directiveNames() []string {
3130
}
3231
}
3332

34-
func (l *Lua) RegisterExternalLexer(s *SubScanner) []string {
33+
func (l *Lua) RegisterLexer(s *SubScanner) []string {
3534
l.s = s
3635
return l.directiveNames()
3736
}
@@ -154,18 +153,14 @@ func (l *Lua) Lex(matchedToken string) <-chan NgxToken {
154153
return tokenCh
155154
}
156155

157-
func (l *Lua) RegisterExternalBuilder() []string {
156+
func (l *Lua) RegisterBuilder() []string {
158157
return l.directiveNames()
159158
}
160159

161-
func (l *Lua) Build(sb io.StringWriter, stmt *Directive) error {
160+
func (l *Lua) Build(stmt *Directive) string {
162161
if stmt.Directive == "set_by_lua_block" {
163-
s := fmt.Sprintf("%s {%s}", stmt.Args[0], stmt.Args[1])
164-
_, err := sb.WriteString(s)
165-
return err
162+
return fmt.Sprintf("%s {%s}", stmt.Args[0], stmt.Args[1])
166163
}
167164

168-
s := fmt.Sprintf("{%s}", stmt.Args[0])
169-
_, err := sb.WriteString(s)
170-
return err
165+
return fmt.Sprintf("{%s}", stmt.Args[0])
171166
}

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: []ExternalLexer{
1710+
ExternalLexers: []Lexer{
17111711
&Lua{},
17121712
},
17131713
},
@@ -1838,7 +1838,7 @@ var parseFixtures = []parseFixture{
18381838
ErrorOnUnknownDirectives: true,
18391839
MatchFuncs: []MatchFunc{MatchLua},
18401840
LexOptions: LexOptions{
1841-
ExternalLexers: []ExternalLexer{
1841+
ExternalLexers: []Lexer{
18421842
&Lua{},
18431843
},
18441844
},
@@ -1932,7 +1932,7 @@ var parseFixtures = []parseFixture{
19321932
ParseComments: true,
19331933
MatchFuncs: []MatchFunc{MatchLua},
19341934
LexOptions: LexOptions{
1935-
ExternalLexers: []ExternalLexer{
1935+
ExternalLexers: []Lexer{
19361936
&Lua{},
19371937
},
19381938
},

0 commit comments

Comments
 (0)