@@ -10,7 +10,6 @@ package crossplane
1010import (
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
4041const 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
126127func 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 {
0 commit comments