Skip to content
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

Add functionality for Protocol Buffers stable marshaling #588

Merged
merged 1 commit into from
Jul 2, 2024

Conversation

cthulhu-rider
Copy link
Contributor

#583 became a fat sketch. Starting from this, changes will come partially leaving code ambiguity while facilitating review

@cthulhu-rider cthulhu-rider force-pushed the feature/protobuf-encoding branch 2 times, most recently from 92adf36 to af67961 Compare June 26, 2024 18:57
internal/proto/encoding.go Outdated Show resolved Hide resolved
// MarshalBytes encodes 'bytes' or 'string' protobuf field with given number and
// value into b and returns the number of bytes written. If the buffer is too
// small, MarshalBytes will panic.
func MarshalBytes[T Bytes](b []byte, num protowire.Number, v T) int {
Copy link
Member

@carpawell carpawell Jun 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think i would have failed to find string here and it would have confused me

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

name *BytesOrString or add 4 more functions (incl. repeated)?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

imo the clearest solution is more funcs (why 4?), but i understand that it may look like a waste of code

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SizeString, MarshalString, SizeRepeatedString, MarshalRepeatedString

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, sizes, got you

// MarshalDouble encodes 'double' protobuf field with given number and value
// into b and returns the number of bytes written. If the buffer is too small,
// MarshalDouble will panic.
func MarshalDouble(b []byte, num protowire.Number, v float64) int {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is it Double? api-go has it as float64 as i remember. i expect this package to be a translator b/w "go language" and "profobuff language"

Copy link
Contributor Author

@cthulhu-rider cthulhu-rider Jun 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these are encoders of protobuf fields, so double

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

they are marshallers of the go's types into some raw data, it is MarshalDouble(... v float64), why? it was like this before and i agree with it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not some raw data but protobuf. Any bool, proto.Integer or float32 can be marshalled into double, and float64 - into uint64 or fixed64 or even bytes. With this, Double wins

what i can suggest is to call it MarshalToDouble - would this reduce ur doubts?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any bool, proto.Integer or float32 can be marshalled into double, and float64 - into uint64 or fixed64 or even bytes

do we marshal or plan to marshal bool, etc. into double? all i want is to have it as clear as possible. i expect a go struct that stores fields for double to have it float64. anyway, you do not call it MarshalFloat64ToDouble, so you do not plan to marshal anything except float64 (me neither), why isn't it MarshalFloat64 (a func that chooses the most appropriate container (double) for the provided go type)?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MarshalToY is a good compromise for me that i can accept. but i have to mention that is not a flexible approach when we are talking about two different types (e.g float64 and float32 to double)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

im talking about Go->protobuf uint64->fixed64 and uint64->uint64

btw, is it a real example? do we have it? bad coding if you store smth in uint64 and do not know what proto container will be chosen (mb uint64, mb fixed64), imo

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

example: this and this

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MarshalToY is a good compromise for me that i can accept

so it's decided

not a flexible approach when we are talking about two different types (e.g float64 and float32 to double)

normally, generics will help. Otherwise, we'll reconsider naming pattern

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

renamed

This introduces `proto` package providing specific protobuf encoding -
strict ascending field order. The format is used in NeoFS for
cryptographic signatures requiring consistent checksums. Same lib is
provided by `github.com/nspcc-dev/neofs-api-go/v2` module, but it is
planned to be deprecated soon.

Although all functionality is duplicated, the code is improved with
generic typing that have appeared during this time.

The library is intended to be used by other SDK packages, and does not
yet imply external import.

Signed-off-by: Leonard Lyubich <leonard@morphbits.io>
@carpawell carpawell merged commit 81aa461 into master Jul 2, 2024
7 of 10 checks passed
@carpawell carpawell deleted the feature/protobuf-encoding branch July 2, 2024 23:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants