Make roundTrip generic on response type

This commit is contained in:
Ian Gulliver
2026-04-03 17:47:03 +09:00
parent 21ea6c9332
commit d6541a76ed

View File

@@ -70,21 +70,23 @@ func (c *Client) receive(expectedID uint32) (any, error) {
return inner, nil return inner, nil
} }
func (c *Client) roundTrip(req any) (any, error) { func roundTrip[T any](c *Client, req any) (*T, error) {
id, err := c.send(req) id, err := c.send(req)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return c.receive(id) resp, err := c.receive(id)
if err != nil {
return nil, err
}
typed, ok := resp.(*T)
if !ok {
return nil, fmt.Errorf("unexpected response: %T", resp)
}
return typed, nil
} }
func (c *Client) PICOBOOT() error { func (c *Client) PICOBOOT() error {
resp, err := c.roundTrip(&RequestPICOBOOT{}) _, err := roundTrip[ResponsePICOBOOT](c, &RequestPICOBOOT{})
if err != nil {
return err return err
} }
if _, ok := resp.(*ResponsePICOBOOT); !ok {
return fmt.Errorf("unexpected response: %T", resp)
}
return nil
}