Skip to content

Commit

Permalink
Merge pull request #3 from gman-au/2-is-primarykey-ok
Browse files Browse the repository at this point in the history
#2 added builder for primary key
  • Loading branch information
gubpalma authored Jul 8, 2024
2 parents d4beb6c + 6fe14f2 commit 1ef4226
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,21 @@ public class EntityBuilder : IEntityBuilder
{
private const string EntityMethodName = "Entity";
private readonly IBuildConfigurationProvider _buildConfigurationProvider;
private readonly IKeyBuilder _keyBuilder;
private readonly IPropertyBuilder _propertyBuilder;
private readonly ITableBuilder _tableBuilder;

public EntityBuilder(
IBuildConfigurationProvider buildConfigurationProvider,
IPropertyBuilder propertyBuilder,
ITableBuilder tableBuilder
IPropertyBuilder propertyBuilder,
ITableBuilder tableBuilder,
IKeyBuilder keyBuilder
)
{
_buildConfigurationProvider = buildConfigurationProvider;
_propertyBuilder = propertyBuilder;
_tableBuilder = tableBuilder;
_keyBuilder = keyBuilder;
}

public bool IsApplicable(Instruction instr)
Expand Down Expand Up @@ -60,7 +63,7 @@ public ExtractedEntity Process(Instruction instr)

if (currInstr.OpCode == OpCodes.Ldstr)
{
var splitName =
var splitName =
currInstr
.Operand
.ToString()
Expand All @@ -84,17 +87,38 @@ public ExtractedEntity Process(Instruction instr)
methodReference
.Body
.Instructions
.Where(o =>
_propertyBuilder
.IsApplicable(o))
.Where(
o =>
_propertyBuilder
.IsApplicable(o)
)
.ToList();

var extractedProperties =
propertyInstructions
.Select(o => _propertyBuilder.Process(o))
.Where(o => o != null)
.ToList();


var keyInstructions =
methodReference
.Body
.Instructions
.Where(
o =>
_keyBuilder
.IsApplicable(o)
);

foreach (var keyInstruction in keyInstructions)
{
_keyBuilder
.Process(
keyInstruction,
extractedProperties
);
}

// Extract table information
var tableInstr =
methodReference
Expand Down
13 changes: 13 additions & 0 deletions src/6.0/Siren.Infrastructure.AssemblyLoad/Builders/IKeyBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System.Collections.Generic;
using Mono.Cecil.Cil;
using Siren.Infrastructure.AssemblyLoad.Domain;

namespace Siren.Infrastructure.AssemblyLoad.Builders
{
public interface IKeyBuilder
{
public bool IsApplicable(Instruction instr);

public void Process(Instruction instr, IEnumerable<ExtractedProperty> properties);
}
}
46 changes: 46 additions & 0 deletions src/6.0/Siren.Infrastructure.AssemblyLoad/Builders/KeyBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System.Collections.Generic;
using System.Linq;
using Mono.Cecil;
using Mono.Cecil.Cil;
using Siren.Infrastructure.AssemblyLoad.Domain;
using Siren.Infrastructure.AssemblyLoad.Extensions;

namespace Siren.Infrastructure.AssemblyLoad.Builders
{
public class KeyBuilder : IKeyBuilder
{
private const string KeyMethodName = "HasKey";

public bool IsApplicable(Instruction instr)
{
if (instr.OpCode != OpCodes.Callvirt) return false;

if (instr.Operand is not MethodReference mr) return false;

return mr.Name == KeyMethodName;
}

public void Process(Instruction instr, IEnumerable<ExtractedProperty> properties)
{
var currInstr =
instr
.StepPrevious(2);

if (currInstr.OpCode != OpCodes.Ldstr) return;

var propertyName =
currInstr
.Operand
.ToString();

var matchingProperty =
properties
.FirstOrDefault(o => o.PropertyName == propertyName);

if (matchingProperty != null)
{
matchingProperty.IsPrimaryKey = true;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public class ExtractedProperty

public string DataType { get; set; }

public bool IsPrimaryKey { get; set; }

public bool IsForeignKey { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public Universe Map(
{
Name = p.PropertyName?.ToEscaped(),
Type = p.DataType?.ToEscaped(),
IsPrimaryKey = false,
IsPrimaryKey = p.IsPrimaryKey,
IsForeignKey = p.IsForeignKey,
IsUniqueKey = false
}
Expand Down
1 change: 1 addition & 0 deletions src/6.0/Siren.Tool/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public static IServiceCollection AddServices()
.AddSingleton<IPropertyBuilder, PropertyBuilder>()
.AddSingleton<ITableBuilder, TableBuilder>()
.AddSingleton<IRelationshipBuilder, RelationshipBuilder>()
.AddSingleton<IKeyBuilder, KeyBuilder>()
.AddSingleton<IRelationshipFilter, RelationshipFilter>()
.AddSingleton<IDomainRenderer, MermaidRenderer>()
.AddSingleton<IProgramArgumentsParser, ProgramArgumentsParser>();
Expand Down

0 comments on commit 1ef4226

Please sign in to comment.