Skip to content

Commit

Permalink
Merge pull request #57 from losisin/feature/add-patternProperties
Browse files Browse the repository at this point in the history
add patternProperties from comments
  • Loading branch information
losisin committed Apr 12, 2024
2 parents ba032c9 + ebddf6e commit 25feb0c
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 27 deletions.
6 changes: 6 additions & 0 deletions pkg/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ func mergeSchemas(dest, src *Schema) *Schema {
if src.MinProperties != nil {
dest.MinProperties = src.MinProperties
}
if src.PatternProperties != nil {
dest.PatternProperties = src.PatternProperties
}
if src.Title != "" {
dest.Title = src.Title
}
Expand Down Expand Up @@ -149,6 +152,9 @@ func convertSchemaToMapRec(schema *Schema, visited map[uintptr]bool) (map[string
if schema.MinProperties != nil {
schemaMap["minProperties"] = *schema.MinProperties
}
if schema.PatternProperties != nil {
schemaMap["patternProperties"] = schema.PatternProperties
}
if schema.Title != "" {
schemaMap["title"] = schema.Title
}
Expand Down
13 changes: 7 additions & 6 deletions pkg/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,13 @@ func TestMergeSchemas(t *testing.T) {
dest: &Schema{Type: "object", Properties: map[string]*Schema{
"foo": {Type: "integer"},
}},
src: &Schema{Type: "object", Properties: map[string]*Schema{
"bar": {Type: "string"},
src: &Schema{Type: "object", PatternProperties: map[string]*Schema{
"^[a-z]$": {Type: "integer"},
}},
want: &Schema{Type: "object", Properties: map[string]*Schema{
"foo": {Type: "integer"},
"bar": {Type: "string"},
}, PatternProperties: map[string]*Schema{
"^[a-z]$": {Type: "integer"},
}},
},
{
Expand Down Expand Up @@ -150,9 +151,9 @@ func TestMergeSchemas(t *testing.T) {
},
{
name: "object properties",
dest: &Schema{Type: "object", MinProperties: uint64Ptr(1), MaxProperties: uint64Ptr(10)},
src: &Schema{Type: "object", MinProperties: uint64Ptr(1), MaxProperties: uint64Ptr(10)},
want: &Schema{Type: "object", MinProperties: uint64Ptr(1), MaxProperties: uint64Ptr(10)},
dest: &Schema{Type: "object", MinProperties: uint64Ptr(1), MaxProperties: uint64Ptr(10), PatternProperties: map[string]*Schema{"^.$": {Type: "string"}}},
src: &Schema{Type: "object", MinProperties: uint64Ptr(1), MaxProperties: uint64Ptr(10), PatternProperties: map[string]*Schema{"^.$": {Type: "string"}}},
want: &Schema{Type: "object", MinProperties: uint64Ptr(1), MaxProperties: uint64Ptr(10), PatternProperties: map[string]*Schema{"^.$": {Type: "string"}}},
},
{
name: "meta-data properties",
Expand Down
46 changes: 26 additions & 20 deletions pkg/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,27 @@ import (
)

type Schema struct {
Type interface{} `json:"type,omitempty"`
Enum []any `json:"enum,omitempty"`
MultipleOf *float64 `json:"multipleOf,omitempty"`
Maximum *float64 `json:"maximum,omitempty"`
Minimum *float64 `json:"minimum,omitempty"`
MaxLength *uint64 `json:"maxLength,omitempty"`
MinLength *uint64 `json:"minLength,omitempty"`
Pattern string `json:"pattern,omitempty"`
MaxItems *uint64 `json:"maxItems,omitempty"`
MinItems *uint64 `json:"minItems,omitempty"`
UniqueItems bool `json:"uniqueItems,omitempty"`
MaxProperties *uint64 `json:"maxProperties,omitempty"`
MinProperties *uint64 `json:"minProperties,omitempty"`
Required []string `json:"required,omitempty"`
Items *Schema `json:"items,omitempty"`
Properties map[string]*Schema `json:"properties,omitempty"`
Title string `json:"title,omitempty"`
Description string `json:"description,omitempty"`
ReadOnly bool `json:"readOnly,omitempty"`
Default interface{} `json:"default,omitempty"`
Type interface{} `json:"type,omitempty"`
Enum []any `json:"enum,omitempty"`
MultipleOf *float64 `json:"multipleOf,omitempty"`
Maximum *float64 `json:"maximum,omitempty"`
Minimum *float64 `json:"minimum,omitempty"`
MaxLength *uint64 `json:"maxLength,omitempty"`
MinLength *uint64 `json:"minLength,omitempty"`
Pattern string `json:"pattern,omitempty"`
MaxItems *uint64 `json:"maxItems,omitempty"`
MinItems *uint64 `json:"minItems,omitempty"`
UniqueItems bool `json:"uniqueItems,omitempty"`
MaxProperties *uint64 `json:"maxProperties,omitempty"`
MinProperties *uint64 `json:"minProperties,omitempty"`
PatternProperties interface{} `json:"patternProperties,omitempty"`
Required []string `json:"required,omitempty"`
Items *Schema `json:"items,omitempty"`
Properties map[string]*Schema `json:"properties,omitempty"`
Title string `json:"title,omitempty"`
Description string `json:"description,omitempty"`
ReadOnly bool `json:"readOnly,omitempty"`
Default interface{} `json:"default,omitempty"`
}

func getKind(value string) string {
Expand Down Expand Up @@ -156,6 +157,11 @@ func processComment(schema *Schema, comment string) (isRequired bool) {
if v, err := strconv.ParseUint(value, 10, 64); err == nil {
schema.MinProperties = &v
}
case "patternProperties":
var jsonObject interface{}
if err := json.Unmarshal([]byte(value), &jsonObject); err == nil {
schema.PatternProperties = jsonObject
}
case "required":
if strings.TrimSpace(value) == "true" {
isRequired = strings.TrimSpace(value) == "true"
Expand Down
5 changes: 5 additions & 0 deletions testdata/full.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
"items": {
"properties": {
"preference": {
"patternProperties": {
"^[a-z]$": {
"type": "string"
}
},
"properties": {
"matchExpressions": {
"items": {
Expand Down
2 changes: 1 addition & 1 deletion testdata/full.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ affinity:
- antarctica-west1
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 1
preference:
preference: # @schema patternProperties: {"^[a-z]$": {"type": "string"}}
matchExpressions:
- key: another-node-label-key
operator: In
Expand Down

0 comments on commit 25feb0c

Please sign in to comment.