Skip to content

Commit

Permalink
fix: Check custom annotations more dynamically
Browse files Browse the repository at this point in the history
  • Loading branch information
dadav committed Dec 7, 2024
1 parent 3f28428 commit 21ff86a
Showing 1 changed file with 25 additions and 18 deletions.
43 changes: 25 additions & 18 deletions pkg/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"io"
"os"
"reflect"
"regexp"
"slices"
"strconv"
Expand Down Expand Up @@ -262,6 +263,17 @@ func NewSchema(schemaType string) *Schema {
}
}

func (s Schema) getJsonKeys() []string {
result := []string{}
t := reflect.TypeOf(s)

for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
result = append(result, field.Tag.Get("json"))
}
return result
}

// UnmarshalYAML custom unmarshal method
func (s *Schema) UnmarshalYAML(node *yaml.Node) error {
// Create an alias type to avoid recursion
Expand All @@ -278,32 +290,27 @@ func (s *Schema) UnmarshalYAML(node *yaml.Node) error {
// Initialize CustomAnnotations map
alias.CustomAnnotations = make(map[string]interface{})

knownKeys := s.getJsonKeys()

// Iterate through all node fields
for i := 0; i < len(node.Content)-1; i += 2 {
keyNode := node.Content[i]
valueNode := node.Content[i+1]
key := keyNode.Value

// Check if the key is a known field
switch key {
case "additionalProperties", "default", "then", "patternProperties", "properties",
"if", "minimum", "multipleOf", "exclusiveMaximum", "items", "exclusiveMinimum",
"maximum", "else", "pattern", "const", "$ref", "$schema", "$id", "format",
"description", "title", "type", "anyOf", "allOf", "oneOf", "requiredProperties",
"examples", "enum", "deprecated", "required", "not":
// Skip known fields
if slices.Contains(knownKeys, key) {
continue
default:
// Unmarshal unknown fields into the CustomAnnotations map
if !strings.HasPrefix(key, CustomAnnotationPrefix) {
continue
}
var value interface{}
if err := valueNode.Decode(&value); err != nil {
return err
}
alias.CustomAnnotations[key] = value
}

// Unmarshal unknown fields into the CustomAnnotations map
if !strings.HasPrefix(key, CustomAnnotationPrefix) {
continue
}
var value interface{}
if err := valueNode.Decode(&value); err != nil {
return err
}
alias.CustomAnnotations[key] = value
}

// Copy alias to the main struct
Expand Down

0 comments on commit 21ff86a

Please sign in to comment.