// Copyright (c) Microsoft. All rights reserved. using System; using System.Collections.Generic; using System.Linq; using System.Text.Json; using Microsoft.Shared.Diagnostics; namespace Microsoft.Agents.ObjectModel; /// /// Extension methods for . /// public static class RecordDataValueExtensions { /// /// Retrieves a 'number' property from a /// /// Instance of /// Path of the property to retrieve public static decimal? GetNumber(this RecordDataValue recordData, string propertyPath) { Throw.IfNull(recordData); var numberValue = recordData.GetPropertyOrNull(InitializablePropertyPath.Create(propertyPath)); return numberValue?.Value; } /// /// Retrieves a nullable boolean value from the specified property path within the given record data. /// /// Instance of /// Path of the property to retrieve public static bool? GetBoolean(this RecordDataValue recordData, string propertyPath) { Throw.IfNull(recordData); var booleanValue = recordData.GetPropertyOrNull(InitializablePropertyPath.Create(propertyPath)); return booleanValue?.Value; } /// /// Converts a to a . /// /// Instance of public static IReadOnlyDictionary ToDictionary(this RecordDataValue recordData) { Throw.IfNull(recordData); return recordData.Properties.ToDictionary( kvp => kvp.Key, kvp => kvp.Value?.ToString() ?? string.Empty ); } /// /// Retrieves the 'schema' property from a . /// /// Instance of #pragma warning disable IL2026 // Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code #pragma warning disable IL3050 // Calling members annotated with 'RequiresDynamicCodeAttribute' may break functionality when AOT compiling. public static JsonElement? GetSchema(this RecordDataValue recordData) { Throw.IfNull(recordData); try { var schemaStr = recordData.GetPropertyOrNull(InitializablePropertyPath.Create("json_schema.schema")); if (schemaStr?.Value is not null) { return JsonSerializer.Deserialize(schemaStr.Value); } } catch (InvalidCastException) { // Ignore and try next } var responseFormRec = recordData.GetPropertyOrNull(InitializablePropertyPath.Create("json_schema.schema")); if (responseFormRec is not null) { var json = JsonSerializer.Serialize(responseFormRec, ElementSerializer.CreateOptions()); return JsonSerializer.Deserialize(json); } return null; } #pragma warning restore IL3050 // Calling members annotated with 'RequiresDynamicCodeAttribute' may break functionality when AOT compiling. #pragma warning restore IL2026 // Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code internal static object? ToObject(this DataValue? value) { if (value is null) { return null; } return value switch { StringDataValue s => s.Value, NumberDataValue n => n.Value, BooleanDataValue b => b.Value, TableDataValue t => t.Values.Select(v => v.ToObject()).ToList(), RecordDataValue r => r.Properties.ToDictionary(kvp => kvp.Key, kvp => kvp.Value?.ToObject()), _ => throw new NotSupportedException($"Unsupported DataValue type: {value.GetType().FullName}"), }; } }