Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Increase default max number of significant digits for floating-point values in CsvWriter #1261

Merged
merged 1 commit into from
Feb 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Bonsai.System/Bonsai.System.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<Description>Bonsai System Library containing reactive infrastructure to interface with the underlying operating system.</Description>
<PackageTags>Bonsai Rx Reactive Extensions IO Serial Port Resources</PackageTags>
<TargetFrameworks>net462;netstandard2.0</TargetFrameworks>
<VersionPrefix>2.7.0</VersionPrefix>
<VersionPrefix>2.7.1</VersionPrefix>
</PropertyGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<PackageReference Include="System.IO.Ports" Version="6.0.0" />
Expand Down
23 changes: 16 additions & 7 deletions Bonsai.System/IO/CsvWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ namespace Bonsai.IO
public class CsvWriter : CombinatorExpressionBuilder
{
string delimiter;
static readonly Expression InvariantCulture = Expression.Constant(CultureInfo.InvariantCulture);
static readonly MethodInfo stringJoinMethod = typeof(string).GetMethods().Single(m => m.Name == nameof(string.Join) &&
m.GetParameters().Length == 2 &&
m.GetParameters()[1].ParameterType == typeof(string[]));
Expand Down Expand Up @@ -184,23 +185,31 @@ static bool IsNullable(Type type)
static Expression GetDataString(Expression expression)
{
if (expression.Type == typeof(string)) return expression;
if (expression.Type == typeof(float))
{
return Expression.Call(expression, nameof(ToString), null, Expression.Constant("G9"), InvariantCulture);
}
else if (expression.Type == typeof(double))
{
return Expression.Call(expression, nameof(ToString), null, Expression.Constant("G17"), InvariantCulture);
}
else if (expression.Type == typeof(DateTime) || expression.Type == typeof(DateTimeOffset))
{
return Expression.Call(expression, "ToString", null, Expression.Constant("o"));
return Expression.Call(expression, nameof(ToString), null, Expression.Constant("o"));
}
else if (expression.Type == typeof(IntPtr) || expression.Type == typeof(TimeSpan))
{
return Expression.Call(expression, "ToString", null);
return Expression.Call(expression, nameof(ToString), null);
}
else if (IsNullable(expression.Type))
{
var hasValue = Expression.Property(expression, "HasValue");
var value = Expression.Property(expression, "Value");
var hasValue = Expression.Property(expression, nameof(Nullable<int>.HasValue));
var value = Expression.Property(expression, nameof(Nullable<int>.Value));
return Expression.Condition(hasValue, GetDataString(value), Expression.Constant(string.Empty));
}
else
{
return Expression.Call(expression, "ToString", null, Expression.Constant(CultureInfo.InvariantCulture));
return Expression.Call(expression, nameof(ToString), null, InvariantCulture);
}
}

Expand Down Expand Up @@ -254,7 +263,7 @@ protected override Expression BuildCombinator(IEnumerable<Expression> arguments)
var converterExpression = Expression.Lambda(lineExpression, dataParameter);
lineExpression = Expression.Call(
typeof(CsvWriter),
"ListJoin",
nameof(ListJoin),
new[] { dataType },
inputParameter,
converterExpression,
Expand All @@ -281,7 +290,7 @@ protected override Expression BuildCombinator(IEnumerable<Expression> arguments)

var csvWriter = Expression.Constant(this);
var headerExpression = Expression.Constant(header);
return Expression.Call(csvWriter, "Process", new[] { parameterType }, source, headerExpression, writeAction);
return Expression.Call(csvWriter, nameof(Process), new[] { parameterType }, source, headerExpression, writeAction);
}

static string ListJoin<TSource>(IList<TSource> source, Func<TSource, string> converter, string separator)
Expand Down