//
#nullable disable
using System;
using System.ClientModel.Primitives;
using System.Text;
using System.Text.Json;
using OpenAI;
namespace OpenAI.Responses
{
public partial class ApplyPatchCallItem : ResponseItem, IJsonModel
{
internal ApplyPatchCallItem() : this(ResponseItemKind.ApplyPatchCall, null, default, null, default, null, null)
{
}
protected override ResponseItem 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 DeserializeApplyPatchCallItem(document.RootElement, data, options);
}
default:
throw new FormatException($"The model {nameof(ApplyPatchCallItem)} 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(ApplyPatchCallItem)} does not support writing '{options.Format}' format.");
}
}
BinaryData IPersistableModel.Write(ModelReaderWriterOptions options) => PersistableModelWriteCore(options);
ApplyPatchCallItem IPersistableModel.Create(BinaryData data, ModelReaderWriterOptions options) => (ApplyPatchCallItem)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(ApplyPatchCallItem)} 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("$.call_id"u8))
{
writer.WritePropertyName("call_id"u8);
writer.WriteStringValue(CallId);
}
// Plugin customization: remove options.Format != "W" check
// Plugin customization: apply Optional.Is*Defined() check based on type name dictionary lookup
if (Optional.IsDefined(Status) && !Patch.Contains("$.status"u8))
{
writer.WritePropertyName("status"u8);
writer.WriteStringValue(Status.Value.ToString());
}
if (!Patch.Contains("$.operation"u8))
{
writer.WritePropertyName("operation"u8);
writer.WriteObjectValue(Operation, options);
}
if (Optional.IsDefined(CreatedBy) && !Patch.Contains("$.created_by"u8))
{
writer.WritePropertyName("created_by"u8);
writer.WriteStringValue(CreatedBy);
}
Patch.WriteTo(writer);
#pragma warning restore SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates.
}
ApplyPatchCallItem IJsonModel.Create(ref Utf8JsonReader reader, ModelReaderWriterOptions options) => (ApplyPatchCallItem)JsonModelCreateCore(ref reader, options);
protected override ResponseItem 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(ApplyPatchCallItem)} does not support reading '{format}' format.");
}
using JsonDocument document = JsonDocument.ParseValue(ref reader);
return DeserializeApplyPatchCallItem(document.RootElement, null, options);
}
internal static ApplyPatchCallItem DeserializeApplyPatchCallItem(JsonElement element, BinaryData data, ModelReaderWriterOptions options)
{
if (element.ValueKind == JsonValueKind.Null)
{
return null;
}
ResponseItemKind kind = default;
string id = 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 callId = default;
ApplyPatchCallStatus? status = default;
ApplyPatchOperation operation = default;
string createdBy = default;
foreach (var prop in element.EnumerateObject())
{
if (prop.NameEquals("type"u8))
{
kind = new ResponseItemKind(prop.Value.GetString());
continue;
}
if (prop.NameEquals("id"u8))
{
id = prop.Value.GetString();
continue;
}
if (prop.NameEquals("call_id"u8))
{
callId = prop.Value.GetString();
continue;
}
if (prop.NameEquals("status"u8))
{
status = new ApplyPatchCallStatus(prop.Value.GetString());
continue;
}
if (prop.NameEquals("operation"u8))
{
operation = ApplyPatchOperation.DeserializeApplyPatchOperation(prop.Value, prop.Value.GetUtf8Bytes(), options);
continue;
}
if (prop.NameEquals("created_by"u8))
{
createdBy = prop.Value.GetString();
continue;
}
patch.Set([.. "$."u8, .. Encoding.UTF8.GetBytes(prop.Name)], prop.Value.GetUtf8Bytes());
}
return new ApplyPatchCallItem(
kind,
id,
patch,
callId,
status,
operation,
createdBy);
}
#pragma warning disable SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates.
private bool PropagateGet(ReadOnlySpan jsonPath, out JsonPatch.EncodedValue value)
{
ReadOnlySpan local = jsonPath.SliceToStartOfPropertyName();
value = default;
if (local.StartsWith("operation"u8))
{
return Operation.Patch.TryGetEncodedValue([.. "$"u8, .. local.Slice("operation"u8.Length)], out value);
}
return false;
}
#pragma warning restore SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates.
#pragma warning disable SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates.
private bool PropagateSet(ReadOnlySpan jsonPath, JsonPatch.EncodedValue value)
{
ReadOnlySpan local = jsonPath.SliceToStartOfPropertyName();
if (local.StartsWith("operation"u8))
{
Operation.Patch.Set([.. "$"u8, .. local.Slice("operation"u8.Length)], value);
return true;
}
return false;
}
#pragma warning restore SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates.
}
}