1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2026-01-03 15:55:20 -05:00

global config creator

This commit is contained in:
Darien Raymond
2017-01-12 22:47:10 +01:00
parent 5401cab7a5
commit db1c9131f0
14 changed files with 121 additions and 93 deletions

31
common/type.go Normal file
View File

@@ -0,0 +1,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.Name() + " is not registered.")
}
return creator(ctx, config)
}