Skip to content

Commit

Permalink
fix: Fixes #47, non-defaulted fields are marked as required
Browse files Browse the repository at this point in the history
Signed-off-by: Dustin Scott <dustin.scott18@gmail.com>
  • Loading branch information
scottd018 committed Jul 23, 2022
1 parent 8818d63 commit ca14d4a
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 16 deletions.
21 changes: 11 additions & 10 deletions internal/workload/v1/kinds/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ type APIFields struct {
Children []*APIFields
Default string
Sample string
Last bool
}

func (api *APIFields) AddField(path string, fieldType markers.FieldType, comments []string, sample interface{}, hasDefault bool) error {
Expand Down Expand Up @@ -70,7 +69,6 @@ func (api *APIFields) AddField(path string, fieldType markers.FieldType, comment
}

newChild := obj.newChild(last, fieldType, sample)
newChild.Last = true

newChild.setCommentsAndDefault(comments, sample, hasDefault)

Expand Down Expand Up @@ -265,22 +263,25 @@ func (api *APIFields) setSample(sampleVal interface{}) {

func (api *APIFields) setDefault(sampleVal interface{}) {
api.Default = api.getSampleValue(sampleVal)
api.appendMarkers(
fmt.Sprintf("+kubebuilder:default=%s", api.Default),
"+kubebuilder:validation:Optional",
fmt.Sprintf("(Default: %s)", api.Default),
)
api.setSample(sampleVal)
}

func (api *APIFields) appendMarkers(apiMarkers ...string) {
if len(api.Markers) == 0 {
api.Markers = append(
api.Markers,
fmt.Sprintf("+kubebuilder:default=%s", api.Default),
"+kubebuilder:validation:Optional",
fmt.Sprintf("(Default: %s)", api.Default),
)
api.Markers = append(api.Markers, apiMarkers...)
}

api.setSample(sampleVal)
}

func (api *APIFields) setCommentsAndDefault(comments []string, sampleVal interface{}, hasDefault bool) {
if hasDefault {
api.setDefault(sampleVal)
} else {
api.appendMarkers("+kubebuilder:validation:Required")
}

if comments != nil {
Expand Down
1 change: 0 additions & 1 deletion internal/workload/v1/kinds/api_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,6 @@ func TestAPIFields_getSampleValue(t *testing.T) {
Children: tt.fields.Children,
Default: tt.fields.Default,
Sample: tt.fields.Sample,
Last: tt.fields.Last,
}
if got := api.getSampleValue(tt.args.sampleVal); got != tt.want {
t.Errorf("APIFields.getSampleValue() = %v, want %v", got, tt.want)
Expand Down
4 changes: 2 additions & 2 deletions internal/workload/v1/kinds/workload.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,15 +347,15 @@ func (ws *WorkloadSpec) processMarkers(manifestFile *manifests.Manifest, markerT
}

func (ws *WorkloadSpec) processMarkerResults(markerResults []*inspect.YAMLResult) error {
for _, markerResult := range markerResults {
for i := range markerResults {
var defaultFound bool

var sampleVal interface{}

// convert to interface
var marker markers.FieldMarkerProcessor

switch t := markerResult.Object.(type) {
switch t := markerResults[i].Object.(type) {
case *markers.FieldMarker:
marker = t
ws.FieldMarkers = append(ws.FieldMarkers, t)
Expand Down
13 changes: 10 additions & 3 deletions internal/workload/v1/markers/markers.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ var (
ErrMissingParentOrName = errors.New("missing either parent=value or name=value marker")
ErrInvalidReplaceMarkerFieldType = errors.New("invalid marker type using replace")
ErrInvalidParentField = errors.New("invalid parent field")
ErrInvalidMarker = errors.New("invalid marker discovered")
)

// MarkerType defines the types of markers that are accepted by the parser.
Expand Down Expand Up @@ -106,8 +107,8 @@ func initializeMarkerInspector(markerTypes ...MarkerType) (*inspect.Inspector, e

var err error

for _, markerType := range markerTypes {
switch markerType {
for i := range markerTypes {
switch markerTypes[i] {
case FieldMarkerType:
err = defineFieldMarker(registry)
case CollectionMarkerType:
Expand Down Expand Up @@ -148,8 +149,14 @@ func transformYAML(results ...*inspect.YAMLResult) error {

t.sourceCodeVar = sourceCodeVar
marker = &t
default:
case ResourceMarker:
continue
default:
return fmt.Errorf(
"%w; found marker with unknown definition %s",
ErrInvalidMarker,
result.MarkerText,
)
}

// ensure that either a parent or a name is set
Expand Down

0 comments on commit ca14d4a

Please sign in to comment.