Skip to content

Commit

Permalink
ClientModel: Reshuffle some whitespace and files for consistency acro…
Browse files Browse the repository at this point in the history
…ss ClientModel (Azure#41425)

* Initial implementation of SetHeader

* Add tests for SetHeader

* more tests

* updates

* Reshuffle a bit for consistency across ClientModel files

* remove SetHeader changes that will be part of a separate PR

* nits

* Add .editorconfig

* remove most NoWarn suppressions from .csproj
  • Loading branch information
annelo-msft authored Jan 20, 2024
1 parent 7f7ee04 commit f628c98
Show file tree
Hide file tree
Showing 51 changed files with 1,481 additions and 1,380 deletions.
4 changes: 2 additions & 2 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ trim_trailing_whitespace = true
# Default settings:
# A newline ending every file
# Use 4 spaces as indentation
[sdk/*/Azure.*/**]
[sdk/*/{Azure.*,System.*}/**]
insert_final_newline = true
indent_style = space
indent_size = 4

# C# files
[sdk/*/Azure.*/**.cs]
[sdk/*/{Azure.*,System.*}/**.cs]
# New line preferences
csharp_new_line_before_open_brace = all # vs-default: any
csharp_new_line_before_else = true # vs-default: true
Expand Down
2 changes: 2 additions & 0 deletions sdk/core/System.ClientModel/src/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[/**.cs]
csharp_style_namespace_declarations = file_scoped:error
41 changes: 20 additions & 21 deletions sdk/core/System.ClientModel/src/ModelReaderWriter/IJsonModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,32 @@

using System.Text.Json;

namespace System.ClientModel.Primitives
namespace System.ClientModel.Primitives;

/// <summary>
/// Allows an object to control its own JSON writing and reading.
/// </summary>
/// <typeparam name="T">The type the model can be converted into.</typeparam>
public interface IJsonModel<out T> : IPersistableModel<T>
{
/// <summary>
/// Allows an object to control its own JSON writing and reading.
/// Writes the model to the provided <see cref="Utf8JsonWriter"/>.
/// </summary>
/// <typeparam name="T">The type the model can be converted into.</typeparam>
public interface IJsonModel<out T> : IPersistableModel<T>
{
/// <summary>
/// Writes the model to the provided <see cref="Utf8JsonWriter"/>.
/// </summary>
/// <param name="writer">The <see cref="Utf8JsonWriter"/> to write into.</param>
/// <param name="options">The <see cref="ModelReaderWriterOptions"/> to use.</param>
/// <exception cref="FormatException">If the model does not support the requested <see cref="ModelReaderWriterOptions.Format"/>.</exception>
/// <param name="writer">The <see cref="Utf8JsonWriter"/> to write into.</param>
/// <param name="options">The <see cref="ModelReaderWriterOptions"/> to use.</param>
/// <exception cref="FormatException">If the model does not support the requested <see cref="ModelReaderWriterOptions.Format"/>.</exception>
#pragma warning disable AZC0014 // Avoid using banned types in public API
void Write(Utf8JsonWriter writer, ModelReaderWriterOptions options);
void Write(Utf8JsonWriter writer, ModelReaderWriterOptions options);
#pragma warning restore AZC0014 // Avoid using banned types in public API

/// <summary>
/// Reads one JSON value (including objects or arrays) from the provided reader and converts it to a model.
/// </summary>
/// <param name="reader">The <see cref="Utf8JsonReader"/> to read.</param>
/// <param name="options">The <see cref="ModelReaderWriterOptions"/> to use.</param>
/// <returns>A <typeparamref name="T"/> representation of the JSON value.</returns>
/// <exception cref="FormatException">If the model does not support the requested <see cref="ModelReaderWriterOptions.Format"/>.</exception>
/// <summary>
/// Reads one JSON value (including objects or arrays) from the provided reader and converts it to a model.
/// </summary>
/// <param name="reader">The <see cref="Utf8JsonReader"/> to read.</param>
/// <param name="options">The <see cref="ModelReaderWriterOptions"/> to use.</param>
/// <returns>A <typeparamref name="T"/> representation of the JSON value.</returns>
/// <exception cref="FormatException">If the model does not support the requested <see cref="ModelReaderWriterOptions.Format"/>.</exception>
#pragma warning disable AZC0014 // Avoid using banned types in public API
T Create(ref Utf8JsonReader reader, ModelReaderWriterOptions options);
T Create(ref Utf8JsonReader reader, ModelReaderWriterOptions options);
#pragma warning restore AZC0014 // Avoid using banned types in public API
}
}
Original file line number Diff line number Diff line change
@@ -1,37 +1,36 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

namespace System.ClientModel.Primitives
namespace System.ClientModel.Primitives;

/// <summary>
/// Allows an object to control its own writing and reading.
/// The format is determined by the implementer.
/// </summary>
/// <typeparam name="T">The type the model can be converted into.</typeparam>
public interface IPersistableModel<out T>
{
/// <summary>
/// Allows an object to control its own writing and reading.
/// The format is determined by the implementer.
/// Writes the model into a <see cref="BinaryData"/>.
/// </summary>
/// <typeparam name="T">The type the model can be converted into.</typeparam>
public interface IPersistableModel<out T>
{
/// <summary>
/// Writes the model into a <see cref="BinaryData"/>.
/// </summary>
/// <param name="options">The <see cref="ModelReaderWriterOptions"/> to use.</param>
/// <returns>A binary representation of the written model.</returns>
/// <exception cref="FormatException">If the model does not support the requested <see cref="ModelReaderWriterOptions.Format"/>.</exception>
BinaryData Write(ModelReaderWriterOptions options);
/// <param name="options">The <see cref="ModelReaderWriterOptions"/> to use.</param>
/// <returns>A binary representation of the written model.</returns>
/// <exception cref="FormatException">If the model does not support the requested <see cref="ModelReaderWriterOptions.Format"/>.</exception>
BinaryData Write(ModelReaderWriterOptions options);

/// <summary>
/// Converts the provided <see cref="BinaryData"/> into a model.
/// </summary>
/// <param name="data">The <see cref="BinaryData"/> to parse.</param>
/// <param name="options">The <see cref="ModelReaderWriterOptions"/> to use.</param>
/// <returns>A <typeparamref name="T"/> representation of the data.</returns>
/// <exception cref="FormatException">If the model does not support the requested <see cref="ModelReaderWriterOptions.Format"/>.</exception>
T Create(BinaryData data, ModelReaderWriterOptions options);
/// <summary>
/// Converts the provided <see cref="BinaryData"/> into a model.
/// </summary>
/// <param name="data">The <see cref="BinaryData"/> to parse.</param>
/// <param name="options">The <see cref="ModelReaderWriterOptions"/> to use.</param>
/// <returns>A <typeparamref name="T"/> representation of the data.</returns>
/// <exception cref="FormatException">If the model does not support the requested <see cref="ModelReaderWriterOptions.Format"/>.</exception>
T Create(BinaryData data, ModelReaderWriterOptions options);

/// <summary>
/// Gets the data interchange format (JSON, Xml, etc) that the model uses when communicating with the service.
/// <param name="options">The <see cref="ModelReaderWriterOptions"/> to use.</param>
/// </summary>
/// <returns>The format that the model uses when communicating with the serivce.</returns>
string GetFormatFromOptions(ModelReaderWriterOptions options);
}
/// <summary>
/// Gets the data interchange format (JSON, Xml, etc) that the model uses when communicating with the service.
/// <param name="options">The <see cref="ModelReaderWriterOptions"/> to use.</param>
/// </summary>
/// <returns>The format that the model uses when communicating with the serivce.</returns>
string GetFormatFromOptions(ModelReaderWriterOptions options);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,57 +5,56 @@
using System.Text.Json;
using System.Text.Json.Serialization;

namespace System.ClientModel.Primitives
namespace System.ClientModel.Primitives;

/// <summary>
/// A generic converter which allows <see cref="JsonSerializer"/> to be able to write and read any models that implement <see cref="IJsonModel{T}"/>.
/// </summary>
[RequiresUnreferencedCode("The constructors of the type being deserialized are dynamically accessed and may be trimmed.")]
#pragma warning disable AZC0014 // Avoid using banned types in public API
internal class JsonModelConverter : JsonConverter<IJsonModel<object>>
#pragma warning restore AZC0014 // Avoid using banned types in public API
{
/// <summary>
/// A generic converter which allows <see cref="JsonSerializer"/> to be able to write and read any models that implement <see cref="IJsonModel{T}"/>.
/// Gets the <see cref="ModelReaderWriterOptions"/> used to read and write models.
/// </summary>
[RequiresUnreferencedCode("The constructors of the type being deserialized are dynamically accessed and may be trimmed.")]
#pragma warning disable AZC0014 // Avoid using banned types in public API
internal class JsonModelConverter : JsonConverter<IJsonModel<object>>
#pragma warning restore AZC0014 // Avoid using banned types in public API
public ModelReaderWriterOptions Options { get; }

/// <summary>
/// Initializes a new instance of <see cref="JsonModelConverter"/> with a default options of <see cref="ModelReaderWriterOptions.Json"/>.
/// </summary>
public JsonModelConverter()
: this(ModelReaderWriterOptions.Json) { }

/// <summary>
/// Initializes a new instance of <see cref="JsonModelConverter"/>.
/// </summary>
/// <param name="options">The <see cref="ModelReaderWriterOptions"/> to use.</param>
public JsonModelConverter(ModelReaderWriterOptions options)
{
/// <summary>
/// Gets the <see cref="ModelReaderWriterOptions"/> used to read and write models.
/// </summary>
public ModelReaderWriterOptions Options { get; }

/// <summary>
/// Initializes a new instance of <see cref="JsonModelConverter"/> with a default options of <see cref="ModelReaderWriterOptions.Json"/>.
/// </summary>
public JsonModelConverter()
: this(ModelReaderWriterOptions.Json) { }

/// <summary>
/// Initializes a new instance of <see cref="JsonModelConverter"/>.
/// </summary>
/// <param name="options">The <see cref="ModelReaderWriterOptions"/> to use.</param>
public JsonModelConverter(ModelReaderWriterOptions options)
{
Options = options;
}

/// <inheritdoc/>
public override bool CanConvert(Type typeToConvert)
{
return !Attribute.IsDefined(typeToConvert, typeof(JsonConverterAttribute));
}

/// <inheritdoc/>
Options = options;
}

/// <inheritdoc/>
public override bool CanConvert(Type typeToConvert)
{
return !Attribute.IsDefined(typeToConvert, typeof(JsonConverterAttribute));
}

/// <inheritdoc/>
#pragma warning disable AZC0014 // Avoid using banned types in public API
public override IJsonModel<object> Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
public override IJsonModel<object> Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
#pragma warning restore AZC0014 // Avoid using banned types in public API
{
using JsonDocument document = JsonDocument.ParseValue(ref reader);
return (IJsonModel<object>)ModelReaderWriter.Read(BinaryData.FromString(document.RootElement.GetRawText()), typeToConvert, Options)!;
}
{
using JsonDocument document = JsonDocument.ParseValue(ref reader);
return (IJsonModel<object>)ModelReaderWriter.Read(BinaryData.FromString(document.RootElement.GetRawText()), typeToConvert, Options)!;
}

/// <inheritdoc/>
/// <inheritdoc/>
#pragma warning disable AZC0014 // Avoid using banned types in public API
public override void Write(Utf8JsonWriter writer, IJsonModel<object> value, JsonSerializerOptions options)
public override void Write(Utf8JsonWriter writer, IJsonModel<object> value, JsonSerializerOptions options)
#pragma warning restore AZC0014 // Avoid using banned types in public API
{
value.Write(writer, Options);
}
{
value.Write(writer, Options);
}
}
Loading

0 comments on commit f628c98

Please sign in to comment.