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

Add a common definition for sub-resource #20524

Merged
merged 3 commits into from
Apr 21, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,22 @@ namespace Azure.ResourceManager.Core
/// A class representing a sub-resource that contains only the ID.
/// </summary>
[ReferenceType]
public abstract class SubResource
public partial class SubResource
{
/// <summary> Initializes a new instance of SubResource. </summary>
/// <param name="id"> ARM resource Id. </param>
internal SubResource(string id)
{
Id = id;
}

/// <summary>
/// ARM resource identifier.
/// </summary>
/// <value></value>
public virtual ResourceIdentifier Id { get; set; }
Copy link
Member

Choose a reason for hiding this comment

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

This should be get only with a protected constructor that takes in the id and sets it.

This is for deserialization scenarios

Copy link
Member Author

Choose a reason for hiding this comment

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

I put a todo in this file for us to consider how to make SubResource/SubResourceReadOnly to follow the current pattern (in Resource and TrackedResource) that the read-only one should be the default.
And I added serilization code. Thanks for the comments!

}
}

// Todo: we want to make the default one (SubResource) to represent read-only data,
// that is the pattern we used in Resource and TrackedResource
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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,19 @@ namespace Azure.ResourceManager.Core
/// A class representing a sub-resource that contains only the read-only ID.
/// </summary>
[ReferenceType]
public abstract class SubResourceReadOnly
public partial class SubResourceReadOnly
{
/// <summary> Initializes a new instance of SubResourceReadOnly. </summary>
/// <param name="id"> ARM resource Id. </param>
internal SubResourceReadOnly(string id)
Copy link
Member

Choose a reason for hiding this comment

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

needs to be protected internal otherwise the classes that inherit this from the other RPs won't be able to access it

Copy link
Member Author

Choose a reason for hiding this comment

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

Absolutely!

{
Id = id;
}

/// <summary>
/// ARM resource identifier (read-only).
/// </summary>
/// <value></value>
public virtual ResourceIdentifier Id { get; }
public virtual ResourceIdentifier Id { get; protected set; }
}
}