@@ -10,6 +10,7 @@ package crossplane
1010import (
1111 "bytes"
1212 "io"
13+ "log"
1314 "os"
1415 "path/filepath"
1516 "strings"
@@ -18,15 +19,22 @@ import (
1819)
1920
2021type 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
3240const MaxIndent = 100
@@ -45,7 +53,7 @@ const header = `# This config was built from JSON using NGINX crossplane.
4553
4654type 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
156186func 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 {
0 commit comments