Skip to content

Commit

Permalink
Merge pull request #39 from dbulha/binaryextension
Browse files Browse the repository at this point in the history
Write abi type for binary extension types
  • Loading branch information
mmcs85 committed Jan 22, 2020
2 parents 64d3ae5 + 6da2fe8 commit e618f27
Showing 1 changed file with 43 additions and 13 deletions.
56 changes: 43 additions & 13 deletions EosSharp/EosSharp.Core/Providers/AbiSerializationProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -661,18 +661,26 @@ private void WriteAction(MemoryStream ms, Core.Api.v1.Action action, Abi abi)
WriteBytes(ms, SerializeActionData(action, abi));
}

private void WriteAbiType(MemoryStream ms, object value, string type, Abi abi)
private void WriteAbiType(MemoryStream ms, object value, string type, Abi abi, bool isBinaryExtensionAllowed)
{
var uwtype = UnwrapTypeDef(abi, type);

// binary extension type
if(uwtype.EndsWith("$"))
{
if (!isBinaryExtensionAllowed) throw new Exception("Binary Extension type not allowed.");
WriteAbiType(ms, value, uwtype.Substring(0, uwtype.Length - 1), abi, isBinaryExtensionAllowed);

return;
}

//optional type
if (uwtype.EndsWith("?"))
{
WriteByte(ms, value != null ? 1 : 0);
if(value != null)
{
WriteByte(ms, 1);
uwtype.Substring(0, uwtype.Length - 1);
type = uwtype.Substring(0, uwtype.Length - 1);
}
else
{
Expand All @@ -689,7 +697,7 @@ private void WriteAbiType(MemoryStream ms, object value, string type, Abi abi)

WriteVarUint32(ms, items.Count);
foreach (var item in items)
WriteAbiType(ms, item, arrayType, abi);
WriteAbiType(ms, item, arrayType, abi, false);

return;
}
Expand Down Expand Up @@ -726,20 +734,33 @@ private void WriteAbiStruct(MemoryStream ms, object value, AbiStruct abiStruct,

if(!string.IsNullOrWhiteSpace(abiStruct.@base))
{
WriteAbiType(ms, value, abiStruct.@base, abi);
WriteAbiType(ms, value, abiStruct.@base, abi, true);
}

if(value is System.Collections.IDictionary)
{
var skippedBinaryExtension = false;
var valueDict = value as System.Collections.IDictionary;
foreach (var field in abiStruct.fields)
{
var fieldName = FindObjectFieldName(field.name, valueDict);

if (string.IsNullOrWhiteSpace(fieldName))
{
if (field.type.EndsWith("$"))
{
skippedBinaryExtension = true;
continue;
}

throw new Exception("Missing " + abiStruct.name + "." + field.name + " (type=" + field.type + ")");
}
else if (skippedBinaryExtension)
{
throw new Exception("Unexpected " + abiStruct.name + "." + field.name + " (type=" + field.type + ")");
}

WriteAbiType(ms, valueDict[fieldName], field.type, abi);
WriteAbiType(ms, valueDict[fieldName], field.type, abi, true);
}
}
else
Expand All @@ -750,13 +771,13 @@ private void WriteAbiStruct(MemoryStream ms, object value, AbiStruct abiStruct,
var fieldInfo = valueType.GetField(field.name);

if(fieldInfo != null)
WriteAbiType(ms, fieldInfo.GetValue(value), field.type, abi);
WriteAbiType(ms, fieldInfo.GetValue(value), field.type, abi, true);
else
{
var propInfo = valueType.GetProperty(field.name);

if(propInfo != null)
WriteAbiType(ms, propInfo.GetValue(value), field.type, abi);
WriteAbiType(ms, propInfo.GetValue(value), field.type, abi, true);
else
throw new Exception("Missing " + abiStruct.name + "." + field.name + " (type=" + field.type + ")");

Expand Down Expand Up @@ -1214,10 +1235,17 @@ private List<AbiTable> ReadAbiTableList(byte[] data, ref int readIndex)
return items;
}

private object ReadAbiType(byte[] data, string type, Abi abi, ref int readIndex)
private object ReadAbiType(byte[] data, string type, Abi abi, ref int readIndex, bool isBinaryExtensionAllowed)
{
var uwtype = UnwrapTypeDef(abi, type);

// binary extension type
if(uwtype.EndsWith("$"))
{
if (!isBinaryExtensionAllowed) throw new Exception("Binary Extension type not allowed.");
return ReadAbiType(data, uwtype.Substring(0, uwtype.Length - 1), abi, ref readIndex, isBinaryExtensionAllowed);
}

//optional type
if (uwtype.EndsWith("?"))
{
Expand All @@ -1238,7 +1266,7 @@ private object ReadAbiType(byte[] data, string type, Abi abi, ref int readIndex)

for (int i = 0; i < size; i++)
{
items.Add(ReadAbiType(data, arrayType, abi, ref readIndex));
items.Add(ReadAbiType(data, arrayType, abi, ref readIndex, false));
}

return items;
Expand Down Expand Up @@ -1278,7 +1306,7 @@ private T ReadAbiStruct<T>(byte[] data, AbiStruct abiStruct, Abi abi, ref int re

if (!string.IsNullOrWhiteSpace(abiStruct.@base))
{
value = (T)ReadAbiType(data, abiStruct.@base, abi, ref readIndex);
value = (T)ReadAbiType(data, abiStruct.@base, abi, ref readIndex, true);
}
else
{
Expand All @@ -1290,7 +1318,8 @@ private T ReadAbiStruct<T>(byte[] data, AbiStruct abiStruct, Abi abi, ref int re
var valueDict = value as IDictionary<string, object>;
foreach (var field in abiStruct.fields)
{
var abiValue = ReadAbiType(data, field.type, abi, ref readIndex);
var abiValue = ReadAbiType(data, field.type, abi, ref readIndex, true);
if (field.type.EndsWith("$") && abiValue == null) break;
valueDict.Add(field.name, abiValue);
}
}
Expand All @@ -1299,7 +1328,8 @@ private T ReadAbiStruct<T>(byte[] data, AbiStruct abiStruct, Abi abi, ref int re
var valueType = value.GetType();
foreach (var field in abiStruct.fields)
{
var abiValue = ReadAbiType(data, field.type, abi, ref readIndex);
var abiValue = ReadAbiType(data, field.type, abi, ref readIndex, true);
if (field.type.EndsWith("$") && abiValue == null) break;
var fieldName = FindObjectFieldName(field.name, value.GetType());
valueType.GetField(fieldName).SetValue(value, abiValue);
}
Expand Down

0 comments on commit e618f27

Please sign in to comment.