add functionality to override service in registry

This commit is contained in:
Marcus Efraimsson 2018-10-30 13:37:30 +01:00
parent 6f9c0241af
commit d0c00388e6
No known key found for this signature in database
GPG Key ID: EBFE0FB04612DD4A

View File

@ -29,11 +29,42 @@ func Register(descriptor *Descriptor) {
} }
func GetServices() []*Descriptor { func GetServices() []*Descriptor {
sort.Slice(services, func(i, j int) bool { slice := getServicesWithOverrides()
return services[i].InitPriority > services[j].InitPriority
sort.Slice(slice, func(i, j int) bool {
return slice[i].InitPriority > slice[j].InitPriority
}) })
return services return slice
}
type OverrideServiceFunc func(descriptor Descriptor) (*Descriptor, bool)
var overrides []OverrideServiceFunc
func RegisterOverride(fn OverrideServiceFunc) {
overrides = append(overrides, fn)
}
func getServicesWithOverrides() []*Descriptor {
slice := []*Descriptor{}
for _, s := range services {
var descriptor *Descriptor
for _, fn := range overrides {
if newDescriptor, override := fn(*s); override {
descriptor = newDescriptor
break
}
}
if descriptor != nil {
slice = append(slice, descriptor)
} else {
slice = append(slice, s)
}
}
return slice
} }
// Service interface is the lowest common shape that services // Service interface is the lowest common shape that services