Skip to content
Snippets Groups Projects
Select Git revision
  • 050cf5970095464cbb3295ffe2f6f054a4a40f99
  • master default protected
  • feature/PRXS-3383-CollectionsRankSortAPI
  • feature/PRXS-3383-CollectionsSort
  • feature/3109-SerializeFeature
  • release/0.33
  • feature/3109-RecoverySchema
  • feature/3109-feature
  • fix/PRXS-3369-ValidateFields
  • refactor/PRXS-3306-MovePkgGroup1
  • refactor/6-pkg-refactor-expr
  • fix/PRXS-3360-TemplateBuilderPatch
  • feature/3293-MongoV2
  • feature/3272-GoVersionUp
  • feature/PRXS-3218-HideTemplateActions
  • feature/PRXS-3234-PruneIdents
  • feature/3146-UpdateItemStorageInterface
  • feature/3274-ObjectIndexesFixes
  • feature/PRXS-3143-3235-ReferenceOptions
  • feature/PRXS-3143-3237-ExecuteOptions
  • feature/3149-LocaleCodeAsID-Implementation
  • v0.33.1
  • v0.32.0
  • v0.31.1
  • v0.31.0
  • v0.30.0
  • v0.29.0
  • v0.28.0
  • v0.27.0-alpha.1+16
  • v0.27.0-alpha.1+15
  • v0.27.0-alpha.1+14
  • v0.27.0-alpha.1+13
  • v0.27.0-alpha.1+12
  • v0.27.0-alpha.1+11
  • v0.27.0-alpha.1+10
  • v0.27.0-alpha.1+9
  • v0.27.0-alpha.1+8
  • v0.27.0-alpha.1+7
  • v0.27.0-alpha.1+6
  • v0.27.0-alpha.1+5
  • v0.27.0-alpha.1+4
41 results

client_encode_middleware.go

Blame
  • proto_encoder.go 1.32 KiB
    package events
    
    import (
    	"git.perx.ru/perxis/perxis-go/pkg/errors"
    	"github.com/nats-io/nats.go"
    	"github.com/nats-io/nats.go/encoders/protobuf"
    	"google.golang.org/protobuf/proto"
    )
    
    type ProtoEncoder interface {
    	ToProto() (proto.Message, error)
    	FromProto(message proto.Message) error
    }
    
    const (
    	ProtobufEncoderName = "protobuf"
    )
    
    func init() {
    	nats.RegisterEncoder(ProtobufEncoderName, &ProtobufEncoder{})
    }
    
    type ProtobufEncoder struct {
    	protobuf.ProtobufEncoder
    }
    
    var (
    	ErrInvalidProtoMsgEncode = errors.New("events: object passed to encode must implement ProtoEncoder")
    	ErrInvalidProtoMsgDecode = errors.New("events: object passed to decode must implement ProtoDecoder")
    )
    
    func (pb *ProtobufEncoder) Encode(subject string, v interface{}) ([]byte, error) {
    	if v == nil {
    		return nil, nil
    	}
    	e, ok := v.(ProtoEncoder)
    	if !ok {
    		return nil, ErrInvalidProtoMsgEncode
    	}
    
    	m, err := e.ToProto()
    	if err != nil {
    		return nil, errors.Wrap(err, "nats: encode to proto")
    	}
    
    	return pb.ProtobufEncoder.Encode(subject, m)
    }
    
    func (pb *ProtobufEncoder) Decode(subject string, data []byte, vPtr interface{}) error {
    
    	enc, ok := vPtr.(ProtoEncoder)
    	if !ok {
    		return ErrInvalidProtoMsgDecode
    	}
    
    	msg, _ := enc.ToProto()
    
    	if err := pb.ProtobufEncoder.Decode(subject, data, msg); err != nil {
    		return err
    	}
    
    	return enc.FromProto(msg)
    }