Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for .NET Trimming #40

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from
Draft
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
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ jobs:
run: dotnet test -c Release ValveKeyValue/ValveKeyValue.Test/ValveKeyValue.Test.csproj /p:CollectCoverage=true /p:CoverletOutputFormat=lcov /p:CoverletOutput='./lcov.info' /p:Include="[ValveKeyValue*]*"

- name: Upload test coverage
uses: coverallsapp/github-action@v1.1.1
uses: coverallsapp/[email protected].3
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
path-to-lcov: ./ValveKeyValue/ValveKeyValue.Test/lcov.info
path-to-lcov: ./ValveKeyValue/ValveKeyValue.Test/lcov.net5.0.info
flag-name: run-${{ matrix.test_number }}
parallel: true

Expand All @@ -37,7 +37,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Coveralls Finished
uses: coverallsapp/github-action@v1.1.1
uses: coverallsapp/[email protected].3
with:
github-token: ${{ secrets.github_token }}
parallel-finished: true
2 changes: 0 additions & 2 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
<DebugType>embedded</DebugType>
<DebugSymbols>true</DebugSymbols>
<NeutralLanguage>en</NeutralLanguage>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<AppendRuntimeIdentifierToOutputPath>false</AppendRuntimeIdentifierToOutputPath>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>
<PropertyGroup>
Expand Down
2 changes: 1 addition & 1 deletion ValveKeyValue/ValveKeyValue.Test/ValveKeyValue.Test.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFrameworks>netcoreapp3.1;net5.0</TargetFrameworks>
<PackageId>ValveKeyValue.Test</PackageId>
<Product>Valve KeyValue Library - Unit Tests</Product>
<Description>Unit Tests for library to parse and write Valve KeyValue formats</Description>
Expand Down
20 changes: 17 additions & 3 deletions ValveKeyValue/ValveKeyValue/DefaultObjectReflector.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,29 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;

namespace ValveKeyValue
{
sealed class DefaultObjectReflector : IObjectReflector
{
IEnumerable<IObjectMember> IObjectReflector.GetMembers(object @object)
IEnumerable<IObjectMember> IObjectReflector.GetMembers(
#if NET5_0_OR_GREATER
[DynamicallyAccessedMembers(Trimming.Properties)]
#endif
Type type,
object @object)
{
Require.NotNull(type, nameof(type));
Require.NotNull(@object, nameof(@object));
var properties = @object.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);

var properties = type.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
return GetMembersFromProperties(@object, properties);

}

static IEnumerable<IObjectMember> GetMembersFromProperties(object @object, PropertyInfo[] properties)
{
foreach (var property in properties)
{
if (property.GetCustomAttribute<KVIgnoreAttribute>() != null)
Expand Down
11 changes: 9 additions & 2 deletions ValveKeyValue/ValveKeyValue/IObjectReflector.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;

namespace ValveKeyValue
{
interface IObjectReflector
{
IEnumerable<IObjectMember> GetMembers(object @object);
IEnumerable<IObjectMember> GetMembers(
#if NET5_0_OR_GREATER
[DynamicallyAccessedMembers(Trimming.Properties)]
#endif
Type type,
object @object);
}
}
9 changes: 9 additions & 0 deletions ValveKeyValue/ValveKeyValue/KVSerializer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using ValveKeyValue.Abstraction;
using ValveKeyValue.Deserialization;
Expand Down Expand Up @@ -52,7 +53,11 @@ public KVObject Deserialize(Stream stream, KVSerializerOptions options = null)
/// <param name="options">Options to use that can influence the deserialization process.</param>
/// <returns>A <typeparamref name="TObject" /> instance representing the KeyValues structure in the stream.</returns>
/// <typeparam name="TObject">The type of object to deserialize.</typeparam>;
#if NET5_0_OR_GREATER
public TObject Deserialize<[DynamicallyAccessedMembers(Trimming.Constructors | Trimming.Properties)] TObject>(Stream stream, KVSerializerOptions options = null)
#else
public TObject Deserialize<TObject>(Stream stream, KVSerializerOptions options = null)
#endif
{
Require.NotNull(stream, nameof(stream));

Expand Down Expand Up @@ -84,7 +89,11 @@ public void Serialize(Stream stream, KVObject data, KVSerializerOptions options
/// <param name="name">The top-level object name</param>
/// <param name="options">Options to use that can influence the serialization process.</param>
/// <typeparam name="TData">The type of object to serialize.</typeparam>
#if NET5_0_OR_GREATER
public void Serialize<[DynamicallyAccessedMembers(Trimming.Properties)] TData>(Stream stream, TData data, string name, KVSerializerOptions options = null)
#else
public void Serialize<TData>(Stream stream, TData data, string name, KVSerializerOptions options = null)
#endif
{
Require.NotNull(stream, nameof(stream));

Expand Down
Loading