Skip to content

Commit

Permalink
api polish
Browse files Browse the repository at this point in the history
  • Loading branch information
malstraem committed Oct 4, 2024
1 parent c3ae852 commit 2fdeaef
Show file tree
Hide file tree
Showing 13 changed files with 72 additions and 36 deletions.
3 changes: 2 additions & 1 deletion source/library/Arinc424.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
<RepositoryUrl>https://github.com/malstraem/arinc424.net</RepositoryUrl>
<PublishRepositoryUrl>true</PublishRepositoryUrl>

<PackageReadmeFile>readme.md</PackageReadmeFile>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>

Expand All @@ -41,6 +42,6 @@
<ItemGroup>
<ProjectReference Include="../../codegen/Arinc424.Generators.csproj" OutputItemType="analyzer" ReferenceOutputAssembly="false" />

<None Include="../../docs/package.md" Pack="true" PackagePath="/README.md" Visible="false"/>
<None Include="../../docs/package.md" Pack="true" PackagePath="/readme.md" Visible="false"/>
</ItemGroup>
</Project>
4 changes: 3 additions & 1 deletion source/library/Data424.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Arinc424.Routing;
using Arinc424.Tables;
using Arinc424.Waypoints;
using Arinc424.Building;

namespace Arinc424;

Expand All @@ -26,7 +27,8 @@ internal static Dictionary<PropertyInfo, Section> GetProperties()
return properties;
}

public static Data424 Create(IEnumerable<string> strings, Supplement supplement) => new Parser424(supplement).Parse(strings);
public static Data424 Create(Meta424 meta, IEnumerable<string> strings, out string[] skipped, out Build[] invalid)
=> new Parser424(meta).Parse(strings, out skipped, out invalid);

/// <summary>
/// <c>Grid MORA</c> records.
Expand Down
25 changes: 19 additions & 6 deletions source/library/Meta424.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,27 +84,40 @@

namespace Arinc424;

internal class Meta424
/// <summary>
/// Metadata that defines how entities should be created.
/// </summary>
public class Meta424
{
private Meta424(RecordInfo[] info, FrozenDictionary<Section, Type> types, FrozenDictionary<Type, RecordInfo> typeInfo)
{
Info = info;
Types = types;
TypeInfo = typeInfo;
}

/// <summary>
/// Creates metadata using target <paramref name="supplement"/>.
/// </summary>
/// <returns>Runtime compiled metadata.</returns>
[Obsolete("todo: supplement versioning (v18 - v23)")]
internal Meta424(Supplement supplement)
public static Meta424 Create(Supplement supplement)
{
var attributes = Assembly.GetExecutingAssembly().GetCustomAttributes<InfoAttribute>();

Info = attributes.SelectMany(x => x.GetInfo(supplement)).ToArray();
var infos = attributes.SelectMany(x => x.GetInfo(supplement)).ToArray();

Dictionary<Section, Type> types = [];
Dictionary<Type, RecordInfo> typeInfo = [];

foreach (var info in Info)
foreach (var info in infos)
{
types.Add(info.Section, info.Type);

// types with multiple sections will be stored once
_ = typeInfo.TryAdd(info.Type, info);
}
Types = types.ToFrozenDictionary();
TypeInfo = typeInfo.ToFrozenDictionary();
return new Meta424(infos, types.ToFrozenDictionary(), typeInfo.ToFrozenDictionary());
}

internal RecordInfo[] Info { get; }
Expand Down
24 changes: 11 additions & 13 deletions source/library/Parser424.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,42 +56,40 @@ private void Link(Unique unique)
info.Link(builds[info.Section], unique, meta);
}
#endif
internal Parser424(Supplement supplement)
internal Parser424(Meta424 meta)
{
meta = new(supplement);
this.meta = meta;

foreach (var info in meta.Info)
strings[info.Section] = ([], []);
}

internal Data424 Parse(IEnumerable<string> strings)
internal Data424 Parse(IEnumerable<string> strings, out string[] skipped, out Build[] invalid)
{
Process(strings);

Build();

Link(new Unique(meta.Info, builds));

ConcurrentBag<Build> invalid = []; // todo api
ConcurrentQueue<Build> invalidBuilds = [];

var data = new Data424();

_ = Parallel.ForEach(Data424.GetProperties(), property =>
_ = Parallel.ForEach(Data424.GetProperties(), pair =>
{
var type = property.Key.PropertyType.GetGenericArguments().First();
var list = (IList)pair.Key.GetValue(data)!;

var list = (IList)property.Key.GetValue(data)!;

foreach (var build in builds[property.Value])
foreach (var build in builds[pair.Value])
{
if (build.Diagnostics is null)
{
_ = list.Add(build.Record);
continue;
}
invalid.Add(build);
else
invalidBuilds.Enqueue(build);
}
});
skipped = [.. this.skipped];
invalid = [.. invalidBuilds];
return data;
}
}
12 changes: 12 additions & 0 deletions source/library/attributes/converting/NumericAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace Arinc424.Attributes;

