|
| 1 | +using System; |
| 2 | +using System.ClientModel; |
| 3 | +using System.ClientModel.Primitives; |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.Text; |
| 6 | +using System.Text.Json; |
| 7 | + |
| 8 | +namespace OpenAI.Responses |
| 9 | +{ |
| 10 | + public partial class ResponseItemList : IJsonModel<ResponseItemList> |
| 11 | + { |
| 12 | + internal ResponseItemList() : this(null, null, default, null, null, default) |
| 13 | + { |
| 14 | + } |
| 15 | + |
| 16 | + void IJsonModel<ResponseItemList>.Write(Utf8JsonWriter writer, ModelReaderWriterOptions options) |
| 17 | + { |
| 18 | +#pragma warning disable SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. |
| 19 | + if (Patch.Contains("$"u8)) |
| 20 | + { |
| 21 | + writer.WriteRawValue(Patch.GetJson("$"u8)); |
| 22 | + return; |
| 23 | + } |
| 24 | +#pragma warning restore SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. |
| 25 | + |
| 26 | + writer.WriteStartObject(); |
| 27 | + JsonModelWriteCore(writer, options); |
| 28 | + writer.WriteEndObject(); |
| 29 | + } |
| 30 | + |
| 31 | + protected virtual void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options) |
| 32 | + { |
| 33 | + string format = options.Format == "W" ? ((IPersistableModel<ResponseItemList>)this).GetFormatFromOptions(options) : options.Format; |
| 34 | + if (format != "J") |
| 35 | + { |
| 36 | + throw new FormatException($"The model {nameof(ResponseItemList)} does not support writing '{format}' format."); |
| 37 | + } |
| 38 | +#pragma warning disable SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. |
| 39 | + if (!Patch.Contains("$.object"u8)) |
| 40 | + { |
| 41 | + writer.WritePropertyName("object"u8); |
| 42 | + writer.WriteStringValue(Object); |
| 43 | + } |
| 44 | + if (Patch.Contains("$.data"u8)) |
| 45 | + { |
| 46 | + if (!Patch.IsRemoved("$.data"u8)) |
| 47 | + { |
| 48 | + writer.WritePropertyName("data"u8); |
| 49 | + writer.WriteRawValue(Patch.GetJson("$.data"u8)); |
| 50 | + } |
| 51 | + } |
| 52 | + else |
| 53 | + { |
| 54 | + writer.WritePropertyName("data"u8); |
| 55 | + writer.WriteStartArray(); |
| 56 | + for (int i = 0; i < Data.Count; i++) |
| 57 | + { |
| 58 | + if (Data[i].Patch.IsRemoved("$"u8)) |
| 59 | + { |
| 60 | + continue; |
| 61 | + } |
| 62 | + writer.WriteObjectValue(Data[i], options); |
| 63 | + } |
| 64 | + Patch.WriteTo(writer, "$.data"u8); |
| 65 | + writer.WriteEndArray(); |
| 66 | + } |
| 67 | + if (!Patch.Contains("$.has_more"u8)) |
| 68 | + { |
| 69 | + writer.WritePropertyName("has_more"u8); |
| 70 | + writer.WriteBooleanValue(HasMore); |
| 71 | + } |
| 72 | + if (!Patch.Contains("$.first_id"u8)) |
| 73 | + { |
| 74 | + writer.WritePropertyName("first_id"u8); |
| 75 | + writer.WriteStringValue(FirstId); |
| 76 | + } |
| 77 | + if (!Patch.Contains("$.last_id"u8)) |
| 78 | + { |
| 79 | + writer.WritePropertyName("last_id"u8); |
| 80 | + writer.WriteStringValue(LastId); |
| 81 | + } |
| 82 | + |
| 83 | + Patch.WriteTo(writer); |
| 84 | +#pragma warning restore SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. |
| 85 | + } |
| 86 | + |
| 87 | + ResponseItemList IJsonModel<ResponseItemList>.Create(ref Utf8JsonReader reader, ModelReaderWriterOptions options) => JsonModelCreateCore(ref reader, options); |
| 88 | + |
| 89 | + protected virtual ResponseItemList JsonModelCreateCore(ref Utf8JsonReader reader, ModelReaderWriterOptions options) |
| 90 | + { |
| 91 | + string format = options.Format == "W" ? ((IPersistableModel<ResponseItemList>)this).GetFormatFromOptions(options) : options.Format; |
| 92 | + if (format != "J") |
| 93 | + { |
| 94 | + throw new FormatException($"The model {nameof(ResponseItemList)} does not support reading '{format}' format."); |
| 95 | + } |
| 96 | + using JsonDocument document = JsonDocument.ParseValue(ref reader); |
| 97 | + return DeserializeResponseItemList(document.RootElement, null, options); |
| 98 | + } |
| 99 | + |
| 100 | + internal static ResponseItemList DeserializeResponseItemList(JsonElement element, BinaryData data, ModelReaderWriterOptions options) |
| 101 | + { |
| 102 | + if (element.ValueKind == JsonValueKind.Null) |
| 103 | + { |
| 104 | + return null; |
| 105 | + } |
| 106 | + string @object = default; |
| 107 | + IList<ResponseItem> data0 = default; |
| 108 | + bool hasMore = default; |
| 109 | + string firstId = default; |
| 110 | + string lastId = default; |
| 111 | +#pragma warning disable SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. |
| 112 | + JsonPatch patch = new JsonPatch(data is null ? ReadOnlyMemory<byte>.Empty : data.ToMemory()); |
| 113 | +#pragma warning restore SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. |
| 114 | + foreach (var prop in element.EnumerateObject()) |
| 115 | + { |
| 116 | + if (prop.NameEquals("object"u8)) |
| 117 | + { |
| 118 | + @object = prop.Value.GetString(); |
| 119 | + continue; |
| 120 | + } |
| 121 | + if (prop.NameEquals("data"u8)) |
| 122 | + { |
| 123 | + List<ResponseItem> array = new List<ResponseItem>(); |
| 124 | + foreach (var item in prop.Value.EnumerateArray()) |
| 125 | + { |
| 126 | + array.Add(ResponseItem.DeserializeResponseItem(item, item.GetUtf8Bytes(), options)); |
| 127 | + } |
| 128 | + data0 = array; |
| 129 | + continue; |
| 130 | + } |
| 131 | + if (prop.NameEquals("has_more"u8)) |
| 132 | + { |
| 133 | + hasMore = prop.Value.GetBoolean(); |
| 134 | + continue; |
| 135 | + } |
| 136 | + if (prop.NameEquals("first_id"u8)) |
| 137 | + { |
| 138 | + firstId = prop.Value.GetString(); |
| 139 | + continue; |
| 140 | + } |
| 141 | + if (prop.NameEquals("last_id"u8)) |
| 142 | + { |
| 143 | + lastId = prop.Value.GetString(); |
| 144 | + continue; |
| 145 | + } |
| 146 | + patch.Set([.. "$."u8, .. Encoding.UTF8.GetBytes(prop.Name)], prop.Value.GetUtf8Bytes()); |
| 147 | + } |
| 148 | + return new ResponseItemList( |
| 149 | + @object, |
| 150 | + data0, |
| 151 | + hasMore, |
| 152 | + firstId, |
| 153 | + lastId, |
| 154 | + patch); |
| 155 | + } |
| 156 | + |
| 157 | + BinaryData IPersistableModel<ResponseItemList>.Write(ModelReaderWriterOptions options) => PersistableModelWriteCore(options); |
| 158 | + |
| 159 | + protected virtual BinaryData PersistableModelWriteCore(ModelReaderWriterOptions options) |
| 160 | + { |
| 161 | + string format = options.Format == "W" ? ((IPersistableModel<ResponseItemList>)this).GetFormatFromOptions(options) : options.Format; |
| 162 | + switch (format) |
| 163 | + { |
| 164 | + case "J": |
| 165 | + return ModelReaderWriter.Write(this, options, OpenAIContext.Default); |
| 166 | + default: |
| 167 | + throw new FormatException($"The model {nameof(ResponseItemList)} does not support writing '{options.Format}' format."); |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + ResponseItemList IPersistableModel<ResponseItemList>.Create(BinaryData data, ModelReaderWriterOptions options) => PersistableModelCreateCore(data, options); |
| 172 | + |
| 173 | + protected virtual ResponseItemList PersistableModelCreateCore(BinaryData data, ModelReaderWriterOptions options) |
| 174 | + { |
| 175 | + string format = options.Format == "W" ? ((IPersistableModel<ResponseItemList>)this).GetFormatFromOptions(options) : options.Format; |
| 176 | + switch (format) |
| 177 | + { |
| 178 | + case "J": |
| 179 | + using (JsonDocument document = JsonDocument.Parse(data)) |
| 180 | + { |
| 181 | + return DeserializeResponseItemList(document.RootElement, data, options); |
| 182 | + } |
| 183 | + default: |
| 184 | + throw new FormatException($"The model {nameof(ResponseItemList)} does not support reading '{options.Format}' format."); |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | + string IPersistableModel<ResponseItemList>.GetFormatFromOptions(ModelReaderWriterOptions options) => "J"; |
| 189 | + |
| 190 | + public static explicit operator ResponseItemList(ClientResult result) |
| 191 | + { |
| 192 | + using PipelineResponse response = result.GetRawResponse(); |
| 193 | + BinaryData data = response.Content; |
| 194 | + using JsonDocument document = JsonDocument.Parse(data); |
| 195 | + return DeserializeResponseItemList(document.RootElement, data, ModelSerializationExtensions.WireOptions); |
| 196 | + } |
| 197 | + |
| 198 | +#pragma warning disable SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. |
| 199 | + private bool PropagateGet(ReadOnlySpan<byte> jsonPath, out JsonPatch.EncodedValue value) |
| 200 | + { |
| 201 | + ReadOnlySpan<byte> local = jsonPath.SliceToStartOfPropertyName(); |
| 202 | + value = default; |
| 203 | + |
| 204 | + if (local.StartsWith("data"u8)) |
| 205 | + { |
| 206 | + int propertyLength = "data"u8.Length; |
| 207 | + ReadOnlySpan<byte> currentSlice = local.Slice(propertyLength); |
| 208 | + if (!currentSlice.TryGetIndex(out int index, out int bytesConsumed)) |
| 209 | + { |
| 210 | + return false; |
| 211 | + } |
| 212 | + return Data[index].Patch.TryGetEncodedValue([.. "$"u8, .. currentSlice.Slice(bytesConsumed)], out value); |
| 213 | + } |
| 214 | + return false; |
| 215 | + } |
| 216 | +#pragma warning restore SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. |
| 217 | + |
| 218 | +#pragma warning disable SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. |
| 219 | + private bool PropagateSet(ReadOnlySpan<byte> jsonPath, JsonPatch.EncodedValue value) |
| 220 | + { |
| 221 | + ReadOnlySpan<byte> local = jsonPath.SliceToStartOfPropertyName(); |
| 222 | + |
| 223 | + if (local.StartsWith("data"u8)) |
| 224 | + { |
| 225 | + int propertyLength = "data"u8.Length; |
| 226 | + ReadOnlySpan<byte> currentSlice = local.Slice(propertyLength); |
| 227 | + if (!currentSlice.TryGetIndex(out int index, out int bytesConsumed)) |
| 228 | + { |
| 229 | + return false; |
| 230 | + } |
| 231 | + Data[index].Patch.Set([.. "$"u8, .. currentSlice.Slice(bytesConsumed)], value); |
| 232 | + return true; |
| 233 | + } |
| 234 | + return false; |
| 235 | + } |
| 236 | +#pragma warning restore SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. |
| 237 | + } |
| 238 | +} |
0 commit comments