1 /*
2  * hunt-proton: AMQP Protocol library for D programming language.
3  *
4  * Copyright (C) 2018-2019 HuntLabs
5  *
6  * Website: https://www.huntlabs.net/
7  *
8  * Licensed under the Apache-2.0 License.
9  *
10  */
11 
12 
13 module hunt.proton.codec.transaction.DeclareType;
14 
15 import hunt.collection.Collections;
16 import hunt.collection.List;
17 import hunt.proton.amqp.Symbol;
18 import hunt.proton.amqp.UnsignedLong;
19 import hunt.proton.amqp.transaction.Declare;
20 import hunt.proton.amqp.transaction.GlobalTxId;
21 import hunt.proton.codec.AbstractDescribedType;
22 import hunt.proton.codec.Decoder;
23 import hunt.proton.codec.DescribedTypeConstructor;
24 import hunt.proton.codec.EncoderImpl;
25 import hunt.Object;
26 import hunt.logging;
27 import std.concurrency : initOnce;
28 import hunt.Exceptions;
29 
30 class DeclareType : AbstractDescribedType!(Declare,List!GlobalTxId) , DescribedTypeConstructor!(Declare)
31 {
32     //private static Object[] DESCRIPTORS =
33     //{
34     //    UnsignedLong.valueOf(0x0000000000000031L), Symbol.valueOf("amqp:declare:list"),
35     //};
36     //
37     //private static UnsignedLong DESCRIPTOR = UnsignedLong.valueOf(0x0000000000000031L);
38 
39     static Object[]  DESCRIPTORS() {
40         __gshared Object[]  inst;
41         return initOnce!inst([UnsignedLong.valueOf(0x0000000000000031L), Symbol.valueOf("amqp:declare:list")]);
42     }
43 
44     static UnsignedLong  DESCRIPTOR() {
45         __gshared UnsignedLong  inst;
46         return initOnce!inst(UnsignedLong.valueOf(0x0000000000000031L));
47     }
48 
49     this(EncoderImpl encoder)
50     {
51         super(encoder);
52     }
53 
54     override
55     public UnsignedLong getDescriptor()
56     {
57         return DESCRIPTOR;
58     }
59 
60     override
61     protected List!GlobalTxId wrap(Declare val)
62     {
63         GlobalTxId globalId = val.getGlobalId();
64         return globalId is null ? null : Collections.singletonList!GlobalTxId(globalId);
65     }
66 
67     public Declare newInstance(Object described)
68     {
69         List!Object l = cast(List!Object) described;
70 
71         Declare o = new Declare();
72 
73         if(!l.isEmpty())
74         {
75             o.setGlobalId( cast(GlobalTxId) l.get( 0 ) );
76         }
77 
78         return o;
79     }
80 
81     public TypeInfo getTypeClass()
82     {
83         return typeid(Declare);
84     }
85 
86 
87 
88     public static void register(Decoder decoder, EncoderImpl encoder)
89     {
90         DeclareType type = new DeclareType(encoder);
91         //implementationMissing(false);
92         foreach(Object descriptor ; DESCRIPTORS)
93         {
94             decoder.registerDynamic(descriptor, type);
95         }
96         encoder.register(type);
97     }
98 }
99