using static System.Globalization.NumberStyles;

[AttributeUsage(AttributeTargets.Property)]
internal sealed class NumericAttribute<TParsable> : DecodeAttribute<TParsable>
where TParsable : ISpanParsable<TParsable>
{
private const System.Globalization.NumberStyles style = None | AllowLeadingSign | AllowLeadingWhite;

internal override Result<TParsable> Convert(ReadOnlySpan<char> @string) => TParsable.TryParse(@string, null, out var value) ? value : @string;
}
15 changes: 8 additions & 7 deletions source/library/diagnostics/DiagnosticType.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
namespace Arinc424.Diagnostics;

public enum DiagnosticType
[Flags]
public enum DiagnosticType : byte
{
Null,
Duplicate,
InvalidLink,
InvalidType,
InvalidValue,
InvalidSection,
Null = 0,
Duplicate = 1,
InvalidLink = 1 << 1,
InvalidType = 1 << 2,
InvalidValue = 1 << 3,
InvalidSection = 1 << 4,
}
7 changes: 7 additions & 0 deletions source/library/records/Record424.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,11 @@ public abstract class Record424
/// <remarks>See section 5.31.</remarks>
[Field(124, 128), Integer]
public int Number { get; set; }

/// <summary>
/// <c>Cycle Date</c> field.
/// </summary>
/// <remarks>See section 5.32</remarks>
[Field(129, 132), Integer]
public int Date { get; set; }
}
2 changes: 1 addition & 1 deletion source/view/source/viewmodels/DataViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public async void Load(string[] paths)
{
string[] strings = File.ReadAllLines(paths.First());

return Data424.Create(strings, Supplement.V18);
return Data424.Create(Meta424.Create(Supplement.V18), strings, out string[] _, out var _);
});

Sections = await Task.Run(() => GetSections(data));
Expand Down
6 changes: 4 additions & 2 deletions tests/benchmarks/LoadBench.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ namespace Arinc424.Bench;
[SimpleJob]
public class LoadBench
{
private readonly string[] data = File.ReadAllLines("data/unknown");
private readonly string[] strings = File.ReadAllLines("data/unknown");

private readonly Meta424 meta = Meta424.Create(Supplement.V18);

[Benchmark]
public Data424 LoadWorld() => Data424.Create(data, Supplement.V18);
public Data424 LoadWorld() => Data424.Create(meta, strings, out string[] _, out var _);
}
2 changes: 1 addition & 1 deletion tests/benchmarks/MetaBench.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ namespace Arinc424.Bench;
public class MetaBench
{
[Benchmark]
public ImmutableArray<Type> GrabTypesInfo() => new Meta424(Supplement.V23).TypeInfo.Keys;
public ImmutableArray<Type> GrabTypesInfo() => Meta424.Create(Supplement.V23).TypeInfo.Keys;
}
2 changes: 1 addition & 1 deletion tests/tests/IntegrityTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ public class IntegrityTests
public void CheckSupplements()
{
foreach (var value in Enum.GetValues<Supplement>())
_ = new Meta424(value);
_ = Meta424.Create(value);
}
}
4 changes: 2 additions & 2 deletions tests/tests/RecordCountRegressionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class RecordCountRegressionTests
#pragma warning restore xUnit1004
public void MakeRegression(string file, Supplement supplement)
{
var data = Data424.Create(File.ReadAllLines($"data/{file}"), supplement);
var data = Data424.Create(Meta424.Create(supplement), File.ReadAllLines($"data/{file}"), out string[] _, out var _);

Dictionary<string, int> counts = [];

Expand All @@ -31,7 +31,7 @@ public void MakeRegression(string file, Supplement supplement)
[InlineData("supplement-18", Supplement.V18)]
public void CheckRegression(string file, Supplement supplement)
{
var data = Data424.Create(File.ReadAllLines($"data/{file}"), supplement);
var data = Data424.Create(Meta424.Create(supplement), File.ReadAllLines($"data/{file}"), out string[] _, out var _);

var counts = JsonSerializer.Deserialize<Dictionary<string, int>>(File.ReadAllText($"data/regression/{file}.json"))!;

Expand Down
2 changes: 1 addition & 1 deletion tests/tests/StructuralRegressionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ public class StructuralRegressionTests
public void MakeRegression()
{
// todo: save entire properties of all entities
// light size case and yaml or binary serialization is needed
// light size case and json serialization
}

public void CheckRegression(string file)
Expand Down

0 comments on commit 2fdeaef

Please sign in to comment.