Skip to content

Commit

Permalink
feat: net6.0 tfm support
Browse files Browse the repository at this point in the history
  • Loading branch information
neon-sunset committed Aug 15, 2023
1 parent 1ff5e7f commit dda2cdf
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 7 deletions.
31 changes: 30 additions & 1 deletion src/ITransport.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,37 @@
using System.Diagnostics.CodeAnalysis;
using Pinecone.Grpc;
using Pinecone.Rest;

namespace Pinecone;

public interface ITransport<T> : 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<IndexStats> DescribeStats(MetadataMap? filter = null);
Task<ScoredVector[]> Query(
Expand Down
8 changes: 6 additions & 2 deletions src/Index.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
using System.Diagnostics.CodeAnalysis;
using System.Text.Json.Serialization;

namespace Pinecone;

// Contract
public sealed partial record Index<TTransport>
where TTransport : ITransport<TTransport>
public sealed partial record Index<
#if NET6_0
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]
#endif
TTransport> where TTransport : ITransport<TTransport>
{
[JsonPropertyName("database")]
public required IndexDetails Details { get; init; }
Expand Down
12 changes: 8 additions & 4 deletions src/Pinecone.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,20 @@
</PropertyGroup>

<PropertyGroup>
<TargetFrameworks>net7.0;net8.0</TargetFrameworks>
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<WarningsAsErrors>nullable</WarningsAsErrors>
<IsAotCompatible>true</IsAotCompatible>
<ImplicitUsings>enable</ImplicitUsings>
<LangVersion>preview</LangVersion>
<Features>strict</Features>
<LangVersion>11</LangVersion>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net7.0'">
<PropertyGroup Condition="'$(TargetFramework)' == 'net6.0'">
<PolySharpIncludeRuntimeSupportedAttributes>true</PolySharpIncludeRuntimeSupportedAttributes>
</PropertyGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net7.0' Or '$(TargetFramework)' == 'net6.0'">
<PackageReference Include="System.Net.Http.Json" Version="8.0.0-preview.7.23375.6" />
<PackageReference Include="System.Text.Json" Version="8.0.0-preview.7.23375.6" />
<PackageReference Include="PolySharp" Version="1.13.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
10 changes: 10 additions & 0 deletions src/PineconeClient.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Diagnostics.CodeAnalysis;
using System.Net.Http.Json;
using CommunityToolkit.Diagnostics;
using Pinecone.Grpc;
Expand Down Expand Up @@ -61,7 +62,12 @@ public async Task CreateIndex(

public Task<Index<GrpcTransport>> GetIndex(IndexName name) => GetIndex<GrpcTransport>(name);

#if NET7_0_OR_GREATER
public async Task<Index<TTransport>> GetIndex<TTransport>(IndexName name)
#else
public async Task<Index<TTransport>> GetIndex<
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] TTransport>(IndexName name)
#endif
where TTransport : ITransport<TTransport>
{
var response = await Http.GetFromJsonAsync(
Expand All @@ -73,7 +79,11 @@ public async Task<Index<TTransport>> GetIndex<TTransport>(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<TTransport>.Create(host, apiKey);
#endif
return index;
}

Expand Down

0 comments on commit dda2cdf

Please sign in to comment.