add FieldsOf to inject fields of a struct directly (#138)
This commit is contained in:
28
wire.go
28
wire.go
@@ -30,7 +30,8 @@ type ProviderSet struct{}
|
||||
|
||||
// NewSet creates a new provider set that includes the providers in its
|
||||
// arguments. Each argument is a function value, a struct (zero) value, a
|
||||
// provider set, a call to Bind, a call to Value, or a call to InterfaceValue.
|
||||
// provider set, a call to Bind, a call to Value, a call to InterfaceValue or a
|
||||
// call to FieldsOf.
|
||||
//
|
||||
// Passing a function value to NewSet declares that the function's first
|
||||
// return value type will be provided by calling the function. The arguments
|
||||
@@ -135,3 +136,28 @@ func Value(interface{}) ProvidedValue {
|
||||
func InterfaceValue(typ interface{}, x interface{}) ProvidedValue {
|
||||
return ProvidedValue{}
|
||||
}
|
||||
|
||||
// StructFields is a collection of the fields from a struct.
|
||||
type StructFields struct{}
|
||||
|
||||
// FieldsOf declares that the fields named of the given struct type will be used
|
||||
// to provide the types of those fields. The structType argument must be a
|
||||
// pointer to the struct or a pointer to a pointer to the struct it wishes to reference.
|
||||
//
|
||||
// The following example would provide *Foo and *Bar using S.MyFoo and S.MyBar respectively:
|
||||
//
|
||||
// type S struct {
|
||||
// MyFoo *Foo
|
||||
// MyBar *Bar
|
||||
// }
|
||||
//
|
||||
// func NewStruct() S { /* ... */ }
|
||||
// var Set = wire.NewSet(wire.FieldsOf(new(S), "MyFoo", "MyBar"))
|
||||
//
|
||||
// or
|
||||
//
|
||||
// func NewStruct() *S { /* ... */ }
|
||||
// var Set = wire.NewSet(wire.FieldsOf(new(*S), "MyFoo", "MyBar"))
|
||||
func FieldsOf(structType interface{}, fieldNames ...string) StructFields {
|
||||
return StructFields{}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user