MCPcopy
hub / github.com/livebud/bud / tryFunction

Function tryFunction

package/di/function.go:88–176  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

86// func ...(...) (*Web, error)
87// func ...(...) (Web, error)
88func 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()

Callers 1

FindMethod · 0.85

Calls 15

UnqualifyFunction · 0.92
ImportPathFunction · 0.92
BuiltinFunction · 0.92
ReceiverMethod · 0.80
ModuleMethod · 0.80
TypeMethod · 0.65
StringMethod · 0.65
FileMethod · 0.65
NameMethod · 0.65
DefinitionMethod · 0.65
PackageMethod · 0.65
KindMethod · 0.65

Tested by

no test coverage detected