This repository has been archived by the owner on Nov 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
msg.go
137 lines (113 loc) · 2.88 KB
/
msg.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package rony
import (
"fmt"
"github.com/ronaksoft/rony/pools"
"github.com/ronaksoft/rony/registry"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect"
)
/*
Creation Time: 2020 - Jan - 27
Created by: (ehsan)
Maintainers:
1. Ehsan N. Moosa (E2)
Auditor: Ehsan N. Moosa (E2)
Copyright Ronak Software Group 2020
*/
//go:generate protoc -I=. --go_out=paths=source_relative:. msg.proto options.proto
//go:generate protoc -I=. --gorony_out=paths=source_relative,rony_opt=no_edge_dep:. msg.proto
func init() {}
/*
Extra methods for MessageEnvelope
*/
func (x *MessageEnvelope) Fill(reqID uint64, constructor uint64, p proto.Message, kvs ...*KeyValue) {
x.RequestID = reqID
x.Constructor = constructor
// Fill Header
if cap(x.Header) >= len(kvs) {
x.Header = x.Header[:len(kvs)]
} else {
x.Header = make([]*KeyValue, len(kvs))
}
for idx, kv := range kvs {
if x.Header[idx] == nil {
x.Header[idx] = &KeyValue{}
}
kv.DeepCopy(x.Header[idx])
}
// Fill Message
buf := pools.Buffer.FromProto(p)
x.Message = append(x.Message[:0], *buf.Bytes()...)
pools.Buffer.Put(buf)
}
func (x *MessageEnvelope) Get(key, defaultVal string) string {
for _, kv := range x.Header {
if kv.Key == key {
return kv.Value
}
}
return defaultVal
}
func (x *MessageEnvelope) Set(KVs ...*KeyValue) {
x.Header = append(x.Header[:0], KVs...)
}
func (x *MessageEnvelope) Append(key, val string) {
x.Header = append(x.Header, &KeyValue{
Key: key,
Value: val,
})
}
func (x *MessageEnvelope) Unwrap() (protoreflect.Message, error) {
m, err := registry.Unwrap(x)
if err != nil {
return nil, err
}
return m.ProtoReflect(), nil
}
func (x *MessageEnvelope) Carrier() *envelopeCarrier {
return &envelopeCarrier{
e: x,
}
}
// envelopeCarrier is an adapted for MessageEnvelope to implement propagation.TextMapCarrier interface
type envelopeCarrier struct {
e *MessageEnvelope
}
func (e envelopeCarrier) Get(key string) string {
return e.e.Get(key, "")
}
func (e envelopeCarrier) Set(key string, value string) {
e.e.Append(key, value)
}
func (e envelopeCarrier) Keys() []string {
var keys = make([]string, len(e.e.Header))
for idx := range e.e.Header {
keys[idx] = e.e.Header[idx].Key
}
return keys
}
/*
Extra methods for MessageContainer
*/
func (x *MessageContainer) Add(reqID uint64, constructor uint64, p proto.Message, kvs ...*KeyValue) {
me := PoolMessageEnvelope.Get()
me.Fill(reqID, constructor, p, kvs...)
x.Envelopes = append(x.Envelopes, me)
x.Length += 1
}
/*
Extra methods for Error
*/
func (x *Error) Error() string {
if len(x.Description) > 0 {
return fmt.Sprintf("%s:%s (%s)", x.Code, x.Items, x.Description)
} else {
return fmt.Sprintf("%s:%s", x.Code, x.Items)
}
}
func (x *Error) Expand() (string, string) {
return x.Code, x.Items
}
func (x *Error) ToEnvelope(me *MessageEnvelope) {
// me.Fill(me.RequestID, C_Error, x)
}