Skip to content

Commit

Permalink
#357 add in dotnet 6 TFM and tweak dependencies (#361)
Browse files Browse the repository at this point in the history
* #357 add in dotnet 6 TFM and tweak dependencies

* fix better nullability compiler errors on net6.0

---------

Co-authored-by: Martijn Laarman <[email protected]>
  • Loading branch information
thompson-tomo and Mpdreamz authored Apr 4, 2024
1 parent 1c91c00 commit 34c72d7
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 16 deletions.
4 changes: 3 additions & 1 deletion src/Elastic.CommonSchema/EcsDocument.DefaultService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,10 @@ from frame in stackFrames
}


internal static bool IsMsOrElastic(byte[] array)
internal static bool IsMsOrElastic(byte[]? array)
{
if (array == null) return false;

var elasticApmToken = new byte[] { 174, 116, 0, 210, 193, 137, 207, 34 };
var mscorlibToken = new byte[] { 183, 122, 92, 86, 25, 52, 224, 137 };
var systemWebToken = new byte[] { 176, 63, 95, 127, 17, 213, 10, 58 };
Expand Down
12 changes: 7 additions & 5 deletions src/Elastic.CommonSchema/EcsDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,13 @@ internal static IDictionary<string, string> GetOTelResourceAttributes()
return ParseOTelResourceAttributes(resourceAttributes);
}

private static IDictionary<string, string> ParseOTelResourceAttributes(string resourceAttributes)
private static IDictionary<string, string> ParseOTelResourceAttributes(string? resourceAttributes)
{
if (string.IsNullOrEmpty(resourceAttributes)) return new Dictionary<string, string>();

var keyValues = resourceAttributes
// Needed to satisfy all TFM's sadly.
// ReSharper disable once RedundantSuppressNullableWarningExpression
var keyValues = resourceAttributes!
.Split(new[] { ',' }, RemoveEmptyEntries)
.Select(k => k.Split(new[] { '=' }, 2, RemoveEmptyEntries))
.Where(kv => kv.Length == 2)
Expand All @@ -125,14 +127,14 @@ public static Agent CreateAgent(Type typeFromAgentLibrary)
return new Agent { Type = type, Version = version };
}

private static (string, string) GetAssemblyVersion(Assembly assembly)
private static (string?, string?) GetAssemblyVersion(Assembly assembly)
{
var name = assembly.GetName();
var type = name.Name;
var versionAttribute = assembly.GetCustomAttributes(false)
.OfType<AssemblyInformationalVersionAttribute>()
.FirstOrDefault();
var version = versionAttribute?.InformationalVersion ?? name.Version.ToString();
var version = versionAttribute?.InformationalVersion ?? name?.Version?.ToString();
return (type, version);
}

Expand Down Expand Up @@ -209,7 +211,7 @@ Process ReturnFromCache()
private static readonly string UserDomainName = Environment.UserDomainName;

//Can not cache current thread's identity as it's used for role based security, different threads can have different identities
private static User GetUser() => new () { Id = Thread.CurrentPrincipal?.Identity.Name, Name = UserName, Domain = UserDomainName };
private static User GetUser() => new () { Id = Thread.CurrentPrincipal?.Identity?.Name, Name = UserName, Domain = UserDomainName };

private static Error? GetError(Exception? exception)
{
Expand Down
11 changes: 7 additions & 4 deletions src/Elastic.CommonSchema/Elastic.CommonSchema.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netstandard2.0;netstandard2.1;net461</TargetFrameworks>
<TargetFrameworks>netstandard2.0;netstandard2.1;net461;net6.0</TargetFrameworks>
<Title>Elastic Common Schema (ECS) Types</Title>
<Description>Maps Elastic Common Schema (ECS) to .NET types including (de)serialization using System.Text.Json</Description>
<LangVersion>latest</LangVersion>
Expand All @@ -10,12 +10,15 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Ben.Demystifier" Version="0.4.1" />
<PackageReference Include="System.Diagnostics.DiagnosticSource" Version="5.0.0" />
<PackageReference Include="System.Text.Json" Version="6.0.1" />
<PackageReference Include="PolySharp" Version="1.13.2" PrivateAssets="all">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup Condition="$(TargetFramework) == 'net461' OR $(TargetFramework) == 'netstandard2.0' OR $(TargetFramework) == 'netstandard2.1'">
<PackageReference Include="System.Diagnostics.DiagnosticSource" Version="5.0.0" />
<PackageReference Include="System.Text.Json" Version="6.0.1" />

</ItemGroup>
<ItemGroup>
<Compile Include="..\NullableExtensions.cs" />
</ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/Elastic.CommonSchema/Location.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public override string ToString() =>
Longitude.ToString("#0.0#######", CultureInfo.InvariantCulture);

/// <inheritdoc cref="object.Equals(object)"/>>
public override bool Equals(object obj)
public override bool Equals(object? obj)
{
if (ReferenceEquals(null, obj))
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@ public class EcsDocumentJsonConverterFactory : JsonConverterFactory
public override bool CanConvert(Type typeToConvert) => typeof(EcsDocument).IsAssignableFrom(typeToConvert);

/// <inheritdoc cref="JsonConverterFactory.CreateConverter"/>
public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options) =>
typeToConvert == typeof(EcsDocument)
? EcsJsonConfiguration.DefaultEcsDocumentJsonConverter
// TODO validate this is only called once
: (JsonConverter)Activator.CreateInstance(typeof(EcsDocumentJsonConverter<>).MakeGenericType(typeToConvert));
public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options)
{
if (typeToConvert == typeof(EcsDocument))
return EcsJsonConfiguration.DefaultEcsDocumentJsonConverter;

var instance = Activator.CreateInstance(typeof(EcsDocumentJsonConverter<>).MakeGenericType(typeToConvert));
return (JsonConverter)instance!;
}
}
}

0 comments on commit 34c72d7

Please sign in to comment.