Skip to content

Commit

Permalink
Reduced runtime overhead by avoiding unnecessary reflections (#583)
Browse files Browse the repository at this point in the history
Co-authored-by: Sahib Yar <[email protected]>
Co-authored-by: Kevin P. Fleming <[email protected]>
  • Loading branch information
3 people authored Jan 24, 2025
1 parent dadeeb5 commit e9d4a62
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 53 deletions.
12 changes: 5 additions & 7 deletions fastly/account_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
12 changes: 5 additions & 7 deletions fastly/tls_custom_activation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
12 changes: 5 additions & 7 deletions fastly/tls_custom_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
12 changes: 5 additions & 7 deletions fastly/tls_private_keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
12 changes: 5 additions & 7 deletions fastly/waf.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
18 changes: 7 additions & 11 deletions fastly/waf_rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
12 changes: 5 additions & 7 deletions fastly/waf_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down

0 comments on commit e9d4a62

Please sign in to comment.