Skip to content

Commit

Permalink
feat(): add .net6 and .net8 to core project and get it to compile ope…
Browse files Browse the repository at this point in the history
…nsearch-project#680

Signed-off-by: Joe Feser <[email protected]>
  • Loading branch information
joefeser committed Jul 30, 2024
1 parent fbfb823 commit 660f69d
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 26 deletions.
4 changes: 2 additions & 2 deletions global.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"sdk": {
"version": "6.0.403",
"version": "8.0.107",
"rollForward": "latestFeature",
"allowPrerelease": false
},
"version": "2.0.0",
"doc_current": "2.0",
"doc_branch": "2.x"
}
}
4 changes: 4 additions & 0 deletions src/OpenSearch.Net/Connection/MetaData/RuntimeVersionInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,11 @@ private static string GetNetCoreVersion()

private static bool TryGetVersionFromAssemblyPath(Assembly assembly, out string runtimeVersion)
{
#if NET6_0_OR_GREATER
var assemblyPath = assembly.Location.Split(new[] { '/', '\\' }, StringSplitOptions.RemoveEmptyEntries);
#else
var assemblyPath = assembly.CodeBase.Split(new[] { '/', '\\' }, StringSplitOptions.RemoveEmptyEntries);
#endif
var netCoreAppIndex = Array.IndexOf(assemblyPath, "Microsoft.NETCore.App");
if (netCoreAppIndex > 0 && netCoreAppIndex < assemblyPath.Length - 2)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public override void Reseed(IEnumerable<Node> nodes)
{
_readerWriter.EnterWriteLock();
var sortedNodes = SortNodes(nodesArray)
.DistinctBy(n => n.Uri)
.DistinctByInternal(n => n.Uri)
.ToList();

InternalNodes = sortedNodes;
Expand Down
2 changes: 1 addition & 1 deletion src/OpenSearch.Net/ConnectionPool/StaticConnectionPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ private void Initialize(IEnumerable<Node> nodes, IDateTimeProvider dateTimeProvi
}

InternalNodes = SortNodes(nodesProvided)
.DistinctBy(n => n.Uri)
.DistinctByInternal(n => n.Uri)
.ToList();
LastUpdate = DateTimeProvider.Now();
}
Expand Down
2 changes: 1 addition & 1 deletion src/OpenSearch.Net/Extensions/UtilExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ internal static void ThrowIfNull<T>(this T value, string name) where T : class

internal static bool IsNullOrEmpty(this string value) => string.IsNullOrEmpty(value);

internal static IEnumerable<T> DistinctBy<T, TKey>(this IEnumerable<T> items, Func<T, TKey> property) =>
internal static IEnumerable<T> DistinctByInternal<T, TKey>(this IEnumerable<T> items, Func<T, TKey> property) =>
items.GroupBy(property).Select(x => x.First());

internal static string ToTimeUnit(this TimeSpan timeSpan)
Expand Down
4 changes: 3 additions & 1 deletion src/OpenSearch.Net/OpenSearch.Net.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@
<PropertyGroup>
<IsPackable>true</IsPackable>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<TargetFrameworks>netstandard2.0;netstandard2.1</TargetFrameworks>
<TargetFrameworks>netstandard2.0;netstandard2.1;net6.0;net8.0</TargetFrameworks>

<CheckForOverflowUnderflow>true</CheckForOverflowUnderflow>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>

</PropertyGroup>


<ItemGroup>
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
<PackageReference Include="System.Buffers" Version="4.5.1" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,21 @@ private static List<Dictionary<string, object>> FlattenExceptions(Exception e)
private static Dictionary<string, object> ToDictionary(Exception e, int depth)
{
var o = new Dictionary<string, object>(10);

#if NET8_0_OR_GREATER
//NOTE: This is a workaround for the obsolete warning in .NET 8.0. We need to find a work around and remove this pragma
#pragma warning disable SYSLIB0050 // Type or member is obsolete
var si = new SerializationInfo(e.GetType(), new FormatterConverter());
#pragma warning restore SYSLIB0050 // Type or member is obsolete
var sc = new StreamingContext();
#pragma warning disable SYSLIB0051 // Type or member is obsolete
e.GetObjectData(si, sc);

#pragma warning restore SYSLIB0051 // Type or member is obsolete
#else
var si = new SerializationInfo(e.GetType(), new FormatterConverter());
var sc = new StreamingContext();
e.GetObjectData(si, sc);
#endif
var helpUrl = si.GetString("HelpURL");
var stackTrace = si.GetString("StackTraceString");
var remoteStackTrace = si.GetString("RemoteStackTraceString");
Expand Down
4 changes: 2 additions & 2 deletions src/OpenSearch.Net/Transport/PostData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ public abstract class PostData

public static PostData Bytes(byte[] bytes) => new PostData<object>(bytes);

#if NETSTANDARD2_1
public static PostData ReadOnlyMemory(ReadOnlyMemory<byte> bytes) => new PostData<object>(bytes);
#if NETSTANDARD2_1 || NET6_0_OR_GREATER
public static PostData ReadOnlyMemory(ReadOnlyMemory<byte> bytes) => new PostData<object>(bytes.ToArray());
#endif

public static PostData String(string serializedString) => new PostData<object>(serializedString);
Expand Down
21 changes: 14 additions & 7 deletions src/OpenSearch.Net/Utf8Json/Resolvers/DynamicObjectResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -301,8 +301,8 @@ private static TypeInfo BuildType(DynamicAssembly assembly, Type type, Func<stri
il.EmitLdfld(stringByteKeysField);
}, (index, member) =>
{
FieldInfo fi;
if (!customFormatterLookup.TryGetValue(member, out fi)) return false;
if (!customFormatterLookup.TryGetValue(member, out var fi))
return false;

il.EmitLoadThis();
il.EmitLdfld(fi);
Expand All @@ -318,8 +318,8 @@ private static TypeInfo BuildType(DynamicAssembly assembly, Type type, Func<stri
var il = method.GetILGenerator();
BuildDeserialize(type, serializationInfo, il, (index, member) =>
{
FieldInfo fi;
if (!customFormatterLookup.TryGetValue(member, out fi)) return false;
if (!customFormatterLookup.TryGetValue(member, out var fi))
return false;

il.EmitLoadThis();
il.EmitLdfld(fi);
Expand All @@ -343,10 +343,12 @@ public static object BuildAnonymousFormatter(Type type, Func<string, string> nam
}.Select(x => nameMutator(x)));

// special case for exception, modify
serializationInfo = new MetaType(type, nameMutator, propertyMapper, false);
serializationInfo = new MetaType(type, nameMutator, propertyMapper, false)
{
BestMatchConstructor = null,
ConstructorParameters = []
};

serializationInfo.BestMatchConstructor = null;
serializationInfo.ConstructorParameters = new MetaMember[0];
serializationInfo.Members = new[] { new StringConstantValueMetaMember(nameMutator("ClassName"), type.FullName) }
.Concat(serializationInfo.Members.Where(x => !ignoreSet.Contains(x.Name)))
.Concat(new[] { new InnerExceptionMetaMember(nameMutator("InnerException")) })
Expand Down Expand Up @@ -1256,7 +1258,12 @@ internal static class EmitInfo
public static readonly MethodInfo GetCustomAttributeJsonFormatterAttribute = ExpressionUtility.GetMethodInfo(() => CustomAttributeExtensions.GetCustomAttribute<JsonFormatterAttribute>(default(MemberInfo), default(bool)));

public static readonly MethodInfo ActivatorCreateInstance = ExpressionUtility.GetMethodInfo(() => Activator.CreateInstance(default(Type), default(object[])));

#if NET5_0_OR_GREATER
public static readonly MethodInfo GetUninitializedObject = ExpressionUtility.GetMethodInfo(() => System.Runtime.CompilerServices.RuntimeHelpers.GetUninitializedObject(default(Type)));
#else
public static readonly MethodInfo GetUninitializedObject = ExpressionUtility.GetMethodInfo(() => System.Runtime.Serialization.FormatterServices.GetUninitializedObject(default(Type)));
#endif

public static readonly MethodInfo GetTypeMethod = ExpressionUtility.GetMethodInfo((object o) => o.GetType());
public static readonly MethodInfo TypeGetGenericArguments = ExpressionUtility.GetPropertyInfo((Type t) => t.GenericTypeArguments).GetMethod;
Expand Down
24 changes: 14 additions & 10 deletions tests/Tests/ClientConcepts/LowLevel/PostData.doc.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#pragma warning disable IDE1006 // Naming Styles
#pragma warning disable IDE0044 // Add readonly modifier
/* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
Expand Down Expand Up @@ -64,12 +66,12 @@ public class PostingData

public PostingData()
{
this.@object = new { my_object = "value" };
this.collectionOfObjects = Enumerable.Range(0, 5).Select(i => @object).Cast<object>().ToList();
@object = new { my_object = "value" };
collectionOfObjects = Enumerable.Range(0, 5).Select(i => @object).Cast<object>().ToList();
var json = "{\"my_object\":\"value\"}";
this.utf8ObjectBytes = Utf8Bytes(json);
this.utf8BytesOfListOfStrings = Utf8Bytes(string.Join("\n", collectionOfStrings) + "\n");
this.utf8BytesOfCollectionOfObjects = Utf8Bytes(string.Join("\n", collectionOfObjects.Select(o=> json)) + "\n");
utf8ObjectBytes = Utf8Bytes(json);
utf8BytesOfListOfStrings = Utf8Bytes(string.Join("\n", collectionOfStrings) + "\n");
utf8BytesOfCollectionOfObjects = Utf8Bytes(string.Join("\n", collectionOfObjects.Select(o=> json)) + "\n");
}

[U] public void ImplicitConversions()
Expand Down Expand Up @@ -118,9 +120,9 @@ [U] public void ExplicitCreation()
* Let's demonstrate how to use the static helper on `PostData` for these:
*/

PostData fromObject = PostData.Serializable(@object);
PostData fromListOfString = PostData.MultiJson(collectionOfStrings);
PostData fromListOfObject = PostData.MultiJson(collectionOfObjects);
var fromObject = PostData.Serializable(@object);
var fromListOfString = PostData.MultiJson(collectionOfStrings);
var fromListOfObject = PostData.MultiJson(collectionOfObjects);

/** The `Type` property is representative of the original type from which post data is constructed */
fromListOfString.Type.Should().Be(PostType.EnumerableOfString);
Expand All @@ -139,8 +141,8 @@ [U] public void ExplicitCreation()
//hide
[U] public async Task WritesCorrectlyUsingBothLowAndHighLevelSettings()
{
await this.AssertOn(new ConnectionSettings());
await this.AssertOn(new ConnectionConfiguration());
await AssertOn(new ConnectionSettings());
await AssertOn(new ConnectionConfiguration());
}

private async Task AssertOn(IConnectionConfigurationValues settings)
Expand Down Expand Up @@ -260,3 +262,5 @@ private static async Task PostAssertAsync(PostData postData, byte[] writes, bool

}
}
#pragma warning restore IDE0044 // Add readonly modifier
#pragma warning restore IDE1006 // Naming Styles

0 comments on commit 660f69d

Please sign in to comment.