Files
kubevela/pkg/definition/defkit/conditional_param.go
Ayush Kumar 012a134829 Feat: extend fluent builder API validator patterns (#7092)
* Feat: Add NotEmpty and NegativePattern constraints to StringParam; implement Closed for MapParam

Signed-off-by: Ayush Kumar <ayushshyamkumar888@gmail.com>

* feat: add validation support for array and map parameters

- Introduced validators for ArrayParam and MapParam, allowing for cross-field validation within structured parameters.
- Added NonEmpty validation for ArrayParam to ensure arrays are not empty.
- Implemented ConditionalStructOp for conditional struct generation based on specified conditions.
- Created a new Validator type for defining validation rules with optional guard conditions.
- Added tests for various validation scenarios, including mutual exclusion and conditional parameters.
- Enhanced the CUE generation logic to incorporate new validation features and conditional struct handling.

Signed-off-by: Ayush Kumar <ayushshyamkumar888@gmail.com>

* feat: extend fluent API with new scoped field conditions and improve validation checks

Signed-off-by: Ayush Kumar <ayushshyamkumar888@gmail.com>

* feat: enhance ArrayParam with NotEmpty constraint and update ScopedField documentation

Signed-off-by: Ayush Kumar <ayushshyamkumar888@gmail.com>

* feat: rename ScopedField to LocalField for improved clarity in condition building

Signed-off-by: Ayush Kumar <ayushshyamkumar888@gmail.com>

* feat: refactor local field conditions to use RegexMatch and streamline condition building

Signed-off-by: Ayush Kumar <ayushshyamkumar888@gmail.com>

* feat: simplify condition handling by removing unused comparison types and refactoring NotCondition usage

Signed-off-by: Ayush Kumar <ayushshyamkumar888@gmail.com>

* refactor: remove unused raw CUE block handling from baseDefinition and ComponentDefinition

Signed-off-by: Ayush Kumar <ayushshyamkumar888@gmail.com>

* test: update condition handling in parameter tests to use NotExpr and Cond methods

Signed-off-by: Ayush Kumar <ayushshyamkumar888@gmail.com>

* refactor: remove negative pattern handling from StringParam and related tests

Signed-off-by: Ayush Kumar <ayushshyamkumar888@gmail.com>

* feat: add support for emitting raw header blocks in template generation

Signed-off-by: Ayush Kumar <ayushshyamkumar888@gmail.com>

* refactor: remove non-empty check from ArrayParam and update related tests

Signed-off-by: Ayush Kumar <ayushshyamkumar888@gmail.com>

* refactor: convert parameter constraint tests to use Ginkgo and Gomega for improved readability and maintainability

Signed-off-by: Ayush Kumar <ayushshyamkumar888@gmail.com>

* feat: extend fluent APIs for OAM with new CUE generation tests and condition evaluations

Signed-off-by: Ayush Kumar <ayushshyamkumar888@gmail.com>

* refactor: clean up whitespace in component, cuegen, expr, param, and resource files

Signed-off-by: Ayush Kumar <ayushshyamkumar888@gmail.com>

* feat: enhance CUE generation by adding support for new expression types and iterator references

Signed-off-by: Ayush Kumar <aykumar@guidewire.com>
Signed-off-by: Ayush Kumar <ayushshyamkumar888@gmail.com>

* refactor: remove unnecessary whitespace in cuegen.go

Signed-off-by: Ayush Kumar <ayushshyamkumar888@gmail.com>

* refactor: rename LenOf to LenOfExpr for clarity in comparison methods

Signed-off-by: Ayush Kumar <ayushshyamkumar888@gmail.com>

* feat: enhance CUE generation and validation for string arrays in ArrayParam

Signed-off-by: Ayush Kumar <ayushshyamkumar888@gmail.com>

* ci: retrigger checks

Signed-off-by: Ayush Kumar <ayushshyamkumar888@gmail.com>

---------

Signed-off-by: Ayush Kumar <ayushshyamkumar888@gmail.com>
Signed-off-by: Ayush Kumar <aykumar@guidewire.com>
Co-authored-by: Ayush Kumar <aykumar@guidewire.com>
2026-04-09 14:25:06 +01:00

88 lines
2.8 KiB
Go

/*
Copyright 2025 The KubeVela Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package defkit
// ConditionalParamBlock represents a set of conditional parameter branches.
// Each branch has a guard condition and emits different parameters when active.
//
// Example:
//
// ConditionalParams(
// WhenParam(existingResources.Eq(false)).Params(
// defkit.Bool("forceDestroy").Default(false),
// ),
// WhenParam(existingResources.Eq(true)).Params(
// defkit.Bool("forceDestroy").Optional(),
// ),
// )
//
// Emits:
//
// if existingResources == false {
// forceDestroy: *false | bool
// }
// if existingResources == true {
// forceDestroy?: bool
// }
type ConditionalParamBlock struct {
branches []*ConditionalBranch
}
// ConditionalParams creates a new conditional parameter block from branches.
func ConditionalParams(branches ...*ConditionalBranch) *ConditionalParamBlock {
return &ConditionalParamBlock{branches: branches}
}
// Branches returns all conditional branches.
func (b *ConditionalParamBlock) Branches() []*ConditionalBranch {
return b.branches
}
// ConditionalBranch represents a single branch in a conditional parameter block.
// It contains a guard condition, parameters to emit when active, and optional validators.
type ConditionalBranch struct {
condition Condition
params []Param
validators []*Validator
}
// WhenParam creates a new conditional branch with the given guard condition.
func WhenParam(cond Condition) *ConditionalBranch {
return &ConditionalBranch{condition: cond}
}
// Params sets the parameters emitted when this branch's condition is true.
func (b *ConditionalBranch) Params(params ...Param) *ConditionalBranch {
b.params = append(b.params, params...)
return b
}
// Validators adds validators emitted inside this branch's conditional block.
func (b *ConditionalBranch) Validators(validators ...*Validator) *ConditionalBranch {
b.validators = append(b.validators, validators...)
return b
}
// Condition returns the branch's guard condition.
func (b *ConditionalBranch) Condition() Condition { return b.condition }
// GetParams returns the branch's parameters.
func (b *ConditionalBranch) GetParams() []Param { return b.params }
// GetValidators returns the branch's validators.
func (b *ConditionalBranch) GetValidators() []*Validator { return b.validators }