From dda2cdf2b6adc546fe973f3a64bb3b53e2046343 Mon Sep 17 00:00:00 2001 From: neon-sunset Date: Tue, 15 Aug 2023 21:02:10 +0300 Subject: [PATCH] feat: net6.0 tfm support --- src/ITransport.cs | 31 ++++++++++++++++++++++++++++++- src/Index.cs | 8 ++++++-- src/Pinecone.csproj | 12 ++++++++---- src/PineconeClient.cs | 10 ++++++++++ 4 files changed, 54 insertions(+), 7 deletions(-) diff --git a/src/ITransport.cs b/src/ITransport.cs index 9a9b008..d264cd5 100644 --- a/src/ITransport.cs +++ b/src/ITransport.cs @@ -1,8 +1,37 @@ +using System.Diagnostics.CodeAnalysis; +using Pinecone.Grpc; +using Pinecone.Rest; + namespace Pinecone; -public interface ITransport : IDisposable +public interface ITransport< +#if NET7_0_OR_GREATER + T> : IDisposable +#else + [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] T> : IDisposable +#endif { +#if NET7_0_OR_GREATER static abstract T Create(string host, string apiKey); +#else + static T Create(string host, string apiKey) + { + if (typeof(T) == typeof(GrpcTransport)) + { + return (T)(object)new GrpcTransport(host, apiKey); + } + else if (typeof(T) == typeof(RestTransport)) + { + return (T)(object)new RestTransport(host, apiKey); + } + else + { + var instance = (T?)Activator.CreateInstance(typeof(T), host, apiKey); + + return instance ?? throw new InvalidOperationException($"Unable to create instance of {typeof(T)}"); + } + } +#endif Task DescribeStats(MetadataMap? filter = null); Task Query( diff --git a/src/Index.cs b/src/Index.cs index f588282..169d1be 100644 --- a/src/Index.cs +++ b/src/Index.cs @@ -1,10 +1,14 @@ +using System.Diagnostics.CodeAnalysis; using System.Text.Json.Serialization; namespace Pinecone; // Contract -public sealed partial record Index - where TTransport : ITransport +public sealed partial record Index< +#if NET6_0 + [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] +#endif + TTransport> where TTransport : ITransport { [JsonPropertyName("database")] public required IndexDetails Details { get; init; } diff --git a/src/Pinecone.csproj b/src/Pinecone.csproj index 317cc13..fce7e9b 100644 --- a/src/Pinecone.csproj +++ b/src/Pinecone.csproj @@ -13,16 +13,20 @@ - net7.0;net8.0 + net6.0;net7.0;net8.0 nullable true enable - preview - strict + 11 enable - + + true + + + + runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/src/PineconeClient.cs b/src/PineconeClient.cs index 581bcfa..d0b6371 100644 --- a/src/PineconeClient.cs +++ b/src/PineconeClient.cs @@ -1,3 +1,4 @@ +using System.Diagnostics.CodeAnalysis; using System.Net.Http.Json; using CommunityToolkit.Diagnostics; using Pinecone.Grpc; @@ -61,7 +62,12 @@ public async Task CreateIndex( public Task> GetIndex(IndexName name) => GetIndex(name); +#if NET7_0_OR_GREATER public async Task> GetIndex(IndexName name) +#else + public async Task> GetIndex< + [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] TTransport>(IndexName name) +#endif where TTransport : ITransport { var response = await Http.GetFromJsonAsync( @@ -73,7 +79,11 @@ public async Task> GetIndex(IndexName name) var host = index.Status.Host; var apiKey = Http.DefaultRequestHeaders.GetValues(Constants.RestApiKey).First(); +#if NET7_0_OR_GREATER index.Transport = TTransport.Create(host, apiKey); +#else + index.Transport = ITransport.Create(host, apiKey); +#endif return index; }