add FieldsOf to inject fields of a struct directly (#138)

This commit is contained in:
shantuo
2019-03-01 13:52:07 -08:00
committed by GitHub
parent 58e5de342a
commit 327f42724c
37 changed files with 869 additions and 56 deletions

28
wire.go
View File

@@ -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{}
}