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
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
322 changes: 251 additions & 71 deletions sdk/storage/Azure.Storage.Blobs/src/Generated/BlobRestClient.cs

Large diffs are not rendered by default.

18 changes: 18 additions & 0 deletions sdk/storage/Azure.Storage.Blobs/swagger/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ directive:
transform: >
$.get.description = "Returns the sku name and account kind";
$.get.responses["200"]["x-az-response-name"] = "AccountInfo";
$.get.responses["200"]["x-az-struct"] = true;
- from: swagger-document
where: $["x-ms-paths"]
transform: >
Expand Down Expand Up @@ -693,6 +694,7 @@ directive:
transform: >
$.put.responses["201"]["x-az-response-name"] = "PageInfo";
$.put.responses["201"].description = "The operation completed successfully.";
$.put.responses["201"]["x-az-struct"] = true;
$.put.responses["201"].headers["x-ms-blob-sequence-number"].description = "The current sequence number for the page blob. This is only returned for page blobs.";
$.put.responses["201"].headers["x-ms-request-server-encrypted"]["x-az-demote-header"] = true;
```
Expand All @@ -705,13 +707,19 @@ directive:
transform: >
$.put.responses["201"]["x-az-response-name"] = "PageInfo";
$.put.responses["201"].description = "The operation completed successfully.";
$.put.responses["201"]["x-az-struct"] = true;
$.put.responses["201"].headers["x-ms-blob-sequence-number"].description = "The current sequence number for the page blob. This is only returned for page blobs.";
$.put.responses["201"].headers["x-ms-request-server-encrypted"] = {
"x-ms-client-name": "IsServerEncrypted",
"type": "boolean",
"x-az-demote-header": true,
"description": "The value of this header is set to true if the contents of the request are successfully encrypted using the specified algorithm, and false otherwise."
};
$.put.responses["201"].headers["x-ms-encryption-key-sha256"] = {
"x-ms-client-name": "EncryptionKeySha256",
"type": "string",
"description": "The SHA-256 hash of the encryption key used to encrypt the blob. This header is only returned when the blob was encrypted with a customer-provided key."
};
```

### /{containerName}/{blob}?comp=page&update&fromUrl
Expand All @@ -723,6 +731,7 @@ directive:
$.put.operationId = "PageBlob_UploadPagesFromUri";
$.put.responses["201"]["x-az-response-name"] = "PageInfo";
$.put.responses["201"].description = "The operation completed successfully.";
$.put.responses["201"]["x-az-struct"] = true;
$.put.responses["201"].headers["x-ms-blob-sequence-number"].description = "The current sequence number for the page blob. This is only returned for page blobs.";
$.put.responses["201"].headers["x-ms-request-server-encrypted"]["x-az-demote-header"] = true;
$.put.responses["304"] = {
Expand Down Expand Up @@ -766,6 +775,15 @@ directive:
};
```

### Define PageRange as struct
``` yaml
directive:
- from: swagger-document
where: $.definitions.PageRange
transform: >
$["x-az-struct"] = true;
```

### /{containerName}/{blob}?comp=properties&Resize
``` yaml
directive:
Expand Down
208 changes: 208 additions & 0 deletions sdk/storage/Azure.Storage.Blobs/tests/PageInfoTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using Azure.Storage.Blobs.Models;
using NUnit.Framework;

namespace Azure.Storage.Blobs.Tests
{
/// <summary>
/// These tests are related to our generated struct behavior.
/// </summary>
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.

{
[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.

{
var hash = new byte[] { 1, 2, 3 };

var info1 = new PageInfo(
new Core.Http.ETag("A"),
new DateTimeOffset(2019, 9, 25, 1, 1, 1, TimeSpan.Zero),
hash,
hash,
1,
"key1");

var info2 = new PageInfo(
new Core.Http.ETag("A"),
new DateTimeOffset(2019, 9, 25, 1, 1, 1, TimeSpan.Zero),
hash,
hash,
1,
"key1");

Assert.True(info1.Equals(info2));
Assert.True(info2.Equals(info1));

Assert.AreEqual(info1.GetHashCode(), info2.GetHashCode());
}

[Test]
public void EqualsReturnsTrueForNullValues()
{
var info1 = new PageInfo(
new Core.Http.ETag("A"),
new DateTimeOffset(2019, 9, 25, 1, 1, 1, TimeSpan.Zero),
null,
null,
1,
null);

var info2 = new PageInfo(
new Core.Http.ETag("A"),
new DateTimeOffset(2019, 9, 25, 1, 1, 1, TimeSpan.Zero),
null,
null,
1,
null);

Assert.True(info1.Equals(info2));
Assert.True(info2.Equals(info1));

Assert.AreEqual(info1.GetHashCode(), info2.GetHashCode());

}

[Test]
public void EqualsReturnsTrueIfCompareContentHashByValues()
{
var hash1 = new byte[] { 1, 2, 3 };
var hash2 = new byte[] { 1, 2, 3 };

var info1 = new PageInfo(
new Core.Http.ETag("A"),
new DateTimeOffset(2019, 9, 25, 1, 1, 1, TimeSpan.Zero),
hash1,
hash1,
1,
"key1");

var info2 = new PageInfo(
new Core.Http.ETag("A"),
new DateTimeOffset(2019, 9, 25, 1, 1, 1, TimeSpan.Zero),
hash2,
hash1,
1,
"key1");

Assert.True(info1.Equals(info2));
Assert.True(info2.Equals(info1));

Assert.AreEqual(info1.GetHashCode(), info2.GetHashCode());
}

[Test]
public void EqualsReturnsTrueIfCompareContentCrc64ByValues()
{
var hash1 = new byte[] { 1, 2, 3 };
var hash2 = new byte[] { 1, 2, 3 };

var info1 = new PageInfo(
new Core.Http.ETag("A"),
new DateTimeOffset(2019, 9, 25, 1, 1, 1, TimeSpan.Zero),
hash1,
hash1,
1,
"key1");

var info2 = new PageInfo(
new Core.Http.ETag("A"),
new DateTimeOffset(2019, 9, 25, 1, 1, 1, TimeSpan.Zero),
hash1,
hash2,
1,
"key1");

Assert.True(info1.Equals(info2));
Assert.True(info2.Equals(info1));

Assert.AreEqual(info1.GetHashCode(), info2.GetHashCode());
}

[Test]
public void EqualsReturnsFalseIfCompareContentHashWithNull()
{
var hash = new byte[] { 1, 2, 3 };

var info1 = new PageInfo(
new Core.Http.ETag("A"),
new DateTimeOffset(2019, 9, 25, 1, 1, 1, TimeSpan.Zero),
null,
hash,
1,
"key1");

var info2 = new PageInfo(
new Core.Http.ETag("A"),
new DateTimeOffset(2019, 9, 25, 1, 1, 1, TimeSpan.Zero),
hash,
hash,
1,
"key1");

Assert.True(!info1.Equals(info2));
Assert.True(!info2.Equals(info1));

Assert.AreNotEqual(info1.GetHashCode(), info2.GetHashCode());

}

[Test]
public void EqualsReturnsFalseIfCompareContentCrc64WithNull()
{
var hash = new byte[] { 1, 2, 3 };

var info1 = new PageInfo(
new Core.Http.ETag("A"),
new DateTimeOffset(2019, 9, 25, 1, 1, 1, TimeSpan.Zero),
hash,
null,
1,
"key1");

var info2 = new PageInfo(
new Core.Http.ETag("A"),
new DateTimeOffset(2019, 9, 25, 1, 1, 1, TimeSpan.Zero),
hash,
hash,
1,
"key1");

Assert.True(!info1.Equals(info2));
Assert.True(!info2.Equals(info1));

Assert.AreNotEqual(info1.GetHashCode(), info2.GetHashCode());

}


[Test]
public void EqualsReturnFalseIfCompareDifferentValues()
{
var hash = new byte[] { 1, 2, 3 };

var info1 = new PageInfo(
new Core.Http.ETag("B"),
new DateTimeOffset(2019, 9, 25, 1, 1, 1, TimeSpan.Zero),
hash,
hash,
1,
"key1");

var info2 = new PageInfo(
new Core.Http.ETag("A"),
new DateTimeOffset(2019, 11, 25, 1, 1, 1, TimeSpan.Zero),
hash,
hash,
2,
"key2");

Assert.True(!info1.Equals(info2));
Assert.True(!info2.Equals(info1));

Assert.AreNotEqual(info1.GetHashCode(), info2.GetHashCode());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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" Link="AzureCoreShared\%(RecursiveDir)\%(Filename)%(Extension)"/>
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ We support a number of extensions including using the vendor prefix `x-az-`:
- `x-az-skip-path-components`: Whether to skip any path components and always assume a fully formed URL to the resource (this currently must be set to `true`).
- `x-az-include-sync-methods`: Whether to generate support for sync methods. The default value is `false` (this flag should go away soon and always be `true`).
- `x-az-stream`: Whether to generate a non buffered request that takes owhership of the response stream. The default value is `false`.
- `x-az-struct`: Indicates whether a model is struct or not. The default value is `false`.
- `x-az-nullable-array`: Allows list to be null. The default value is `false`.

### Autorest plugin configuration
Expand Down
Loading