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.PropertiesType;
14 
15 import hunt.collection.AbstractList;
16 import hunt.time.LocalDateTime;
17 import hunt.Object;
18 import hunt.collection.List;
19 import hunt.proton.amqp.Binary;
20 import hunt.proton.amqp.Symbol;
21 import hunt.proton.amqp.UnsignedInteger;
22 import hunt.proton.amqp.UnsignedLong;
23 import hunt.proton.amqp.messaging.Properties;
24 import hunt.proton.codec.AbstractDescribedType;
25 import hunt.proton.codec.Decoder;
26 import hunt.proton.codec.DescribedTypeConstructor;
27 import hunt.proton.codec.EncoderImpl;
28 import hunt.Exceptions;
29 import hunt.String;
30 import std.concurrency : initOnce;
31 import std.conv : to;
32 
33 alias Date = LocalDateTime;
34 
35 class PropertiesWrapper : AbstractList!Object
36 {
37 
38     private Properties _impl;
39 
40     this(Properties propertiesType)
41     {
42         _impl = propertiesType;
43     }
44 
45     override
46     public Object get(int index)
47     {
48 
49         switch(index)
50         {
51             case 0:
52             return _impl.getMessageId();
53             case 1:
54             return _impl.getUserId();
55             case 2:
56             return _impl.getTo();
57             case 3:
58             return _impl.getSubject();
59             case 4:
60             return _impl.getReplyTo();
61             case 5:
62             return _impl.getCorrelationId();
63             case 6:
64             return _impl.getContentType();
65             case 7:
66             return _impl.getContentEncoding();
67             case 8:
68             return _impl.getAbsoluteExpiryTime();
69             case 9:
70             return _impl.getCreationTime();
71             case 10:
72             return _impl.getGroupId();
73             case 11:
74             return _impl.getGroupSequence();
75             case 12:
76             return _impl.getReplyToGroupId();
77             default:
78             break;
79         }
80 
81         throw new IllegalStateException("Unknown index " ~ to!string(index));
82 
83     }
84 
85     override
86     public int size()
87     {
88         return _impl.getReplyToGroupId() !is null
89         ? 13
90         : _impl.getGroupSequence() !is null
91         ? 12
92         : _impl.getGroupId() !is null
93         ? 11
94         : _impl.getCreationTime() !is null
95         ? 10
96         : _impl.getAbsoluteExpiryTime() !is null
97         ? 9
98         : _impl.getContentEncoding() !is null
99         ? 8
100         : _impl.getContentType() !is null
101         ? 7
102         : _impl.getCorrelationId() !is null
103         ? 6
104         : _impl.getReplyTo() !is null
105         ? 5
106         : _impl.getSubject() !is null
107         ? 4
108         : _impl.getTo() !is null
109         ? 3
110         : _impl.getUserId() !is null
111         ? 2
112         : _impl.getMessageId() !is null
113         ? 1
114         : 0;
115 
116     }
117 
118 }
119 
120 class PropertiesType  : AbstractDescribedType!(Properties,List!Object) , DescribedTypeConstructor!(Properties)
121 {
122     //private static Object[] DESCRIPTORS =
123     //{
124     //    UnsignedLong.valueOf(0x0000000000000073L), Symbol.valueOf("amqp:properties:list"),
125     //};
126 
127     static Object[]  DESCRIPTORS() {
128         __gshared Object[]  inst;
129         return initOnce!inst([UnsignedLong.valueOf(0x0000000000000073L), Symbol.valueOf("amqp:properties:list")]);
130     }
131 
132     static UnsignedLong  DESCRIPTOR() {
133         __gshared UnsignedLong  inst;
134         return initOnce!inst(UnsignedLong.valueOf(0x0000000000000073L));
135     }
136 
137    // private static UnsignedLong DESCRIPTOR = UnsignedLong.valueOf(0x0000000000000073L);
138 
139     this(EncoderImpl encoder)
140     {
141         super(encoder);
142     }
143 
144     override
145     public UnsignedLong getDescriptor()
146     {
147         return DESCRIPTOR;
148     }
149 
150     override
151     protected List!Object wrap(Properties val)
152     {
153         return new PropertiesWrapper(val);
154     }
155 
156 
157 
158         public Properties newInstance(Object described)
159         {
160             List!Object l = cast(List!Object) described;
161 
162             Properties o = new Properties();
163 
164 
165             switch(13 - l.size())
166             {
167 
168                 case 0:
169                     o.setReplyToGroupId( cast(String) l.get( 12 ) );
170                     goto case;
171                 case 1:
172                     o.setGroupSequence( cast(UnsignedInteger) l.get( 11 ) );
173                     goto case;
174                 case 2:
175                     o.setGroupId( cast(String) l.get( 10 ) );
176                     goto case;
177                 case 3:
178                     o.setCreationTime( cast(Date) l.get( 9 ) );
179                     goto case;
180                 case 4:
181                     o.setAbsoluteExpiryTime( cast(Date) l.get( 8 ) );
182                     goto case;
183                 case 5:
184                     o.setContentEncoding( cast(Symbol) l.get( 7 ) );
185                     goto case;
186                 case 6:
187                     o.setContentType( cast(Symbol) l.get( 6 ) );
188                     goto case;
189                 case 7:
190                     o.setCorrelationId( cast(String) l.get( 5 ) );
191                     goto case;
192                 case 8:
193                     o.setReplyTo( cast(String) l.get( 4 ) );
194                     goto case;
195                 case 9:
196                     o.setSubject( cast(String) l.get( 3 ) );
197                     goto case;
198                 case 10:
199                     o.setTo( cast(String) l.get( 2 ) );
200                     goto case;
201                 case 11:
202                     o.setUserId( cast(Binary) l.get( 1 ) );
203                     goto case;
204                 case 12:
205                     o.setMessageId(cast(String) l.get( 0 ) );
206                     break;
207                 default:
208                     break;
209             }
210 
211 
212             return o;
213         }
214 
215         public TypeInfo getTypeClass()
216         {
217             return typeid(Properties);
218         }
219 
220     public static void register(Decoder decoder, EncoderImpl encoder)
221     {
222         PropertiesType type = new PropertiesType(encoder);
223         //implementationMissing(false);
224         foreach(Object descriptor ; DESCRIPTORS)
225         {
226             decoder.registerDynamic(descriptor, type);
227         }
228         encoder.register(type);
229     }
230 }