Skip to content

Commit

Permalink
[BMSPT-283] builder delegate draft
Browse files Browse the repository at this point in the history
  • Loading branch information
BalintBende committed Aug 12, 2024
1 parent 6957093 commit 9b8583f
Show file tree
Hide file tree
Showing 12 changed files with 76 additions and 81 deletions.
5 changes: 5 additions & 0 deletions bcf-toolkit.sln.DotSettings.user
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
<s:String x:Key="/Default/Environment/Highlighting/HighlightingSourceSnapshotLocation/@EntryValue">/Users/balintbende/Library/Caches/JetBrains/Rider2024.1/resharper-host/temp/Rider/vAny/CoverageData/_bcf-toolkit.-1315391344/Snapshot/snapshot.utdcvr</s:String>
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=00488d9b_002Ddfb9_002D47fc_002Da7da_002D60da862eab61/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" Name="All tests from Solution #2" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;
&lt;Solution /&gt;
&lt;/SessionState&gt;</s:String>
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=14575ace_002D843e_002D446c_002Daad2_002Da5b832d528be/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" Name="BuildBcfFromV30StreamTest" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;
&lt;TestAncestor&gt;
&lt;TestId&gt;NUnit3x::8113526D-8A68-4E3E-B4DB-CE235875DDD1::net8.0::Tests.WorkerTests.BuildBcfFromV30StreamTest&lt;/TestId&gt;
&lt;/TestAncestor&gt;
&lt;/SessionState&gt;</s:String>
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=4dc5af78_002D6a8f_002D4f5b_002D8833_002Df0ff81e0c049/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" IsActive="True" Name="All tests from Solution #3" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;
&lt;Solution /&gt;
Expand Down
2 changes: 0 additions & 2 deletions src/bcf-toolkit/Builder/Bcf21/BcfBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ public partial class BcfBuilder : IBcfBuilder<
ProjectExtensionBuilder>,
IDefaultBuilder<BcfBuilder> {
private readonly Bcf _bcf = new();



public BcfBuilder AddMarkup(Action<MarkupBuilder> builder) {
var markup =
(Markup)BuilderUtils.BuildItem<MarkupBuilder, IMarkup>(builder);
Expand Down
7 changes: 3 additions & 4 deletions src/bcf-toolkit/Builder/Bcf21/BcfBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Threading.Tasks;
using BcfToolkit.Model;
using BcfToolkit.Model.Bcf21;
using BcfToolkit.Utils;
using IBcfBuilderDelegate = BcfToolkit.Builder.Bcf21.Interfaces.IBcfBuilderDelegate;
using Interfaces_IBcfBuilderDelegate = BcfToolkit.Builder.Interfaces.IBcfBuilderDelegate;

namespace BcfToolkit.Builder.Bcf21;

public partial class BcfBuilder {
private readonly IBcfBuilderDelegate? _delegate;
private readonly Interfaces_IBcfBuilderDelegate? _delegate;

public BcfBuilder(IBcfBuilderDelegate? builderDelegate = null) {
public BcfBuilder(Interfaces_IBcfBuilderDelegate builderDelegate = null) {
this._delegate = builderDelegate;

_bcf.Version = new VersionBuilder()
Expand Down
17 changes: 0 additions & 17 deletions src/bcf-toolkit/Builder/Bcf21/Interfaces/IBcfBuilderDelegate.cs

This file was deleted.

21 changes: 11 additions & 10 deletions src/bcf-toolkit/Builder/Bcf30/BcfBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using BcfToolkit.Builder.Interfaces;
using BcfToolkit.Model.Bcf30;
using Bcf = BcfToolkit.Model.Bcf30.Bcf;
using IBcfBuilderDelegate = BcfToolkit.Builder.Bcf30.Interfaces.IBcfBuilderDelegate;
using Markup = BcfToolkit.Model.Bcf30.Markup;

namespace BcfToolkit.Builder.Bcf30;
Expand All @@ -15,16 +14,11 @@ public partial class BcfBuilder : IBcfBuilder<
ExtensionsBuilder,
DocumentInfoBuilder>,
IDefaultBuilder<BcfBuilder> {

private readonly Bcf _bcf = new();

private readonly IBcfBuilderDelegate? _delegate;

public BcfBuilder(IBcfBuilderDelegate? builderDelegate = null) {
this._delegate = builderDelegate;

_bcf.Version = new VersionBuilder()
.WithDefaults()
.Build();

public BcfBuilder() {
SetVersion();
}

public BcfBuilder AddMarkup(Action<MarkupBuilder> builder) {
Expand Down Expand Up @@ -55,6 +49,13 @@ public BcfBuilder SetDocument(Action<DocumentInfoBuilder> builder) {
return this;
}

public BcfBuilder SetVersion() {
_bcf.Version = new VersionBuilder()
.WithDefaults()
.Build();
return this;
}

public BcfBuilder WithDefaults() {
this
.AddMarkup(m => m.WithDefaults())
Expand Down
13 changes: 11 additions & 2 deletions src/bcf-toolkit/Builder/Bcf30/BcfBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,29 @@
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using BcfToolkit.Builder.Interfaces;
using BcfToolkit.Model.Bcf30;
using BcfToolkit.Utils;

namespace BcfToolkit.Builder.Bcf30;

public partial class BcfBuilder {

private IBcfBuilderDelegate? _delegate;

public void SetDelegate(IBcfBuilderDelegate? builderDelegate) {
this._delegate = builderDelegate;
}

public async Task ProcessStream(Stream source) {
if (_delegate is null) {
Console.WriteLine("IBcfBuilderDelegate is not set.");
return;
}

// await BcfExtensions.ParseMarkups<Markup, VisualizationInfo>(source,
// _delegate.MarkupCreated);
await BcfExtensions.ParseMarkups<Markup, VisualizationInfo>(
source,
_delegate.MarkupCreated);

// var extensions = await BcfExtensions.ParseExtensions<Extensions>(source);
// _delegate.ExtensionsCreated(extensions);
Expand Down
26 changes: 0 additions & 26 deletions src/bcf-toolkit/Builder/Bcf30/Interfaces/IBcfBuilderDelegate.cs

This file was deleted.

24 changes: 16 additions & 8 deletions src/bcf-toolkit/Builder/Interfaces/IBcfBuilderDelegate.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
using BcfToolkit.Model;
using BcfToolkit.Model.Bcf21;
using BcfToolkit.Model.Interfaces;

namespace BcfToolkit.Builder.Interfaces;

public interface IBcfBuilderDelegate {
public delegate void OnMarkupCreated<in TMarkup>(TMarkup markup)
where TMarkup : IMarkup;

public delegate void
OnProjectCreated<in TProject>(ProjectExtension projectInfo)
where TProject : IProject;


public delegate void OnProjectCreated<in TProjectInfo>(
TProjectInfo projectInfo)
where TProjectInfo : IProject;

public delegate void OnExtensionsCreated<in TExtensions>(
TExtensions extensions)
where TExtensions : IExtensions;

public delegate void OnDocumentCreated<in TDocumentInfo>(
TDocumentInfo documentInfo)
where TDocumentInfo : IDocumentInfo;

public OnMarkupCreated<IMarkup> MarkupCreated { get; }

public OnExtensionsCreated<IExtensions> ExtensionsCreated { get; }
public OnProjectCreated<IProject> ProjectCreated { get; }
public OnDocumentCreated<IDocumentInfo> DocumentCreatedCreated { get; }
}
7 changes: 3 additions & 4 deletions src/bcf-toolkit/Converter/Bcf21/Converter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,15 +147,14 @@ public Task ToJson(IBcf bcf, string target) {

public async Task<T> BcfFromStream<T>(Stream stream) {
var bcf = await _builder.BuildInMemoryFromStream(stream);

var targetVersion = BcfVersion.TryParse(typeof(T));
var converterFn = _converterFn[targetVersion];
return (T)converterFn(bcf);
}

public async Task ProcessStream<T>(Stream stream) {
var targetVersion = BcfVersion.TryParse(typeof(T));

public async Task ProcessStream(Stream stream) {
// var targetVersion = BcfVersion.TryParse(typeof(T));
_builder.Set
await _builder.ProcessStream(stream);
}
}
12 changes: 10 additions & 2 deletions src/bcf-toolkit/Converter/Bcf30/Converter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Threading;
using System.Threading.Tasks;
using BcfToolkit.Builder.Bcf30;
using BcfToolkit.Builder.Interfaces;
using BcfToolkit.Utils;
using BcfToolkit.Model;
using BcfToolkit.Model.Bcf30;
Expand Down Expand Up @@ -58,7 +59,7 @@ private readonly Dictionary<

public async Task BcfToJson(Stream source, string target) {
var builder = new BcfBuilder();
var bcf = await builder.BuildFromStream(source);
var bcf = await builder.BuildInMemoryFromStream(source);
await FileWriter.WriteJson(bcf, target);
}

Expand Down Expand Up @@ -140,9 +141,16 @@ public Task ToJson(IBcf bcf, string target) {
}

public async Task<T> BcfFromStream<T>(Stream stream) {
var bcf = await _builder.BuildFromStream(stream);
var bcf = await _builder.BuildInMemoryFromStream(stream);
var targetVersion = BcfVersion.TryParse(typeof(T));
var converterFn = _converterFn[targetVersion];
return (T)converterFn(bcf);
}

public async Task ProcessStream(
Stream stream,
IBcfBuilderDelegate builderDelegate) {
_builder.SetDelegate(builderDelegate);
await _builder.ProcessStream(stream);
}
}
7 changes: 7 additions & 0 deletions src/bcf-toolkit/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,17 @@ public IBcfBuilderDelegate.OnMarkupCreated<IMarkup>
Console.WriteLine(d);
};

public IBcfBuilderDelegate.OnExtensionsCreated<IExtensions> ExtensionsCreated {
get;
}


public IBcfBuilderDelegate.OnProjectCreated<IProject>
ProjectCreated { get; } = Console.WriteLine;

public IBcfBuilderDelegate.OnDocumentCreated<IDocumentInfo> DocumentCreatedCreated {
get;
}
}

internal static class Program {
Expand Down
16 changes: 10 additions & 6 deletions src/bcf-toolkit/Utils/BcfExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
using System.Threading.Tasks;
using System.Xml.Linq;
using System.Xml.Serialization;
using BcfToolkit.Builder.Bcf30.Interfaces;
using BcfToolkit.Builder.Interfaces;
using BcfToolkit.Model;
using BcfToolkit.Model.Interfaces;

Expand Down Expand Up @@ -55,8 +55,10 @@ public static async Task<ConcurrentBag<TMarkup>> ParseMarkups<
return await _ParseMarkups<TMarkup, TVisualizationInfo>(stream);
}

private static async Task<ConcurrentBag<TMarkup>> _ParseMarkups<TMarkup,
TVisualizationInfo>(Stream stream,
private static async Task<ConcurrentBag<TMarkup>> _ParseMarkups<
TMarkup,
TVisualizationInfo>(
Stream stream,
IBcfBuilderDelegate.OnMarkupCreated<TMarkup>? onMarkupCreated = null)
where TMarkup : IMarkup
where TVisualizationInfo : IVisualizationInfo {
Expand Down Expand Up @@ -170,9 +172,11 @@ private static void WritingOutMarkup<TMarkup, TVisualizationInfo>(
if (markup != null) {
markup.SetViewPoints(visInfos, snapshots);

onMarkupCreated?.Invoke(markup);

markups.Add(markup);
// If a delegate is provided, invoke it without adding markup to the BCF
if(onMarkupCreated is not null)
onMarkupCreated.Invoke(markup);
else
markups.Add(markup);

// Null-ing external references
markup = default;
Expand Down

0 comments on commit 9b8583f

Please sign in to comment.