Make roundTrip generic on response type
This commit is contained in:
@@ -70,21 +70,23 @@ func (c *Client) receive(expectedID uint32) (any, error) {
|
||||
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)
|
||||
if err != nil {
|
||||
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 {
|
||||
resp, err := c.roundTrip(&RequestPICOBOOT{})
|
||||
if err != nil {
|
||||
_, err := roundTrip[ResponsePICOBOOT](c, &RequestPICOBOOT{})
|
||||
return err
|
||||
}
|
||||
if _, ok := resp.(*ResponsePICOBOOT); !ok {
|
||||
return fmt.Errorf("unexpected response: %T", resp)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user