-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a common definition for sub-resource (#20524)
* add SubResource * serialization of the two classes * protected internal; full subresource namespace Co-authored-by: Yeming Liu <yeliu@microsoft.com>
- Loading branch information
Showing
12 changed files
with
182 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
sdk/resourcemanager/Azure.ResourceManager.Core/src/Resources/SubResource.Serialization.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
using System.Text.Json; | ||
using Azure.Core; | ||
|
||
namespace Azure.ResourceManager.Core | ||
{ | ||
/// <summary> | ||
/// A class representing a sub-resource that contains only the ID. | ||
/// </summary> | ||
public partial class SubResource : IUtf8JsonSerializable | ||
{ | ||
/// <summary> | ||
/// Serialize the input SubResource object. | ||
/// </summary> | ||
/// <param name="writer"> Input Json writer. </param> | ||
void IUtf8JsonSerializable.Write(Utf8JsonWriter writer) | ||
{ | ||
if (writer is null) | ||
{ | ||
throw new ArgumentNullException(nameof(writer)); | ||
} | ||
|
||
writer.WriteStartObject(); | ||
if (Optional.IsDefined(Id)) | ||
{ | ||
writer.WritePropertyName("id"); | ||
writer.WriteStringValue(Id); | ||
} | ||
writer.WriteEndObject(); | ||
} | ||
|
||
/// <summary> | ||
/// Deserialize the input JSON element to a SubResource object. | ||
/// </summary> | ||
/// <param name="element">The JSON element to be deserialized.</param> | ||
/// <returns>Deserialized SubResource object.</returns> | ||
internal static SubResource DeserializeSubResource(JsonElement element) | ||
{ | ||
Optional<string> id = default; | ||
foreach (var property in element.EnumerateObject()) | ||
{ | ||
if (property.NameEquals("id")) | ||
{ | ||
id = property.Value.GetString(); | ||
continue; | ||
} | ||
} | ||
return new SubResource(id.Value); | ||
} | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
sdk/resourcemanager/Azure.ResourceManager.Core/src/Resources/SubResource.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
namespace Azure.ResourceManager.Core | ||
{ | ||
/// <summary> | ||
/// A class representing a sub-resource that contains only the ID. | ||
/// </summary> | ||
[ReferenceType] | ||
public partial class SubResource | ||
{ | ||
/// <summary> Initializes a new instance of SubResource. </summary> | ||
/// <param name="id"> ARM resource Id. </param> | ||
protected internal SubResource(string id) | ||
{ | ||
Id = id; | ||
} | ||
|
||
/// <summary> | ||
/// ARM resource identifier. | ||
/// </summary> | ||
/// <value></value> | ||
public virtual ResourceIdentifier Id { get; set; } | ||
} | ||
} | ||
|
||
// Todo: we want to make the default one (SubResource) to represent read-only data, | ||
// that is the pattern we used in Resource and TrackedResource |
49 changes: 49 additions & 0 deletions
49
...urcemanager/Azure.ResourceManager.Core/src/Resources/SubResourceReadOnly.Serialization.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
using System.Text.Json; | ||
using Azure.Core; | ||
|
||
namespace Azure.ResourceManager.Core | ||
{ | ||
/// <summary> | ||
/// A class representing a sub-resource that contains only the ID. | ||
/// </summary> | ||
public partial class SubResourceReadOnly : IUtf8JsonSerializable | ||
{ | ||
/// <summary> | ||
/// Serialize the input SubResourceReadOnly object. | ||
/// </summary> | ||
/// <param name="writer"> Input Json writer. </param> | ||
void IUtf8JsonSerializable.Write(Utf8JsonWriter writer) | ||
{ | ||
if (writer is null) | ||
{ | ||
throw new ArgumentNullException(nameof(writer)); | ||
} | ||
|
||
writer.WriteStartObject(); | ||
writer.WriteEndObject(); | ||
} | ||
|
||
/// <summary> | ||
/// Deserialize the input JSON element to a SubResourceReadOnly object. | ||
/// </summary> | ||
/// <param name="element">The JSON element to be deserialized.</param> | ||
/// <returns>Deserialized SubResourceReadOnly object.</returns> | ||
internal static SubResourceReadOnly DeserializeSubResourceReadOnly(JsonElement element) | ||
{ | ||
Optional<string> id = default; | ||
foreach (var property in element.EnumerateObject()) | ||
{ | ||
if (property.NameEquals("id")) | ||
{ | ||
id = property.Value.GetString(); | ||
continue; | ||
} | ||
} | ||
return new SubResourceReadOnly(id.Value); | ||
} | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
sdk/resourcemanager/Azure.ResourceManager.Core/src/Resources/SubResourceReadOnly.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
namespace Azure.ResourceManager.Core | ||
{ | ||
/// <summary> | ||
/// A class representing a sub-resource that contains only the read-only ID. | ||
/// </summary> | ||
[ReferenceType] | ||
public partial class SubResourceReadOnly | ||
{ | ||
/// <summary> Initializes a new instance of SubResourceReadOnly. </summary> | ||
/// <param name="id"> ARM resource Id. </param> | ||
protected internal SubResourceReadOnly(string id) | ||
{ | ||
Id = id; | ||
} | ||
|
||
/// <summary> | ||
/// ARM resource identifier (read-only). | ||
/// </summary> | ||
/// <value></value> | ||
public virtual ResourceIdentifier Id { get; protected set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters