Skip to content

Commit

Permalink
refactor: implement SizeVT on RawMessage
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Stewart <christian@aperture.us>
  • Loading branch information
paralin committed May 11, 2024
1 parent eb038fc commit eafdd1b
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 8 deletions.
2 changes: 1 addition & 1 deletion go.mod
Expand Up @@ -9,7 +9,7 @@ replace github.com/libp2p/go-libp2p => github.com/aperturerobotics/go-libp2p v0.
replace nhooyr.io/websocket => github.com/paralin/nhooyr-websocket v1.8.12-0.20240504231911-2358de657064 // aperture-1

require (
github.com/aperturerobotics/protobuf-go-lite v0.6.2 // latest
github.com/aperturerobotics/protobuf-go-lite v0.6.3 // latest
github.com/aperturerobotics/util v1.23.1 // latest
)

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Expand Up @@ -2,8 +2,8 @@ github.com/aperturerobotics/go-libp2p v0.33.1-0.20240504075939-591fc65373be h1:P
github.com/aperturerobotics/go-libp2p v0.33.1-0.20240504075939-591fc65373be/go.mod h1:gA6iEEVpQcx0xpygG/U0wkm+DfII4zoPc3EWViXC5b0=
github.com/aperturerobotics/json-iterator-lite v1.0.0 h1:cihbrYWoK/S2RYXhJLpDZd+GUjVvFJN+D3w1VOqqHRI=
github.com/aperturerobotics/json-iterator-lite v1.0.0/go.mod h1:snaApCEDtrHHP6UWSLKiYNOZU9A5NyzccKenx9oZEzg=
github.com/aperturerobotics/protobuf-go-lite v0.6.2 h1:ee0+7IAh11lneWhR1R61prV2/7Nb/1x8muuHnqOKK7E=
github.com/aperturerobotics/protobuf-go-lite v0.6.2/go.mod h1:YTbfnUj3feSULhs8VgepAHFnF3wUc0CPj4jd2axy21I=
github.com/aperturerobotics/protobuf-go-lite v0.6.3 h1:AZ821ntYqzCpq6jE5rWJwJLikYbaa63Nka7YfW9EJdE=
github.com/aperturerobotics/protobuf-go-lite v0.6.3/go.mod h1:YTbfnUj3feSULhs8VgepAHFnF3wUc0CPj4jd2axy21I=
github.com/aperturerobotics/util v1.23.1 h1:pEeBrN5UUNy83OFyv9d7BQod8O++j5ZwIkEuYGdpRXw=
github.com/aperturerobotics/util v1.23.1/go.mod h1:dO8P8Ut5xNPpLrPPpn7kM/aJL+qT20C5Ppcs1GE0VNg=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down
2 changes: 1 addition & 1 deletion hack/go.mod
Expand Up @@ -8,7 +8,7 @@ replace google.golang.org/protobuf => github.com/aperturerobotics/protobuf-go v1

require (
github.com/aperturerobotics/goprotowrap v0.3.1
github.com/aperturerobotics/protobuf-go-lite v0.6.2
github.com/aperturerobotics/protobuf-go-lite v0.6.3
github.com/aperturerobotics/starpc v0.28.1
)

Expand Down
4 changes: 2 additions & 2 deletions hack/go.sum
Expand Up @@ -80,8 +80,8 @@ github.com/aperturerobotics/json-iterator-lite v1.0.0 h1:cihbrYWoK/S2RYXhJLpDZd+
github.com/aperturerobotics/json-iterator-lite v1.0.0/go.mod h1:snaApCEDtrHHP6UWSLKiYNOZU9A5NyzccKenx9oZEzg=
github.com/aperturerobotics/protobuf-go v1.33.1-0.20240411062030-e36f75e0a3b8 h1:Juij9Gj5jVOJjqiu3EGHWnX9nH6fkBeGGu/3U8i5qss=
github.com/aperturerobotics/protobuf-go v1.33.1-0.20240411062030-e36f75e0a3b8/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
github.com/aperturerobotics/protobuf-go-lite v0.6.2 h1:ee0+7IAh11lneWhR1R61prV2/7Nb/1x8muuHnqOKK7E=
github.com/aperturerobotics/protobuf-go-lite v0.6.2/go.mod h1:YTbfnUj3feSULhs8VgepAHFnF3wUc0CPj4jd2axy21I=
github.com/aperturerobotics/protobuf-go-lite v0.6.3 h1:AZ821ntYqzCpq6jE5rWJwJLikYbaa63Nka7YfW9EJdE=
github.com/aperturerobotics/protobuf-go-lite v0.6.3/go.mod h1:YTbfnUj3feSULhs8VgepAHFnF3wUc0CPj4jd2axy21I=
github.com/aperturerobotics/util v1.23.1 h1:pEeBrN5UUNy83OFyv9d7BQod8O++j5ZwIkEuYGdpRXw=
github.com/aperturerobotics/util v1.23.1/go.mod h1:dO8P8Ut5xNPpLrPPpn7kM/aJL+qT20C5Ppcs1GE0VNg=
github.com/ashanbrown/forbidigo v1.6.0 h1:D3aewfM37Yb3pxHujIPSpTf6oQk9sc9WZi8gerOIVIY=
Expand Down
21 changes: 19 additions & 2 deletions srpc/message.go
@@ -1,9 +1,12 @@
package srpc

import protobuf_go_lite "github.com/aperturerobotics/protobuf-go-lite"
import (
"errors"

protobuf_go_lite "github.com/aperturerobotics/protobuf-go-lite"
)

// Message is the vtprotobuf message interface.
// TODO use VTMessage interface
type Message = protobuf_go_lite.Message

// RawMessage is a raw protobuf message container.
Expand Down Expand Up @@ -68,5 +71,19 @@ func (m *RawMessage) UnmarshalVT(data []byte) error {
return nil
}

// SizeVT returns the size of the message when marshaled.
func (m *RawMessage) SizeVT() int {
return len(m.data)
}

// MarshalToSizedBufferVT marshals to a buffer that already is SizeVT bytes long.
func (m *RawMessage) MarshalToSizedBufferVT(dAtA []byte) (int, error) {
if len(dAtA) != len(m.data) {
return 0, errors.New("invalid buffer length")
}
copy(dAtA, m.data)
return len(dAtA), nil
}

// _ is a type assertion
var _ Message = ((*RawMessage)(nil))

0 comments on commit eafdd1b

Please sign in to comment.