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.ModifiedType;
14 
15 import hunt.Object;
16 import hunt.collection.AbstractList;
17 import hunt.collection.List;
18 import hunt.collection.Map;
19 import hunt.proton.amqp.Symbol;
20 import hunt.proton.amqp.UnsignedLong;
21 import hunt.proton.amqp.messaging.Modified;
22 import hunt.proton.codec.AbstractDescribedType;
23 import hunt.proton.codec.Decoder;
24 import hunt.proton.codec.DescribedTypeConstructor;
25 import hunt.proton.codec.EncoderImpl;
26 import hunt.Exceptions;
27 import hunt.String;
28 import hunt.Boolean;
29 import std.concurrency : initOnce;
30 import std.conv : to;
31 
32 class ModifiedWrapper : AbstractList!Object
33 {
34     private Modified _impl;
35 
36     this(Modified impl)
37     {
38         _impl = impl;
39     }
40 
41     override
42     public Object get(int index)
43     {
44 
45         switch(index)
46         {
47             case 0:
48             return _impl.getDeliveryFailed();
49             case 1:
50             return _impl.getUndeliverableHere();
51             case 2:
52             return cast(Object)_impl.getMessageAnnotations();
53             default:
54             return null;
55         }
56 
57         // throw new IllegalStateException("Unknown index " ~ to!string(index));
58 
59     }
60 
61     override
62     public int size()
63     {
64         return _impl.getMessageAnnotations() !is null
65         ? 3
66         : _impl.getUndeliverableHere() !is null
67         ? 2
68         : _impl.getDeliveryFailed() !is null
69         ? 1
70         : 0;
71 
72     }
73 
74 }
75 
76 
77 
78 class ModifiedType  : AbstractDescribedType!(Modified,List!Object) , DescribedTypeConstructor!(Modified)
79 {
80     //private static Object[] DESCRIPTORS =
81     //{
82     //    UnsignedLong.valueOf(0x0000000000000027L), Symbol.valueOf("amqp:modified:list"),
83     //};
84 
85    // private static UnsignedLong DESCRIPTOR = UnsignedLong.valueOf(0x0000000000000027L);
86 
87 
88     static Object[]  DESCRIPTORS() {
89         __gshared Object[]  inst;
90         return initOnce!inst([UnsignedLong.valueOf(0x0000000000000027L), Symbol.valueOf("amqp:modified:list")]);
91     }
92 
93     static UnsignedLong  DESCRIPTOR() {
94         __gshared UnsignedLong  inst;
95         return initOnce!inst(UnsignedLong.valueOf(0x0000000000000027L));
96     }
97 
98     this(EncoderImpl encoder)
99     {
100         super(encoder);
101     }
102 
103     override
104     public UnsignedLong getDescriptor()
105     {
106         return DESCRIPTOR;
107     }
108 
109     override
110     protected List!Object wrap(Modified val)
111     {
112         return new ModifiedWrapper(val);
113     }
114 
115 
116 
117     public Modified newInstance(Object described)
118     {
119         List!Object l = cast(List!Object) described;
120 
121         Modified o = new Modified();
122 
123 
124         switch(3 - l.size())
125         {
126 
127             case 0:
128                 o.setMessageAnnotations( cast(Map!(Symbol,Object)) l.get( 2 ) );
129                 goto case;
130             case 1:
131                 o.setUndeliverableHere( cast(Boolean) l.get( 1 ) );
132                 goto case;
133             case 2:
134                 o.setDeliveryFailed( cast(Boolean) l.get( 0 ) );
135                 break;
136             default:
137                 break;
138         }
139 
140 
141         return o;
142     }
143 
144     public TypeInfo getTypeClass()
145     {
146         return typeid(Modified);
147     }
148 
149 
150 
151     public static void register(Decoder decoder, EncoderImpl encoder)
152     {
153         ModifiedType type = new ModifiedType(encoder);
154        // implementationMissing(false);
155         foreach(Object descriptor ; DESCRIPTORS)
156         {
157             decoder.registerDynamic(descriptor, type);
158         }
159         encoder.register(type);
160     }
161 }
162