-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunc.go
65 lines (51 loc) · 1.37 KB
/
func.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
package simplyddns
import (
"context"
"fmt"
"net"
)
type SourceFunc func(context.Context, *SourceConfig) (*net.IP, error)
type TargetFunc func(context.Context, *net.IP, *TargetConfig) error
var (
sourceFuncs map[string]SourceFunc
targetFuncs map[string]TargetFunc
)
func init() {
sourceFuncs = make(map[string]SourceFunc)
targetFuncs = make(map[string]TargetFunc)
}
func GetAllSupportSourceFunc() []string {
var funcNames []string
for k := range sourceFuncs {
funcNames = append(funcNames, k)
}
return funcNames
}
func SourceFuncByName(name string) (fn SourceFunc, err error) {
if fn = sourceFuncs[name]; fn == nil {
err = fmt.Errorf("the source function which name %s is not found", name)
return
}
return
}
func RegisterSourceFunc(name string, fn SourceFunc) (err error) {
if found, _ := SourceFuncByName(name); found != nil {
return fmt.Errorf("source func %s is already registered", name)
}
sourceFuncs[name] = fn
return nil
}
func TargetFuncByName(name string) (fn TargetFunc, err error) {
if fn = targetFuncs[name]; fn == nil {
err = fmt.Errorf("the target function which name %s is not found", name)
return
}
return
}
func RegisterTargetFunc(name string, fn TargetFunc) (err error) {
if found, _ := TargetFuncByName(name); found != nil {
return fmt.Errorf("target func %s is already registered", name)
}
targetFuncs[name] = fn
return nil
}