-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunc.go
79 lines (64 loc) · 1.75 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*!*
* Copyright (c) 2025 Hangzhou Guanwaii Technology Co,.Ltd.
*
* This source code is licensed under the MIT License,
* which is located in the LICENSE file in the source tree's root directory.
*
* File: func.go
* Author: mingcheng ([email protected])
* File Created: 2025-03-12 13:23:45
*
* Modified By: mingcheng ([email protected])
* Last Modified: 2025-03-12 13:38:53
*/
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
}