forked from v2ray/v2ray-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
type.go
31 lines (26 loc) · 818 Bytes
/
type.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
package common
import (
"context"
"errors"
"reflect"
)
type creator func(ctx context.Context, config interface{}) (interface{}, error)
var (
typeCreatorRegistry = make(map[reflect.Type]creator)
)
func RegisterConfig(config interface{}, configCreator creator) error {
configType := reflect.TypeOf(config)
if _, found := typeCreatorRegistry[configType]; found {
return errors.New("Common: " + configType.Name() + " is already registered.")
}
typeCreatorRegistry[configType] = configCreator
return nil
}
func CreateObject(ctx context.Context, config interface{}) (interface{}, error) {
configType := reflect.TypeOf(config)
creator, found := typeCreatorRegistry[configType]
if !found {
return nil, errors.New("Common: " + configType.String() + " is not registered.")
}
return creator(ctx, config)
}