mirror of
https://github.com/v2fly/v2ray-core.git
synced 2024-11-18 10:26:01 -05:00
32 lines
818 B
Go
32 lines
818 B
Go
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)
|
|
}
|