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

Cleanup TypeConverter replacements #203

Merged
merged 5 commits into from
Oct 5, 2023
Merged

Conversation

hughbe
Copy link
Contributor

@hughbe hughbe commented Dec 22, 2018

  • Delete DateTimeValueSerializerContext as it is a) unused in the DateTimeValueSerializer base class and it has b) absolutely no functionality - passing null will suffice!
  • Put DateTimeOffsetConverter2 in the right namespace
  • Light code modernization and cleanup

xref with #200

@@ -64,11 +60,13 @@ public override object ConvertTo(ITypeDescriptorContext context, CultureInfo cul
intType,
intType,
typeof(TimeSpan)
});

if (constructor == null)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think this is dead code. This API has existed basically forever and we'll always be able to get it right? https://apisof.net/catalog/System.DateTimeOffset..ctor(Int32,Int32,Int32,Int32,Int32,Int32,TimeSpan)

I say remove it but please confirm :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@dipeshmsft what do you reckon?

Copy link
Member

Choose a reason for hiding this comment

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

I will take a look at this PR as well. I will ping you back once reviewed.

@grubioe grubioe added the PR metadata: Label to tag PRs, to facilitate with triage label May 28, 2019
Base automatically changed from master to main March 17, 2021 17:38
@ryalanms ryalanms requested a review from a team as a code owner March 17, 2021 17:38
@pchaurasia14 pchaurasia14 added the Community Contribution A label for all community Contributions label Jul 20, 2022
@ghost ghost assigned hughbe Jul 20, 2022
@dipeshmsft dipeshmsft self-assigned this May 3, 2023
@hughbe
Copy link
Contributor Author

hughbe commented May 3, 2023

@dipeshmsft I will try to break this up to some more commits to provide more context :)

@dipeshmsft
Copy link
Member

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@dipeshmsft
Copy link
Member

@hughbe can you take a look at the build failure for this PR ?

types[i] = (Type)typeTypeConverter.ConvertFrom(context, TypeConverterHelper.InvariantEnglishUS, tl[i]);
}
return types;
throw new NullReferenceException();
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this code may be controversial, why throw a null exception when the variable is not null?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah - basically this always used to throw NullReferenceException, because StringHelpers.SplitTypeList always returns null and then tl.Length would throw NullReferenceException.

Welcome comments here.

Copy link
Contributor

Choose a reason for hiding this comment

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

Hey @hughbe ,
We are considering merging this PR. I'd like to offer a suggestion: rather than removing the code and returning null , we could set it up like this:
public static string[] SplitTypeList(string typeList) => Array.Empty<string>();

With this approach, StringHelpers.SplitTypeList will return an empty string, allowing the code to proceed. This setup also leaves the door open for us to potentially utilize this feature in the future.

Thank you for your consideration.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK - have changed this to return Array.Empty()


public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (destinationType != null && value is DateTime)
{
_dateTimeValueSerializer.ConvertToString( value as string, _valueSerializerContext );
_dateTimeValueSerializer.ConvertToString(value as string, null);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

🚩 this doesn't return the actual value?!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

@hughbe My apologies for missing this while reviewing. It appears that this should return:
return _dateTimeValueSerializer.ConvertToString(value as string, null);

Regarding the build failure, we cannot define StringHelpers as private.

If you would like, I can add a commit with the above changes, as they are very small.

{
return _dateTimeValueSerializer.ConvertFromString( value as string, _valueSerializerContext );
}
=> _dateTimeValueSerializer.ConvertFromString(value as string, null);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Since DateTimeValueSerializerContext doesn't have any functionality, we can just pass null and remove the class here and in the other DateTimeConverter2

{
// Use the year, month, day, hour, minute, second, millisecond, offset constructor
ConstructorInfo constructor = typeof(DateTimeOffset).GetConstructor(new Type[]
Copy link
Contributor Author

Choose a reason for hiding this comment

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

constructor will always exist, remove null check

@hughbe hughbe force-pushed the typeconverter-rep branch 2 times, most recently from 2064e14 to 8b55ab8 Compare September 20, 2023 08:59
Update DateTimeConverter2.cs
- Remove dead `if (constructor != null)` check - this ctor will always be there.
- Cleanup code
@rchauhan18 rchauhan18 merged commit d05df2b into dotnet:main Oct 5, 2023
9 checks passed
@rchauhan18
Copy link
Contributor

Thanks @hughbe for your contribution.

@hughbe hughbe deleted the typeconverter-rep branch October 5, 2023 06:56
@ghost ghost locked as resolved and limited conversation to collaborators Nov 4, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Community Contribution A label for all community Contributions Included in test pass PR metadata: Label to tag PRs, to facilitate with triage
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants