Skip to content

Commit

Permalink
Handle null values in array attributes (#3273)
Browse files Browse the repository at this point in the history
  • Loading branch information
alanwest authored May 13, 2022
1 parent e7f06af commit d983cb1
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ private static OtlpCommon.AnyValue ToOtlpValue(object value)
default:
try
{
return new OtlpCommon.AnyValue { StringValue = value.ToString() };
return value != null
? new OtlpCommon.AnyValue { StringValue = value.ToString() }
: null;
}
catch
{
Expand Down Expand Up @@ -105,7 +107,7 @@ private static OtlpCommon.AnyValue ToOtlpArrayValue(Array array)
case double[]:
foreach (var item in array)
{
arrayValue.Values.Add(ToOtlpValue(item));
arrayValue.Values.Add(ToOtlpValue(item) ?? new OtlpCommon.AnyValue { });
}

return new OtlpCommon.AnyValue { ArrayValue = arrayValue };
Expand All @@ -114,7 +116,10 @@ private static OtlpCommon.AnyValue ToOtlpArrayValue(Array array)
{
try
{
arrayValue.Values.Add(ToOtlpValue(item.ToString()));
var value = item != null
? ToOtlpValue(item.ToString())
: new OtlpCommon.AnyValue { };
arrayValue.Values.Add(value);
}
catch
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,19 +163,32 @@ public void StringTypesSupported(object value)
public void StringArrayTypesSupported()
{
var charArray = new char[] { 'a', 'b', 'c' };
var stringArray = new string[] { "a", "b", "c" };
var stringArray = new string[] { "a", "b", "c", string.Empty, null };

var kvp = new KeyValuePair<string, object>("key", charArray);
var attribute = kvp.ToOtlpAttribute();
Assert.NotNull(attribute);
Assert.Equal(OtlpCommon.AnyValue.ValueOneofCase.ArrayValue, attribute.Value.ValueCase);
Assert.Equal(stringArray, attribute.Value.ArrayValue.Values.Select(x => x.StringValue));
Assert.Equal(charArray.Select(x => x.ToString()), attribute.Value.ArrayValue.Values.Select(x => x.StringValue));

kvp = new KeyValuePair<string, object>("key", stringArray);
attribute = kvp.ToOtlpAttribute();
Assert.NotNull(attribute);
Assert.Equal(OtlpCommon.AnyValue.ValueOneofCase.ArrayValue, attribute.Value.ValueCase);
Assert.Equal(stringArray, attribute.Value.ArrayValue.Values.Select(x => x.StringValue));

for (var i = 0; i < stringArray.Length; ++i)
{
var expectedValue = stringArray[i];
var expectedValueCase = expectedValue != null
? OtlpCommon.AnyValue.ValueOneofCase.StringValue
: OtlpCommon.AnyValue.ValueOneofCase.None;

Assert.Equal(expectedValueCase, attribute.Value.ArrayValue.Values[i].ValueCase);
if (expectedValueCase != OtlpCommon.AnyValue.ValueOneofCase.None)
{
Assert.Equal(expectedValue, attribute.Value.ArrayValue.Values[i].StringValue);
}
}
}

[Fact]
Expand All @@ -194,7 +207,7 @@ public void ToStringIsCalledForAllOtherTypes()
new nint[] { 1, 2, 3 },
new nuint[] { 1, 2, 3 },
new decimal[] { 1, 2, 3 },
new object[] { 1, new object(), false },
new object[] { 1, new object(), false, null },
};

foreach (var value in testValues)
Expand All @@ -216,8 +229,16 @@ public void ToStringIsCalledForAllOtherTypes()
var array = value as Array;
for (var i = 0; i < attribute.Value.ArrayValue.Values.Count; ++i)
{
Assert.Equal(OtlpCommon.AnyValue.ValueOneofCase.StringValue, attribute.Value.ArrayValue.Values[i].ValueCase);
Assert.Equal(array.GetValue(i).ToString(), attribute.Value.ArrayValue.Values[i].StringValue);
var expectedValue = array.GetValue(i)?.ToString();
var expectedValueCase = expectedValue != null
? OtlpCommon.AnyValue.ValueOneofCase.StringValue
: OtlpCommon.AnyValue.ValueOneofCase.None;

Assert.Equal(expectedValueCase, attribute.Value.ArrayValue.Values[i].ValueCase);
if (expectedValueCase != OtlpCommon.AnyValue.ValueOneofCase.None)
{
Assert.Equal(array.GetValue(i).ToString(), attribute.Value.ArrayValue.Values[i].StringValue);
}
}
}
}
Expand Down

0 comments on commit d983cb1

Please sign in to comment.