From e9d4a62d13ad2c8b3fd3382238e145760b6af839 Mon Sep 17 00:00:00 2001 From: Sahib Yar Date: Sat, 25 Jan 2025 00:54:27 +0500 Subject: [PATCH] Reduced runtime overhead by avoiding unnecessary reflections (#583) Co-authored-by: Sahib Yar Co-authored-by: Kevin P. Fleming --- fastly/account_events.go | 12 +++++------- fastly/tls_custom_activation.go | 12 +++++------- fastly/tls_custom_configuration.go | 12 +++++------- fastly/tls_private_keys.go | 12 +++++------- fastly/waf.go | 12 +++++------- fastly/waf_rules.go | 18 +++++++----------- fastly/waf_version.go | 12 +++++------- 7 files changed, 37 insertions(+), 53 deletions(-) diff --git a/fastly/account_events.go b/fastly/account_events.go index 4de5705c2..ce042e38b 100644 --- a/fastly/account_events.go +++ b/fastly/account_events.go @@ -185,15 +185,13 @@ func (i *GetAPIEventsFilterInput) formatEventFilters() map[string]string { // of any of these filters. It doesn't appear we would need to at present. for key, value := range pairings { - switch t := reflect.TypeOf(value).String(); t { - case "string": - if value != "" { - v, _ := value.(string) // type assert to avoid runtime panic (v will have zero value for its type) + switch v := value.(type) { + case string: + if v != "" { result[key] = v } - case "int": - if value != 0 { - v, _ := value.(int) // type assert to avoid runtime panic (v will have zero value for its type) + case int: + if v != 0 { result[key] = strconv.Itoa(v) } } diff --git a/fastly/tls_custom_activation.go b/fastly/tls_custom_activation.go index e94222a0f..4e8d9699d 100644 --- a/fastly/tls_custom_activation.go +++ b/fastly/tls_custom_activation.go @@ -48,15 +48,13 @@ func (i *ListTLSActivationsInput) formatFilters() map[string]string { } for key, value := range pairings { - switch t := reflect.TypeOf(value).String(); t { - case "string": - if value != "" { - v, _ := value.(string) // type assert to avoid runtime panic (v will have zero value for its type) + switch v := value.(type) { + case string: + if v != "" { result[key] = v } - case "int": - if value != 0 { - v, _ := value.(int) // type assert to avoid runtime panic (v will have zero value for its type) + case int: + if v != 0 { result[key] = strconv.Itoa(v) } } diff --git a/fastly/tls_custom_configuration.go b/fastly/tls_custom_configuration.go index 0b310ba82..8300fdad5 100644 --- a/fastly/tls_custom_configuration.go +++ b/fastly/tls_custom_configuration.go @@ -52,15 +52,13 @@ func (i *ListCustomTLSConfigurationsInput) formatFilters() map[string]string { } for key, value := range pairings { - switch t := reflect.TypeOf(value).String(); t { - case "string": - if value != "" { - v, _ := value.(string) // type assert to avoid runtime panic (v will have zero value for its type) + switch v := value.(type) { + case string: + if v != "" { result[key] = v } - case "int": - if value != 0 { - v, _ := value.(int) // type assert to avoid runtime panic (v will have zero value for its type) + case int: + if v != 0 { result[key] = strconv.Itoa(v) } } diff --git a/fastly/tls_private_keys.go b/fastly/tls_private_keys.go index 2fcee28a2..ad9762e16 100644 --- a/fastly/tls_private_keys.go +++ b/fastly/tls_private_keys.go @@ -47,15 +47,13 @@ func (i *ListPrivateKeysInput) formatFilters() map[string]string { } for key, value := range pairings { - switch t := reflect.TypeOf(value).String(); t { - case "string": - if value != "" { - v, _ := value.(string) // type assert to avoid runtime panic (v will have zero value for its type) + switch v := value.(type) { + case string: + if v != "" { result[key] = v } - case "int": - if value != 0 { - v, _ := value.(int) // type assert to avoid runtime panic (v will have zero value for its type) + case int: + if v != 0 { result[key] = strconv.Itoa(v) } } diff --git a/fastly/waf.go b/fastly/waf.go index 664fd0c12..1b396b932 100644 --- a/fastly/waf.go +++ b/fastly/waf.go @@ -71,15 +71,13 @@ func (i *ListWAFsInput) formatFilters() map[string]string { } for key, value := range pairings { - switch t := reflect.TypeOf(value).String(); t { - case "string": - if value != "" { - v, _ := value.(string) // type assert to avoid runtime panic (v will have zero value for its type) + switch v := value.(type) { + case string: + if v != "" { result[key] = v } - case "int": - if value != 0 { - v, _ := value.(int) // type assert to avoid runtime panic (v will have zero value for its type) + case int: + if v != 0 { result[key] = strconv.Itoa(v) } } diff --git a/fastly/waf_rules.go b/fastly/waf_rules.go index dc27d9749..c79b4aff1 100644 --- a/fastly/waf_rules.go +++ b/fastly/waf_rules.go @@ -74,24 +74,20 @@ func (i *ListWAFRulesInput) formatFilters() map[string]string { } for key, value := range pairings { - switch t := reflect.TypeOf(value).String(); t { - case "string": - if value != "" { - v, _ := value.(string) // type assert to avoid runtime panic (v will have zero value for its type) + switch v := value.(type) { + case string: + if v != "" { result[key] = v } - case "int": - if value != 0 { - v, _ := value.(int) // type assert to avoid runtime panic (v will have zero value for its type) + case int: + if v != 0 { result[key] = strconv.Itoa(v) } - case "[]string": - v, _ := value.([]string) // type assert to avoid runtime panic (v will have zero value for its type) + case []string: if len(v) > 0 { result[key] = strings.Join(v, ",") } - case "[]int": - v, _ := value.([]int) // type assert to avoid runtime panic (v will have zero value for its type) + case []int: if len(v) > 0 { stringSlice := make([]string, len(v)) for i, id := range v { diff --git a/fastly/waf_version.go b/fastly/waf_version.go index 82de17640..29ed0a728 100644 --- a/fastly/waf_version.go +++ b/fastly/waf_version.go @@ -111,15 +111,13 @@ func (i *ListWAFVersionsInput) formatFilters() map[string]string { } for key, value := range pairings { - switch t := reflect.TypeOf(value).String(); t { - case "string": - if value != "" { - v, _ := value.(string) // type assert to avoid runtime panic (v will have zero value for its type) + switch v := value.(type) { + case string: + if v != "" { result[key] = v } - case "int": - if value != 0 { - v, _ := value.(int) // type assert to avoid runtime panic (v will have zero value for its type) + case int: + if v != 0 { result[key] = strconv.Itoa(v) } }