-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
alias.go
86 lines (78 loc) · 3.12 KB
/
alias.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
package sprout
// FunctionAliasMap is a map that stores a list of aliases for each function.
type FunctionAliasMap = map[string][]string
// AssignAliases assigns all aliases defined in the handler to their original
// functions. This function is used to ensure that all aliases are properly
// associated with their original functions in the handler instance.
//
// It should be called after all functions and aliases have been added and
// inside the Build function in case of using a custom handler.
func AssignAliases(h Handler) {
for originalName, aliases := range h.RawAliases() {
fn, exists := h.RawFunctions()[originalName]
if !exists {
continue
}
for _, alias := range aliases {
h.RawFunctions()[alias] = fn
}
}
}
// WithAlias returns a FunctionOption[**DefaultHandler] that associates one or more alias
// names with an original function name.
// This allows the function to be called by any of its aliases.
//
// originalFunction specifies the original function name to which aliases will
// be added. aliases is a variadic parameter that takes one or more strings as
// aliases for the original function.
//
// The function does nothing if no aliases are provided.
// If the original function name does not already have associated aliases in
// the DefaultHandler, a new slice of strings is created to hold its aliases.
// Each provided alias is then appended to this slice.
//
// This option must be applied to a DefaultHandler using the DefaultHandler's
// options mechanism for the aliases to take effect.
//
// Example:
//
// handler := New(WithAlias("originalFunc", "alias1", "alias2"))
func WithAlias(originalFunction string, aliases ...string) HandlerOption[*DefaultHandler] {
return func(p *DefaultHandler) error {
if len(aliases) == 0 {
return nil
}
if _, ok := p.cachedFuncsAlias[originalFunction]; !ok {
p.cachedFuncsAlias[originalFunction] = make([]string, 0)
}
p.cachedFuncsAlias[originalFunction] = append(p.cachedFuncsAlias[originalFunction], aliases...)
return nil
}
}
// WithAliases returns a FunctionOption[**DefaultHandler] that configures multiple aliases
// for function names in a single call. It allows a batch of functions to be
// associated with their respective aliases, facilitating the creation of
// aliases for multiple functions at once.
//
// This option must be applied to a DefaultHandler using the DefaultHandler's
// options mechanism for the aliases to take effect.
// It complements the WithAlias function by providing a means to configure
// multiple aliases in one operation, rather than one at a time.
//
// Example:
//
// handler := New(WithAliases(sprout.FunctionAliasMap{
// "originalFunc1": {"alias1_1", "alias1_2"},
// "originalFunc2": {"alias2_1", "alias2_2"},
// }))
func WithAliases(aliases FunctionAliasMap) HandlerOption[*DefaultHandler] {
return func(p *DefaultHandler) error {
for originalFunction, aliasList := range aliases {
if _, ok := p.cachedFuncsAlias[originalFunction]; !ok {
p.cachedFuncsAlias[originalFunction] = make([]string, 0)
}
p.cachedFuncsAlias[originalFunction] = append(p.cachedFuncsAlias[originalFunction], aliasList...)
}
return nil
}
}