-
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.
Initial move to separate model reader writer in System.ClientModel (#…
…39478) * initial move to separate model reader writer in System.Net.ClientModel * update api * update doc settings for readme * address pr feedback * first cut at readme / changelog * fix perf issue in helper methods * add perf for srcgen * address feedback * update IModel to have a GetWireFormat * update api * update ci * remove GetOptions * WIP on new format changes (#39817) * wip on new format changes * more tweaks to the API * impl tweaks * address feedback * update api * fix tests * address feedback * rename based on feedback (#39885) * Remove wire (#40021) * remove wire * Update sdk/core/Azure.Core/tests/common/Internal/ModelReaderWriterHelper.cs Co-authored-by: Anne Thompson <[email protected]> * revert nullability since azure core tests don't support this right now --------- Co-authored-by: Anne Thompson <[email protected]> * Rename package (#40030) * rename package * comment out bad test * feedback * more feedback * update basic sample * remove next steps * update docs * address feedback * remove unused frozen code * address feedback * address additional feedback * update ci.yml * move file inside mrw folder * Add exception documentation * move helper file * update comment * readme feedback * follow static field convention * remove unecessary interlock * add azure sdk analyzers back * throw when cancelled * remove unused #if * remove azureicon * update readme * fix package icon * remove unnecessary conditions --------- Co-authored-by: Anne Thompson <[email protected]>
- Loading branch information
1 parent
9af180c
commit 8b96eb1
Showing
199 changed files
with
38,740 additions
and
1 deletion.
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
File renamed without changes
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
30 changes: 30 additions & 0 deletions
30
sdk/core/Azure.Core/tests/common/ModelReaderWriter/Internal/ModelReaderWriterHelper.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,30 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
using System.ClientModel; | ||
using System.ClientModel.Primitives; | ||
using System.Runtime.CompilerServices; | ||
|
||
namespace Azure.Core.Tests.Common | ||
{ | ||
internal static class ModelReaderWriterHelper | ||
{ | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static void ValidateFormat<T>(IPersistableModel<T> model, string format) | ||
{ | ||
bool implementsJson = model is IJsonModel<T>; | ||
bool isValid = (format == "J" && implementsJson) || format == "W"; | ||
if (!isValid) | ||
{ | ||
throw new FormatException($"The model {model.GetType().Name} does not support '{format}' format."); | ||
} | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static void ValidateFormat(IPersistableModel<object> model, string format) => ValidateFormat<object>(model, format); | ||
|
||
private static ModelReaderWriterOptions _wireOptions; | ||
public static ModelReaderWriterOptions WireOptions => _wireOptions ??= new ModelReaderWriterOptions("W"); | ||
} | ||
} |
173 changes: 173 additions & 0 deletions
173
sdk/core/Azure.Core/tests/common/ModelReaderWriter/Models/ChildModelXml.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,173 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.ClientModel; | ||
using System.ClientModel.Primitives; | ||
using System.Text.Json; | ||
using System.Xml; | ||
using System.Xml.Linq; | ||
using System.Xml.Serialization; | ||
using Azure.Core.Tests.Common; | ||
|
||
namespace Azure.Core.Tests.ModelReaderWriterTests.Models | ||
{ | ||
[XmlRoot("ChildTag")] | ||
public class ChildModelXml : IXmlSerializable, IPersistableModel<ChildModelXml>, IJsonModel<ChildModelXml>, IUtf8JsonSerializable | ||
{ | ||
internal ChildModelXml() { } | ||
|
||
/// <summary> Initializes a new instance of ModelXml for testing. </summary> | ||
/// <param name="value"></param> | ||
/// <exception cref="ArgumentNullException"> <paramref name="value"/> is null. </exception> | ||
public ChildModelXml(string value, string readonlyProperty) | ||
{ | ||
Argument.AssertNotNull(value, nameof(value)); | ||
|
||
ChildValue = value; | ||
ChildReadOnlyProperty = readonlyProperty; | ||
} | ||
|
||
/// <summary> Gets or sets the value. </summary> | ||
[XmlElement("ChildValue")] | ||
public string ChildValue { get; set; } | ||
/// <summary> Gets or sets the value. </summary> | ||
[XmlElement("ChildReadOnlyProperty")] | ||
public string ChildReadOnlyProperty { get; } | ||
|
||
void IXmlSerializable.Write(XmlWriter writer, string nameHint) => | ||
Serialize(writer, ModelReaderWriterHelper.WireOptions, nameHint); | ||
|
||
private void Serialize(XmlWriter writer, ModelReaderWriterOptions options, string nameHint) | ||
{ | ||
writer.WriteStartElement(nameHint ?? "ChildTag"); | ||
writer.WriteStartElement("ChildValue"); | ||
writer.WriteValue(ChildValue); | ||
writer.WriteEndElement(); | ||
if (options.Format == "J") | ||
{ | ||
writer.WriteStartElement("ChildReadOnlyProperty"); | ||
writer.WriteValue(ChildReadOnlyProperty); | ||
writer.WriteEndElement(); | ||
} | ||
writer.WriteEndElement(); | ||
} | ||
|
||
internal static ChildModelXml DeserializeChildModelXml(XElement element, ModelReaderWriterOptions options = default) | ||
{ | ||
options ??= ModelReaderWriterHelper.WireOptions; | ||
|
||
string value = default; | ||
string readonlyProperty = default; | ||
if (element.Element("ChildValue") is XElement valueElement) | ||
{ | ||
value = (string)valueElement; | ||
} | ||
if (element.Element("ChildReadOnlyProperty") is XElement readonlyPropertyElement) | ||
{ | ||
readonlyProperty = (string)readonlyPropertyElement; | ||
} | ||
return new ChildModelXml(value, readonlyProperty); | ||
} | ||
|
||
internal static ChildModelXml DeserializeChildModelXml(JsonElement element, ModelReaderWriterOptions options = default) | ||
{ | ||
options ??= ModelReaderWriterHelper.WireOptions; | ||
|
||
string childValue = default; | ||
string childReadOnlyProperty = default; | ||
|
||
Dictionary<string, BinaryData> rawData = new Dictionary<string, BinaryData>(); | ||
foreach (var property in element.EnumerateObject()) | ||
{ | ||
if (property.NameEquals("childValue"u8)) | ||
{ | ||
childValue = property.Value.GetString(); | ||
continue; | ||
} | ||
if (property.NameEquals("childReadOnlyProperty"u8)) | ||
{ | ||
childReadOnlyProperty = property.Value.GetString(); | ||
continue; | ||
} | ||
} | ||
return new ChildModelXml(childValue, childReadOnlyProperty); | ||
} | ||
|
||
ChildModelXml IPersistableModel<ChildModelXml>.Create(BinaryData data, ModelReaderWriterOptions options) | ||
{ | ||
ModelReaderWriterHelper.ValidateFormat(this, options.Format); | ||
|
||
if (options.Format == "J") | ||
{ | ||
using var doc = JsonDocument.Parse(data); | ||
return DeserializeChildModelXml(doc.RootElement, options); | ||
} | ||
else | ||
{ | ||
return DeserializeChildModelXml(XElement.Load(data.ToStream()), options); | ||
} | ||
} | ||
|
||
BinaryData IPersistableModel<ChildModelXml>.Write(ModelReaderWriterOptions options) | ||
{ | ||
ModelReaderWriterHelper.ValidateFormat(this, options.Format); | ||
|
||
if (options.Format == "J") | ||
{ | ||
return ModelReaderWriter.Write(this, options); | ||
} | ||
else | ||
{ | ||
options ??= ModelReaderWriterHelper.WireOptions; | ||
using MemoryStream stream = new MemoryStream(); | ||
using XmlWriter writer = XmlWriter.Create(stream); | ||
Serialize(writer, options, null); | ||
writer.Flush(); | ||
if (stream.Position > int.MaxValue) | ||
{ | ||
return BinaryData.FromStream(stream); | ||
} | ||
else | ||
{ | ||
return new BinaryData(stream.GetBuffer().AsMemory(0, (int)stream.Position)); | ||
} | ||
} | ||
} | ||
|
||
private void Serialize(Utf8JsonWriter writer, ModelReaderWriterOptions options) | ||
{ | ||
writer.WriteStartObject(); | ||
writer.WritePropertyName("childValue"u8); | ||
writer.WriteStringValue(ChildValue); | ||
if (options.Format == "J") | ||
{ | ||
writer.WritePropertyName("childReadOnlyProperty"u8); | ||
writer.WriteStringValue(ChildReadOnlyProperty); | ||
} | ||
writer.WriteEndObject(); | ||
} | ||
|
||
void IJsonModel<ChildModelXml>.Write(Utf8JsonWriter writer, ModelReaderWriterOptions options) | ||
{ | ||
ModelReaderWriterHelper.ValidateFormat(this, options.Format); | ||
|
||
Serialize(writer, options); | ||
} | ||
|
||
ChildModelXml IJsonModel<ChildModelXml>.Create(ref Utf8JsonReader reader, ModelReaderWriterOptions options) | ||
{ | ||
ModelReaderWriterHelper.ValidateFormat(this, options.Format); | ||
|
||
using var doc = JsonDocument.ParseValue(ref reader); | ||
return DeserializeChildModelXml(doc.RootElement, options); | ||
} | ||
|
||
void IUtf8JsonSerializable.Write(Utf8JsonWriter writer) => | ||
Serialize(writer, ModelReaderWriterHelper.WireOptions); | ||
|
||
string IPersistableModel<ChildModelXml>.GetFormatFromOptions(ModelReaderWriterOptions options) => "X"; | ||
} | ||
} |
Oops, something went wrong.