Skip to content

Commit

Permalink
Merge pull request #12 from vimeda/update-api
Browse files Browse the repository at this point in the history
Update pletter API
  • Loading branch information
mpraski authored Jun 5, 2020
2 parents 5758c4b + e0b7aed commit b195cd3
Showing 1 changed file with 22 additions and 10 deletions.
32 changes: 22 additions & 10 deletions any/any.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
package any

import (
"errors"
"fmt"
"strings"

"github.com/golang/protobuf/proto"
proto_old "github.com/golang/protobuf/proto"
"github.com/golang/protobuf/ptypes"
"github.com/golang/protobuf/ptypes/any"
"github.com/vimeda/pletter/pb"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/anypb"
)

// ErrEmptyMessage when message is nil
var ErrEmptyMessage = errors.New("message is nil")

// PackAndMarshal packs a proto message into an envelope message and marshal it
func PackAndMarshal(m proto.Message) ([]byte, error) {
e, err := Pack(m)
Expand All @@ -21,14 +27,22 @@ func PackAndMarshal(m proto.Message) ([]byte, error) {

// Pack packs a proto message into an envelope message
func Pack(m proto.Message) (pb.Envelope, error) {
if m == nil {
return pb.Envelope{}, ErrEmptyMessage
}

raw, err := proto.Marshal(m)
if err != nil {
return pb.Envelope{}, err
}

fullName := m.ProtoReflect().Descriptor().FullName()

fmt.Println(fullName)

return pb.Envelope{
InnerMessage: &any.Any{
TypeUrl: "github.com/lykon/pletter/" + proto.MessageName(m),
InnerMessage: &anypb.Any{
TypeUrl: fmt.Sprintf("github.com/lykon/pletter/%s", fullName),
Value: raw,
},
}, nil
Expand All @@ -42,12 +56,10 @@ func Unpack(m []byte, t proto.Message) error {
return err
}

err = ptypes.UnmarshalAny(e.GetInnerMessage(), t)
if err != nil {
return err
}

return nil
return ptypes.UnmarshalAny(
e.GetInnerMessage(),
proto_old.MessageV1(t),
)
}

// GetMessageName returns the message name from the wrapped proto message
Expand Down

0 comments on commit b195cd3

Please sign in to comment.