1 module IDL.IdlFlatbufferCreateCode;
2 
3 import IDL.IdlParseStruct;
4 import IDL.IdlUnit;
5 
6 import std.array : appender;
7 import std.format;
8 
9 
10 static string [string]idlFlatbufferVariable;
11 
12 class IdlFlatbufferCode
13 {
14 
15 	static string createFlatbufferCode(IdlParseStruct structInterface)
16 	{
17 		idlFlatbufferVariable["bool"] = "bool";
18 		idlFlatbufferVariable["byte"] = "byte";
19 		idlFlatbufferVariable["ubyte"] = "ubyte";
20 		idlFlatbufferVariable["short"] = "short";
21 		idlFlatbufferVariable["ushort"] = "ushort";
22 		idlFlatbufferVariable["int"] = "int";
23 		idlFlatbufferVariable["uint"] = "uint";
24 		idlFlatbufferVariable["long"] = "long";
25 		idlFlatbufferVariable["ulong"] = "ulong";
26 		idlFlatbufferVariable["float"] = "float";
27 		idlFlatbufferVariable["double"] = "double";
28 		idlFlatbufferVariable["char"] = "byte";
29 		idlFlatbufferVariable["string"] = "string";
30 
31 		auto strings = appender!string();
32 
33 		formattedWrite(strings, "table %sFB{\n", structInterface.structName);
34 
35 		for(int i =1; i <= structInterface.memberAttrInfo.length; ++i)
36 		{
37 			auto v = structInterface.memberAttrInfo[i];
38 
39 			auto typeName = idlFlatbufferVariable.get(v.getTypeName, null);
40 
41 			if(typeName is null)
42 			{
43 				auto structName = idlStructList.get(v.getTypeName, null);
44 
45 				if(structName !is null)
46 				{
47 					typeName = (structName.getName()~"FB");
48 				}
49 			}
50 
51 
52 			if(typeName !is null)
53 			{
54 				if(v.isArray)
55 				{
56 					formattedWrite(strings, "\t%s:[%s];\n", v.memberName, typeName);
57 					
58 				}else
59 				{
60 					formattedWrite(strings, "\t%s:%s;\n", v.memberName, typeName);
61 				}
62 			}else
63 			{
64 				throw new Exception("create flatbuffer file is faild, message name: "~ structInterface.structName ~ ", type:"~ v.typeName~" is not exits!");
65 			}
66 		}
67 		
68 		formattedWrite(strings, "}\n\n\n");
69 		
70 		return strings.data;
71 
72 	}
73 }