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