Check to see if the function initializes the dependency. Given the following dependency: *Web, tryFunction will match on the following functions: func ...(...) *Web func ...(...) Web func ...(...) (*Web, error) func ...(...) (Web, error)
(fn *parser.Function, importPath, dataType string)
| 86 | // func ...(...) (*Web, error) |
| 87 | // func ...(...) (Web, error) |
| 88 | func tryFunction(fn *parser.Function, importPath, dataType string) (*function, error) { |
| 89 | if fn.Private() || fn.Receiver() != nil { |
| 90 | return nil, ErrNoMatch |
| 91 | } |
| 92 | results := fn.Results() |
| 93 | if len(results) < 1 || len(results) > 2 { |
| 94 | return nil, ErrNoMatch |
| 95 | } |
| 96 | resultType := results[0].Type() |
| 97 | innerType := parser.Unqualify(resultType).String() |
| 98 | innerName := strings.TrimPrefix(innerType, "*") |
| 99 | depName := strings.TrimPrefix(dataType, "*") |
| 100 | if innerName != depName { |
| 101 | return nil, ErrNoMatch |
| 102 | } |
| 103 | typeImport, err := parser.ImportPath(resultType) |
| 104 | if err != nil { |
| 105 | return nil, err |
| 106 | } |
| 107 | if typeImport != importPath { |
| 108 | return nil, ErrNoMatch |
| 109 | } |
| 110 | fileImportPath, err := fn.File().Import() |
| 111 | if err != nil { |
| 112 | return nil, err |
| 113 | } |
| 114 | function := &function{ |
| 115 | Import: fileImportPath, |
| 116 | Name: fn.Name(), |
| 117 | } |
| 118 | for _, param := range fn.Params() { |
| 119 | pt := param.Type() |
| 120 | // Ensure there are no builtin types (e.g. string) as parameters |
| 121 | if gois.Builtin(pt.String()) { |
| 122 | return nil, ErrNoMatch |
| 123 | } |
| 124 | def, err := param.Definition() |
| 125 | if err != nil { |
| 126 | importPath, err := parser.ImportPath(pt) |
| 127 | if err != nil { |
| 128 | return nil, err |
| 129 | } |
| 130 | return nil, fmt.Errorf("di: unable to find definition for param %q.%s in %q.%s. %w", importPath, parser.Unqualify(pt).String(), importPath, dataType, err) |
| 131 | } |
| 132 | importPath, err := def.Package().Import() |
| 133 | if err != nil { |
| 134 | return nil, err |
| 135 | } |
| 136 | module := def.Package().Module() |
| 137 | function.Params = append(function.Params, &Type{ |
| 138 | Import: importPath, |
| 139 | Type: parser.Unqualify(pt).String(), |
| 140 | kind: def.Kind(), |
| 141 | module: module, |
| 142 | }) |
| 143 | } |
| 144 | for _, result := range results { |
| 145 | rt := result.Type() |
no test coverage detected