Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Worker formatting parameters #53

Merged
merged 4 commits into from
Dec 29, 2023
Merged
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
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,8 @@ go.work

# H0llyW00dzZ Debug
debug.go

# H0llyW00dzZ Config Testing
test.json
test.yaml
test.yml
2 changes: 2 additions & 0 deletions language/init_const.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ const (
ErrorparameterstorageSize = "parameter 'storageSize' is required and must be a string"
ErrorParameterMissing = "parameter '%s' is required"
ErrorParameterInvalid = "parameter '%s' is invalid"
ErrorParameterMustBeString = "parameter '%s' must be a string"
ErrorParameterMustBeInteger = "parameter '%s' must be an integer"
ErrorParameterPolicyName = "parameter 'policyName' is required and must be a string"
ErrorParameterPolicySpec = "parameter 'policySpec' is required and must be a string"
ErrorParaMetterPolicySpecJSONorYAML = "parameter 'policySpec' contains invalid JSON or YAML: %v"
Expand Down
77 changes: 54 additions & 23 deletions worker/list_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,69 @@ import (
)

// getListOptions constructs a ListOptions struct from a map of parameters.
// It extracts the 'labelSelector', 'fieldSelector', and 'limit' values from the parameters map,
// performing type assertions as needed. This function is designed to parse and validate
// the parameters required for listing Kubernetes resources.
// It extracts 'labelSelector', 'fieldSelector', and 'limit' from the map.
// This function is designed to parse and validate the parameters required for listing Kubernetes resources.
//
// Parameters:
// - parameters: A map containing the keys and values for constructing the ListOptions.
// Expected keys are 'labelSelector', 'fieldSelector', and 'limit'.
// params - a map containing the keys and values for constructing the ListOptions.
//
// Returns:
// - A v1.ListOptions struct initialized with the values from the parameters map.
// - An error if any of the required parameters are missing or if the type assertion fails,
// indicating invalid or malformed input.
// Expected keys are 'labelSelector', 'fieldSelector', and 'limit'.
//
// Returns a v1.ListOptions struct initialized with the values from the parameters map,
// and an error if any of the required parameters are missing or if the type assertion fails.
func getListOptions(params map[string]interface{}) (v1.ListOptions, error) {
listOptions := v1.ListOptions{}
labelSelector, err := getParamAsString(params, labelSelector)
if err != nil {
return v1.ListOptions{}, fmt.Errorf(language.ErrorParamLabelSelector)
}

labelSelector, ok := params[labelSelector].(string)
if !ok {
return listOptions, fmt.Errorf(language.ErrorParamLabelSelector)
fieldSelector, err := getParamAsString(params, fieldSelector)
if err != nil {
return v1.ListOptions{}, fmt.Errorf(language.ErrorParamFieldSelector)
}
listOptions.LabelSelector = labelSelector

fieldSelector, ok := params[fieldSelector].(string)
if !ok {
return listOptions, fmt.Errorf(language.ErrorParamFieldSelector)
limit, err := getParamAsInt64(params, limIt)
if err != nil {
return v1.ListOptions{}, fmt.Errorf(language.ErrorParamLimit)
}
listOptions.FieldSelector = fieldSelector

limit, ok := params[limIt].(int)
if !ok {
return listOptions, fmt.Errorf(language.ErrorParamLimit)
listOptions := v1.ListOptions{
LabelSelector: labelSelector,
FieldSelector: fieldSelector,
Limit: limit,
}
listOptions.Limit = int64(limit)

return listOptions, nil
}

// getParamAsString retrieves a string value from a map based on a key.
// It returns an error if the key is not present or the value is not a string.
//
// params - a map of parameters where the key is expected to be associated with a string value.
// key - the key for which to retrieve the string value.
//
// Returns the string value and nil on success, or an empty string and an error on failure.
func getParamAsString(params map[string]interface{}, key string) (string, error) {
value, ok := params[key].(string)
if !ok {
return "", fmt.Errorf(language.ErrorParameterMustBeString, key)
}
return value, nil
}

// getParamAsInt64 retrieves an integer value from a map based on a key.
// It handles both int and float64 data types due to the way JSON and YAML unmarshal numbers.
// It returns an error if the key is not present or the value is not a number.
//
// params - a map of parameters where the key is expected to be associated with an integer value.
// key - the key for which to retrieve the integer value.
//
// Returns the int64 value and nil on success, or 0 and an error on failure.
func getParamAsInt64(params map[string]interface{}, key string) (int64, error) {
if value, ok := params[key].(int); ok {
return int64(value), nil
}
if value, ok := params[key].(float64); ok {
return int64(value), nil
}
return 0, fmt.Errorf(language.ErrorParameterMustBeInteger, key)
}