Skip to content

Commit

Permalink
Merge pull request #21 from muzzammilshahid/yield-parser
Browse files Browse the repository at this point in the history
unpack payload in yield parser
  • Loading branch information
muzzammilshahid authored Jun 14, 2024
2 parents c77249f + fe19387 commit d8b674a
Showing 1 changed file with 51 additions and 23 deletions.
74 changes: 51 additions & 23 deletions go/parsers/yield.go
Original file line number Diff line number Diff line change
@@ -1,58 +1,86 @@
package parsers

import (
"log"
"sync"

"google.golang.org/protobuf/proto"

"github.com/xconnio/wampproto-go/messages"
"github.com/xconnio/wampproto-protobuf/go/gen"
)

type yieldFields struct {
type Yield struct {
gen *gen.Yield

args []any
kwArgs map[string]any
once sync.Once
}

func NewYieldFields(gen *gen.Yield) messages.YieldFields {
return &yieldFields{gen: gen}
return &Yield{gen: gen}
}

func (r *yieldFields) RequestID() int64 {
return r.gen.GetRequestId()
func (y *Yield) RequestID() int64 {
return y.gen.GetRequestId()
}

func (r *yieldFields) Options() map[string]any {
func (y *Yield) Options() map[string]any {
return map[string]any{}
}

func (r *yieldFields) Args() []any {
return nil
func (y *Yield) unpack() {
unpacked, err := FromCBORPayload(y.Payload())
if err != nil {
log.Println("error parsing CBOR payload:", err)
} else {
y.args = unpacked.Args()
y.kwArgs = unpacked.KwArgs()
}
}

func (y *Yield) Args() []any {
y.once.Do(y.unpack)
return y.args
}

func (r *yieldFields) KwArgs() map[string]any {
return nil
func (y *Yield) KwArgs() map[string]any {
y.once.Do(y.unpack)
return y.kwArgs
}

func (r *yieldFields) PayloadIsBinary() bool {
func (y *Yield) PayloadIsBinary() bool {
return true
}

func (r *yieldFields) Payload() []byte {
return r.gen.GetPayload()
func (y *Yield) Payload() []byte {
return y.gen.GetPayload()
}

func (r *yieldFields) PayloadSerializer() int {
return int(r.gen.GetPayloadSerializer())
func (y *Yield) PayloadSerializer() int {
return int(y.gen.GetPayloadSerializer())
}

func YieldToProtobuf(yield *messages.Yield) ([]byte, error) {
payload, serializer, err := ToCBORPayload(yield)
if err != nil {
return nil, err
}

msg := &gen.Yield{
RequestId: yield.RequestID(),
PayloadSerializer: int32(serializer),
Payload: payload,
var msg *gen.Yield
if yield.PayloadIsBinary() {
msg = &gen.Yield{
RequestId: yield.RequestID(),
PayloadSerializer: int32(yield.PayloadSerializer()),
Payload: yield.Payload(),
}
} else {
payload, serializer, err := ToCBORPayload(yield)
if err != nil {
return nil, err
}

msg = &gen.Yield{
RequestId: yield.RequestID(),
PayloadSerializer: int32(serializer),
Payload: payload,
}
}

data, err := proto.Marshal(msg)
Expand Down

0 comments on commit d8b674a

Please sign in to comment.