Skip to content

Commit

Permalink
#24: Fixed issue when sequence diagram actor had some unexpected char…
Browse files Browse the repository at this point in the history
…s (like space) in its name
  • Loading branch information
MarcinCelej committed Jul 13, 2023
1 parent c095e2a commit 6458605
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,7 @@ public record SequenceDiagramActor(
SequenceDiagramArchetype Archetype = SequenceDiagramArchetype.Actor,
string? Note = null,
string? Colour = null
);
)
{
internal string CodeName => this.Name.CodeName();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Text.RegularExpressions;

namespace Synergy.Architecture.Diagrams.Sequence;

internal static class SequenceDiagramExtensions
{
public static string CodeName(this string name)
=> Regex.Replace(name.Replace("\\n", ""), "[^a-zA-Z]", "_");
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using PlantUml.Net;
using Synergy.Architecture.Annotations.Diagrams.Sequence;
using Synergy.Architecture.Diagrams.Api;
Expand All @@ -18,8 +17,9 @@ public record SequenceDiagramUrl(
TechnicalBlueprint.DiagramComponents Components
)
{
private Type _type => _root.DeclaringType.OrFail(nameof(_root.DeclaringType));
private readonly List<SequenceDiagramNode> _nodes = new() { new(_actor.Name, _actor.Name, _actor.Archetype, _actor.Note, _actor.Colour) };
private Type _type => _root.DeclaringType ?? throw new ArgumentNullException(nameof(_root.DeclaringType));

private readonly List<SequenceDiagramNode> _nodes = new() { new(_actor.CodeName, _actor.Name, _actor.Archetype, _actor.Note, _actor.Colour) };
private const Type? unknownType = null;

public override string ToString()
Expand Down Expand Up @@ -61,12 +61,12 @@ public StringBuilder GenerateDiagramContent()
colour = $" #{node.Colour}";

var @as = "";
if (node.Name != node.FullName)
if (node.CodeName != node.FullName)
@as = $" as \"{node.FullName}\"";
diagram.AppendLine($"{node.Archetype.ToString().ToLowerInvariant()} {node.Name}{@as}{colour}");
diagram.AppendLine($"{node.Archetype.ToString().ToLowerInvariant()} {node.CodeName}{@as}{colour}");
if (node.Note != null)
{
diagram.AppendLine($"/ note over {node.Name}: {node.Note}");
diagram.AppendLine($"/ note over {node.CodeName}: {node.Note}");
}
}

Expand Down Expand Up @@ -108,9 +108,9 @@ private string GenerateSequences(string? request)
var diagram = new StringBuilder();

if(request != null)
diagram.AppendLine($"{_actor.Name}->{rootTypeName}: [{_root.Name}()] {request}");
diagram.AppendLine($"{_actor.CodeName}->{rootTypeName}: [{_root.Name}()] {request}");
else
diagram.AppendLine($"{_actor.Name}->{rootTypeName}: {_root.Name}({SequenceDiagramUrl.GetArguments(_root.GetParameters())})");
diagram.AppendLine($"{_actor.CodeName}->{rootTypeName}: {_root.Name}({SequenceDiagramUrl.GetArguments(_root.GetParameters())})");

var note =_root.GetCustomAttributesBasedOn<SequenceDiagramNoteAttribute>().FirstOrDefault()?.Note;
if (note != null)
Expand All @@ -121,7 +121,7 @@ private string GenerateSequences(string? request)
if (request != null)
diagram.AppendLine($"Browser<--{rootTypeName}: HTTP/1.1 200 OK");
else
diagram.AppendLine($"{_actor.Name}<--{rootTypeName}");
diagram.AppendLine($"{_actor.CodeName}<--{rootTypeName}");

return diagram.ToString();
}
Expand Down Expand Up @@ -159,6 +159,7 @@ private string NextLevel(Type sourceType, string sourceTypeName, MethodBase sour
}
else if (activeGroup != null)
{
// TODO: Marcin Celej [from: Marcin Celej on: 12-07-2023]: does not work properly for loop after loop
diagram.AppendLine("end");
activeGroup = null;
}
Expand Down Expand Up @@ -229,6 +230,7 @@ private void InsertCall(SequenceDiagramCallAttribute nextStep, StringBuilder dia

private void InsertSelfCall(SequenceDiagramSelfCallAttribute nextStep, StringBuilder diagram, Type sourceType, string sourceTypeName)
{
// TODO: Marcin Celej [from: Marcin Celej on: 06-07-2023]: Allow to inline self call
var currentType = sourceType;
var currentTypeName= this.AppendNode(sourceTypeName, SequenceDiagramArchetype.Participant, currentType);

Expand Down Expand Up @@ -274,6 +276,7 @@ private void InsertActivation(SequenceDiagramActivationAttribute nextStep, Strin
if (currentTypeName != sourceTypeName)
{
diagram.AppendLine($"create {currentTypeName}");
// TODO: Marcin Celej [from: Marcin Celej on: 06-07-2023]: Add configuration setting to generate only 'new' word here without exact ctor mentioned here - to simplify diagram
diagram.AppendLine($"{sourceTypeName}->{currentTypeName}: {@throw}new {currentTypeName}({arguments})");
}

Expand Down Expand Up @@ -335,11 +338,11 @@ private string AppendNode(Type type, SequenceDiagramArchetype archetype)

private string AppendNode(string fullName, SequenceDiagramArchetype archetype, Type? type)
{
var name = Regex.Replace(fullName, "[^a-zA-Z]", "_");
var codeName = fullName.CodeName();

var found = this._nodes.FirstOrDefault(n => n.Name == name);
var found = this._nodes.FirstOrDefault(n => n.CodeName == codeName);
if (found != null)
return found.Name;
return found.CodeName;

string? colour = null;
string? note = null;
Expand All @@ -356,10 +359,16 @@ private string AppendNode(string fullName, SequenceDiagramArchetype archetype, T
}
}

this._nodes.Add(new SequenceDiagramNode(name, fullName, archetype, note, colour));
this._nodes.Add(new SequenceDiagramNode(codeName, fullName, archetype, note, colour));

return name;
return codeName;
}
}

internal record SequenceDiagramNode(string Name, string FullName, SequenceDiagramArchetype Archetype, string? Note, string? Colour);
internal record SequenceDiagramNode(
string CodeName,
string FullName,
SequenceDiagramArchetype Archetype,
string? Note,
string? Colour
);
Original file line number Diff line number Diff line change
@@ -1,16 +1,5 @@
# Synergy.Architecture.Diagrams

## Synergy.Reflection.CustomAttributeExtensions (abstract class)
- CustomAttributeExtensions.GetCustomAttributesBasedOn<TAttribute>(
element: MemberInfo
) : TAttribute[] [Extension]
- CustomAttributeExtensions.GetCustomAttributesBasedOn<TAttribute>(
parameter: ParameterInfo
) : TAttribute[] [Extension]
- CustomAttributeExtensions.GetCustomAttributesBasedOn<TAttribute>(
type: Type
) : TAttribute[] [Extension]

## Sequence.SequenceDiagram (record) : IEquatable<SequenceDiagram>
- Actor: SequenceDiagramActor [Nullable] { get; set; }
- Components: TechnicalBlueprint+DiagramComponents? { get; set; }
Expand Down Expand Up @@ -87,15 +76,15 @@
## Documentation.TechnicalBlueprint (class)
- ctor()
- Add(
diagram: params SequenceDiagram[] [ParamArray]
diagrams: params SequenceDiagram[] [ParamArray]
) : TechnicalBlueprint
- Add(
diagram: IEnumerable<SequenceDiagram>
diagrams: IEnumerable<SequenceDiagram>
) : TechnicalBlueprint
- Intro(
markdown: string
) : TechnicalBlueprint
- Register<TComponent, TImplementation>() : TechnicalBlueprint [NullableContext]
- Register<TComponent, TImplementation>() : TechnicalBlueprint
- Register(
interface: Type,
services: IServiceProvider
Expand Down

0 comments on commit 6458605

Please sign in to comment.