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

Improve ValueSetConverter.ToValueSetString #240

Open
wants to merge 3 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
31 changes: 21 additions & 10 deletions CDP4Common.Tests/Helpers/ValueSetConverterTestFixture.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ValueSetConverterTestFixture.cs" company="RHEA System S.A.">
// Copyright (c) 2015-2020 RHEA System S.A.
// <copyright file="ActionItem.cs" company="RHEA System S.A.">
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

incorrect class name?

// Copyright (c) 2015-2022 RHEA System S.A.
//
// Author: Sam Gerené, Merlin Bieze, Alex Vorobiev, Naron Phou, Alexander van Delft
// Author: Sam Gerené, Alex Vorobiev, Alexander van Delft, Nathanael Smiechowski, Antoine Théate, Omar Elebiary
//
// This file is part of CDP4-SDK Community Edition
// This file is part of COMET-SDK Community Edition
//
// The CDP4-SDK Community Edition is free software; you can redistribute it and/or
// The COMET-SDK Community Edition is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 3 of the License, or (at your option) any later version.
//
// The CDP4-SDK Community Edition is distributed in the hope that it will be useful,
// The COMET-SDK Community Edition is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program; if not, write to the Free Software Foundation,
// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

namespace CDP4Common.Tests.Helpers
Expand Down Expand Up @@ -112,20 +111,32 @@ public void VerifyThatValueSetValuesAreCalculatedCorrectly(object value, string
new object[] { " ", "-", "-", new SimpleQuantityKind()},
new object[] { true, "true", "true", new BooleanParameterType()},
new object[] { false, "false", "false", new BooleanParameterType()},
new object[] { "True", "true", "true", new BooleanParameterType()},
new object[] { "False", "false", "false", new BooleanParameterType()},
new object[] { "True", "True", "True", new TextParameterType()},
new object[] { "False", "False", "False", new TextParameterType()},
new object[] { null, string.Empty, string.Empty, new TextParameterType()},
new object[] { "true", "true", "true", new TextParameterType()},
new object[] { "false", "false", "false", new TextParameterType()},
new object[] { "True", "true", "true", new TextParameterType()},
new object[] { "False", "false", "false", new TextParameterType()},
new object[] { "0000001", "0000001", "0000001", new TextParameterType()},
new object[] { "000.000,000", "000.000,000", "000.000,000", new TextParameterType()},
new object[] { 11.11F, "11.11", "11.11", new SimpleQuantityKind()},
new object[] { 11.11D, "11.11", "11.11", new SimpleQuantityKind()},
new object[] { 11.11F, "11,11", "11.11", new TextParameterType()},
new object[] { 11.11D, "11,11", "11.11", new TextParameterType()},
new object[] { true, "True", "True", new TextParameterType()},
new object[] { false, "False", "False", new TextParameterType()},

new object[] { new EnumerationValueDefinition() {ShortName = "enumValue"}, "enumValue", "enumValue", new EnumerationParameterType()},
new object[] { "SomeValue", "SomeValue", "SomeValue", new EnumerationParameterType()},
new object[]
{
new []
{
new EnumerationValueDefinition {ShortName = "enumValue1"},
new EnumerationValueDefinition {ShortName = "enumValue2"}
}, $"enumValue1{Constants.PaddedMultiEnumSeparator}enumValue2", $"enumValue1{Constants.PaddedMultiEnumSeparator}enumValue2", new EnumerationParameterType()
},
$"enumValue1{Constants.PaddedMultiEnumSeparator}enumValue2", $"enumValue1{Constants.PaddedMultiEnumSeparator}enumValue2", new EnumerationParameterType()
},
new object[] { DateTime.ParseExact("2020-09-23T12:11:30", "yyyy-MM-ddTHH:mm:ss", null), "2020-09-23T12:11:30", "2020-09-23T12:11:30", new DateTimeParameterType()},
new object[] { DateTime.ParseExact("2020-09-23T12:11:30", "yyyy-MM-ddTHH:mm:ss", null), "2020-09-23", "2020-09-23", new DateParameterType()},
Expand Down
29 changes: 19 additions & 10 deletions CDP4Common/Helpers/ValueSetConverter.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ValueSetConverter.cs" company="RHEA System S.A.">
// Copyright (c) 2015-2020 RHEA System S.A.
// Copyright (c) 2015-2022 RHEA System S.A.
//
// Author: Sam Gerené, Merlin Bieze, Alex Vorobiev, Naron Phou, Alexander van Delft
// Author: Sam Gerené, Alex Vorobiev, Alexander van Delft, Nathanael Smiechowski, Antoine Théate, Omar Elebiary
//
// This file is part of CDP4-SDK Community Edition
// This file is part of COMET-SDK Community Edition
//
// The CDP4-SDK Community Edition is free software; you can redistribute it and/or
// The COMET-SDK Community Edition is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 3 of the License, or (at your option) any later version.
//
// The CDP4-SDK Community Edition is distributed in the hope that it will be useful,
// The COMET-SDK Community Edition is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program; if not, write to the Free Software Foundation,
// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

namespace CDP4Common.Helpers
Expand Down Expand Up @@ -51,6 +50,16 @@ public static string ToValueSetString(this object value, ParameterType parameter
throw new ArgumentNullException(nameof(parameterType));
}

if (parameterType.ClassKind == ClassKind.TextParameterType)
{
if (value == null)
{
return string.Empty;
}

return value.ToString();
}

if (value == null)
{
return "-";
Expand Down Expand Up @@ -82,18 +91,18 @@ public static string ToValueSetString(this object value, ParameterType parameter
var stringValue = value.ToString();

// try parse double in any culture - allow the user to still use "." as separator
if (double.TryParse(stringValue, NumberStyles.Float, CultureInfo.InvariantCulture, out var doubleValue))
if (parameterType is QuantityKind && double.TryParse(stringValue, NumberStyles.Float, CultureInfo.InvariantCulture, out var doubleValue))
{
return doubleValue.ToString(CultureInfo.InvariantCulture);
}

// convert the comma separator to dot
if (double.TryParse(stringValue, NumberStyles.Float, CultureInfo.CurrentCulture, out doubleValue))
if (parameterType is QuantityKind && double.TryParse(stringValue, NumberStyles.Float, CultureInfo.CurrentCulture, out doubleValue))
{
return doubleValue.ToString(CultureInfo.InvariantCulture);
}

if (bool.TryParse(stringValue, out var booleanValue))
if (parameterType.ClassKind == ClassKind.BooleanParameterType && bool.TryParse(stringValue, out var booleanValue))
{
return booleanValue ? "true" : "false";
}
Expand All @@ -102,7 +111,7 @@ public static string ToValueSetString(this object value, ParameterType parameter
}

// single enum
if (value is EnumerationValueDefinition enumValueDef)
if (parameterType.ClassKind == ClassKind.EnumerationParameterType && value is EnumerationValueDefinition enumValueDef)
{
return enumValueDef.ShortName;
}
Expand Down