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 8ba2cf3
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 15 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
4 changes: 3 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 Expand Up @@ -683,6 +682,9 @@ func TestAPIFields_setCommentsAndDefault(t *testing.T) {
},
expect: &APIFields{
manifestName: "other",
Markers: []string{
"+kubebuilder:validation:Required",
},
},
},
}
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
4 changes: 2 additions & 2 deletions internal/workload/v1/markers/markers.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,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

0 comments on commit 8ba2cf3

Please sign in to comment.