forked from vcabbage/amqp
/
types.go
2152 lines (1778 loc) · 46.2 KB
/
types.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package encoding
import (
"encoding/binary"
"encoding/hex"
"errors"
"fmt"
"math"
"reflect"
"time"
"unicode/utf8"
"github.com/Azure/go-amqp/internal/buffer"
)
type AMQPType uint8
// Type codes
const (
TypeCodeNull AMQPType = 0x40
// Bool
TypeCodeBool AMQPType = 0x56 // boolean with the octet 0x00 being false and octet 0x01 being true
TypeCodeBoolTrue AMQPType = 0x41
TypeCodeBoolFalse AMQPType = 0x42
// Unsigned
TypeCodeUbyte AMQPType = 0x50 // 8-bit unsigned integer (1)
TypeCodeUshort AMQPType = 0x60 // 16-bit unsigned integer in network byte order (2)
TypeCodeUint AMQPType = 0x70 // 32-bit unsigned integer in network byte order (4)
TypeCodeSmallUint AMQPType = 0x52 // unsigned integer value in the range 0 to 255 inclusive (1)
TypeCodeUint0 AMQPType = 0x43 // the uint value 0 (0)
TypeCodeUlong AMQPType = 0x80 // 64-bit unsigned integer in network byte order (8)
TypeCodeSmallUlong AMQPType = 0x53 // unsigned long value in the range 0 to 255 inclusive (1)
TypeCodeUlong0 AMQPType = 0x44 // the ulong value 0 (0)
// Signed
TypeCodeByte AMQPType = 0x51 // 8-bit two's-complement integer (1)
TypeCodeShort AMQPType = 0x61 // 16-bit two's-complement integer in network byte order (2)
TypeCodeInt AMQPType = 0x71 // 32-bit two's-complement integer in network byte order (4)
TypeCodeSmallint AMQPType = 0x54 // 8-bit two's-complement integer (1)
TypeCodeLong AMQPType = 0x81 // 64-bit two's-complement integer in network byte order (8)
TypeCodeSmalllong AMQPType = 0x55 // 8-bit two's-complement integer
// Decimal
TypeCodeFloat AMQPType = 0x72 // IEEE 754-2008 binary32 (4)
TypeCodeDouble AMQPType = 0x82 // IEEE 754-2008 binary64 (8)
TypeCodeDecimal32 AMQPType = 0x74 // IEEE 754-2008 decimal32 using the Binary Integer Decimal encoding (4)
TypeCodeDecimal64 AMQPType = 0x84 // IEEE 754-2008 decimal64 using the Binary Integer Decimal encoding (8)
TypeCodeDecimal128 AMQPType = 0x94 // IEEE 754-2008 decimal128 using the Binary Integer Decimal encoding (16)
// Other
TypeCodeChar AMQPType = 0x73 // a UTF-32BE encoded Unicode character (4)
TypeCodeTimestamp AMQPType = 0x83 // 64-bit two's-complement integer representing milliseconds since the unix epoch
TypeCodeUUID AMQPType = 0x98 // UUID as defined in section 4.1.2 of RFC-4122
// Variable Length
TypeCodeVbin8 AMQPType = 0xa0 // up to 2^8 - 1 octets of binary data (1 + variable)
TypeCodeVbin32 AMQPType = 0xb0 // up to 2^32 - 1 octets of binary data (4 + variable)
TypeCodeStr8 AMQPType = 0xa1 // up to 2^8 - 1 octets worth of UTF-8 Unicode (with no byte order mark) (1 + variable)
TypeCodeStr32 AMQPType = 0xb1 // up to 2^32 - 1 octets worth of UTF-8 Unicode (with no byte order mark) (4 +variable)
TypeCodeSym8 AMQPType = 0xa3 // up to 2^8 - 1 seven bit ASCII characters representing a symbolic value (1 + variable)
TypeCodeSym32 AMQPType = 0xb3 // up to 2^32 - 1 seven bit ASCII characters representing a symbolic value (4 + variable)
// Compound
TypeCodeList0 AMQPType = 0x45 // the empty list (i.e. the list with no elements) (0)
TypeCodeList8 AMQPType = 0xc0 // up to 2^8 - 1 list elements with total size less than 2^8 octets (1 + compound)
TypeCodeList32 AMQPType = 0xd0 // up to 2^32 - 1 list elements with total size less than 2^32 octets (4 + compound)
TypeCodeMap8 AMQPType = 0xc1 // up to 2^8 - 1 octets of encoded map data (1 + compound)
TypeCodeMap32 AMQPType = 0xd1 // up to 2^32 - 1 octets of encoded map data (4 + compound)
TypeCodeArray8 AMQPType = 0xe0 // up to 2^8 - 1 array elements with total size less than 2^8 octets (1 + array)
TypeCodeArray32 AMQPType = 0xf0 // up to 2^32 - 1 array elements with total size less than 2^32 octets (4 + array)
// Composites
TypeCodeOpen AMQPType = 0x10
TypeCodeBegin AMQPType = 0x11
TypeCodeAttach AMQPType = 0x12
TypeCodeFlow AMQPType = 0x13
TypeCodeTransfer AMQPType = 0x14
TypeCodeDisposition AMQPType = 0x15
TypeCodeDetach AMQPType = 0x16
TypeCodeEnd AMQPType = 0x17
TypeCodeClose AMQPType = 0x18
TypeCodeSource AMQPType = 0x28
TypeCodeTarget AMQPType = 0x29
TypeCodeError AMQPType = 0x1d
TypeCodeMessageHeader AMQPType = 0x70
TypeCodeDeliveryAnnotations AMQPType = 0x71
TypeCodeMessageAnnotations AMQPType = 0x72
TypeCodeMessageProperties AMQPType = 0x73
TypeCodeApplicationProperties AMQPType = 0x74
TypeCodeApplicationData AMQPType = 0x75
TypeCodeAMQPSequence AMQPType = 0x76
TypeCodeAMQPValue AMQPType = 0x77
TypeCodeFooter AMQPType = 0x78
TypeCodeStateReceived AMQPType = 0x23
TypeCodeStateAccepted AMQPType = 0x24
TypeCodeStateRejected AMQPType = 0x25
TypeCodeStateReleased AMQPType = 0x26
TypeCodeStateModified AMQPType = 0x27
TypeCodeSASLMechanism AMQPType = 0x40
TypeCodeSASLInit AMQPType = 0x41
TypeCodeSASLChallenge AMQPType = 0x42
TypeCodeSASLResponse AMQPType = 0x43
TypeCodeSASLOutcome AMQPType = 0x44
TypeCodeDeleteOnClose AMQPType = 0x2b
TypeCodeDeleteOnNoLinks AMQPType = 0x2c
TypeCodeDeleteOnNoMessages AMQPType = 0x2d
TypeCodeDeleteOnNoLinksOrMessages AMQPType = 0x2e
)
// Durability Policies
const (
// No terminus state is retained durably.
DurabilityNone Durability = 0
// Only the existence and configuration of the terminus is
// retained durably.
DurabilityConfiguration Durability = 1
// In addition to the existence and configuration of the
// terminus, the unsettled state for durable messages is
// retained durably.
DurabilityUnsettledState Durability = 2
)
// Durability specifies the durability of a link.
type Durability uint32
func (d *Durability) String() string {
if d == nil {
return "<nil>"
}
switch *d {
case DurabilityNone:
return "none"
case DurabilityConfiguration:
return "configuration"
case DurabilityUnsettledState:
return "unsettled-state"
default:
return fmt.Sprintf("unknown durability %d", *d)
}
}
func (d Durability) Marshal(wr *buffer.Buffer) error {
return Marshal(wr, uint32(d))
}
func (d *Durability) Unmarshal(r *buffer.Buffer) error {
return Unmarshal(r, (*uint32)(d))
}
// Expiry Policies
const (
// The expiry timer starts when terminus is detached.
ExpiryLinkDetach ExpiryPolicy = "link-detach"
// The expiry timer starts when the most recently
// associated session is ended.
ExpirySessionEnd ExpiryPolicy = "session-end"
// The expiry timer starts when most recently associated
// connection is closed.
ExpiryConnectionClose ExpiryPolicy = "connection-close"
// The terminus never expires.
ExpiryNever ExpiryPolicy = "never"
)
// ExpiryPolicy specifies when the expiry timer of a terminus
// starts counting down from the timeout value.
//
// If the link is subsequently re-attached before the terminus is expired,
// then the count down is aborted. If the conditions for the
// terminus-expiry-policy are subsequently re-met, the expiry timer restarts
// from its originally configured timeout value.
type ExpiryPolicy Symbol
func ValidateExpiryPolicy(e ExpiryPolicy) error {
switch e {
case ExpiryLinkDetach,
ExpirySessionEnd,
ExpiryConnectionClose,
ExpiryNever:
return nil
default:
return fmt.Errorf("unknown expiry-policy %q", e)
}
}
func (e ExpiryPolicy) Marshal(wr *buffer.Buffer) error {
return Symbol(e).Marshal(wr)
}
func (e *ExpiryPolicy) Unmarshal(r *buffer.Buffer) error {
err := Unmarshal(r, (*Symbol)(e))
if err != nil {
return err
}
return ValidateExpiryPolicy(*e)
}
func (e *ExpiryPolicy) String() string {
if e == nil {
return "<nil>"
}
return string(*e)
}
// Sender Settlement Modes
const (
// Sender will send all deliveries initially unsettled to the receiver.
SenderSettleModeUnsettled SenderSettleMode = 0
// Sender will send all deliveries settled to the receiver.
SenderSettleModeSettled SenderSettleMode = 1
// Sender MAY send a mixture of settled and unsettled deliveries to the receiver.
SenderSettleModeMixed SenderSettleMode = 2
)
// SenderSettleMode specifies how the sender will settle messages.
type SenderSettleMode uint8
func (m SenderSettleMode) Ptr() *SenderSettleMode {
return &m
}
func (m *SenderSettleMode) String() string {
if m == nil {
return "<nil>"
}
switch *m {
case SenderSettleModeUnsettled:
return "unsettled"
case SenderSettleModeSettled:
return "settled"
case SenderSettleModeMixed:
return "mixed"
default:
return fmt.Sprintf("unknown sender mode %d", uint8(*m))
}
}
func (m SenderSettleMode) Marshal(wr *buffer.Buffer) error {
return Marshal(wr, uint8(m))
}
func (m *SenderSettleMode) Unmarshal(r *buffer.Buffer) error {
n, err := ReadUbyte(r)
*m = SenderSettleMode(n)
return err
}
// Receiver Settlement Modes
const (
// Receiver will spontaneously settle all incoming transfers.
ReceiverSettleModeFirst ReceiverSettleMode = 0
// Receiver will only settle after sending the disposition to the
// sender and receiving a disposition indicating settlement of
// the delivery from the sender.
ReceiverSettleModeSecond ReceiverSettleMode = 1
)
// ReceiverSettleMode specifies how the receiver will settle messages.
type ReceiverSettleMode uint8
func (m ReceiverSettleMode) Ptr() *ReceiverSettleMode {
return &m
}
func (m *ReceiverSettleMode) String() string {
if m == nil {
return "<nil>"
}
switch *m {
case ReceiverSettleModeFirst:
return "first"
case ReceiverSettleModeSecond:
return "second"
default:
return fmt.Sprintf("unknown receiver mode %d", uint8(*m))
}
}
func (m ReceiverSettleMode) Marshal(wr *buffer.Buffer) error {
return Marshal(wr, uint8(m))
}
func (m *ReceiverSettleMode) Unmarshal(r *buffer.Buffer) error {
n, err := ReadUbyte(r)
*m = ReceiverSettleMode(n)
return err
}
type Role bool
const (
RoleSender Role = false
RoleReceiver Role = true
)
func (rl Role) String() string {
if rl {
return "Receiver"
}
return "Sender"
}
func (rl *Role) Unmarshal(r *buffer.Buffer) error {
b, err := readBool(r)
*rl = Role(b)
return err
}
func (rl Role) Marshal(wr *buffer.Buffer) error {
return Marshal(wr, (bool)(rl))
}
type SASLCode uint8
// SASL Codes
const (
CodeSASLOK SASLCode = iota // Connection authentication succeeded.
CodeSASLAuth // Connection authentication failed due to an unspecified problem with the supplied credentials.
CodeSASLSysPerm // Connection authentication failed due to a system error that is unlikely to be corrected without intervention.
)
func (s SASLCode) Marshal(wr *buffer.Buffer) error {
return Marshal(wr, uint8(s))
}
func (s *SASLCode) Unmarshal(r *buffer.Buffer) error {
n, err := ReadUbyte(r)
*s = SASLCode(n)
return err
}
// DeliveryState encapsulates the various concrete delivery states.
// http://docs.oasis-open.org/amqp/core/v1.0/os/amqp-core-messaging-v1.0-os.html#section-delivery-state
// TODO: http://docs.oasis-open.org/amqp/core/v1.0/os/amqp-core-transactions-v1.0-os.html#type-declared
type DeliveryState interface {
deliveryState() // marker method
}
type Unsettled map[string]DeliveryState
func (u Unsettled) Marshal(wr *buffer.Buffer) error {
return writeMap(wr, u)
}
func (u *Unsettled) Unmarshal(r *buffer.Buffer) error {
count, err := readMapHeader(r)
if err != nil {
return err
}
m := make(Unsettled, count/2)
for i := uint32(0); i < count; i += 2 {
key, err := ReadString(r)
if err != nil {
return err
}
var value DeliveryState
err = Unmarshal(r, &value)
if err != nil {
return err
}
m[key] = value
}
*u = m
return nil
}
type Filter map[Symbol]*DescribedType
func (f Filter) Marshal(wr *buffer.Buffer) error {
return writeMap(wr, f)
}
func (f *Filter) Unmarshal(r *buffer.Buffer) error {
count, err := readMapHeader(r)
if err != nil {
return err
}
m := make(Filter, count/2)
for i := uint32(0); i < count; i += 2 {
key, err := ReadString(r)
if err != nil {
return err
}
var value DescribedType
err = Unmarshal(r, &value)
if err != nil {
return err
}
m[Symbol(key)] = &value
}
*f = m
return nil
}
// peekMessageType reads the message type without
// modifying any data.
func PeekMessageType(buf []byte) (uint8, uint8, error) {
if len(buf) < 3 {
return 0, 0, errors.New("invalid message")
}
if buf[0] != 0 {
return 0, 0, fmt.Errorf("invalid composite header %02x", buf[0])
}
// copied from readUlong to avoid allocations
t := AMQPType(buf[1])
if t == TypeCodeUlong0 {
return 0, 2, nil
}
if t == TypeCodeSmallUlong {
if len(buf[2:]) == 0 {
return 0, 0, errors.New("invalid ulong")
}
return buf[2], 3, nil
}
if t != TypeCodeUlong {
return 0, 0, fmt.Errorf("invalid type for uint32 %02x", t)
}
if len(buf[2:]) < 8 {
return 0, 0, errors.New("invalid ulong")
}
v := binary.BigEndian.Uint64(buf[2:10])
return uint8(v), 10, nil
}
func tryReadNull(r *buffer.Buffer) bool {
if r.Len() > 0 && AMQPType(r.Bytes()[0]) == TypeCodeNull {
r.Skip(1)
return true
}
return false
}
// Annotations keys must be of type string, int, or int64.
//
// String keys are encoded as AMQP Symbols.
type Annotations map[any]any
func (a Annotations) Marshal(wr *buffer.Buffer) error {
return writeMap(wr, a)
}
func (a *Annotations) Unmarshal(r *buffer.Buffer) error {
count, err := readMapHeader(r)
if err != nil {
return err
}
m := make(Annotations, count/2)
for i := uint32(0); i < count; i += 2 {
key, err := ReadAny(r)
if err != nil {
return err
}
value, err := ReadAny(r)
if err != nil {
return err
}
m[key] = value
}
*a = m
return nil
}
// ErrCond is one of the error conditions defined in the AMQP spec.
type ErrCond string
func (ec ErrCond) Marshal(wr *buffer.Buffer) error {
return (Symbol)(ec).Marshal(wr)
}
func (ec *ErrCond) Unmarshal(r *buffer.Buffer) error {
s, err := ReadString(r)
*ec = ErrCond(s)
return err
}
/*
<type name="error" class="composite" source="list">
<descriptor name="amqp:error:list" code="0x00000000:0x0000001d"/>
<field name="condition" type="symbol" requires="error-condition" mandatory="true"/>
<field name="description" type="string"/>
<field name="info" type="fields"/>
</type>
*/
// Error is an AMQP error.
type Error struct {
// A symbolic value indicating the error condition.
Condition ErrCond
// descriptive text about the error condition
//
// This text supplies any supplementary details not indicated by the condition field.
// This text can be logged as an aid to resolving issues.
Description string
// map carrying information about the error condition
Info map[string]any
}
func (e *Error) Marshal(wr *buffer.Buffer) error {
return MarshalComposite(wr, TypeCodeError, []MarshalField{
{Value: &e.Condition, Omit: false},
{Value: &e.Description, Omit: e.Description == ""},
{Value: e.Info, Omit: len(e.Info) == 0},
})
}
func (e *Error) Unmarshal(r *buffer.Buffer) error {
return UnmarshalComposite(r, TypeCodeError, []UnmarshalField{
{Field: &e.Condition, HandleNull: func() error { return errors.New("Error.Condition is required") }},
{Field: &e.Description},
{Field: &e.Info},
}...)
}
func (e *Error) String() string {
if e == nil {
return "*Error(nil)"
}
return fmt.Sprintf("*Error{Condition: %s, Description: %s, Info: %v}",
e.Condition,
e.Description,
e.Info,
)
}
func (e *Error) Error() string {
return e.String()
}
/*
<type name="received" class="composite" source="list" provides="delivery-state">
<descriptor name="amqp:received:list" code="0x00000000:0x00000023"/>
<field name="section-number" type="uint" mandatory="true"/>
<field name="section-offset" type="ulong" mandatory="true"/>
</type>
*/
type StateReceived struct {
// When sent by the sender this indicates the first section of the message
// (with section-number 0 being the first section) for which data can be resent.
// Data from sections prior to the given section cannot be retransmitted for
// this delivery.
//
// When sent by the receiver this indicates the first section of the message
// for which all data might not yet have been received.
SectionNumber uint32
// When sent by the sender this indicates the first byte of the encoded section
// data of the section given by section-number for which data can be resent
// (with section-offset 0 being the first byte). Bytes from the same section
// prior to the given offset section cannot be retransmitted for this delivery.
//
// When sent by the receiver this indicates the first byte of the given section
// which has not yet been received. Note that if a receiver has received all of
// section number X (which contains N bytes of data), but none of section number
// X + 1, then it can indicate this by sending either Received(section-number=X,
// section-offset=N) or Received(section-number=X+1, section-offset=0). The state
// Received(section-number=0, section-offset=0) indicates that no message data
// at all has been transferred.
SectionOffset uint64
}
func (sr *StateReceived) deliveryState() {}
func (sr *StateReceived) Marshal(wr *buffer.Buffer) error {
return MarshalComposite(wr, TypeCodeStateReceived, []MarshalField{
{Value: &sr.SectionNumber, Omit: false},
{Value: &sr.SectionOffset, Omit: false},
})
}
func (sr *StateReceived) Unmarshal(r *buffer.Buffer) error {
return UnmarshalComposite(r, TypeCodeStateReceived, []UnmarshalField{
{Field: &sr.SectionNumber, HandleNull: func() error { return errors.New("StateReceiver.SectionNumber is required") }},
{Field: &sr.SectionOffset, HandleNull: func() error { return errors.New("StateReceiver.SectionOffset is required") }},
}...)
}
/*
<type name="accepted" class="composite" source="list" provides="delivery-state, outcome">
<descriptor name="amqp:accepted:list" code="0x00000000:0x00000024"/>
</type>
*/
type StateAccepted struct{}
func (sr *StateAccepted) deliveryState() {}
func (sa *StateAccepted) Marshal(wr *buffer.Buffer) error {
return MarshalComposite(wr, TypeCodeStateAccepted, nil)
}
func (sa *StateAccepted) Unmarshal(r *buffer.Buffer) error {
return UnmarshalComposite(r, TypeCodeStateAccepted)
}
func (sa *StateAccepted) String() string {
return "Accepted"
}
/*
<type name="rejected" class="composite" source="list" provides="delivery-state, outcome">
<descriptor name="amqp:rejected:list" code="0x00000000:0x00000025"/>
<field name="error" type="error"/>
</type>
*/
type StateRejected struct {
Error *Error
}
func (sr *StateRejected) deliveryState() {}
func (sr *StateRejected) Marshal(wr *buffer.Buffer) error {
return MarshalComposite(wr, TypeCodeStateRejected, []MarshalField{
{Value: sr.Error, Omit: sr.Error == nil},
})
}
func (sr *StateRejected) Unmarshal(r *buffer.Buffer) error {
return UnmarshalComposite(r, TypeCodeStateRejected,
UnmarshalField{Field: &sr.Error},
)
}
func (sr *StateRejected) String() string {
return fmt.Sprintf("Rejected{Error: %v}", sr.Error)
}
/*
<type name="released" class="composite" source="list" provides="delivery-state, outcome">
<descriptor name="amqp:released:list" code="0x00000000:0x00000026"/>
</type>
*/
type StateReleased struct{}
func (sr *StateReleased) deliveryState() {}
func (sr *StateReleased) Marshal(wr *buffer.Buffer) error {
return MarshalComposite(wr, TypeCodeStateReleased, nil)
}
func (sr *StateReleased) Unmarshal(r *buffer.Buffer) error {
return UnmarshalComposite(r, TypeCodeStateReleased)
}
func (sr *StateReleased) String() string {
return "Released"
}
/*
<type name="modified" class="composite" source="list" provides="delivery-state, outcome">
<descriptor name="amqp:modified:list" code="0x00000000:0x00000027"/>
<field name="delivery-failed" type="boolean"/>
<field name="undeliverable-here" type="boolean"/>
<field name="message-annotations" type="fields"/>
</type>
*/
type StateModified struct {
// count the transfer as an unsuccessful delivery attempt
//
// If the delivery-failed flag is set, any messages modified
// MUST have their delivery-count incremented.
DeliveryFailed bool
// prevent redelivery
//
// If the undeliverable-here is set, then any messages released MUST NOT
// be redelivered to the modifying link endpoint.
UndeliverableHere bool
// message attributes
// Map containing attributes to combine with the existing message-annotations
// held in the message's header section. Where the existing message-annotations
// of the message contain an entry with the same key as an entry in this field,
// the value in this field associated with that key replaces the one in the
// existing headers; where the existing message-annotations has no such value,
// the value in this map is added.
MessageAnnotations Annotations
}
func (sr *StateModified) deliveryState() {}
func (sm *StateModified) Marshal(wr *buffer.Buffer) error {
return MarshalComposite(wr, TypeCodeStateModified, []MarshalField{
{Value: &sm.DeliveryFailed, Omit: !sm.DeliveryFailed},
{Value: &sm.UndeliverableHere, Omit: !sm.UndeliverableHere},
{Value: sm.MessageAnnotations, Omit: sm.MessageAnnotations == nil},
})
}
func (sm *StateModified) Unmarshal(r *buffer.Buffer) error {
return UnmarshalComposite(r, TypeCodeStateModified, []UnmarshalField{
{Field: &sm.DeliveryFailed},
{Field: &sm.UndeliverableHere},
{Field: &sm.MessageAnnotations},
}...)
}
func (sm *StateModified) String() string {
return fmt.Sprintf("Modified{DeliveryFailed: %t, UndeliverableHere: %t, MessageAnnotations: %v}", sm.DeliveryFailed, sm.UndeliverableHere, sm.MessageAnnotations)
}
// symbol is an AMQP symbolic string.
type Symbol string
func (s Symbol) Marshal(wr *buffer.Buffer) error {
l := len(s)
switch {
// Sym8
case l < 256:
wr.Append([]byte{
byte(TypeCodeSym8),
byte(l),
})
wr.AppendString(string(s))
// Sym32
case uint(l) < math.MaxUint32:
wr.AppendByte(uint8(TypeCodeSym32))
wr.AppendUint32(uint32(l))
wr.AppendString(string(s))
default:
return errors.New("too long")
}
return nil
}
type Milliseconds time.Duration
func (m Milliseconds) Marshal(wr *buffer.Buffer) error {
writeUint32(wr, uint32(m/Milliseconds(time.Millisecond)))
return nil
}
func (m *Milliseconds) Unmarshal(r *buffer.Buffer) error {
n, err := readUint(r)
*m = Milliseconds(time.Duration(n) * time.Millisecond)
return err
}
// mapAnyAny is used to decode AMQP maps who's keys are undefined or
// inconsistently typed.
type mapAnyAny map[any]any
func (m mapAnyAny) Marshal(wr *buffer.Buffer) error {
return writeMap(wr, map[any]any(m))
}
func (m *mapAnyAny) Unmarshal(r *buffer.Buffer) error {
count, err := readMapHeader(r)
if err != nil {
return err
}
mm := make(mapAnyAny, count/2)
for i := uint32(0); i < count; i += 2 {
key, err := ReadAny(r)
if err != nil {
return err
}
value, err := ReadAny(r)
if err != nil {
return err
}
// https://golang.org/ref/spec#Map_types:
// The comparison operators == and != must be fully defined
// for operands of the key type; thus the key type must not
// be a function, map, or slice.
switch reflect.ValueOf(key).Kind() {
case reflect.Slice, reflect.Func, reflect.Map:
return errors.New("invalid map key")
}
mm[key] = value
}
*m = mm
return nil
}
// mapStringAny is used to decode AMQP maps that have string keys
type mapStringAny map[string]any
func (m mapStringAny) Marshal(wr *buffer.Buffer) error {
return writeMap(wr, map[string]any(m))
}
func (m *mapStringAny) Unmarshal(r *buffer.Buffer) error {
count, err := readMapHeader(r)
if err != nil {
return err
}
mm := make(mapStringAny, count/2)
for i := uint32(0); i < count; i += 2 {
key, err := ReadString(r)
if err != nil {
return err
}
value, err := ReadAny(r)
if err != nil {
return err
}
mm[key] = value
}
*m = mm
return nil
}
// mapStringAny is used to decode AMQP maps that have Symbol keys
type mapSymbolAny map[Symbol]any
func (m mapSymbolAny) Marshal(wr *buffer.Buffer) error {
return writeMap(wr, map[Symbol]any(m))
}
func (m *mapSymbolAny) Unmarshal(r *buffer.Buffer) error {
count, err := readMapHeader(r)
if err != nil {
return err
}
mm := make(mapSymbolAny, count/2)
for i := uint32(0); i < count; i += 2 {
key, err := ReadString(r)
if err != nil {
return err
}
value, err := ReadAny(r)
if err != nil {
return err
}
mm[Symbol(key)] = value
}
*m = mm
return nil
}
// UUID is a 128 bit identifier as defined in RFC 4122.
type UUID [16]byte
// String returns the hex encoded representation described in RFC 4122, Section 3.
func (u UUID) String() string {
var buf [36]byte
hex.Encode(buf[:8], u[:4])
buf[8] = '-'
hex.Encode(buf[9:13], u[4:6])
buf[13] = '-'
hex.Encode(buf[14:18], u[6:8])
buf[18] = '-'
hex.Encode(buf[19:23], u[8:10])
buf[23] = '-'
hex.Encode(buf[24:], u[10:])
return string(buf[:])
}
func (u UUID) Marshal(wr *buffer.Buffer) error {
wr.AppendByte(byte(TypeCodeUUID))
wr.Append(u[:])
return nil
}
func (u *UUID) Unmarshal(r *buffer.Buffer) error {
un, err := readUUID(r)
*u = un
return err
}
type LifetimePolicy uint8
const (
DeleteOnClose = LifetimePolicy(TypeCodeDeleteOnClose)
DeleteOnNoLinks = LifetimePolicy(TypeCodeDeleteOnNoLinks)
DeleteOnNoMessages = LifetimePolicy(TypeCodeDeleteOnNoMessages)
DeleteOnNoLinksOrMessages = LifetimePolicy(TypeCodeDeleteOnNoLinksOrMessages)
)
func (p LifetimePolicy) Marshal(wr *buffer.Buffer) error {
wr.Append([]byte{
0x0,
byte(TypeCodeSmallUlong),
byte(p),
byte(TypeCodeList0),
})
return nil
}
func (p *LifetimePolicy) Unmarshal(r *buffer.Buffer) error {
typ, fields, err := readCompositeHeader(r)
if err != nil {
return err
}
if fields != 0 {
return fmt.Errorf("invalid size %d for lifetime-policy", fields)
}
*p = LifetimePolicy(typ)
return nil
}
type DescribedType struct {
Descriptor any
Value any
}
func (t DescribedType) Marshal(wr *buffer.Buffer) error {
wr.AppendByte(0x0) // descriptor constructor
err := Marshal(wr, t.Descriptor)
if err != nil {
return err
}
return Marshal(wr, t.Value)
}
func (t *DescribedType) Unmarshal(r *buffer.Buffer) error {
b, err := r.ReadByte()
if err != nil {
return err
}
if b != 0x0 {
return fmt.Errorf("invalid described type header %02x", b)
}
err = Unmarshal(r, &t.Descriptor)
if err != nil {
return err
}
return Unmarshal(r, &t.Value)
}
func (t DescribedType) String() string {
return fmt.Sprintf("DescribedType{descriptor: %v, value: %v}",
t.Descriptor,
t.Value,
)
}
// SLICES
// ArrayUByte allows encoding []uint8/[]byte as an array
// rather than binary data.
type ArrayUByte []uint8
func (a ArrayUByte) Marshal(wr *buffer.Buffer) error {
const typeSize = 1
writeArrayHeader(wr, len(a), typeSize, TypeCodeUbyte)
wr.Append(a)
return nil
}
func (a *ArrayUByte) Unmarshal(r *buffer.Buffer) error {
length, err := readArrayHeader(r)
if err != nil {
return err
}
type_, err := readType(r)
if err != nil {
return err
}
if type_ != TypeCodeUbyte {
return fmt.Errorf("invalid type for []uint16 %02x", type_)