fix(alerting): Use reflection to set invalid providers to nil instead of re-validating on every alert trigger/resolve

This commit is contained in:
TwiN
2022-12-15 20:54:38 -05:00
parent a5f135c675
commit dfcea93080
4 changed files with 22 additions and 7 deletions

View File

@ -1,6 +1,9 @@
package alerting
import (
"fmt"
"reflect"
"github.com/TwiN/gatus/v5/alerting/alert"
"github.com/TwiN/gatus/v5/alerting/provider"
"github.com/TwiN/gatus/v5/alerting/provider/custom"
@ -154,3 +157,17 @@ func (config Config) GetAlertingProviderByAlertType(alertType alert.Type) provid
}
return nil
}
// SetAlertingProviderToNil Sets an alerting provider to nil to avoid having to revalidate it every time an
// alert of its corresponding type is sent.
func (config *Config) SetAlertingProviderToNil(p provider.AlertProvider) {
fmt.Println(config.GitHub)
entityType := reflect.TypeOf(config).Elem()
for i := 0; i < entityType.NumField(); i++ {
field := entityType.Field(i)
if field.Type == reflect.TypeOf(p) {
fmt.Println("Setting", field.Name, "to nil")
reflect.ValueOf(config).Elem().Field(i).Set(reflect.Zero(field.Type))
}
}
}