MCPcopy
hub / github.com/sqlc-dev/sqlc / Invoke

Method Invoke

internal/ext/wasm/wasm.go:188–245  ·  view source on GitHub ↗
(ctx context.Context, method string, args any, reply any, opts ...grpc.CallOption)

Source from the content-addressed store, hash-verified

186}
187
188func (r *Runner) Invoke(ctx context.Context, method string, args any, reply any, opts ...grpc.CallOption) error {
189 req, ok := args.(protoreflect.ProtoMessage)
190 if !ok {
191 return status.Error(codes.InvalidArgument, "args isn't a protoreflect.ProtoMessage")
192 }
193
194 // Remove the pg_catalog schema. Its sheer size causes unknown issues with wasm plugins
195 genReq, ok := req.(*plugin.GenerateRequest)
196 if ok {
197 removePGCatalog(genReq)
198 req = genReq
199 }
200
201 stdinBlob, err := proto.Marshal(req)
202 if err != nil {
203 return fmt.Errorf("failed to encode codegen request: %w", err)
204 }
205
206 runtimeAndCode, err := r.loadAndCompile(ctx)
207 if err != nil {
208 return fmt.Errorf("loadBytes: %w", err)
209 }
210
211 var stderr, stdout bytes.Buffer
212
213 conf := wazero.NewModuleConfig().
214 WithName("").
215 WithArgs("plugin.wasm", method).
216 WithStdin(bytes.NewReader(stdinBlob)).
217 WithStdout(&stdout).
218 WithStderr(&stderr).
219 WithEnv("SQLC_VERSION", info.Version)
220 for _, key := range r.Env {
221 conf = conf.WithEnv(key, os.Getenv(key))
222 }
223
224 result, err := runtimeAndCode.rt.InstantiateModule(ctx, runtimeAndCode.code, conf)
225 if err == nil {
226 defer result.Close(ctx)
227 }
228 if cerr := checkError(err, stderr); cerr != nil {
229 return cerr
230 }
231
232 // Print WASM stdout
233 stdoutBlob := stdout.Bytes()
234
235 resp, ok := reply.(protoreflect.ProtoMessage)
236 if !ok {
237 return fmt.Errorf("reply isn't a GenerateResponse")
238 }
239
240 if err := proto.Unmarshal(stdoutBlob, resp); err != nil {
241 return err
242 }
243
244 return nil
245}

Callers

nothing calls this directly

Calls 5

loadAndCompileMethod · 0.95
removePGCatalogFunction · 0.85
checkErrorFunction · 0.85
CloseMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected