Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/StreamJsonRpc.Analyzers/GeneratorModels/FullModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,9 @@ void WriteGroup(SourceWriter writer)
string visibility = iface.IsPublic ? "public" : "internal";
writer.WriteLine($$"""
/// <inheritdoc cref="global::StreamJsonRpc.IClientProxy.Is(global::System.Type)"/>
{{visibility}} static bool Is(this {{iface.FullName}} self, global::System.Type type) => self is global::StreamJsonRpc.IClientProxy proxy ? proxy.Is(type) : type.IsAssignableFrom(self.GetType());
{{visibility}} static bool Is(this {{iface.FullName}}? self, global::System.Type type) => self switch { null => false, global::StreamJsonRpc.IClientProxy proxy => proxy.Is(type), _ => type.IsAssignableFrom(self.GetType()) };
/// <inheritdoc cref="global::StreamJsonRpc.JsonRpcExtensions.As{T}(global::StreamJsonRpc.IClientProxy)"/>
{{visibility}} static T? As<T>(this {{iface.FullName}} self) where T : class => self is global::StreamJsonRpc.IClientProxy proxy ? proxy.As<T>() : self as T;
{{visibility}} static T? As<T>(this {{iface.FullName}}? self) where T : class => self is global::StreamJsonRpc.IClientProxy proxy ? proxy.As<T>() : self as T;
""");
if (first)
{
Expand Down
4 changes: 2 additions & 2 deletions src/StreamJsonRpc/JsonRpcExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ private interface IRpcEnumerable
/// In such cases, this method can be used to determine whether the proxy was intentionally created to implement the interface
/// or not, allowing feature testing to still happen since conditional casting might lead to false positives.
/// </remarks>
public static T? As<T>(this IClientProxy proxy)
where T : class => Requires.NotNull(proxy).Is(typeof(T)) ? (T)(object)proxy : null;
public static T? As<T>(this IClientProxy? proxy)
where T : class => proxy?.Is(typeof(T)) is true ? (T)(object)proxy : null;

#pragma warning disable VSTHRD200 // Use "Async" suffix in names of methods that return an awaitable type.
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
public static partial class StreamJsonRpcOptionalInterfaceAccessors
{
/// <inheritdoc cref="global::StreamJsonRpc.IClientProxy.Is(global::System.Type)"/>
public static bool Is(this SomeExperimentalInterface self, global::System.Type type) => self is global::StreamJsonRpc.IClientProxy proxy ? proxy.Is(type) : type.IsAssignableFrom(self.GetType());
public static bool Is(this SomeExperimentalInterface? self, global::System.Type type) => self switch { null => false, global::StreamJsonRpc.IClientProxy proxy => proxy.Is(type), _ => type.IsAssignableFrom(self.GetType()) };
/// <inheritdoc cref="global::StreamJsonRpc.JsonRpcExtensions.As{T}(global::StreamJsonRpc.IClientProxy)"/>
public static T? As<T>(this SomeExperimentalInterface self) where T : class => self is global::StreamJsonRpc.IClientProxy proxy ? proxy.As<T>() : self as T;
public static T? As<T>(this SomeExperimentalInterface? self) where T : class => self is global::StreamJsonRpc.IClientProxy proxy ? proxy.As<T>() : self as T;

/// <inheritdoc cref="global::StreamJsonRpc.IClientProxy.Is(global::System.Type)"/>
public static bool Is(this SomeExperimentalInterface2 self, global::System.Type type) => self is global::StreamJsonRpc.IClientProxy proxy ? proxy.Is(type) : type.IsAssignableFrom(self.GetType());
public static bool Is(this SomeExperimentalInterface2? self, global::System.Type type) => self switch { null => false, global::StreamJsonRpc.IClientProxy proxy => proxy.Is(type), _ => type.IsAssignableFrom(self.GetType()) };
/// <inheritdoc cref="global::StreamJsonRpc.JsonRpcExtensions.As{T}(global::StreamJsonRpc.IClientProxy)"/>
public static T? As<T>(this SomeExperimentalInterface2 self) where T : class => self is global::StreamJsonRpc.IClientProxy proxy ? proxy.As<T>() : self as T;
public static T? As<T>(this SomeExperimentalInterface2? self) where T : class => self is global::StreamJsonRpc.IClientProxy proxy ? proxy.As<T>() : self as T;
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
public static partial class StreamJsonRpcOptionalInterfaceAccessors
{
/// <inheritdoc cref="global::StreamJsonRpc.IClientProxy.Is(global::System.Type)"/>
public static bool Is(this IMarshalable self, global::System.Type type) => self is global::StreamJsonRpc.IClientProxy proxy ? proxy.Is(type) : type.IsAssignableFrom(self.GetType());
public static bool Is(this IMarshalable? self, global::System.Type type) => self switch { null => false, global::StreamJsonRpc.IClientProxy proxy => proxy.Is(type), _ => type.IsAssignableFrom(self.GetType()) };
/// <inheritdoc cref="global::StreamJsonRpc.JsonRpcExtensions.As{T}(global::StreamJsonRpc.IClientProxy)"/>
public static T? As<T>(this IMarshalable self) where T : class => self is global::StreamJsonRpc.IClientProxy proxy ? proxy.As<T>() : self as T;
public static T? As<T>(this IMarshalable? self) where T : class => self is global::StreamJsonRpc.IClientProxy proxy ? proxy.As<T>() : self as T;

/// <inheritdoc cref="global::StreamJsonRpc.IClientProxy.Is(global::System.Type)"/>
internal static bool Is(this IOptional self, global::System.Type type) => self is global::StreamJsonRpc.IClientProxy proxy ? proxy.Is(type) : type.IsAssignableFrom(self.GetType());
internal static bool Is(this IOptional? self, global::System.Type type) => self switch { null => false, global::StreamJsonRpc.IClientProxy proxy => proxy.Is(type), _ => type.IsAssignableFrom(self.GetType()) };
/// <inheritdoc cref="global::StreamJsonRpc.JsonRpcExtensions.As{T}(global::StreamJsonRpc.IClientProxy)"/>
internal static T? As<T>(this IOptional self) where T : class => self is global::StreamJsonRpc.IClientProxy proxy ? proxy.As<T>() : self as T;
internal static T? As<T>(this IOptional? self) where T : class => self is global::StreamJsonRpc.IClientProxy proxy ? proxy.As<T>() : self as T;
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ namespace NS
internal static partial class StreamJsonRpcOptionalInterfaceAccessors
{
/// <inheritdoc cref="global::StreamJsonRpc.IClientProxy.Is(global::System.Type)"/>
internal static bool Is(this NS.Wrapper.IMarshalable self, global::System.Type type) => self is global::StreamJsonRpc.IClientProxy proxy ? proxy.Is(type) : type.IsAssignableFrom(self.GetType());
internal static bool Is(this NS.Wrapper.IMarshalable? self, global::System.Type type) => self switch { null => false, global::StreamJsonRpc.IClientProxy proxy => proxy.Is(type), _ => type.IsAssignableFrom(self.GetType()) };
/// <inheritdoc cref="global::StreamJsonRpc.JsonRpcExtensions.As{T}(global::StreamJsonRpc.IClientProxy)"/>
internal static T? As<T>(this NS.Wrapper.IMarshalable self) where T : class => self is global::StreamJsonRpc.IClientProxy proxy ? proxy.As<T>() : self as T;
internal static T? As<T>(this NS.Wrapper.IMarshalable? self) where T : class => self is global::StreamJsonRpc.IClientProxy proxy ? proxy.As<T>() : self as T;

/// <inheritdoc cref="global::StreamJsonRpc.IClientProxy.Is(global::System.Type)"/>
internal static bool Is(this NS.Wrapper.IOptional self, global::System.Type type) => self is global::StreamJsonRpc.IClientProxy proxy ? proxy.Is(type) : type.IsAssignableFrom(self.GetType());
internal static bool Is(this NS.Wrapper.IOptional? self, global::System.Type type) => self switch { null => false, global::StreamJsonRpc.IClientProxy proxy => proxy.Is(type), _ => type.IsAssignableFrom(self.GetType()) };
/// <inheritdoc cref="global::StreamJsonRpc.JsonRpcExtensions.As{T}(global::StreamJsonRpc.IClientProxy)"/>
internal static T? As<T>(this NS.Wrapper.IOptional self) where T : class => self is global::StreamJsonRpc.IClientProxy proxy ? proxy.As<T>() : self as T;
internal static T? As<T>(this NS.Wrapper.IOptional? self) where T : class => self is global::StreamJsonRpc.IClientProxy proxy ? proxy.As<T>() : self as T;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
internal static partial class StreamJsonRpcOptionalInterfaceAccessors
{
/// <inheritdoc cref="global::StreamJsonRpc.IClientProxy.Is(global::System.Type)"/>
public static bool Is(this IMarshalable self, global::System.Type type) => self is global::StreamJsonRpc.IClientProxy proxy ? proxy.Is(type) : type.IsAssignableFrom(self.GetType());
public static bool Is(this IMarshalable? self, global::System.Type type) => self switch { null => false, global::StreamJsonRpc.IClientProxy proxy => proxy.Is(type), _ => type.IsAssignableFrom(self.GetType()) };
/// <inheritdoc cref="global::StreamJsonRpc.JsonRpcExtensions.As{T}(global::StreamJsonRpc.IClientProxy)"/>
public static T? As<T>(this IMarshalable self) where T : class => self is global::StreamJsonRpc.IClientProxy proxy ? proxy.As<T>() : self as T;
public static T? As<T>(this IMarshalable? self) where T : class => self is global::StreamJsonRpc.IClientProxy proxy ? proxy.As<T>() : self as T;

/// <inheritdoc cref="global::StreamJsonRpc.IClientProxy.Is(global::System.Type)"/>
internal static bool Is(this IOptional self, global::System.Type type) => self is global::StreamJsonRpc.IClientProxy proxy ? proxy.Is(type) : type.IsAssignableFrom(self.GetType());
internal static bool Is(this IOptional? self, global::System.Type type) => self switch { null => false, global::StreamJsonRpc.IClientProxy proxy => proxy.Is(type), _ => type.IsAssignableFrom(self.GetType()) };
/// <inheritdoc cref="global::StreamJsonRpc.JsonRpcExtensions.As{T}(global::StreamJsonRpc.IClientProxy)"/>
internal static T? As<T>(this IOptional self) where T : class => self is global::StreamJsonRpc.IClientProxy proxy ? proxy.As<T>() : self as T;
internal static T? As<T>(this IOptional? self) where T : class => self is global::StreamJsonRpc.IClientProxy proxy ? proxy.As<T>() : self as T;
}
7 changes: 7 additions & 0 deletions test/StreamJsonRpc.Tests/JsonRpcExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ public JsonRpcExtensionsTests(ITestOutputHelper logger)
{
}

[Fact]
public void As_NullArg()
{
// Non-null tests are exercised elsewhere.
Assert.Null(JsonRpcExtensions.As<IDisposable>(null));
}

[Fact]
public async Task AsAsyncEnumerable()
{
Expand Down
Loading