//
#nullable disable
using System;
using System.ClientModel.Primitives;
using System.Text;
using System.Text.Json;
using OpenAI;
namespace OpenAI.Responses
{
public partial class ApplyPatchDeleteFileOperation : ApplyPatchOperation, IJsonModel
{
internal ApplyPatchDeleteFileOperation() : this(InternalApplyPatchOperationType.DeleteFile, default, null)
{
}
protected override ApplyPatchOperation PersistableModelCreateCore(BinaryData data, ModelReaderWriterOptions options)
{
string format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format;
switch (format)
{
case "J":
using (JsonDocument document = JsonDocument.Parse(data, ModelSerializationExtensions.JsonDocumentOptions))
{
return DeserializeApplyPatchDeleteFileOperation(document.RootElement, data, options);
}
default:
throw new FormatException($"The model {nameof(ApplyPatchDeleteFileOperation)} does not support reading '{options.Format}' format.");
}
}
protected override BinaryData PersistableModelWriteCore(ModelReaderWriterOptions options)
{
string format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format;
switch (format)
{
case "J":
return ModelReaderWriter.Write(this, options, OpenAIContext.Default);
default:
throw new FormatException($"The model {nameof(ApplyPatchDeleteFileOperation)} does not support writing '{options.Format}' format.");
}
}
BinaryData IPersistableModel.Write(ModelReaderWriterOptions options) => PersistableModelWriteCore(options);
ApplyPatchDeleteFileOperation IPersistableModel.Create(BinaryData data, ModelReaderWriterOptions options) => (ApplyPatchDeleteFileOperation)PersistableModelCreateCore(data, options);
string IPersistableModel.GetFormatFromOptions(ModelReaderWriterOptions options) => "J";
void IJsonModel.Write(Utf8JsonWriter writer, ModelReaderWriterOptions options)
{
#pragma warning disable SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates.
if (Patch.Contains("$"u8))
{
writer.WriteRawValue(Patch.GetJson("$"u8));
return;
}
#pragma warning restore SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates.
writer.WriteStartObject();
JsonModelWriteCore(writer, options);
writer.WriteEndObject();
}
protected override void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options)
{
string format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format;
if (format != "J")
{
throw new FormatException($"The model {nameof(ApplyPatchDeleteFileOperation)} does not support writing '{format}' format.");
}
base.JsonModelWriteCore(writer, options);
#pragma warning disable SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates.
if (!Patch.Contains("$.path"u8))
{
writer.WritePropertyName("path"u8);
writer.WriteStringValue(FilePath);
}
Patch.WriteTo(writer);
#pragma warning restore SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates.
}
ApplyPatchDeleteFileOperation IJsonModel.Create(ref Utf8JsonReader reader, ModelReaderWriterOptions options) => (ApplyPatchDeleteFileOperation)JsonModelCreateCore(ref reader, options);
protected override ApplyPatchOperation JsonModelCreateCore(ref Utf8JsonReader reader, ModelReaderWriterOptions options)
{
string format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format;
if (format != "J")
{
throw new FormatException($"The model {nameof(ApplyPatchDeleteFileOperation)} does not support reading '{format}' format.");
}
using JsonDocument document = JsonDocument.ParseValue(ref reader);
return DeserializeApplyPatchDeleteFileOperation(document.RootElement, null, options);
}
internal static ApplyPatchDeleteFileOperation DeserializeApplyPatchDeleteFileOperation(JsonElement element, BinaryData data, ModelReaderWriterOptions options)
{
if (element.ValueKind == JsonValueKind.Null)
{
return null;
}
InternalApplyPatchOperationType kind = default;
#pragma warning disable SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates.
JsonPatch patch = new JsonPatch(data is null ? ReadOnlyMemory.Empty : data.ToMemory());
#pragma warning restore SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates.
string filePath = default;
foreach (var prop in element.EnumerateObject())
{
if (prop.NameEquals("type"u8))
{
kind = new InternalApplyPatchOperationType(prop.Value.GetString());
continue;
}
if (prop.NameEquals("path"u8))
{
filePath = prop.Value.GetString();
continue;
}
patch.Set([.. "$."u8, .. Encoding.UTF8.GetBytes(prop.Name)], prop.Value.GetUtf8Bytes());
}
return new ApplyPatchDeleteFileOperation(kind, patch, filePath);
}
}
}