From e4cb620a0ef2322ee1ca5293eeb3e2384efd4bdf Mon Sep 17 00:00:00 2001 From: H0llyW00dzZ Date: Sat, 30 Dec 2023 00:13:27 +0700 Subject: [PATCH 1/4] Chore Git Ignore - [+] chore(.gitignore): add test.json, test.yaml, and test.yml to ignore list - [+] chore(.gitignore): remove debug.go from ignore list --- .gitignore | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.gitignore b/.gitignore index 5c902a3..e39445e 100644 --- a/.gitignore +++ b/.gitignore @@ -22,3 +22,8 @@ go.work # H0llyW00dzZ Debug debug.go + +# H0llyW00dzZ Config Testing +test.json +test.yaml +test.yml From c2eb6292c77b2f211bd495e86f2fc64bf8207f03 Mon Sep 17 00:00:00 2001 From: H0llyW00dzZ Date: Sat, 30 Dec 2023 01:02:13 +0700 Subject: [PATCH 2/4] Fix [Worker] [List Options Pods] formatting parameters - [+] fix(worker): handle both int and float64 types for 'limit' parameter in getListOptions function --- worker/list_options.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/worker/list_options.go b/worker/list_options.go index 6562c85..76a59ba 100644 --- a/worker/list_options.go +++ b/worker/list_options.go @@ -34,12 +34,14 @@ func getListOptions(params map[string]interface{}) (v1.ListOptions, error) { return listOptions, fmt.Errorf(language.ErrorParamFieldSelector) } listOptions.FieldSelector = fieldSelector - - limit, ok := params[limIt].(int) - if !ok { + // Check for both int and float64 types for 'limit'. + if limitValue, ok := params[limIt].(int); ok { + listOptions.Limit = int64(limitValue) + } else if limitValue, ok := params[limIt].(float64); ok { + listOptions.Limit = int64(limitValue) + } else { return listOptions, fmt.Errorf(language.ErrorParamLimit) } - listOptions.Limit = int64(limit) return listOptions, nil } From 46e597666df06fdcd89794605c515b3c74aab81b Mon Sep 17 00:00:00 2001 From: H0llyW00dzZ Date: Sat, 30 Dec 2023 01:16:03 +0700 Subject: [PATCH 3/4] Chore [Language] Update Constants - [+] chore(init_const.go): add error messages for parameter validation --- language/init_const.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/language/init_const.go b/language/init_const.go index a4637c9..d38efaf 100644 --- a/language/init_const.go +++ b/language/init_const.go @@ -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" From 331bee5f4b15852e95cb6ae71b08700f231da770 Mon Sep 17 00:00:00 2001 From: H0llyW00dzZ Date: Sat, 30 Dec 2023 01:17:53 +0700 Subject: [PATCH 4/4] Refactor [Worker] [List Options Pods] Reduce Cyclomatic - [+] refactor(list_options.go): improve getListOptions function to handle parameter retrieval and type assertion errors - [+] feat(list_options.go): add getParamAsString and getParamAsInt64 helper functions for retrieving string and integer values from a map --- worker/list_options.go | 81 ++++++++++++++++++++++++++++-------------- 1 file changed, 55 insertions(+), 26 deletions(-) diff --git a/worker/list_options.go b/worker/list_options.go index 76a59ba..cc311d8 100644 --- a/worker/list_options.go +++ b/worker/list_options.go @@ -8,40 +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 - // Check for both int and float64 types for 'limit'. - if limitValue, ok := params[limIt].(int); ok { - listOptions.Limit = int64(limitValue) - } else if limitValue, ok := params[limIt].(float64); ok { - listOptions.Limit = int64(limitValue) - } else { - return listOptions, fmt.Errorf(language.ErrorParamLimit) + + listOptions := v1.ListOptions{ + LabelSelector: labelSelector, + FieldSelector: fieldSelector, + Limit: 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) +}