Skip to content

Commit

Permalink
[c#] Fix alias conversion issue for generic field
Browse files Browse the repository at this point in the history
Closes #928
  • Loading branch information
ara-ayvazyan authored and lalo committed Jul 6, 2018
1 parent c913deb commit bd4b46e
Show file tree
Hide file tree
Showing 17 changed files with 6,242 additions and 4,238 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -33,6 +33,10 @@ different versioning scheme, following the Haskell community's

* There were no C# changes in this release.

### C# ###
* Fixed alias conversion issue for generic fields [Issue
#928](https://github.com/Microsoft/bond/issues/928).

## 8.0.0: 2018-05-30 ##
* `gbc` & compiler library: 0.11.0.0
* IDL core version: 3.0
Expand Down
31 changes: 18 additions & 13 deletions cs/src/core/expressions/ObjectParser.cs
Expand Up @@ -143,30 +143,35 @@ Expression Field(ITransform transform, Expression structVar, UInt16 id, ISchemaF
processField);
}
}
else if (fieldSchemaType.IsBondBlob())
{
var notEqual = Expression.NotEqual(
convertedBlob,
Expression.Default(typeof(ArraySegment<byte>)));

return Expression.Block(
new[] { convertedBlob },
Expression.Assign(convertedBlob, blob),
PrunedExpression.IfThenElse(notEqual, processField, omitField));
}
else
{
var defaultValue = schemaField.GetDefaultValue();

if (fieldSchemaType.IsBondBlob())
if (defaultValue == null)
{
var notEqual = Expression.NotEqual(
convertedBlob,
Expression.Default(typeof(ArraySegment<byte>)));

return Expression.Block(
new[] { convertedBlob },
Expression.Assign(convertedBlob, blob),
PrunedExpression.IfThenElse(notEqual, processField, omitField));
cannotOmit = Expression.NotEqual(fieldValue, Expression.Constant(null));
}
else if (fieldSchemaType.IsBondContainer())
{
cannotOmit = defaultValue == null
? Expression.NotEqual(fieldValue, Expression.Constant(null))
: Expression.NotEqual(ContainerCount(fieldValue), Expression.Constant(0));
cannotOmit = Expression.NotEqual(ContainerCount(fieldValue), Expression.Constant(0));
}
else
{
cannotOmit = Expression.NotEqual(fieldValue, Expression.Constant(defaultValue));
var comparand = defaultValue.GetType() != fieldValue.Type
? (Expression)Expression.Default(fieldValue.Type)
: Expression.Constant(defaultValue);
cannotOmit = Expression.NotEqual(fieldValue, comparand);
}
}

Expand Down
5 changes: 5 additions & 0 deletions cs/src/core/expressions/TypeAlias.cs
Expand Up @@ -50,6 +50,11 @@ public Expression Convert(Expression value, Type type)
value.Type.GetGenericTypeDefinition() == typeof (Nullable<>))
{
value = Expression.Convert(value, value.Type.GetTypeInfo().GenericTypeArguments[0]);

if (type.IsGenericType() && type.GetGenericTypeDefinition() == typeof(Nullable<>))
{
type = type.GetTypeInfo().GenericTypeArguments[0];
}
}

if (type != value.Type)
Expand Down
2 changes: 2 additions & 0 deletions cs/test/core/UnitTest.bond
Expand Up @@ -612,6 +612,7 @@ struct Generics
11: GenericScalar<float> sf;
12: GenericScalar<uint64> sui64;
13: GenericScalar<EnumType1> se;
14: GenericScalar<DateTime> sdt;

20: GenericClass<set<int32>> ci32;
21: GenericClass<blob> cblob;
Expand All @@ -637,6 +638,7 @@ struct GenericsWithNothing
11: GenericNothingScalar<float> sf;
12: GenericNothingScalar<uint64> sui64;
13: GenericNothingScalar<EnumType1> se;
14: GenericNothingScalar<DateTime> sdt;

20: GenericNothingClass<set<int32>> ci32;
21: GenericNothingClass<blob> cblob;
Expand Down
38 changes: 38 additions & 0 deletions cs/test/expressions/Clone.expressions
Expand Up @@ -809,6 +809,44 @@
} .Else {
.Default(System.Void)
};
.Block(System.Object $obj) {
$obj = (System.Object)$Example_obj._dt;
.Block() {
.Block(ExpressionsTest.Generic`1[System.DateTime] $Generic`1_obj) {
$Generic`1_obj = (ExpressionsTest.Generic`1[System.DateTime])$obj;
.Default(System.Void);
.If ($Generic`1_obj.value != .Default(System.DateTime)) {
($Example_result._dt).value = .Call ExpressionsTest.BondTypeAliasConverter.Convert(
.Call ExpressionsTest.BondTypeAliasConverter.Convert(
$Generic`1_obj.value,
.Default(System.Int64)),
.Default(System.DateTime))
} .Else {
.Default(System.Void)
};
.Default(System.Void)
}
}
};
.Block(System.Object $obj) {
$obj = (System.Object)$Example_obj._dt2;
.Block() {
.Block(ExpressionsTest.GenericNothing`1[System.DateTime] $GenericNothing`1_obj) {
$GenericNothing`1_obj = (ExpressionsTest.GenericNothing`1[System.DateTime])$obj;
.Default(System.Void);
.If ($GenericNothing`1_obj.value != null) {
($Example_result._dt2).value = (System.Nullable`1[System.DateTime]).Call ExpressionsTest.BondTypeAliasConverter.Convert(
(System.Int64)((System.Nullable`1[System.Int64]).Call ExpressionsTest.BondTypeAliasConverter.Convert(
(System.DateTime)$GenericNothing`1_obj.value,
.Default(System.Int64))),
.Default(System.DateTime))
} .Else {
.Default(System.Void)
};
.Default(System.Void)
}
}
};
.Default(System.Void)
}
};
Expand Down

0 comments on commit bd4b46e

Please sign in to comment.