Skip to content

Commit

Permalink
Made the key for the initializer map a string based on the name of th…
Browse files Browse the repository at this point in the history
…e function
  • Loading branch information
akeemphilbert committed Jul 19, 2023
1 parent 042f4c6 commit b203116
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions controllers/rest/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"net/http"
"os"
"reflect"
"runtime"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -128,27 +129,29 @@ 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)
}

}

//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)
}

}
Expand Down

0 comments on commit b203116

Please sign in to comment.