-
Notifications
You must be signed in to change notification settings - Fork 98
codec: implement codec v2 #138
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jzelinskie
wants to merge
1
commit into
planetscale:main
Choose a base branch
from
jzelinskie:codecv2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,33 +1,63 @@ | ||||||
| package grpc | ||||||
|
|
||||||
| import "fmt" | ||||||
| import ( | ||||||
| "google.golang.org/grpc/encoding" | ||||||
| "google.golang.org/grpc/mem" | ||||||
|
|
||||||
| // Guarantee that the built-in proto is called registered before this one | ||||||
| // so that it can be replaced. | ||||||
| _ "google.golang.org/grpc/encoding/proto" | ||||||
| ) | ||||||
|
|
||||||
| // Name is the name registered for the proto compressor. | ||||||
| const Name = "proto" | ||||||
|
|
||||||
| type Codec struct{} | ||||||
|
|
||||||
| type vtprotoMessage interface { | ||||||
| MarshalVT() ([]byte, error) | ||||||
| MarshalToSizedBufferVT(data []byte) (int, error) | ||||||
| UnmarshalVT([]byte) error | ||||||
| SizeVT() int | ||||||
| } | ||||||
|
|
||||||
| type Codec struct { | ||||||
| fallback encoding.CodecV2 | ||||||
| } | ||||||
|
|
||||||
| func (Codec) Marshal(v interface{}) ([]byte, error) { | ||||||
| vt, ok := v.(vtprotoMessage) | ||||||
| if !ok { | ||||||
| return nil, fmt.Errorf("failed to marshal, message is %T (missing vtprotobuf helpers)", v) | ||||||
| func (Codec) Name() string { return Name } | ||||||
|
|
||||||
| func (c *Codec) Marshal(v any) (mem.BufferSlice, error) { | ||||||
| if m, ok := v.(vtprotoMessage); ok { | ||||||
| size := m.SizeVT() | ||||||
| if mem.IsBelowBufferPoolingThreshold(size) { | ||||||
| buf := make([]byte, 0, size) | ||||||
| if _, err := m.MarshalToSizedBufferVT(buf[:size]); err != nil { | ||||||
| return nil, err | ||||||
| } | ||||||
| return mem.BufferSlice{mem.SliceBuffer(buf)}, nil | ||||||
| } | ||||||
| pool := mem.DefaultBufferPool() | ||||||
| buf := pool.Get(size) | ||||||
| if _, err := m.MarshalToSizedBufferVT((*buf)[:size]); err != nil { | ||||||
| pool.Put(buf) | ||||||
| return nil, err | ||||||
| } | ||||||
| return mem.BufferSlice{mem.NewBuffer(buf, pool)}, nil | ||||||
| } | ||||||
| return vt.MarshalVT() | ||||||
|
|
||||||
| return c.fallback.Marshal(v) | ||||||
| } | ||||||
|
|
||||||
| func (Codec) Unmarshal(data []byte, v interface{}) error { | ||||||
| vt, ok := v.(vtprotoMessage) | ||||||
| if !ok { | ||||||
| return fmt.Errorf("failed to unmarshal, message is %T (missing vtprotobuf helpers)", v) | ||||||
| func (c *Codec) Unmarshal(data mem.BufferSlice, v any) error { | ||||||
| if m, ok := v.(vtprotoMessage); ok { | ||||||
| buf := data.MaterializeToBuffer(mem.DefaultBufferPool()) | ||||||
| defer buf.Free() | ||||||
| return m.UnmarshalVT(buf.ReadOnlyData()) | ||||||
| } | ||||||
| return vt.UnmarshalVT(data) | ||||||
|
|
||||||
| return c.fallback.Unmarshal(data, v) | ||||||
| } | ||||||
|
|
||||||
| func (Codec) Name() string { | ||||||
| return Name | ||||||
| func init() { | ||||||
| encoding.RegisterCodecV2(&Codec{ | ||||||
| fallback: encoding.GetCodecV2("proto"), | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can use name from proto so we will avoid unused import https://pkg.go.dev/google.golang.org/grpc/encoding/proto#pkg-constants
Suggested change
|
||||||
| }) | ||||||
| } | ||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,22 @@ | ||
| module github.com/planetscale/vtprotobuf | ||
|
|
||
| go 1.20 | ||
| go 1.21 | ||
|
|
||
| toolchain go1.23.0 | ||
|
|
||
| require ( | ||
| github.com/stretchr/testify v1.8.4 | ||
| google.golang.org/grpc v1.58.2 | ||
| google.golang.org/protobuf v1.31.0 | ||
| google.golang.org/grpc v1.67.0-dev.0.20240821211139-9ab8b62505c8 | ||
| google.golang.org/protobuf v1.34.2 | ||
| ) | ||
|
|
||
| require ( | ||
| github.com/davecgh/go-spew v1.1.1 // indirect | ||
| github.com/golang/protobuf v1.5.3 // indirect | ||
| github.com/pmezard/go-difflib v1.0.0 // indirect | ||
| golang.org/x/net v0.14.0 // indirect | ||
| golang.org/x/sys v0.11.0 // indirect | ||
| golang.org/x/text v0.12.0 // indirect | ||
| google.golang.org/genproto/googleapis/rpc v0.0.0-20230822172742-b8732ec3820d // indirect | ||
| golang.org/x/net v0.28.0 // indirect | ||
| golang.org/x/sys v0.24.0 // indirect | ||
| golang.org/x/text v0.17.0 // indirect | ||
| google.golang.org/genproto/googleapis/rpc v0.0.0-20240814211410-ddb44dafa142 // indirect | ||
| gopkg.in/yaml.v3 v3.0.1 // indirect | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a bug on this line IMO and it should go like either
make([]byte, size)ormem.BufferSlice{mem.SliceBuffer(buf[:size])}on the line 35. Because the original slice reference will have its len still set to 0 effectively returning 0 length byte array from the method.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes! Apologies -- I have this fixed in my PR for SpiceDB but didn't carry over the changes here.