mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
First round of cleanup of runtime abstractions (#156)
This commit is contained in:
+13
-34
@@ -1,52 +1,31 @@
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
using System;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
|
||||
namespace Microsoft.Extensions.AI.Agents.Runtime;
|
||||
|
||||
/// <summary>
|
||||
/// Provides helper methods for parsing key-value string representations.
|
||||
/// </summary>
|
||||
internal static class KeyValueParserExtensions
|
||||
internal static class KeyValueParser
|
||||
{
|
||||
/// <summary>
|
||||
/// The regular expression pattern used to match key-value pairs in the format "key/value".
|
||||
/// </summary>
|
||||
private const string KVPairPattern = @"^(?<key>\w+)/(?<value>\w+)$";
|
||||
|
||||
/// <summary>
|
||||
/// The compiled regex used for extracting key-value pairs from a string.
|
||||
/// </summary>
|
||||
private static readonly Regex KVPairRegex = new(KVPairPattern, RegexOptions.Compiled);
|
||||
|
||||
/// <summary>
|
||||
/// Parses a string in the format "key/value" into a tuple containing the key and value.
|
||||
/// </summary>
|
||||
/// <param name="inputPair">The input string containing a key-value pair.</param>
|
||||
/// <param name="keyName">The expected name of the key component.</param>
|
||||
/// <param name="valueName">The expected name of the value component.</param>
|
||||
/// <returns>A tuple containing the extracted key and value.</returns>
|
||||
/// <exception cref="FormatException">
|
||||
/// Thrown if the input string does not match the expected "key/value" format.
|
||||
/// </exception>
|
||||
/// <example>
|
||||
/// Example usage:
|
||||
/// <code>
|
||||
/// string input = "agent1/12345";
|
||||
/// var result = input.ToKVPair("Type", "Key");
|
||||
/// Console.WriteLine(result.Item1); // Outputs: agent1
|
||||
/// Console.WriteLine(result.Item2); // Outputs: 12345
|
||||
/// </code>
|
||||
/// </example>
|
||||
public static (string, string) ToKeyValuePair(this string inputPair, string keyName, string valueName)
|
||||
public static bool TryParse(string input, [NotNullWhen(true)] out string? key, [NotNullWhen(true)] out string? value)
|
||||
{
|
||||
Match match = KVPairRegex.Match(inputPair);
|
||||
if (match.Success)
|
||||
if (!string.IsNullOrEmpty(input))
|
||||
{
|
||||
return (match.Groups["key"].Value, match.Groups["value"].Value);
|
||||
int separatorIndex = input.IndexOf('/');
|
||||
if (separatorIndex >= 0)
|
||||
{
|
||||
key = input.Substring(0, separatorIndex);
|
||||
value = input.Substring(separatorIndex + 1);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
throw new FormatException($"Invalid key-value pair format: {inputPair}; expecting \"{{{keyName}}}/{{{valueName}}}\"");
|
||||
key = value = null;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user