openai/openai-dotnet

Public

mirrored from https://github.com/openai/openai-dotnetAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
fe4d6cf1ccfea48fa5c2baf67103495ac9b459dd

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

OpenAI.Responses/src/Generated/Models/ApplyPatchCallItem.Serialization.cs

203lines · modecode

1// <auto-generated/>
2
3#nullable disable
4
5using System;
6using System.ClientModel.Primitives;
7using System.Text;
8using System.Text.Json;
9using OpenAI;
10
11namespace OpenAI.Responses
12{
13 public partial class ApplyPatchCallItem : ResponseItem, IJsonModel<ApplyPatchCallItem>
14 {
15 internal ApplyPatchCallItem() : this(ResponseItemKind.ApplyPatchCall, null, default, null, default, null, null)
16 {
17 }
18
19 protected override ResponseItem PersistableModelCreateCore(BinaryData data, ModelReaderWriterOptions options)
20 {
21 string format = options.Format == "W" ? ((IPersistableModel<ApplyPatchCallItem>)this).GetFormatFromOptions(options) : options.Format;
22 switch (format)
23 {
24 case "J":
25 using (JsonDocument document = JsonDocument.Parse(data, ModelSerializationExtensions.JsonDocumentOptions))
26 {
27 return DeserializeApplyPatchCallItem(document.RootElement, data, options);
28 }
29 default:
30 throw new FormatException($"The model {nameof(ApplyPatchCallItem)} does not support reading '{options.Format}' format.");
31 }
32 }
33
34 protected override BinaryData PersistableModelWriteCore(ModelReaderWriterOptions options)
35 {
36 string format = options.Format == "W" ? ((IPersistableModel<ApplyPatchCallItem>)this).GetFormatFromOptions(options) : options.Format;
37 switch (format)
38 {
39 case "J":
40 return ModelReaderWriter.Write(this, options, OpenAIContext.Default);
41 default:
42 throw new FormatException($"The model {nameof(ApplyPatchCallItem)} does not support writing '{options.Format}' format.");
43 }
44 }
45
46 BinaryData IPersistableModel<ApplyPatchCallItem>.Write(ModelReaderWriterOptions options) => PersistableModelWriteCore(options);
47
48 ApplyPatchCallItem IPersistableModel<ApplyPatchCallItem>.Create(BinaryData data, ModelReaderWriterOptions options) => (ApplyPatchCallItem)PersistableModelCreateCore(data, options);
49
50 string IPersistableModel<ApplyPatchCallItem>.GetFormatFromOptions(ModelReaderWriterOptions options) => "J";
51
52 void IJsonModel<ApplyPatchCallItem>.Write(Utf8JsonWriter writer, ModelReaderWriterOptions options)
53 {
54#pragma warning disable SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates.
55 if (Patch.Contains("$"u8))
56 {
57 writer.WriteRawValue(Patch.GetJson("$"u8));
58 return;
59 }
60#pragma warning restore SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates.
61
62 writer.WriteStartObject();
63 JsonModelWriteCore(writer, options);
64 writer.WriteEndObject();
65 }
66
67 protected override void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options)
68 {
69 string format = options.Format == "W" ? ((IPersistableModel<ApplyPatchCallItem>)this).GetFormatFromOptions(options) : options.Format;
70 if (format != "J")
71 {
72 throw new FormatException($"The model {nameof(ApplyPatchCallItem)} does not support writing '{format}' format.");
73 }
74 base.JsonModelWriteCore(writer, options);
75#pragma warning disable SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates.
76 if (!Patch.Contains("$.call_id"u8))
77 {
78 writer.WritePropertyName("call_id"u8);
79 writer.WriteStringValue(CallId);
80 }
81 // Plugin customization: remove options.Format != "W" check
82 // Plugin customization: apply Optional.Is*Defined() check based on type name dictionary lookup
83 if (Optional.IsDefined(Status) && !Patch.Contains("$.status"u8))
84 {
85 writer.WritePropertyName("status"u8);
86 writer.WriteStringValue(Status.Value.ToString());
87 }
88 if (!Patch.Contains("$.operation"u8))
89 {
90 writer.WritePropertyName("operation"u8);
91 writer.WriteObjectValue(Operation, options);
92 }
93 if (Optional.IsDefined(CreatedBy) && !Patch.Contains("$.created_by"u8))
94 {
95 writer.WritePropertyName("created_by"u8);
96 writer.WriteStringValue(CreatedBy);
97 }
98
99 Patch.WriteTo(writer);
100#pragma warning restore SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates.
101 }
102
103 ApplyPatchCallItem IJsonModel<ApplyPatchCallItem>.Create(ref Utf8JsonReader reader, ModelReaderWriterOptions options) => (ApplyPatchCallItem)JsonModelCreateCore(ref reader, options);
104
105 protected override ResponseItem JsonModelCreateCore(ref Utf8JsonReader reader, ModelReaderWriterOptions options)
106 {
107 string format = options.Format == "W" ? ((IPersistableModel<ApplyPatchCallItem>)this).GetFormatFromOptions(options) : options.Format;
108 if (format != "J")
109 {
110 throw new FormatException($"The model {nameof(ApplyPatchCallItem)} does not support reading '{format}' format.");
111 }
112 using JsonDocument document = JsonDocument.ParseValue(ref reader);
113 return DeserializeApplyPatchCallItem(document.RootElement, null, options);
114 }
115
116 internal static ApplyPatchCallItem DeserializeApplyPatchCallItem(JsonElement element, BinaryData data, ModelReaderWriterOptions options)
117 {
118 if (element.ValueKind == JsonValueKind.Null)
119 {
120 return null;
121 }
122 ResponseItemKind kind = default;
123 string id = default;
124#pragma warning disable SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates.
125 JsonPatch patch = new JsonPatch(data is null ? ReadOnlyMemory<byte>.Empty : data.ToMemory());
126#pragma warning restore SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates.
127 string callId = default;
128 ApplyPatchCallStatus? status = default;
129 ApplyPatchOperation operation = default;
130 string createdBy = default;
131 foreach (var prop in element.EnumerateObject())
132 {
133 if (prop.NameEquals("type"u8))
134 {
135 kind = new ResponseItemKind(prop.Value.GetString());
136 continue;
137 }
138 if (prop.NameEquals("id"u8))
139 {
140 id = prop.Value.GetString();
141 continue;
142 }
143 if (prop.NameEquals("call_id"u8))
144 {
145 callId = prop.Value.GetString();
146 continue;
147 }
148 if (prop.NameEquals("status"u8))
149 {
150 status = new ApplyPatchCallStatus(prop.Value.GetString());
151 continue;
152 }
153 if (prop.NameEquals("operation"u8))
154 {
155 operation = ApplyPatchOperation.DeserializeApplyPatchOperation(prop.Value, prop.Value.GetUtf8Bytes(), options);
156 continue;
157 }
158 if (prop.NameEquals("created_by"u8))
159 {
160 createdBy = prop.Value.GetString();
161 continue;
162 }
163 patch.Set([.. "$."u8, .. Encoding.UTF8.GetBytes(prop.Name)], prop.Value.GetUtf8Bytes());
164 }
165 return new ApplyPatchCallItem(
166 kind,
167 id,
168 patch,
169 callId,
170 status,
171 operation,
172 createdBy);
173 }
174
175#pragma warning disable SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates.
176 private bool PropagateGet(ReadOnlySpan<byte> jsonPath, out JsonPatch.EncodedValue value)
177 {
178 ReadOnlySpan<byte> local = jsonPath.SliceToStartOfPropertyName();
179 value = default;
180
181 if (local.StartsWith("operation"u8))
182 {
183 return Operation.Patch.TryGetEncodedValue([.. "$"u8, .. local.Slice("operation"u8.Length)], out value);
184 }
185 return false;
186 }
187#pragma warning restore SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates.
188
189#pragma warning disable SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates.
190 private bool PropagateSet(ReadOnlySpan<byte> jsonPath, JsonPatch.EncodedValue value)
191 {
192 ReadOnlySpan<byte> local = jsonPath.SliceToStartOfPropertyName();
193
194 if (local.StartsWith("operation"u8))
195 {
196 Operation.Patch.Set([.. "$"u8, .. local.Slice("operation"u8.Length)], value);
197 return true;
198 }
199 return false;
200 }
201#pragma warning restore SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates.
202 }
203}
204