Skip to content

Commit

Permalink
VariantValue: eliminate breaking changes for values that hold ObjectP…
Browse files Browse the repository at this point in the history
…ath or arrays of ObjectPaths. (#323)
  • Loading branch information
tmds authored Nov 20, 2024
1 parent 5e38a17 commit b3b8de2
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions src/Tmds.DBus.Protocol/VariantValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -584,7 +585,8 @@ public Dictionary<TKey, TValue> GetDictionary
where T : notnull
{
EnsureTypeIs(VariantValueType.Array);
EnsureCanUnsafeGet<T>(ItemType);
VariantValueType itemType = UnsafeDetermineInnerType(ArrayItemTypeShift);
EnsureCanUnsafeGet<T>(itemType);

if (UnsafeCount == 0)
{
Expand All @@ -602,11 +604,16 @@ public Dictionary<TKey, TValue> 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<T>)!.ToArray();
}
else if (typeof(T) == typeof(string) && itemType == VariantValueType.ObjectPath)
{
IList<ObjectPath> paths = (_o as IList<ObjectPath>)!;
return (T[])(object)paths.Select(path => path.ToString()).ToArray();
}
else
{
var items = (_o as VariantValue[])!.AsSpan();
Expand Down Expand Up @@ -661,7 +668,7 @@ private static void EnsureCanUnsafeGet<T>(VariantValueType type)
}
else if (typeof(T) == typeof(string))
{
EnsureTypeIs(type, VariantValueType.String);
EnsureTypeIs(type, [ VariantValueType.String, VariantValueType.ObjectPath ]);
}
else if (typeof(T) == typeof(ObjectPath))
{
Expand Down

0 comments on commit b3b8de2

Please sign in to comment.