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

feat: Support encoding/emitting nested structures with binary fields #1

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion parser/binary.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func DeconstructPacket(packet *Packet) (pack *Packet, buffers []types.BufferInte
ns.WriteVal(pack.Data)
buffers = ns.Attachment.([]types.BufferInterface)
ns.Flush()
pack.Data = buf.String()
pack.preSerializedData = buf.String()

attachments := uint64(len(buffers))
pack.Attachments = &attachments // number of binary 'attachments'
Expand Down
4 changes: 2 additions & 2 deletions parser/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ func (e *encoder) encodeAsString(packet *Packet) types.BufferInterface {
}
// json data
if nil != packet.Data {
if pds, is := packet.Data.(string); is {
if packet.preSerializedData != "" {
Copy link
Owner

Choose a reason for hiding this comment

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

I need to run more test cases to ensure that this request is OK. It may take longer.

Copy link
Author

Choose a reason for hiding this comment

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

There's no rush. I've already taken the encoder from this branch and I'm using it in my project that way via the config.Encoder.

// Already serialized in the DeconstructPacket function
str.WriteString(pds)
str.WriteString(packet.preSerializedData)
} else {
if b, err := json.Marshal(_encodeData(packet.Data)); err == nil {
str.Write(b)
Expand Down
3 changes: 3 additions & 0 deletions parser/is-binary.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ func HasBinary(data any) bool {
return false
}
dv := reflect.ValueOf(data)
if dv.Kind() == reflect.Pointer {
return IsBinary(dv.Elem().Interface())
}
if dv.Kind() == reflect.Struct {
Copy link
Owner

Choose a reason for hiding this comment

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

Maybe consider adding reflect.Ptr support.

Copy link
Author

Choose a reason for hiding this comment

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

Fair enough! I wasn't sure how well updated this library was and whether you'd like the PR at all since it feels a little hacky to me, so wanted to get something basic out first. :D Will add this...

Copy link
Owner

Choose a reason for hiding this comment

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

Well, given this feature, we can even do more, such as support for Array and Map.

Copy link
Author

Choose a reason for hiding this comment

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

done!

for fi := range dv.NumField() {
dfv := dv.Field(fi)
Expand Down
11 changes: 6 additions & 5 deletions parser/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ type (
PacketType byte

Packet struct {
Type PacketType `json:"type" mapstructure:"type" msgpack:"type"`
Nsp string `json:"nsp" mapstructure:"nsp" msgpack:"nsp"`
Data any `json:"data,omitempty" mapstructure:"data,omitempty" msgpack:"data,omitempty"`
Id *uint64 `json:"id,omitempty" mapstructure:"id,omitempty" msgpack:"id,omitempty"`
Attachments *uint64 `json:"attachments,omitempty" mapstructure:"attachments,omitempty" msgpack:"attachments,omitempty"`
Type PacketType `json:"type" mapstructure:"type" msgpack:"type"`
Nsp string `json:"nsp" mapstructure:"nsp" msgpack:"nsp"`
preSerializedData any
Data any `json:"data,omitempty" mapstructure:"data,omitempty" msgpack:"data,omitempty"`
Id *uint64 `json:"id,omitempty" mapstructure:"id,omitempty" msgpack:"id,omitempty"`
Attachments *uint64 `json:"attachments,omitempty" mapstructure:"attachments,omitempty" msgpack:"attachments,omitempty"`
}
)

Expand Down