(ctx context.Context, client pluginClient, method string, request any)
| 138 | } |
| 139 | |
| 140 | func callPlugin[T any](ctx context.Context, client pluginClient, method string, request any) (T, error) { |
| 141 | var zero T |
| 142 | rawRequest, errMarshal := json.Marshal(sanitizePluginRequest(request)) |
| 143 | if errMarshal != nil { |
| 144 | return zero, fmt.Errorf("marshal plugin request %s: %w", method, errMarshal) |
| 145 | } |
| 146 | rawResp, errCall := client.Call(ctx, method, rawRequest) |
| 147 | if errCall != nil { |
| 148 | return zero, errCall |
| 149 | } |
| 150 | var envelope pluginabi.Envelope |
| 151 | if errUnmarshal := json.Unmarshal(rawResp, &envelope); errUnmarshal != nil { |
| 152 | return zero, fmt.Errorf("decode plugin envelope %s: %w", method, errUnmarshal) |
| 153 | } |
| 154 | out, errDecode := decodeEnvelopeResult[T](envelope) |
| 155 | if errDecode != nil { |
| 156 | if !envelope.OK { |
| 157 | return zero, errDecode |
| 158 | } |
| 159 | return zero, fmt.Errorf("decode plugin result %s: %w", method, errDecode) |
| 160 | } |
| 161 | return out, nil |
| 162 | } |
| 163 | |
| 164 | func sanitizePluginRequest(request any) any { |
| 165 | switch req := request.(type) { |
no test coverage detected