Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
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
Expand Up @@ -30,6 +30,9 @@ namespace SampleTypeSpec
[ModelReaderWriterBuildable(typeof(Thing))]
[ModelReaderWriterBuildable(typeof(UnknownAnimal))]
[ModelReaderWriterBuildable(typeof(UnknownPet))]
[ModelReaderWriterBuildable(typeof(XmlAdvancedModel))]
[ModelReaderWriterBuildable(typeof(XmlItem))]
[ModelReaderWriterBuildable(typeof(XmlNestedModel))]
public partial class SampleTypeSpecContext : ModelReaderWriterContext
{
}
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
// <auto-generated/>

#nullable disable

using System;
using System.Collections.Generic;
using System.Linq;

namespace SampleTypeSpec
{
/// <summary> An advanced XML model for testing various property types and XML features. </summary>
public partial class XmlAdvancedModel
{
/// <summary> Keeps track of any properties unknown to the library. </summary>
private protected readonly IDictionary<string, BinaryData> _additionalBinaryDataProperties;

/// <summary> Initializes a new instance of <see cref="XmlAdvancedModel"/>. </summary>
/// <param name="name"> A simple string property. </param>
/// <param name="age"> An integer property. </param>
/// <param name="enabled"> A boolean property. </param>
/// <param name="score"> A float property. </param>
/// <param name="nullableString"> A nullable string. </param>
/// <param name="id"> A string as XML attribute. </param>
/// <param name="version"> An integer as XML attribute. </param>
/// <param name="isActive"> A boolean as XML attribute. </param>
/// <param name="originalName"> A property with a custom XML element name. </param>
/// <param name="xmlIdentifier"> An attribute with a custom XML name. </param>
/// <param name="content"> Text content in the element (unwrapped string). </param>
/// <param name="unwrappedStrings"> An unwrapped array of strings - items appear directly without wrapper. </param>
/// <param name="unwrappedCounts"> An unwrapped array of integers. </param>
/// <param name="unwrappedItems"> An unwrapped array of models. </param>
/// <param name="wrappedColors"> A wrapped array of strings (default). </param>
/// <param name="items"> A wrapped array with custom wrapper name. </param>
/// <param name="nestedModel"> A nested model property. </param>
/// <param name="metadata"> A dictionary property. </param>
/// <param name="createdAt"> A date-time property. </param>
/// <param name="duration"> A duration property. </param>
/// <param name="data"> A bytes property. </param>
internal XmlAdvancedModel(string name, int age, bool enabled, float score, string nullableString, string id, int version, bool isActive, string originalName, string xmlIdentifier, string content, IEnumerable<string> unwrappedStrings, IEnumerable<int> unwrappedCounts, IEnumerable<XmlItem> unwrappedItems, IEnumerable<string> wrappedColors, IEnumerable<XmlItem> items, XmlNestedModel nestedModel, IDictionary<string, string> metadata, DateTimeOffset createdAt, TimeSpan duration, BinaryData data)
{
Name = name;
Age = age;
Enabled = enabled;
Score = score;
NullableString = nullableString;
Id = id;
Version = version;
IsActive = isActive;
OriginalName = originalName;
XmlIdentifier = xmlIdentifier;
Content = content;
UnwrappedStrings = unwrappedStrings.ToList();
UnwrappedCounts = unwrappedCounts.ToList();
UnwrappedItems = unwrappedItems.ToList();
WrappedColors = wrappedColors.ToList();
Items = items.ToList();
NestedModel = nestedModel;
Metadata = metadata;
CreatedAt = createdAt;
Duration = duration;
Data = data;
}

/// <summary> Initializes a new instance of <see cref="XmlAdvancedModel"/>. </summary>
/// <param name="name"> A simple string property. </param>
/// <param name="age"> An integer property. </param>
/// <param name="enabled"> A boolean property. </param>
/// <param name="score"> A float property. </param>
/// <param name="optionalString"> An optional string. </param>
/// <param name="optionalInt"> An optional integer. </param>
/// <param name="nullableString"> A nullable string. </param>
/// <param name="id"> A string as XML attribute. </param>
/// <param name="version"> An integer as XML attribute. </param>
/// <param name="isActive"> A boolean as XML attribute. </param>
/// <param name="originalName"> A property with a custom XML element name. </param>
/// <param name="xmlIdentifier"> An attribute with a custom XML name. </param>
/// <param name="content"> Text content in the element (unwrapped string). </param>
/// <param name="unwrappedStrings"> An unwrapped array of strings - items appear directly without wrapper. </param>
/// <param name="unwrappedCounts"> An unwrapped array of integers. </param>
/// <param name="unwrappedItems"> An unwrapped array of models. </param>
/// <param name="wrappedColors"> A wrapped array of strings (default). </param>
/// <param name="items"> A wrapped array with custom wrapper name. </param>
/// <param name="nestedModel"> A nested model property. </param>
/// <param name="optionalNestedModel"> An optional nested model. </param>
/// <param name="metadata"> A dictionary property. </param>
/// <param name="createdAt"> A date-time property. </param>
/// <param name="duration"> A duration property. </param>
/// <param name="data"> A bytes property. </param>
/// <param name="additionalBinaryDataProperties"> Keeps track of any properties unknown to the library. </param>
internal XmlAdvancedModel(string name, int age, bool enabled, float score, string optionalString, int? optionalInt, string nullableString, string id, int version, bool isActive, string originalName, string xmlIdentifier, string content, IList<string> unwrappedStrings, IList<int> unwrappedCounts, IList<XmlItem> unwrappedItems, IList<string> wrappedColors, IList<XmlItem> items, XmlNestedModel nestedModel, XmlNestedModel optionalNestedModel, IDictionary<string, string> metadata, DateTimeOffset createdAt, TimeSpan duration, BinaryData data, IDictionary<string, BinaryData> additionalBinaryDataProperties)
{
Name = name;
Age = age;
Enabled = enabled;
Score = score;
OptionalString = optionalString;
OptionalInt = optionalInt;
NullableString = nullableString;
Id = id;
Version = version;
IsActive = isActive;
OriginalName = originalName;
XmlIdentifier = xmlIdentifier;
Content = content;
UnwrappedStrings = unwrappedStrings;
UnwrappedCounts = unwrappedCounts;
UnwrappedItems = unwrappedItems;
WrappedColors = wrappedColors;
Items = items;
NestedModel = nestedModel;
OptionalNestedModel = optionalNestedModel;
Metadata = metadata;
CreatedAt = createdAt;
Duration = duration;
Data = data;
_additionalBinaryDataProperties = additionalBinaryDataProperties;
}

/// <summary> A simple string property. </summary>
public string Name { get; }

/// <summary> An integer property. </summary>
public int Age { get; }

/// <summary> A boolean property. </summary>
public bool Enabled { get; }

/// <summary> A float property. </summary>
public float Score { get; }

/// <summary> An optional string. </summary>
public string OptionalString { get; }

/// <summary> An optional integer. </summary>
public int? OptionalInt { get; }

/// <summary> A nullable string. </summary>
public string NullableString { get; }

/// <summary> A string as XML attribute. </summary>
public string Id { get; }

/// <summary> An integer as XML attribute. </summary>
public int Version { get; }

/// <summary> A boolean as XML attribute. </summary>
public bool IsActive { get; }

/// <summary> A property with a custom XML element name. </summary>
public string OriginalName { get; }

/// <summary> An attribute with a custom XML name. </summary>
public string XmlIdentifier { get; }

/// <summary> Text content in the element (unwrapped string). </summary>
public string Content { get; }

/// <summary> An unwrapped array of strings - items appear directly without wrapper. </summary>
public IList<string> UnwrappedStrings { get; }

/// <summary> An unwrapped array of integers. </summary>
public IList<int> UnwrappedCounts { get; }

/// <summary> An unwrapped array of models. </summary>
public IList<XmlItem> UnwrappedItems { get; }

/// <summary> A wrapped array of strings (default). </summary>
public IList<string> WrappedColors { get; }

/// <summary> A wrapped array with custom wrapper name. </summary>
public IList<XmlItem> Items { get; }

/// <summary> A nested model property. </summary>
public XmlNestedModel NestedModel { get; }

/// <summary> An optional nested model. </summary>
public XmlNestedModel OptionalNestedModel { get; }

/// <summary> A dictionary property. </summary>
public IDictionary<string, string> Metadata { get; }

/// <summary> A date-time property. </summary>
public DateTimeOffset CreatedAt { get; }

/// <summary> A duration property. </summary>
public TimeSpan Duration { get; }

/// <summary>
/// A bytes property
/// <para>
/// To assign a byte[] to this property use <see cref="BinaryData.FromBytes(byte[])"/>.
/// The byte[] will be serialized to a Base64 encoded string.
/// </para>
/// <para>
/// Examples:
/// <list type="bullet">
/// <item>
/// <term> BinaryData.FromBytes(new byte[] { 1, 2, 3 }). </term>
/// <description> Creates a payload of "AQID". </description>
/// </item>
/// </list>
/// </para>
/// </summary>
public BinaryData Data { get; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
// <auto-generated/>

#nullable disable

using System;
using System.ClientModel.Primitives;
using System.Collections.Generic;
using System.Text.Json;

namespace SampleTypeSpec
{
/// <summary> An item model for XML array testing. </summary>
public partial class XmlItem : IJsonModel<XmlItem>
{
/// <summary> Initializes a new instance of <see cref="XmlItem"/> for deserialization. </summary>
internal XmlItem()
{
}

/// <param name="writer"> The JSON writer. </param>
/// <param name="options"> The client options for reading and writing models. </param>
void IJsonModel<XmlItem>.Write(Utf8JsonWriter writer, ModelReaderWriterOptions options)
{
writer.WriteStartObject();
JsonModelWriteCore(writer, options);
writer.WriteEndObject();
}

/// <param name="writer"> The JSON writer. </param>
/// <param name="options"> The client options for reading and writing models. </param>
protected virtual void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options)
{
string format = options.Format == "W" ? ((IPersistableModel<XmlItem>)this).GetFormatFromOptions(options) : options.Format;
if (format != "J")
{
throw new FormatException($"The model {nameof(XmlItem)} does not support writing '{format}' format.");
}
writer.WritePropertyName("itemName"u8);
writer.WriteStringValue(ItemName);
writer.WritePropertyName("itemValue"u8);
writer.WriteNumberValue(ItemValue);
writer.WritePropertyName("itemId"u8);
writer.WriteStringValue(ItemId);
if (options.Format != "W" && _additionalBinaryDataProperties != null)
{
foreach (var item in _additionalBinaryDataProperties)
{
writer.WritePropertyName(item.Key);
#if NET6_0_OR_GREATER
writer.WriteRawValue(item.Value);
#else
using (JsonDocument document = JsonDocument.Parse(item.Value))
{
JsonSerializer.Serialize(writer, document.RootElement);
}
#endif
}
}
}

/// <param name="reader"> The JSON reader. </param>
/// <param name="options"> The client options for reading and writing models. </param>
XmlItem IJsonModel<XmlItem>.Create(ref Utf8JsonReader reader, ModelReaderWriterOptions options) => JsonModelCreateCore(ref reader, options);

/// <param name="reader"> The JSON reader. </param>
/// <param name="options"> The client options for reading and writing models. </param>
protected virtual XmlItem JsonModelCreateCore(ref Utf8JsonReader reader, ModelReaderWriterOptions options)
{
string format = options.Format == "W" ? ((IPersistableModel<XmlItem>)this).GetFormatFromOptions(options) : options.Format;
if (format != "J")
{
throw new FormatException($"The model {nameof(XmlItem)} does not support reading '{format}' format.");
}
using JsonDocument document = JsonDocument.ParseValue(ref reader);
return DeserializeXmlItem(document.RootElement, options);
}

/// <param name="element"> The JSON element to deserialize. </param>
/// <param name="options"> The client options for reading and writing models. </param>
internal static XmlItem DeserializeXmlItem(JsonElement element, ModelReaderWriterOptions options)
{
if (element.ValueKind == JsonValueKind.Null)
{
return null;
}
string itemName = default;
int itemValue = default;
string itemId = default;
IDictionary<string, BinaryData> additionalBinaryDataProperties = new ChangeTrackingDictionary<string, BinaryData>();
foreach (var prop in element.EnumerateObject())
{
if (prop.NameEquals("itemName"u8))
{
itemName = prop.Value.GetString();
continue;
}
if (prop.NameEquals("itemValue"u8))
{
itemValue = prop.Value.GetInt32();
continue;
}
if (prop.NameEquals("itemId"u8))
{
itemId = prop.Value.GetString();
continue;
}
if (options.Format != "W")
{
additionalBinaryDataProperties.Add(prop.Name, BinaryData.FromString(prop.Value.GetRawText()));
}
}
return new XmlItem(itemName, itemValue, itemId, additionalBinaryDataProperties);
}

/// <param name="options"> The client options for reading and writing models. </param>
BinaryData IPersistableModel<XmlItem>.Write(ModelReaderWriterOptions options) => PersistableModelWriteCore(options);

/// <param name="options"> The client options for reading and writing models. </param>
protected virtual BinaryData PersistableModelWriteCore(ModelReaderWriterOptions options)
{
string format = options.Format == "W" ? ((IPersistableModel<XmlItem>)this).GetFormatFromOptions(options) : options.Format;
switch (format)
{
case "J":
return ModelReaderWriter.Write(this, options, SampleTypeSpecContext.Default);
default:
throw new FormatException($"The model {nameof(XmlItem)} does not support writing '{options.Format}' format.");
}
}

/// <param name="data"> The data to parse. </param>
/// <param name="options"> The client options for reading and writing models. </param>
XmlItem IPersistableModel<XmlItem>.Create(BinaryData data, ModelReaderWriterOptions options) => PersistableModelCreateCore(data, options);

/// <param name="data"> The data to parse. </param>
/// <param name="options"> The client options for reading and writing models. </param>
protected virtual XmlItem PersistableModelCreateCore(BinaryData data, ModelReaderWriterOptions options)
{
string format = options.Format == "W" ? ((IPersistableModel<XmlItem>)this).GetFormatFromOptions(options) : options.Format;
switch (format)
{
case "J":
using (JsonDocument document = JsonDocument.Parse(data))
{
return DeserializeXmlItem(document.RootElement, options);
}
default:
throw new FormatException($"The model {nameof(XmlItem)} does not support reading '{options.Format}' format.");
}
}

/// <param name="options"> The client options for reading and writing models. </param>
string IPersistableModel<XmlItem>.GetFormatFromOptions(ModelReaderWriterOptions options) => "J";
}
}
Loading
Loading