Skip to content

Commit

Permalink
Merge pull request #92 from losisin/feature/set-properties-on-object
Browse files Browse the repository at this point in the history
feature: set properties in object
  • Loading branch information
losisin authored Sep 23, 2024
2 parents b303504 + e800756 commit 2358d58
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 4 deletions.
8 changes: 8 additions & 0 deletions pkg/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type Schema struct {
Required []string `json:"required,omitempty"`
Items *Schema `json:"items,omitempty"`
ItemsEnum []any `json:"itemsEnum,omitempty"`
ItemProperties map[string]*Schema `json:"itemProperties,omitempty"`
Properties map[string]*Schema `json:"properties,omitempty"`
Title string `json:"title,omitempty"`
Description string `json:"description,omitempty"`
Expand Down Expand Up @@ -196,6 +197,13 @@ func processComment(schema *Schema, comment string) (isRequired bool, isHidden b
schema.Items = &Schema{
Type: value,
}
case "itemProperties":
if schema.Items.Type == "object" {
var itemProps map[string]*Schema
if err := json.Unmarshal([]byte(value), &itemProps); err == nil {
schema.Items.Properties = itemProps
}
}
case "itemEnum":
if schema.Items == nil {
schema.Items = &Schema{}
Expand Down
4 changes: 2 additions & 2 deletions pkg/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,8 +297,8 @@ func TestProcessComment(t *testing.T) {
{
name: "Set array",
schema: &Schema{},
comment: "# @schema minItems:1;maxItems:10;uniqueItems:true;item:boolean",
expectedSchema: &Schema{MinItems: uint64Ptr(1), MaxItems: uint64Ptr(10), UniqueItems: true, Items: &Schema{Type: "boolean"}},
comment: "# @schema minItems:1;maxItems:10;uniqueItems:true;item:object;itemProperties:{\"key\": {\"type\": \"string\"}}",
expectedSchema: &Schema{MinItems: uint64Ptr(1), MaxItems: uint64Ptr(10), UniqueItems: true, Items: &Schema{Type: "object", Properties: map[string]*Schema{"key": {Type: "string"}}}},
expectedRequired: false,
},
{
Expand Down
10 changes: 9 additions & 1 deletion testdata/full.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,17 @@
},
"imagePullSecrets": {
"items": {
"properties": {
"key": {
"type": "string"
}
},
"type": "object"
},
"type": "array"
"type": [
"array",
"null"
]
},
"labels": {
"type": "object"
Expand Down
2 changes: 1 addition & 1 deletion testdata/full.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ replicas: 2 # @schema minimum: 1 ; maximum: 10 ; multipleOf: 2
fullnameOverride: bar # @schema pattern: ^[a-z]$ ; title: My title ; description: My description

# Arrays
imagePullSecrets: [] # @schema item: object
imagePullSecrets: [] # @schema type:[array, null]; item: object ; itemProperties: {"key": {"type": "string"}}

tolerations: # @schema minItems: 1 ; maxItems: 10 ; uniqueItems: true
- key: "bar"
Expand Down

0 comments on commit 2358d58

Please sign in to comment.