rsocket / rsocket-go

rsocket-go implementation
Apache License 2.0
511 stars 44 forks source link

Support Protobuf #3

Closed szihai closed 5 years ago

szihai commented 5 years ago

Protobuf is an important encoding mechanism. It is especially important for golang since GRpc is widely used by go programmers.

robertroeser commented 5 years ago

Once rsocket-go has the interaction basic interaction models working we can create rsocket-rpc-go pretty easily like .NET, Java, JavaScript, etc

szihai commented 5 years ago

Cool. But let's keep this issue open as a place holder for the task.

fiorix commented 5 years ago

Why would you add protobuf to this code? Users should be able to set their MIME for metadata and data and use whatever serialization their applications need. As a transport, rsocket should be agnostic to serialization.

jjeffcaii commented 5 years ago

Implement payload.Payload interface. Here're some examples:

package example

import (
    "encoding/hex"

    "github.com/golang/protobuf/proto"
    "github.com/rsocket/rsocket-go/payload"
)

type ProtobufPayload struct {
    d proto.Message
    m []byte
}

func (p *ProtobufPayload) Metadata() (metadata []byte, ok bool) {
    if len(p.m) < 1 {
        return
    }
    return p.m, true
}

func (p *ProtobufPayload) MetadataUTF8() (metadata string, ok bool) {
    if len(p.m) < 1 {
        return
    }
    return string(p.m), true
}

func (p *ProtobufPayload) Data() []byte {
    bs, err := proto.Marshal(p.d)
    if err != nil {
        panic(err)
    }
    return bs
}

func (p *ProtobufPayload) DataUTF8() string {
    bs, err := proto.Marshal(p.d)
    if err != nil {
        panic(err)
    }
    return hex.EncodeToString(bs)
}

func (p *ProtobufPayload) Release() {
    // ignore
}

func NewProtobufPayload(data proto.Message, metadata []byte) payload.Payload {
    return &ProtobufPayload{
        d: data,
        m: metadata,
    }
}