Skip to content

Commit

Permalink
Improving schema extraction logs (#257)
Browse files Browse the repository at this point in the history
* Improving schema extraction logs

* Addressing comments from Allain
  • Loading branch information
edcdavid authored Oct 16, 2024
1 parent 2b9561e commit 641a932
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
2 changes: 1 addition & 1 deletion internal/controllers/clustertemplate_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ func validateTemplateParameterSchema(object *provisioningv1alpha1.ClusterTemplat
expectedType := param[1]
aSubschema, err := utils.ExtractSubSchema(object.Spec.TemplateParameterSchema.Raw, expectedName)
if err != nil {
if strings.HasPrefix(err.Error(), fmt.Sprintf("subSchema %s does not exist:", expectedName)) {
if strings.HasPrefix(err.Error(), fmt.Sprintf("subSchema '%s' does not exist:", expectedName)) {
missingParameter = append(missingParameter, expectedName)
continue
} else {
Expand Down
22 changes: 11 additions & 11 deletions internal/controllers/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -1243,37 +1243,37 @@ func ExtractSubSchema(mainSchema []byte, subSchemaKey string) (subSchema map[str
return subSchema, fmt.Errorf("failed to UnMarshall Main Schema: %w", err)
}
if _, ok := jsonObject[PropertiesString]; !ok {
return subSchema, fmt.Errorf("non compliant Main Schema, missing properties: %w", err)
return subSchema, fmt.Errorf("non compliant Main Schema, missing 'properties' section: %w", err)
}
properties, ok := jsonObject[PropertiesString].(map[string]any)
if !ok {
return subSchema, fmt.Errorf("could not cast properties as map[string]any: %w", err)
return subSchema, fmt.Errorf("could not cast 'properties' section of schema as map[string]any: %w", err)
}

subSchemaValue, ok := properties[subSchemaKey]
if !ok {
return subSchema, fmt.Errorf("subSchema %s does not exist: %w", subSchemaKey, err)
return subSchema, fmt.Errorf("subSchema '%s' does not exist: %w", subSchemaKey, err)
}

subSchema, ok = subSchemaValue.(map[string]any)
if !ok {
return subSchema, fmt.Errorf("subSchema %s is not a valid map: %w", subSchemaKey, err)
return subSchema, fmt.Errorf("subSchema '%s' is not a valid map: %w", subSchemaKey, err)
}
return subSchema, nil
}

// ExtractMatchingInput extracts the portion of the input data that corresponds to a given subSchema key.
func ExtractMatchingInput(input []byte, subSchemaKey string) (any, error) {
func ExtractMatchingInput(parentSchema []byte, subSchemaKey string) (any, error) {
inputData := make(map[string]any)
err := json.Unmarshal(input, &inputData)
err := json.Unmarshal(parentSchema, &inputData)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal input: %w", err)
return nil, fmt.Errorf("failed to unmarshal parent schema: %w", err)
}

// Check if the input contains the subSchema key
matchingInput, ok := inputData[subSchemaKey]
if !ok {
return nil, fmt.Errorf("input does not contain key %s: %w", subSchemaKey, err)
return nil, fmt.Errorf("parent schema does not contain key '%s': %w", subSchemaKey, err)
}
return matchingInput, nil
}
Expand All @@ -1282,16 +1282,16 @@ func ExtractMatchingInput(input []byte, subSchemaKey string) (any, error) {
func ExtractSchemaRequired(mainSchema []byte) (required []string, err error) {
requireListAny, err := ExtractMatchingInput(mainSchema, requiredString)
if err != nil {
return required, fmt.Errorf("could not extract required fields: %w", err)
return required, fmt.Errorf("could not extract the 'required' section of schema: %w", err)
}
requiredAny, ok := requireListAny.([]any)
if !ok {
return required, fmt.Errorf("could not cast required as []any")
return required, fmt.Errorf("could not cast 'required' section as []any")
}
for _, item := range requiredAny {
itemString, ok := item.(string)
if !ok {
return required, fmt.Errorf("could not cast []any item as string")
return required, fmt.Errorf(`could not cast 'required' section item as a string`)
}
required = append(required, itemString)
}
Expand Down

0 comments on commit 641a932

Please sign in to comment.