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

[Storage] Use structs for model types where appropriate #7807

Merged
merged 17 commits into from
Oct 3, 2019

Conversation

ShivangiReja
Copy link
Member

Changing the code generation to use structs for model types identified in the API review.

This PR changes the AccountInfo, PageInfo and PageRange models to use struct as per the guidelines .

Here is the gist showing how the generated code would look like: https://gist.github.com/ShivangiReja/ccb498e4fba96452be7a52b156d17966

@ShivangiReja ShivangiReja self-assigned this Sep 30, 2019
@ShivangiReja ShivangiReja added the Storage Storage Service (Queues, Blobs, Files) label Sep 30, 2019

// Get response headers
string _header;
Azure.Core.Http.ETag eTag = default;
Copy link
Contributor

Choose a reason for hiding this comment

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

Other variables are defined with _ prefix. While I like how they look without prefix we should be consistent.

Copy link
Member

Choose a reason for hiding this comment

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

I'm not actually sure if we want that here. I prefixed the "well known variables" with an _ to avoid conflicts, but these are the sort of values that could cause conflicts with the _ variables. For example, if somebody adds a struct with a property called xml, we wouldn't want that to conflict with our "well known variable" _xml. I think this should probably change back what you originally had.

@@ -10,7 +10,7 @@ autorest --use=$PSScriptRoot/Azure.Storage.Common/swagger/Generator/ --verbose
cd $PSScriptRoot/Azure.Storage.Queues/swagger/
autorest --use=$PSScriptRoot/Azure.Storage.Common/swagger/Generator/ --verbose

cd $PSScriptRoot/Azure.Storage.Files.DataLake/swagger/
autorest --use=$PSScriptRoot/Azure.Storage.Common/swagger/Generator/ --verbose
# cd $PSScriptRoot/Azure.Storage.Files.DataLake/swagger/
Copy link
Contributor

Choose a reason for hiding this comment

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

revert?

Copy link
Member

Choose a reason for hiding this comment

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

We can comment this out and check it in. (The Storage folks checked in a work-in-progress that errors out.)

@pakrym
Copy link
Contributor

pakrym commented Sep 30, 2019

Use the Ordinal/OrginalIgnoreCase comparer for strings.

@pakrym
Copy link
Contributor

pakrym commented Sep 30, 2019

Figure out if we need to compare byte arrays by value

public class PageInfoTest
{
[Test]
public void EqualsReturnsTrueForEqualValues()
Copy link
Contributor

Choose a reason for hiding this comment

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

Are these tests are code-generated?

Copy link
Member Author

Choose a reason for hiding this comment

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

No, these are hand written tests.

@ShivangiReja
Copy link
Member Author

ShivangiReja commented Oct 1, 2019

@tg-msft @pakrym Using StructuralComparisons.StructuralEquality for array comparison and HashCode generation.

We cannot use System.Linq.Enumerable.SequenceEqual because it is not compatible with HashCode generation logic we were using before. My previous hashCode generation was using the default Object.GetHashCode() method which generates HashCode using the memory reference whereas System.Linq.Enumerable.SequenceEqual uses structural equality for comparison.

@pakrym
Copy link
Contributor

pakrym commented Oct 1, 2019

@tg-msft @pakrym Using StructuralComparisons.StructuralEquality for array comparison and HashCode generation.

This is way better than SequenceEqual good job finding it!


namespace Azure.Storage.Blobs.Tests
{
public class PageInfoTest
Copy link
Member

Choose a reason for hiding this comment

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

Maybe add a comment that these are related to our generated struct behavior so they make a little more sense to anyone else finding them in the future.

Copy link
Member

Choose a reason for hiding this comment

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

I agree, I wasn't sure what this test was for at first.

@@ -24,5 +24,6 @@
-->
<Compile Include="$(AzureCoreSharedSources)ArrayBufferWriter.cs" Link="AzureCoreShared\%(RecursiveDir)\%(Filename)%(Extension)" />
<Compile Include="$(AzureCoreSharedSources)ForwardsClientCallsAttribute.cs" Link="AzureCoreShared\%(RecursiveDir)\%(Filename)%(Extension)" />
<Compile Include="$(AzureCoreSharedSources)HashCodeBuilder.cs" />
Copy link
Member

Choose a reason for hiding this comment

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

Where does this end up placing the file? Do we want a Link="AzureCoreShared\... like the other included files?

Copy link
Member Author

Choose a reason for hiding this comment

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

It was working as expected by using <Compile Include="$(AzureCoreSharedSources)HashCodeBuilder.cs" /> only, that's why I didn't realize about adding Link="AzureCoreShared\%(RecursiveDir)\%(Filename)%(Extension)".

I'll update it :)


// Get response headers
string _header;
Azure.Core.Http.ETag eTag = default;
Copy link
Member

Choose a reason for hiding this comment

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

I'm not actually sure if we want that here. I prefixed the "well known variables" with an _ to avoid conflicts, but these are the sort of values that could cause conflicts with the _ variables. For example, if somebody adds a struct with a property called xml, we wouldn't want that to conflict with our "well known variable" _xml. I think this should probably change back what you originally had.

Copy link
Member

@tg-msft tg-msft left a comment

Choose a reason for hiding this comment

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

Awesome work Shivangi!

@@ -617,6 +626,17 @@ function generateOperation(w: IndentWriter, serviceModel: IServiceModel, group:
});
}
}
if (response.struct) {
w.line();
const Separator = IndentWriter.createFenceposter();
Copy link
Member

Choose a reason for hiding this comment

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

nit - don't we case locals as separator in TypeScript?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, I got confused between Typescript and .Net. Will update it.

w.line(`/// </summary>`);
w.line(`/// <param name="obj">The instance to compare to.</param>`);
w.line(`/// <returns>True if they're equal, false otherwise.</returns>`);
w.line(`public override bool Equals(object obj) => obj is ${naming.type(type.name)} && Equals((${naming.type(type.name)})obj);`);
Copy link
Member

Choose a reason for hiding this comment

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

@pakrym - should we add an [EditorBrowsable(false)] here?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yep

w.line(`/// <summary>`)
w.line(`/// Get a hash code for the ${naming.type(type.name)}.`);
w.line(`/// </summary>`);
w.line(`public override int GetHashCode()`);
Copy link
Member

Choose a reason for hiding this comment

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

We should definitely add [EditorBrowsable(false)] here.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yep


namespace Azure.Storage.Blobs.Tests
{
public class PageInfoTest
Copy link
Member

Choose a reason for hiding this comment

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

I agree, I wasn't sure what this test was for at first.

@ShivangiReja ShivangiReja merged commit 94e7c48 into Azure:master Oct 3, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Storage Storage Service (Queues, Blobs, Files)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants