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.CoordinatorType;
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.Coordinator;
20 import hunt.proton.codec.AbstractDescribedType;
21 import hunt.proton.codec.Decoder;
22 import hunt.proton.codec.DescribedTypeConstructor;
23 import hunt.proton.codec.EncoderImpl;
24 import hunt.Object;
25 import hunt.Exceptions;
26 import hunt.logging;
27 
28 import std.concurrency : initOnce;
29 
30 class CoordinatorType : AbstractDescribedType!(Coordinator,List!(List!Symbol)) , DescribedTypeConstructor!(Coordinator)
31 {
32     //private static Object[] DESCRIPTORS =
33     //{
34     //    UnsignedLong.valueOf(0x0000000000000030L), Symbol.valueOf("amqp:coordinator:list"),
35     //};
36     //
37     //private static UnsignedLong DESCRIPTOR = UnsignedLong.valueOf(0x0000000000000030L);
38 
39     static Object[]  DESCRIPTORS() {
40         __gshared Object[]  inst;
41         return initOnce!inst([UnsignedLong.valueOf(0x0000000000000030L), Symbol.valueOf("amqp:coordinator:list")]);
42     }
43 
44     static UnsignedLong DESCRIPTOR() {
45         __gshared UnsignedLong  inst;
46         return initOnce!inst(UnsignedLong.valueOf(0x0000000000000030L));
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!(List!Symbol) wrap(Coordinator val)
62     {
63         auto capabilities = val.getCapabilities();
64         return capabilities is null || capabilities.size() == 0
65                 ? null
66                 : Collections.singletonList!(List!Symbol)(capabilities);
67     }
68 
69 
70     public Coordinator newInstance(Object described)
71     {
72         List!(List!Symbol) l = cast(List!(List!Symbol)) described;
73 
74         Coordinator o = new Coordinator();
75 
76 
77         switch(1 - l.size())
78         {
79 
80             case 0:
81                  List!Symbol val0 = l.get( 0 );
82                 //if( val0 is null || val0.getClass().isArray() )
83                 //{
84                     o.setCapabilities( val0 );
85                 //}
86                 //else
87                 //{
88                 //    o.setCapabilities( (Symbol) val0 );
89                 //}
90                  break;
91             default:
92                 break;
93         }
94 
95 
96         return o;
97     }
98 
99     public TypeInfo getTypeClass()
100     {
101         return typeid(Coordinator);
102     }
103 
104 
105 
106     public static void register(Decoder decoder, EncoderImpl encoder)
107     {
108         CoordinatorType type = new CoordinatorType(encoder);
109         //implementationMissing(false);
110         foreach(Object descriptor ; DESCRIPTORS)
111         {
112             decoder.registerDynamic(descriptor, type);
113         }
114         encoder.register(type);
115     }
116 }
117