-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathproxy_type.go
More file actions
55 lines (46 loc) · 1.45 KB
/
proxy_type.go
File metadata and controls
55 lines (46 loc) · 1.45 KB
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
package goldi
import (
"fmt"
"reflect"
"unicode"
)
type proxyType struct {
typeID *TypeID
args []interface{}
}
// NewProxyType returns a TypeFactory that uses a function of another type to generate a result.
//
// Goldigen yaml syntax example:
// logger:
// factory: "@logger_provider::GetLogger"
// args: [ "My Logger" ]
func NewProxyType(typeID, functionName string, args ...interface{}) TypeFactory {
if functionName == "" || unicode.IsLower(rune(functionName[0])) {
return newInvalidType(fmt.Errorf("can not use unexported method %q as second argument to NewProxyType", functionName))
}
return &proxyType{
typeID: NewTypeID("@" + typeID + "::" + functionName),
args: args,
}
}
func (t *proxyType) Arguments() []interface{} {
args := make([]interface{}, len(t.args)+1)
args[0] = "@" + t.typeID.ID
for i, a := range t.args {
args[i+1] = a
}
return args
}
func (t *proxyType) Generate(resolver *ParameterResolver) (interface{}, error) {
referencedType, err := resolver.Container.Get(t.typeID.ID)
if err != nil {
return nil, fmt.Errorf("could not generate proxy type %s : type %s does not exist", t.typeID, t.typeID.ID)
}
v := reflect.ValueOf(referencedType)
method := v.MethodByName(t.typeID.FuncReferenceMethod)
if method.IsValid() == false {
return nil, fmt.Errorf("could not generate proxy type %s : method does not exist", t.typeID)
}
t2 := NewType(method.Interface(), t.args...)
return t2.Generate(resolver)
}