-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdingo.go
71 lines (64 loc) · 2.96 KB
/
dingo.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
package dingo
import "strconv"
// Def is the structure containing a service definition.
type Def struct {
// Name is the key that is used to retrieve the object from the container.
Name string
// Scope determines in which container the object is stored.
// Typical scopes are "app" and "request".
Scope string
// NotForAutoFill should be set to true if you
// do not want to use this service automatically
// as a dependency in other services.
NotForAutoFill bool
// Build defines the service constructor. It can be either:
// - a pointer to a structure: (*MyStruct)(nil)
// - a factory function: func(any, any, ...) (any, error)
Build interface{}
// Params are used to assist the service constructor.
Params Params
// Close should be a function: func(any) error.
// With any being the type of the service.
Close interface{}
// Unshared is false by default. That means that the object is only created once in a given container.
// They are singleton and the same instance will be returned each time "Get", "SafeGet" or "Fill" is called.
// If you want to retrieve a new object every time, "Unshared" needs to be set to true.
Unshared bool
// Description is a text that describes the service.
// If provided, the description is used in the comments of the generated code.
Description string
}
// Params are used to assist the service constructor.
// If the Def.Build field is a pointer to a structure, the keys of the map should be
// among the names of the structure fields. These fields will be filled with
// the associated value in the map.
// If the Def.Build field is a function, it works the same way.
// But the key of the map should be the index
// of the function parameters (e.g.: "0", "1", ...).
//
// key=fieldName¦paramIndex value=any¦dingo.Service|dingo.AutoFill
type Params map[string]interface{}
// NewFuncParams creates a Params instance where the key of the map, is the index of the given parameter.
// It is usefull when using a Build function, to provide all the function parameters.
// e.g.: NewFuncParams("a", "b", "c") return Params{"0": "a", "1": "b", "2": "c"}
func NewFuncParams(params ...interface{}) Params {
p := make(Params, len(params))
for k, v := range params {
p[strconv.Itoa(k)] = v
}
return p
}
// Service can be used as Params value.
// It means that the field (or parameter) should be replaced
// by an other service. This service should be retrieved from the container.
type Service string
// AutoFill can be used as Params value to avoid autofill (default is Autofill(true)).
// If a structure field is not in the Params map, the container will try to use
// a service from the container that has the same type.
// Setting the entry to AutoFill(false) will let the field empty in the structure.
type AutoFill bool
// ContainerKey is a type that can be used as key in a context.Context.
// For example it can be use if you want to store
// a container in the Context of an http.Request.
// It is used in the generated C function.
type ContainerKey string