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

refactor: target netstandard #148

Closed

Conversation

ByronMayne
Copy link
Contributor

About the PR

The project currently targets .Net6.0 which there is no real reason to as this just limits the use case of this library. With this PR I have targeted all libraries to .netstandard2.0.

A bunch of the unit tests were broken as well so I fixed them all up, as well as added more converged to the specific things that were breaking.

Changelog

  • Changed to .netstandard2.0
  • Fixed all broken unit tests
  • Added more unit tests.

On an unrelated note, there is a ton of warnings about styling, should this not be disabled if they are just being ignored anyway?

This allows it to be used by any project type rather then just .netframework
Added new unit test to validate the special characters string extensions, which was failing tests
Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Thank you ByronMayne for your first pull request to AsyncAPI.NET repository. Please check out our contributors guide.

// Example 2.20. Floating Point
case "-.inf":
case ".inf":
case ".nan":
Copy link
Contributor Author

Choose a reason for hiding this comment

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

These cases were all missed

Copy link
Collaborator

@VisualBean VisualBean Mar 11, 2024

Choose a reason for hiding this comment

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

good find!
I think the datetime case has gone missing however.
The simplest case would be to simply always quote them.

cases

December 31, 2022
2022-12-31 23:59:59
12/31/2022 23:59:59
2022-12-31
2022/12/31
2022-12-31T23:59:59Z

Copy link
Collaborator

Choose a reason for hiding this comment

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

might get caught by the forbidden combinations though. - dont remember off the top of my head.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It should be covered by the regex however just because I am not 100% sure let me add a test to explicitly check for that.

@ByronMayne ByronMayne changed the title Converted to .NetStandard feat: Converted to .NetStandard Mar 10, 2024
@ByronMayne
Copy link
Contributor Author

#149 Issue

@VisualBean VisualBean changed the title feat: Converted to .NetStandard refactor: target netstandard Mar 11, 2024
@VisualBean
Copy link
Collaborator

VisualBean commented Mar 11, 2024

A great change!
It was on my immediate todo, for this project - awesome timing and good additional changes.

For the styling, generally all warnings should be fixed. - at some point I will enforce warnings as errors.

@ByronMayne
Copy link
Contributor Author

@VisualBean Looking at the spec it looks like as long as it can't be parsed as a number you are not required to quote values. So for the two new failing tests, should they be required to be quoted?

It gives the following examples if plain type for dates like the following

# Example 2.22 Timestamps
canonical: 2001-12-15T02:59:43.1Z
iso8601: 2001-12-14t21:59:43.10-05:00
spaced: 2001-12-14 21:59:43.10 -5
date: 2002-12-14

I must admit I have never analyzed a specification and have only very recently looked into this so I could be missing something.

@ByronMayne
Copy link
Contributor Author

After digging around I think I know what you mean about parsing being weird in some cases. The problem I found is that when you convert Yaml -> Json we have to parse the types, but we don't really know. The current logic is a bit flawed.

https://github.com/LEGO/AsyncAPI.NET/blob/main/src/LEGO.AsyncAPI.Readers/YamlConverter.cs#L55

It only currently handles decimal, boolean, and strings. Decimal is also a bit of a weird choice, is every other parser I have seen they don't use float but opt for double.

Updated the JsonHelper `GetScalerValue` to use the built int logic for serialization rather then the System.ConvertMethod
using (StreamReader reader = new StreamReader(memoryStream))
{
string value = reader.ReadToEnd();
return value.Trim('"');
Copy link
Contributor Author

@ByronMayne ByronMayne Mar 12, 2024

Choose a reason for hiding this comment

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

The old method would not handle conversation of DateTime's correctly which was causing it to fail. Instead now we use the built in logic to serialization of values and then just remove the contain quotes. There are new unit tests that cover this logic

@@ -196,7 +196,7 @@ public string GetReferencePointer()
return null;
}

return refNode.GetScalarValue();
return refNode.AsValue().GetScalarValue();
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 AsValue already has a properly worded exception if it's the wrong type. Before inside of GetScalerValue it would throw a rather vague exception. I changed GetScalerValue to only work with NodeValue.

switch (yaml.Style)
{
case ScalarStyle.Plain:
return decimal.TryParse(yaml.Value, NumberStyles.Float, CultureInfo.InvariantCulture, out var d)
Copy link
Contributor Author

@ByronMayne ByronMayne Mar 12, 2024

Choose a reason for hiding this comment

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

decimal was a weird choice for capturing all number, is most serializers I have worked with they will default to a double. This code now correctly supports parsing to int, double, DateTime, bool, and `string. Totally feel free to push back on anything, after all it's your repository :)

Copy link
Collaborator

@VisualBean VisualBean Mar 12, 2024

Choose a reason for hiding this comment

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

Decimal was ultimately chosen as double looses precision.
And since decimal (and double for that matter) can house simple integers, we never try to convert to regular ol' int.

double looses precision with certain conversions.
A few examples
1.2345678901234567890123456789E25
12345678901234567890.0123456789


[Test]
public void GetYamlCompatibleString_DateTimeDashString_WrappedWithQuotes()
=> this.Compose("2022-12-31 23:59:59", "'2022-12-31 23:59:59'");
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 is my comment about how I don't think these should require quotes anymore.

@VisualBean
Copy link
Collaborator

Hi @ByronMayne the PR is starting to rapidly grow, and is becoming more of a yaml change, than a netstandard change.
Do you mind breaking it apart into separate PR's.
The netstandard change we can skip right through and get it in asap.

@@ -2,3 +2,6 @@

# SA1623: Property summary documentation should match accessors
dotnet_diagnostic.SA1623.severity = suggestion

# SA1101: Prefix local calls with this
dotnet_diagnostic.SA1101.severity = none
Copy link
Collaborator

Choose a reason for hiding this comment

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

lets remove this.
We use stylecop, which enforces prefixing with 'this'

@ByronMayne
Copy link
Contributor Author

Hi @ByronMayne the PR is starting to rapidly grow, and is becoming more of a yaml change, than a netstandard change. Do you mind breaking it apart into separate PR's. The netstandard change we can skip right through and get it in asap.

I don't mind at all, I was thinking the same thing. I got a bit side tracked fixing the unit tests. Let me make two PRs.

@ByronMayne ByronMayne closed this Mar 12, 2024
@ByronMayne ByronMayne deleted the feature/convert-to-netstandard branch March 12, 2024 12:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants