Skip to content

Commit f628c98

Browse files
authored
ClientModel: Reshuffle some whitespace and files for consistency across ClientModel (#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
1 parent 7f7ee04 commit f628c98

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+1481
-1380
lines changed

.editorconfig

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ trim_trailing_whitespace = true
99
# Default settings:
1010
# A newline ending every file
1111
# Use 4 spaces as indentation
12-
[sdk/*/Azure.*/**]
12+
[sdk/*/{Azure.*,System.*}/**]
1313
insert_final_newline = true
1414
indent_style = space
1515
indent_size = 4
1616

1717
# C# files
18-
[sdk/*/Azure.*/**.cs]
18+
[sdk/*/{Azure.*,System.*}/**.cs]
1919
# New line preferences
2020
csharp_new_line_before_open_brace = all # vs-default: any
2121
csharp_new_line_before_else = true # vs-default: true
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[/**.cs]
2+
csharp_style_namespace_declarations = file_scoped:error

sdk/core/System.ClientModel/src/ModelReaderWriter/IJsonModel.cs

+20-21
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,32 @@
33

44
using System.Text.Json;
55

6-
namespace System.ClientModel.Primitives
6+
namespace System.ClientModel.Primitives;
7+
8+
/// <summary>
9+
/// Allows an object to control its own JSON writing and reading.
10+
/// </summary>
11+
/// <typeparam name="T">The type the model can be converted into.</typeparam>
12+
public interface IJsonModel<out T> : IPersistableModel<T>
713
{
814
/// <summary>
9-
/// Allows an object to control its own JSON writing and reading.
15+
/// Writes the model to the provided <see cref="Utf8JsonWriter"/>.
1016
/// </summary>
11-
/// <typeparam name="T">The type the model can be converted into.</typeparam>
12-
public interface IJsonModel<out T> : IPersistableModel<T>
13-
{
14-
/// <summary>
15-
/// Writes the model to the provided <see cref="Utf8JsonWriter"/>.
16-
/// </summary>
17-
/// <param name="writer">The <see cref="Utf8JsonWriter"/> to write into.</param>
18-
/// <param name="options">The <see cref="ModelReaderWriterOptions"/> to use.</param>
19-
/// <exception cref="FormatException">If the model does not support the requested <see cref="ModelReaderWriterOptions.Format"/>.</exception>
17+
/// <param name="writer">The <see cref="Utf8JsonWriter"/> to write into.</param>
18+
/// <param name="options">The <see cref="ModelReaderWriterOptions"/> to use.</param>
19+
/// <exception cref="FormatException">If the model does not support the requested <see cref="ModelReaderWriterOptions.Format"/>.</exception>
2020
#pragma warning disable AZC0014 // Avoid using banned types in public API
21-
void Write(Utf8JsonWriter writer, ModelReaderWriterOptions options);
21+
void Write(Utf8JsonWriter writer, ModelReaderWriterOptions options);
2222
#pragma warning restore AZC0014 // Avoid using banned types in public API
2323

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

4-
namespace System.ClientModel.Primitives
4+
namespace System.ClientModel.Primitives;
5+
6+
/// <summary>
7+
/// Allows an object to control its own writing and reading.
8+
/// The format is determined by the implementer.
9+
/// </summary>
10+
/// <typeparam name="T">The type the model can be converted into.</typeparam>
11+
public interface IPersistableModel<out T>
512
{
613
/// <summary>
7-
/// Allows an object to control its own writing and reading.
8-
/// The format is determined by the implementer.
14+
/// Writes the model into a <see cref="BinaryData"/>.
915
/// </summary>
10-
/// <typeparam name="T">The type the model can be converted into.</typeparam>
11-
public interface IPersistableModel<out T>
12-
{
13-
/// <summary>
14-
/// Writes the model into a <see cref="BinaryData"/>.
15-
/// </summary>
16-
/// <param name="options">The <see cref="ModelReaderWriterOptions"/> to use.</param>
17-
/// <returns>A binary representation of the written model.</returns>
18-
/// <exception cref="FormatException">If the model does not support the requested <see cref="ModelReaderWriterOptions.Format"/>.</exception>
19-
BinaryData Write(ModelReaderWriterOptions options);
16+
/// <param name="options">The <see cref="ModelReaderWriterOptions"/> to use.</param>
17+
/// <returns>A binary representation of the written model.</returns>
18+
/// <exception cref="FormatException">If the model does not support the requested <see cref="ModelReaderWriterOptions.Format"/>.</exception>
19+
BinaryData Write(ModelReaderWriterOptions options);
2020

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

30-
/// <summary>
31-
/// Gets the data interchange format (JSON, Xml, etc) that the model uses when communicating with the service.
32-
/// <param name="options">The <see cref="ModelReaderWriterOptions"/> to use.</param>
33-
/// </summary>
34-
/// <returns>The format that the model uses when communicating with the serivce.</returns>
35-
string GetFormatFromOptions(ModelReaderWriterOptions options);
36-
}
30+
/// <summary>
31+
/// Gets the data interchange format (JSON, Xml, etc) that the model uses when communicating with the service.
32+
/// <param name="options">The <see cref="ModelReaderWriterOptions"/> to use.</param>
33+
/// </summary>
34+
/// <returns>The format that the model uses when communicating with the serivce.</returns>
35+
string GetFormatFromOptions(ModelReaderWriterOptions options);
3736
}

sdk/core/System.ClientModel/src/ModelReaderWriter/JsonModelConverter.cs

+42-43
Original file line numberDiff line numberDiff line change
@@ -5,57 +5,56 @@
55
using System.Text.Json;
66
using System.Text.Json.Serialization;
77

8-
namespace System.ClientModel.Primitives
8+
namespace System.ClientModel.Primitives;
9+
10+
/// <summary>
11+
/// A generic converter which allows <see cref="JsonSerializer"/> to be able to write and read any models that implement <see cref="IJsonModel{T}"/>.
12+
/// </summary>
13+
[RequiresUnreferencedCode("The constructors of the type being deserialized are dynamically accessed and may be trimmed.")]
14+
#pragma warning disable AZC0014 // Avoid using banned types in public API
15+
internal class JsonModelConverter : JsonConverter<IJsonModel<object>>
16+
#pragma warning restore AZC0014 // Avoid using banned types in public API
917
{
1018
/// <summary>
11-
/// A generic converter which allows <see cref="JsonSerializer"/> to be able to write and read any models that implement <see cref="IJsonModel{T}"/>.
19+
/// Gets the <see cref="ModelReaderWriterOptions"/> used to read and write models.
1220
/// </summary>
13-
[RequiresUnreferencedCode("The constructors of the type being deserialized are dynamically accessed and may be trimmed.")]
14-
#pragma warning disable AZC0014 // Avoid using banned types in public API
15-
internal class JsonModelConverter : JsonConverter<IJsonModel<object>>
16-
#pragma warning restore AZC0014 // Avoid using banned types in public API
21+
public ModelReaderWriterOptions Options { get; }
22+
23+
/// <summary>
24+
/// Initializes a new instance of <see cref="JsonModelConverter"/> with a default options of <see cref="ModelReaderWriterOptions.Json"/>.
25+
/// </summary>
26+
public JsonModelConverter()
27+
: this(ModelReaderWriterOptions.Json) { }
28+
29+
/// <summary>
30+
/// Initializes a new instance of <see cref="JsonModelConverter"/>.
31+
/// </summary>
32+
/// <param name="options">The <see cref="ModelReaderWriterOptions"/> to use.</param>
33+
public JsonModelConverter(ModelReaderWriterOptions options)
1734
{
18-
/// <summary>
19-
/// Gets the <see cref="ModelReaderWriterOptions"/> used to read and write models.
20-
/// </summary>
21-
public ModelReaderWriterOptions Options { get; }
22-
23-
/// <summary>
24-
/// Initializes a new instance of <see cref="JsonModelConverter"/> with a default options of <see cref="ModelReaderWriterOptions.Json"/>.
25-
/// </summary>
26-
public JsonModelConverter()
27-
: this(ModelReaderWriterOptions.Json) { }
28-
29-
/// <summary>
30-
/// Initializes a new instance of <see cref="JsonModelConverter"/>.
31-
/// </summary>
32-
/// <param name="options">The <see cref="ModelReaderWriterOptions"/> to use.</param>
33-
public JsonModelConverter(ModelReaderWriterOptions options)
34-
{
35-
Options = options;
36-
}
37-
38-
/// <inheritdoc/>
39-
public override bool CanConvert(Type typeToConvert)
40-
{
41-
return !Attribute.IsDefined(typeToConvert, typeof(JsonConverterAttribute));
42-
}
43-
44-
/// <inheritdoc/>
35+
Options = options;
36+
}
37+
38+
/// <inheritdoc/>
39+
public override bool CanConvert(Type typeToConvert)
40+
{
41+
return !Attribute.IsDefined(typeToConvert, typeof(JsonConverterAttribute));
42+
}
43+
44+
/// <inheritdoc/>
4545
#pragma warning disable AZC0014 // Avoid using banned types in public API
46-
public override IJsonModel<object> Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
46+
public override IJsonModel<object> Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
4747
#pragma warning restore AZC0014 // Avoid using banned types in public API
48-
{
49-
using JsonDocument document = JsonDocument.ParseValue(ref reader);
50-
return (IJsonModel<object>)ModelReaderWriter.Read(BinaryData.FromString(document.RootElement.GetRawText()), typeToConvert, Options)!;
51-
}
48+
{
49+
using JsonDocument document = JsonDocument.ParseValue(ref reader);
50+
return (IJsonModel<object>)ModelReaderWriter.Read(BinaryData.FromString(document.RootElement.GetRawText()), typeToConvert, Options)!;
51+
}
5252

53-
/// <inheritdoc/>
53+
/// <inheritdoc/>
5454
#pragma warning disable AZC0014 // Avoid using banned types in public API
55-
public override void Write(Utf8JsonWriter writer, IJsonModel<object> value, JsonSerializerOptions options)
55+
public override void Write(Utf8JsonWriter writer, IJsonModel<object> value, JsonSerializerOptions options)
5656
#pragma warning restore AZC0014 // Avoid using banned types in public API
57-
{
58-
value.Write(writer, Options);
59-
}
57+
{
58+
value.Write(writer, Options);
6059
}
6160
}

0 commit comments

Comments
 (0)