diff --git a/pkg/contracts/contracts.go b/pkg/contracts/contracts.go index e83f908e..424d059f 100644 --- a/pkg/contracts/contracts.go +++ b/pkg/contracts/contracts.go @@ -19,3 +19,11 @@ type Checksumer interface { type Initer interface { Init() // Init initializes the object. } + +// SafeInit attempts to initialize the passed argument by calling its Init method, +// but only if the argument implements the [Initer] interface. +func SafeInit(v any) { + if initer, ok := v.(Initer); ok { + initer.Init() + } +} diff --git a/pkg/icingadb/history/sla.go b/pkg/icingadb/history/sla.go index 6b53f5fd..3496800e 100644 --- a/pkg/icingadb/history/sla.go +++ b/pkg/icingadb/history/sla.go @@ -12,11 +12,7 @@ import ( var slaStateStructify = structify.MakeMapStructifier( reflect.TypeOf((*history.SlaHistoryState)(nil)).Elem(), "json", - func(a any) { - if initer, ok := a.(contracts.Initer); ok { - initer.Init() - } - }) + contracts.SafeInit) func stateHistoryToSlaEntity(entry redis.XMessage) ([]history.UpserterEntity, error) { slaStateInterface, err := slaStateStructify(entry.Values) diff --git a/pkg/icingadb/history/sync.go b/pkg/icingadb/history/sync.go index c370991f..1375e788 100644 --- a/pkg/icingadb/history/sync.go +++ b/pkg/icingadb/history/sync.go @@ -181,11 +181,7 @@ func writeOneEntityStage(structPtr interface{}) stageFunc { structifier := structify.MakeMapStructifier( reflect.TypeOf(structPtr).Elem(), "json", - func(a any) { - if initer, ok := a.(contracts.Initer); ok { - initer.Init() - } - }) + contracts.SafeInit) return writeMultiEntityStage(func(entry redis.XMessage) ([]v1.UpserterEntity, error) { ptr, err := structifier(entry.Values) @@ -324,11 +320,7 @@ func userNotificationStage(ctx context.Context, s Sync, key string, in <-chan re structifier := structify.MakeMapStructifier( reflect.TypeOf((*NotificationHistory)(nil)).Elem(), "structify", - func(a any) { - if initer, ok := a.(contracts.Initer); ok { - initer.Init() - } - }) + contracts.SafeInit) return writeMultiEntityStage(func(entry redis.XMessage) ([]v1.UpserterEntity, error) { rawNotificationHistory, err := structifier(entry.Values) diff --git a/pkg/icingadb/runtime_updates.go b/pkg/icingadb/runtime_updates.go index fd2dcd3d..0d04d7ae 100644 --- a/pkg/icingadb/runtime_updates.go +++ b/pkg/icingadb/runtime_updates.go @@ -99,11 +99,7 @@ func (r *RuntimeUpdates) Sync( structify.MakeMapStructifier( reflect.TypeOf(s.Entity()).Elem(), "json", - func(a any) { - if initer, ok := a.(contracts.Initer); ok { - initer.Init() - } - }), + contracts.SafeInit), )) g.Go(func() error { @@ -166,11 +162,7 @@ func (r *RuntimeUpdates) Sync( structify.MakeMapStructifier( reflect.TypeOf(cv.Entity()).Elem(), "json", - func(a any) { - if initer, ok := a.(contracts.Initer); ok { - initer.Init() - } - }), + contracts.SafeInit), )) customvars, flatCustomvars, errs := v1.ExpandCustomvars(ctx, upsertEntities)