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.messaging.SourceType;
14 
15 import hunt.String;
16 import hunt.Object;
17 
18 import hunt.collection.AbstractList;
19 import hunt.collection.List;
20 import hunt.collection.Map;
21 import hunt.Boolean;
22 
23 import hunt.proton.amqp.Symbol;
24 import hunt.proton.amqp.UnsignedInteger;
25 import hunt.proton.amqp.UnsignedLong;
26 import hunt.proton.amqp.messaging.Outcome;
27 import hunt.proton.amqp.messaging.Source;
28 import hunt.proton.codec.AbstractDescribedType;
29 import hunt.proton.codec.Decoder;
30 import hunt.proton.codec.DescribedTypeConstructor;
31 import hunt.proton.codec.EncoderImpl;
32 import hunt.proton.amqp.messaging.TerminusDurability;
33 import hunt.proton.amqp.messaging.TerminusExpiryPolicy;
34 import hunt.Exceptions;
35 
36 
37 import std.concurrency : initOnce;
38 import std.conv : to;
39 
40 
41 class SourceWrapper : AbstractList!Object
42 {
43     private Source _impl;
44 
45     this(Source impl)
46     {
47         _impl = impl;
48     }
49 
50     override
51     public Object get(int index)
52     {
53 
54         switch(index)
55         {
56             case 0:
57             return _impl.getAddress();
58             case 1:
59             return _impl.getDurable().getValue();
60             case 2:
61             return _impl.getExpiryPolicy().getPolicy();
62             case 3:
63             return _impl.getTimeout();
64             case 4:
65             return _impl.getDynamic();
66             case 5:
67             return cast(Object)(_impl.getDynamicNodeProperties());
68             case 6:
69             return _impl.getDistributionMode();
70             case 7:
71             return cast(Object)(_impl.getFilter());
72             case 8:
73             return cast(Object)(_impl.getDefaultOutcome());
74             case 9:
75             return cast(Object)(_impl.getOutcomes());
76             case 10:
77             return cast(Object)(_impl.getCapabilities());
78             default:
79             return null;
80         }
81 
82         //   throw new IllegalStateException("Unknown index " ~ to!string(index));
83 
84     }
85 
86     override
87     public int size()
88     {
89         return _impl.getCapabilities() !is null
90         ? 11
91         : _impl.getOutcomes() !is null
92         ? 10
93         : _impl.getDefaultOutcome() !is null
94         ? 9
95         : _impl.getFilter() !is null
96         ? 8
97         : _impl.getDistributionMode() !is null
98         ? 7
99         : _impl.getDynamicNodeProperties() !is null
100         ? 6
101         : _impl.getDynamic().booleanValue
102         ? 5
103         : (_impl.getTimeout() !is null && _impl.getTimeout() != (UnsignedInteger.ZERO))
104         ? 4
105         : _impl.getExpiryPolicy() !is TerminusExpiryPolicy.SESSION_END
106         ? 3
107         : _impl.getDurable() !is TerminusDurability.NONE
108         ? 2
109         : _impl.getAddress() !is null
110         ? 1
111         : 0;
112 
113     }
114 
115 }
116 
117 
118 class SourceType : AbstractDescribedType!(Source,List!Object) , DescribedTypeConstructor!(Source)
119 {
120     //private static Object[] DESCRIPTORS =
121     //{
122     //    UnsignedLong.valueOf(0x0000000000000028L), Symbol.valueOf("amqp:source:list"),
123     //};
124 
125    // private static UnsignedLong DESCRIPTOR = UnsignedLong.valueOf(0x0000000000000028L);
126 
127 
128     static Object[]  DESCRIPTORS() {
129         __gshared Object[]  inst;
130         return initOnce!inst([UnsignedLong.valueOf(0x0000000000000028L), Symbol.valueOf("amqp:source:list")]);
131     }
132 
133     static UnsignedLong  DESCRIPTOR() {
134         __gshared UnsignedLong  inst;
135         return initOnce!inst(UnsignedLong.valueOf(0x0000000000000028L));
136     }
137 
138     this(EncoderImpl encoder)
139     {
140         super(encoder);
141     }
142 
143     override
144     public UnsignedLong getDescriptor()
145     {
146         return DESCRIPTOR;
147     }
148 
149     override
150     protected List!Object wrap(Source val)
151     {
152         return new SourceWrapper(val);
153     }
154 
155 
156 
157     public Source newInstance(Object described)
158     {
159         List!Object l = cast(List!Object) described;
160 
161         Source o = new Source();
162 
163 
164         switch(11 - l.size())
165         {
166 
167             case 0:
168             {
169                 Object val0 = l.get( 10 );
170                 //if( val0 is null || val0.getClass().isArray() )
171                 //{
172                 //    o.setCapabilities( cast(Symbol[]) val0 );
173                 //}
174                 {
175                     o.setCapabilities( val0 is null ? null: cast(List!Symbol) val0 );
176                 }
177                 goto case;
178             }
179 
180 
181             case 1:
182             {
183                 Object val1 = l.get( 9 );
184                 //if( val1 is null || val1.getClass().isArray() )
185                 //{
186                 //    o.setOutcomes( cast(Symbol[]) val1 );
187                 //}
188                 {
189                     o.setOutcomes( val1 is null ? null: cast(List!Symbol) val1 );
190                 }
191                 goto case;
192             }
193 
194             case 2:
195                  Object val2 = l.get( 8 );
196                 o.setDefaultOutcome( val2 is null ? null : cast(Outcome)val2 );
197                 goto case;
198             case 3:
199                  Object val3 = l.get( 7 );
200                 o.setFilter(val3 is null? null : cast(Map!(Object,Object)) val3 );
201                 goto case;
202             case 4:
203                 Object val4 = l.get( 6 );
204                 o.setDistributionMode( val4 is null ? null : cast(Symbol)val4 );
205                 goto case;
206             case 5:
207                 Object val5 =  l.get( 5 );
208                 o.setDynamicNodeProperties(val5 is null ? null:  cast(IObject)val5  );
209                 goto case;
210             case 6:
211             {
212                 Object val6 = l.get( 4);
213                 Boolean dynamic = val6 is null? null : cast(Boolean) val6;
214                 o.setDynamic( dynamic is null ? new Boolean( false) : dynamic);
215                 goto case;
216             }
217 
218             case 7:
219             {
220                 Object val7 = l.get( 3);
221 
222                 UnsignedInteger timeout = val7 is null? null :  cast(UnsignedInteger) val7;
223                 o.setTimeout( timeout is null ? UnsignedInteger.ZERO : timeout);
224                 goto case;
225             }
226             case 8:
227             {
228                 Object val8 = l.get( 2);
229                 Symbol expiryPolicy = val8 is null ? null: cast(Symbol) val8;
230                 o.setExpiryPolicy( expiryPolicy is null ? TerminusExpiryPolicy.SESSION_END : TerminusExpiryPolicy.valueOf( expiryPolicy));
231                 goto case;
232             }
233             case 9:
234             {
235                 Object val9 =  l.get( 1);
236                 UnsignedInteger durable = val9 is null ? null:  cast(UnsignedInteger)val9 ;
237                 o.setDurable( durable is null ? TerminusDurability.NONE : TerminusDurability.get( durable));
238                 goto case;
239             }
240             case 10:
241                 Object val10 = l.get( 0 );
242 
243                 o.setAddress(val10 is null ? null: cast(String) val10);
244                 break;
245             default:
246                 break;
247         }
248 
249 
250         return o;
251     }
252 
253     public TypeInfo getTypeClass()
254     {
255         return typeid(Source);
256     }
257 
258 
259 
260     public static void register(Decoder decoder, EncoderImpl encoder)
261     {
262         SourceType type = new SourceType(encoder);
263         foreach(Object descriptor ; DESCRIPTORS)
264         {
265             decoder.registerDynamic(descriptor, type);
266         }
267         encoder.register(type);
268        // implementationMissing(false);
269     }
270 
271 
272 }
273