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.DischargeType;
14 
15 import hunt.collection.AbstractList;
16 import hunt.collection.List;
17 import hunt.proton.amqp.Binary;
18 import hunt.proton.amqp.Symbol;
19 import hunt.proton.amqp.UnsignedLong;
20 import hunt.proton.amqp.transaction.Discharge;
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 hunt.Object;
27 import hunt.logging;
28 import hunt.Boolean;
29 import hunt.Exceptions;
30 import std.concurrency : initOnce;
31 import std.conv : to;
32 
33 class DischargeWrapper : AbstractList!Object
34 {
35 
36     private Discharge _discharge;
37 
38     this(Discharge discharge)
39     {
40         _discharge = discharge;
41     }
42 
43     override
44     public Object get(int index)
45     {
46 
47         switch(index)
48         {
49             case 0:
50             return _discharge.getTxnId();
51             case 1:
52             return _discharge.getFail();
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 _discharge.getFail() !is null
65         ? 2
66         : 1;
67 
68     }
69 
70 }
71 
72 class DischargeType : AbstractDescribedType!(Discharge,IObject) , DescribedTypeConstructor!(Discharge)
73 {
74     //private static Object[] DESCRIPTORS =
75     //{
76     //    UnsignedLong.valueOf(0x0000000000000032L), Symbol.valueOf("amqp:discharge:list"),
77     //};
78     //
79     //private static UnsignedLong DESCRIPTOR = UnsignedLong.valueOf(0x0000000000000032L);
80 
81     static Object[]  DESCRIPTORS() {
82         __gshared Object[]  inst;
83         return initOnce!inst([UnsignedLong.valueOf(0x0000000000000032L), Symbol.valueOf("amqp:discharge:list")]);
84     }
85 
86     static UnsignedLong  DESCRIPTOR() {
87         __gshared UnsignedLong  inst;
88         return initOnce!inst(UnsignedLong.valueOf(0x0000000000000032L));
89     }
90 
91     this(EncoderImpl encoder)
92     {
93         super(encoder);
94     }
95 
96     override
97     public UnsignedLong getDescriptor()
98     {
99         return DESCRIPTOR;
100     }
101 
102     override
103     protected List!Object wrap(Discharge val)
104     {
105         return new DischargeWrapper(val);
106     }
107 
108 
109 
110 
111     public Discharge newInstance(Object described)
112     {
113         List!Object l = cast(List!Object) described;
114 
115         Discharge o = new Discharge();
116 
117         if(l.isEmpty())
118         {
119             logError("The txn-id field cannot be omitted");
120             //throw new DecodeException("The txn-id field cannot be omitted");
121         }
122 
123         switch(2 - l.size())
124         {
125 
126             case 0:
127                 o.setFail( cast(Boolean) l.get( 1 ) );
128                 goto case;
129             case 1:
130                 o.setTxnId( cast(Binary) l.get( 0 ) );
131                 break;
132             default:
133                 break;
134         }
135 
136 
137         return o;
138     }
139 
140     public TypeInfo getTypeClass()
141     {
142         return typeid(Discharge);
143     }
144 
145 
146 
147     public static void register(Decoder decoder, EncoderImpl encoder)
148     {
149         DischargeType type = new DischargeType(encoder);
150         //implementationMissing(false);
151         foreach(Object descriptor ; DESCRIPTORS)
152         {
153             decoder.registerDynamic(descriptor, type);
154         }
155         encoder.register(type);
156     }
157 }
158