From 937e7d71fa2b770c7227e3c80937306422ee709b Mon Sep 17 00:00:00 2001 From: Tom Deseyn Date: Wed, 20 Nov 2024 13:09:12 +0100 Subject: [PATCH] VariantValue: eliminate breaking changes for values that hold ObjectPath or arrays of ObjectPaths. --- src/Tmds.DBus.Protocol/VariantValue.cs | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/Tmds.DBus.Protocol/VariantValue.cs b/src/Tmds.DBus.Protocol/VariantValue.cs index d5c1ee0..39c3ed1 100644 --- a/src/Tmds.DBus.Protocol/VariantValue.cs +++ b/src/Tmds.DBus.Protocol/VariantValue.cs @@ -433,10 +433,11 @@ private string UnsafeGetString() private ObjectPath UnsafeGetObjectPath() => new ObjectPath(UnsafeGetString()); - public ObjectPath GetObjectPath() + [Obsolete($"Call {nameof(GetObjectPathAsString)} instead. {nameof(GetObjectPath)} will be changed to return a {nameof(ObjectPath)} in a future version, which will cause a breaking change.")] + public string GetObjectPath() { EnsureTypeIs(VariantValueType.ObjectPath); - return UnsafeGetObjectPath(); + return UnsafeGetString(); } public string GetObjectPathAsString() @@ -584,7 +585,8 @@ public Dictionary GetDictionary where T : notnull { EnsureTypeIs(VariantValueType.Array); - EnsureCanUnsafeGet(ItemType); + VariantValueType itemType = UnsafeDetermineInnerType(ArrayItemTypeShift); + EnsureCanUnsafeGet(itemType); if (UnsafeCount == 0) { @@ -602,11 +604,16 @@ public Dictionary GetDictionary || typeof(T) == typeof(uint) || typeof(T) == typeof(ulong) || typeof(T) == typeof(double) - || typeof(T) == typeof(string) + || (typeof(T) == typeof(string) && itemType != VariantValueType.ObjectPath) || typeof(T) == typeof(ObjectPath)) { return _o is T[] a ? a : (_o as List)!.ToArray(); } + else if (typeof(T) == typeof(string) && itemType == VariantValueType.ObjectPath) + { + IList paths = (_o as IList)!; + return (T[])(object)paths.Select(path => path.ToString()).ToArray(); + } else { var items = (_o as VariantValue[])!.AsSpan(); @@ -661,7 +668,7 @@ private static void EnsureCanUnsafeGet(VariantValueType type) } else if (typeof(T) == typeof(string)) { - EnsureTypeIs(type, VariantValueType.String); + EnsureTypeIs(type, [ VariantValueType.String, VariantValueType.ObjectPath ]); } else if (typeof(T) == typeof(ObjectPath)) {