createConstructor validates the given ConstructorFunc and builds a Constructor metadata struct. It ensures that the function is non-nil, is of kind Func, and returns exactly one result. If valid, it extracts the argument types and returns a populated Constructor.
(fn ConstructorFunc)
| 34 | // It ensures that the function is non-nil, is of kind Func, and returns exactly one result. |
| 35 | // If valid, it extracts the argument types and returns a populated Constructor. |
| 36 | func createConstructor(fn ConstructorFunc) (Constructor, error) { |
| 37 | if fn == nil { |
| 38 | return Constructor{}, fmt.Errorf("nil constructor") |
| 39 | } |
| 40 | |
| 41 | fnType := reflect.TypeOf(fn) |
| 42 | if fnType.Kind() != reflect.Func { |
| 43 | return Constructor{}, fmt.Errorf("constructor must be a function") |
| 44 | } |
| 45 | |
| 46 | if fnType.NumOut() != 1 { |
| 47 | return Constructor{}, fmt.Errorf("constructor must only return one result") |
| 48 | } |
| 49 | |
| 50 | return Constructor{ |
| 51 | fnType: fnType, |
| 52 | fnValue: reflect.ValueOf(fn), |
| 53 | args: extractConstructorArgs(fnType), |
| 54 | }, nil |
| 55 | } |
| 56 | |
| 57 | // OutType returns the type of the constructor function's output. |
| 58 | func (f Constructor) OutType() reflect.Type { |