-
Notifications
You must be signed in to change notification settings - Fork 18
/
registrations.go
225 lines (191 loc) · 5.83 KB
/
registrations.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
// SPDX-FileCopyrightText: 2023 SAP SE or an SAP affiliate company and Open Component Model contributors.
//
// SPDX-License-Identifier: Apache-2.0
// Package registrations provides a hierarchical namespace for
// denoting any kind of handlers to be registered on some target.
// Handlers are denoted by names evaluated by HandlerRegistrationHandler
// Such a registration handler is responsible vor a complete sub namespace
// and may delegate the evaluation to nested handler mounted on a sub namespace.
package registrations
import (
"fmt"
"strings"
"sync"
"golang.org/x/exp/slices"
"github.com/open-component-model/ocm/pkg/errors"
"github.com/open-component-model/ocm/pkg/generics"
"github.com/open-component-model/ocm/pkg/utils"
)
type HandlerConfig interface{}
type HandlerRegistrationHandler[T any, O any] interface {
RegisterByName(handler string, target T, config HandlerConfig, opts ...O) (bool, error)
GetHandlers(T) HandlerInfos
}
type HandlerRegistrationRegistry[T any, O any] interface {
HandlerRegistrationRegistryAccess[T, O]
GetAllRegistrationHandlers() []*RegistrationHandlerInfo[T, O]
Copy() HandlerRegistrationRegistry[T, O]
}
type HandlerRegistrationRegistryAccess[T any, O any] interface {
HandlerRegistrationHandler[T, O]
RegisterRegistrationHandler(path string, handler HandlerRegistrationHandler[T, O])
GetRegistrationHandlers(name string) []*RegistrationHandlerInfo[T, O]
}
type NamePath []string
func NewNamePath(path string) NamePath {
if path == "" {
return nil
}
return strings.Split(path, "/")
}
func (p NamePath) Compare(o NamePath) int {
if d := len(p) - len(o); d != 0 {
return d
}
for i, e := range p {
if d := strings.Compare(e, o[i]); d != 0 {
return d
}
}
return 0
}
func (p NamePath) IsPrefixOf(o NamePath) bool {
if len(p) > len(o) {
return false
}
for i, e := range p {
if e != o[i] {
return false
}
}
return true
}
func (p NamePath) String() string {
return strings.Join(p, "/")
}
type RegistrationHandlerInfo[T any, O any] struct {
prefix NamePath
handler HandlerRegistrationHandler[T, O]
}
func NewRegistrationHandlerInfo[T any, O any](path string, handler HandlerRegistrationHandler[T, O]) *RegistrationHandlerInfo[T, O] {
return &RegistrationHandlerInfo[T, O]{
prefix: NewNamePath(path),
handler: handler,
}
}
func (i *RegistrationHandlerInfo[T, O]) RegisterByName(handler string, target T, config HandlerConfig, opts ...O) (bool, error) {
path := NewNamePath(handler)
if !i.prefix.IsPrefixOf(path) {
return false, nil
}
return i.handler.RegisterByName(strings.Join(path[len(i.prefix):], "/"), target, config, opts...)
}
func (i *RegistrationHandlerInfo[T, O]) GetHandlers(target T) HandlerInfos {
infos := i.handler.GetHandlers(target)
if len(infos) > 0 {
prefix := i.prefix.String()
if prefix != "" {
for i := range infos {
infos[i].Name = prefix + generics.Conditional(infos[i].Name == "", "", "/"+infos[i].Name)
}
}
}
return infos
}
type handlerRegistrationRegistry[T any, O any] struct {
lock sync.RWMutex
base HandlerRegistrationRegistry[T, O]
handlers []*RegistrationHandlerInfo[T, O]
}
func NewHandlerRegistrationRegistry[T any, O any](base ...HandlerRegistrationRegistry[T, O]) HandlerRegistrationRegistry[T, O] {
return &handlerRegistrationRegistry[T, O]{base: utils.Optional(base...)}
}
func (c *handlerRegistrationRegistry[T, O]) GetAllRegistrationHandlers() []*RegistrationHandlerInfo[T, O] {
c.lock.Lock()
defer c.lock.Unlock()
r := slices.Clone(c.handlers)
if c.base != nil {
r = append(r, c.base.GetAllRegistrationHandlers()...)
}
return r
}
func (c *handlerRegistrationRegistry[T, O]) Copy() HandlerRegistrationRegistry[T, O] {
return &handlerRegistrationRegistry[T, O]{
handlers: c.GetAllRegistrationHandlers(),
}
}
func (c *handlerRegistrationRegistry[T, O]) RegisterRegistrationHandler(path string, handler HandlerRegistrationHandler[T, O]) {
c.lock.Lock()
defer c.lock.Unlock()
comps := strings.Split(path, "/")
n := &RegistrationHandlerInfo[T, O]{
prefix: comps,
handler: handler,
}
var i int
var h *RegistrationHandlerInfo[T, O]
for i, h = range c.handlers {
if h.prefix.Compare(comps) < 0 {
break
}
}
c.handlers = append(c.handlers[:i], append([]*RegistrationHandlerInfo[T, O]{n}, c.handlers[i:]...)...)
}
func (c *handlerRegistrationRegistry[T, O]) GetRegistrationHandlers(name string) []*RegistrationHandlerInfo[T, O] {
c.lock.RLock()
defer c.lock.RUnlock()
var result []*RegistrationHandlerInfo[T, O]
path := NewNamePath(name)
for _, h := range c.handlers {
if h.prefix.IsPrefixOf(path) {
result = append(result, h)
}
}
if c.base != nil {
base := c.base.GetRegistrationHandlers(name)
i := 0
for _, h := range base {
for i != len(result) && result[i].prefix.Compare(h.prefix) >= 0 {
i++
}
result = append(result[:i], append([]*RegistrationHandlerInfo[T, O]{h}, result[i:]...)...)
i++
}
}
return result
}
func (c *handlerRegistrationRegistry[T, O]) RegisterByName(handler string, target T, config HandlerConfig, opts ...O) (bool, error) {
list := c.GetRegistrationHandlers(handler)
errlist := errors.ErrListf("handler registration")
for _, h := range list {
ok, err := h.RegisterByName(handler, target, config, opts...)
if ok {
return ok, err
}
errlist.Add(err)
}
if errlist.Len() > 0 {
return false, errlist.Result()
}
return false, fmt.Errorf("no registration handler found for %s", handler)
}
func (c *handlerRegistrationRegistry[T, O]) GetHandlers(target T) HandlerInfos {
infos := HandlerInfos{}
for _, h := range c.handlers {
infos = append(infos, h.GetHandlers(target)...)
}
if c.base != nil {
infos = append(infos, c.base.GetHandlers(target)...)
}
set := generics.Set[string]{}
i := 0
for i < len(infos) {
if set.Contains(infos[i].Name) {
infos = append(infos[:i], infos[i+1:]...)
continue
}
set.Add(infos[i].Name)
i++
}
return infos
}