mirror of
https://github.com/Icinga/icingadb.git
synced 2026-05-28 04:35:54 -04:00
Introduce contracts#SafeInit()
This commit is contained in:
parent
647220ca8c
commit
f3eeb61b54
4 changed files with 13 additions and 25 deletions
|
|
@ -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()
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Reference in a new issue