Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion pkg/simpleschema/markers.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,13 @@ const (
MarkerTypeDefault MarkerType = "default"
// MarkerTypeDescription represents the `description` marker.
MarkerTypeDescription MarkerType = "description"
// MarkerTypeEnumeration represents the `enum` marker.
MarkerTypeEnumeration MarkerType = "enum"
)

func markerTypeFromString(s string) (MarkerType, error) {
switch MarkerType(s) {
case MarkerTypeRequired, MarkerTypeDefault, MarkerTypeDescription:
case MarkerTypeRequired, MarkerTypeDefault, MarkerTypeDescription, MarkerTypeEnumeration:
return MarkerType(s), nil
default:
return "", fmt.Errorf("unknown marker type: %s", s)
Expand Down
18 changes: 18 additions & 0 deletions pkg/simpleschema/transform.go
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add some unit tests for enums cases in this package?

Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package simpleschema

import (
"fmt"
"strings"

extv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
)
Expand Down Expand Up @@ -203,6 +204,23 @@ func (tf *transformer) applyMarkers(schema *extv1.JSONSchemaProps, markers []*Ma
schema.Default = &extv1.JSON{Raw: defaultValue}
case MarkerTypeDescription:
schema.Description = marker.Value
case MarkerTypeEnumeration:
enumValues := strings.Split(marker.Value, ",")
enumJSON := make([]extv1.JSON, 0, len(enumValues))
for _, v := range enumValues {
v = strings.TrimSpace(v)
var enumVal []byte
switch schema.Type {
case "string":
enumVal = []byte(fmt.Sprintf("\"%s\"", v))
case "integer", "number":
enumVal = []byte(v)
Comment on lines +214 to +217
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we perform some validation here? thinking "strconv" to check whether the enumVal is indeed a number of integer

default:
enumVal = []byte(v)
Comment on lines +218 to +219
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if we should allow non integer/string values for now

}
enumJSON = append(enumJSON, extv1.JSON{Raw: enumVal})
}
schema.Enum = enumJSON
}
}
}
Expand Down