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.DeclaredType;
14 
15 import hunt.collection.Collections;
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.Declared;
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 
29 import std.concurrency : initOnce;
30 import hunt.Exceptions;
31 
32 class DeclaredType : AbstractDescribedType!(Declared,List!Binary) , DescribedTypeConstructor!(Declared)
33 {
34     //private static Object[] DESCRIPTORS =
35     //{
36     //    UnsignedLong.valueOf(0x0000000000000033L), Symbol.valueOf("amqp:declared:list"),
37     //};
38     //
39     //private static UnsignedLong DESCRIPTOR = UnsignedLong.valueOf(0x0000000000000033L);
40 
41     static Object[]  DESCRIPTORS() {
42         __gshared Object[]  inst;
43         return initOnce!inst([UnsignedLong.valueOf(0x0000000000000033L), Symbol.valueOf("amqp:declared:list")]);
44     }
45 
46     static UnsignedLong  DESCRIPTOR() {
47         __gshared UnsignedLong  inst;
48         return initOnce!inst(UnsignedLong.valueOf(0x0000000000000033L));
49     }
50 
51     this(EncoderImpl encoder)
52     {
53         super(encoder);
54     }
55 
56     override
57     public UnsignedLong getDescriptor()
58     {
59         return DESCRIPTOR;
60     }
61 
62     override
63     protected List!Binary wrap(Declared val)
64     {
65         return Collections.singletonList!Binary(val.getTxnId());
66     }
67 
68     public Declared newInstance(Object described)
69     {
70         List!Object l = cast(List!Object) described;
71 
72         Declared o = new Declared();
73 
74         if(l.isEmpty())
75         {
76             logError("The txn-id field cannot be omitted");
77           //  throw new DecodeException("The txn-id field cannot be omitted");
78         }
79 
80         o.setTxnId( cast(Binary) l.get( 0 ) );
81 
82 
83 
84         return o;
85     }
86 
87     public TypeInfo getTypeClass()
88     {
89         return typeid(Declared);
90     }
91 
92 
93 
94     public static void register(Decoder decoder, EncoderImpl encoder)
95     {
96         DeclaredType type = new DeclaredType(encoder);
97         //implementationMissing(false);
98         foreach(Object descriptor ; DESCRIPTORS)
99         {
100             decoder.registerDynamic(descriptor, type);
101         }
102         encoder.register(type);
103     }
104 }
105