@@ -202,26 +202,13 @@ func (d *DraftConfig) ApplyDefaultVariables() error {
202202 variable .Value = defaultVal
203203 }
204204
205- if len (variable .ActiveWhenConstraints ) > 0 {
206- isVarActive := true
207- for _ , activeWhen := range variable .ActiveWhenConstraints {
208- refVar , err := d .GetVariable (activeWhen .VariableName )
209- if err != nil {
210- return fmt .Errorf ("unable to get ActiveWhen reference variable: %w" , err )
211- }
212-
213- isConditionTrue , err := d .CheckActiveWhenConstraint (refVar , activeWhen )
214- if err != nil {
215- return fmt .Errorf ("unable to check ActiveWhen constraint: %w" , err )
216- }
205+ isVarActive , err := d .CheckActiveWhenConstraint (variable )
206+ if err != nil {
207+ return fmt .Errorf ("unable to check ActiveWhen constraint: %w" , err )
208+ }
217209
218- if ! isConditionTrue {
219- isVarActive = false
220- }
221- }
222- if ! isVarActive {
223- continue
224- }
210+ if ! isVarActive {
211+ continue
225212 }
226213
227214 if variable .Value == "" {
@@ -280,26 +267,13 @@ func (d *DraftConfig) ApplyDefaultVariablesForVersion(version string) error {
280267 variable .Value = defaultVal
281268 }
282269
283- if len (variable .ActiveWhenConstraints ) > 0 {
284- isVarActive := true
285- for _ , activeWhen := range variable .ActiveWhenConstraints {
286- refVar , err := d .GetVariable (activeWhen .VariableName )
287- if err != nil {
288- return fmt .Errorf ("unable to get ActiveWhen reference variable: %w" , err )
289- }
290-
291- isConditionTrue , err := d .CheckActiveWhenConstraint (refVar , activeWhen )
292- if err != nil {
293- return fmt .Errorf ("unable to check ActiveWhen constraint: %w" , err )
294- }
270+ isVarActive , err := d .CheckActiveWhenConstraint (variable )
271+ if err != nil {
272+ return fmt .Errorf ("unable to check ActiveWhen constraint: %w" , err )
273+ }
295274
296- if ! isConditionTrue {
297- isVarActive = false
298- }
299- }
300- if ! isVarActive {
301- continue
302- }
275+ if ! isVarActive {
276+ continue
303277 }
304278
305279 if variable .Value == "" {
@@ -316,34 +290,47 @@ func (d *DraftConfig) ApplyDefaultVariablesForVersion(version string) error {
316290 return nil
317291}
318292
319- func (d * DraftConfig ) CheckActiveWhenConstraint (refVar * BuilderVar , activeWhen ActiveWhenConstraint ) (bool , error ) {
320- checkValue := refVar .Value
321- if checkValue == "" {
322- if refVar .Default .Value != "" {
323- checkValue = refVar .Default .Value
324- }
325-
326- if refVar .Default .ReferenceVar != "" {
327- refValue , err := d .recurseReferenceVars (refVar , refVar , true )
293+ func (d * DraftConfig ) CheckActiveWhenConstraint (variable * BuilderVar ) (bool , error ) {
294+ if len (variable .ActiveWhenConstraints ) > 0 {
295+ isVarActive := true
296+ for _ , activeWhen := range variable .ActiveWhenConstraints {
297+ refVar , err := d .GetVariable (activeWhen .VariableName )
328298 if err != nil {
329- return false , err
299+ return false , fmt . Errorf ( "unable to get ActiveWhen reference variable: %w" , err )
330300 }
331- if refValue == "" {
332- return false , errors .New ("reference variable has no value" )
301+
302+ checkValue := refVar .Value
303+ if checkValue == "" {
304+ if refVar .Default .Value != "" {
305+ checkValue = refVar .Default .Value
306+ }
307+
308+ if refVar .Default .ReferenceVar != "" {
309+ refValue , err := d .recurseReferenceVars (refVar , refVar , true )
310+ if err != nil {
311+ return false , err
312+ }
313+ if refValue == "" {
314+ return false , errors .New ("reference variable has no value" )
315+ }
316+
317+ checkValue = refValue
318+ }
333319 }
334320
335- checkValue = refValue
321+ switch activeWhen .Condition {
322+ case EqualTo :
323+ isVarActive = checkValue == activeWhen .Value
324+ case NotEqualTo :
325+ isVarActive = checkValue != activeWhen .Value
326+ default :
327+ return false , fmt .Errorf ("invalid activeWhen condition: %s" , activeWhen .Condition )
328+ }
336329 }
330+ return isVarActive , nil
337331 }
338332
339- switch activeWhen .Condition {
340- case EqualTo :
341- return checkValue == activeWhen .Value , nil
342- case NotEqualTo :
343- return checkValue != activeWhen .Value , nil
344- }
345-
346- return false , nil
333+ return true , nil
347334}
348335
349336// recurseReferenceVars recursively checks each variable's ReferenceVar if it doesn't have a custom input. If there's no more ReferenceVars, it will return the default value of the last ReferenceVar.
0 commit comments