From b203116f138a65efe720bb3828162616759b1770 Mon Sep 17 00:00:00 2001 From: akeemphilbert Date: Wed, 19 Jul 2023 10:15:39 -0400 Subject: [PATCH] Made the key for the initializer map a string based on the name of the function --- controllers/rest/api.go | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/controllers/rest/api.go b/controllers/rest/api.go index 7e4ea2ed..a82b8f3b 100644 --- a/controllers/rest/api.go +++ b/controllers/rest/api.go @@ -11,6 +11,7 @@ import ( "net/http" "os" "reflect" + "runtime" "strconv" "strings" "time" @@ -56,7 +57,7 @@ type RESTAPI struct { httpClients map[string]*http.Client globalInitializers []GlobalInitializer operationInitializers []OperationInitializer - registeredInitializers map[reflect.Value]int + registeredInitializers map[string]int prePathInitializers []PathInitializer registeredPrePathInitializers map[reflect.Value]int postPathInitializers []PathInitializer @@ -128,13 +129,14 @@ func (p *RESTAPI) RegisterEventStore(name string, repository model.EventReposito //RegisterGlobalInitializer add global initializer if it's not already there func (p *RESTAPI) RegisterGlobalInitializer(initializer GlobalInitializer) { if p.registeredInitializers == nil { - p.registeredInitializers = make(map[reflect.Value]int) + p.registeredInitializers = make(map[string]int) } //only add initializer if it doesn't already exist tpoint := reflect.ValueOf(initializer) - if _, ok := p.registeredInitializers[tpoint]; !ok { + functionName := runtime.FuncForPC(tpoint.Pointer()).Name() + if _, ok := p.registeredInitializers[functionName]; !ok { p.globalInitializers = append(p.globalInitializers, initializer) - p.registeredInitializers[tpoint] = len(p.globalInitializers) + p.registeredInitializers[functionName] = len(p.globalInitializers) } } @@ -142,13 +144,14 @@ func (p *RESTAPI) RegisterGlobalInitializer(initializer GlobalInitializer) { //RegisterOperationInitializer add operation initializer if it's not already there func (p *RESTAPI) RegisterOperationInitializer(initializer OperationInitializer) { if p.registeredInitializers == nil { - p.registeredInitializers = make(map[reflect.Value]int) + p.registeredInitializers = make(map[string]int) } //only add initializer if it doesn't already exist tpoint := reflect.ValueOf(initializer) - if _, ok := p.registeredInitializers[tpoint]; !ok { + functionName := runtime.FuncForPC(tpoint.Pointer()).Name() + if _, ok := p.registeredInitializers[functionName]; !ok { p.operationInitializers = append(p.operationInitializers, initializer) - p.registeredInitializers[tpoint] = len(p.operationInitializers) + p.registeredInitializers[functionName] = len(p.operationInitializers) } }