Skip to content

Commit

Permalink
Added condition to mapping for layouts, so that additional properties…
Browse files Browse the repository at this point in the history
… could be checked for phatboyg#58
  • Loading branch information
phatboyg committed Sep 4, 2018
1 parent 9ab4cb5 commit 010ac95
Show file tree
Hide file tree
Showing 12 changed files with 49 additions and 26 deletions.
4 changes: 2 additions & 2 deletions src/Machete.HL7/Configuration/HL7LayoutMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public abstract class HL7LayoutMap<TLayout, TSchema> :
where TSchema : HL7Entity
where TLayout : HL7Layout
{
protected void Segment<T>(Expression<Func<TLayout, Segment<T>>> propertyExpression, int position, Action<IEntityConfigurator<T>> configure = null)
protected void Segment<T>(Expression<Func<TLayout, Segment<T>>> propertyExpression, int position, Action<IEntityLayoutConfigurator<TSchema, T>> configure = null)
where T : TSchema, HL7Segment
{
var propertyInfo = propertyExpression.GetPropertyInfo();
Expand All @@ -25,7 +25,7 @@ protected void Segment<T>(Expression<Func<TLayout, Segment<T>>> propertyExpressi
Specification.Add(propertyInfo.Name, specification);
}

protected void Segment<T>(Expression<Func<TLayout, SegmentList<T>>> propertyExpression, int position, Action<IEntityConfigurator<T>> configure = null)
protected void Segment<T>(Expression<Func<TLayout, SegmentList<T>>> propertyExpression, int position, Action<IEntityLayoutConfigurator<TSchema, T>> configure = null)
where T : TSchema, HL7Segment
{
var propertyInfo = propertyExpression.GetPropertyInfo();
Expand Down
4 changes: 2 additions & 2 deletions src/Machete.X12/Configuration/X12LayoutMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public abstract class X12LayoutMap<TLayout, TSchema> :
where TSchema : X12Entity
where TLayout : X12Layout
{
protected void Segment<T>(Expression<Func<TLayout, Segment<T>>> propertyExpression, int position, Action<IEntityConfigurator<T>> configure = null)
protected void Segment<T>(Expression<Func<TLayout, Segment<T>>> propertyExpression, int position, Action<IEntityLayoutConfigurator<TSchema, T>> configure = null)
where T : TSchema, X12Segment
{
var propertyInfo = propertyExpression.GetPropertyInfo();
Expand All @@ -25,7 +25,7 @@ protected void Segment<T>(Expression<Func<TLayout, Segment<T>>> propertyExpressi
Specification.Add(propertyInfo.Name, specification);
}

protected void Segment<T>(Expression<Func<TLayout, SegmentList<T>>> propertyExpression, int position, Action<IEntityConfigurator<T>> configure = null)
protected void Segment<T>(Expression<Func<TLayout, SegmentList<T>>> propertyExpression, int position, Action<IEntityLayoutConfigurator<TSchema, T>> configure = null)
where T : TSchema, X12Segment
{
var propertyInfo = propertyExpression.GetPropertyInfo();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ public L1000A_835Map()
Segment(x => x.Address, 1, x => x.IsRequired());
Segment(x => x.GeographicInformation, 2, x => x.IsRequired());
Segment(x => x.AdditionalIdentification, 3);
Segment(x => x.BusinessContactInformation, 4);
Segment(x => x.TechnicalContactInformation, 5, x => x.IsRequired());
Segment(x => x.BusinessContactInformation, 4, x => x.Condition = parser => parser.Where(p => p.ContactFunctionCode.IsEqualTo("CX")));
Segment(x => x.TechnicalContactInformation, 5, x => x.IsRequired().Condition = parser => parser.Where(p => p.ContactFunctionCode.IsEqualTo("BL")));
Segment(x => x.PayerWebsite, 6);
}
}
Expand Down
10 changes: 6 additions & 4 deletions src/Machete/Configuration/EntityConfiguratorExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ public static class EntityConfiguratorExtensions
/// <summary>
/// Sets <paramref name="configurator"/> Required to true
/// </summary>
public static IEntityConfigurator<T> IsRequired<T>(this IEntityConfigurator<T> configurator)
where T : Entity
public static IEntityLayoutConfigurator<TSchema, T> IsRequired<TSchema, T>(this IEntityLayoutConfigurator<TSchema, T> configurator)
where TSchema : Entity
where T : TSchema
{
configurator.Required = true;

Expand All @@ -19,8 +20,9 @@ public static IEntityConfigurator<T> IsRequired<T>(this IEntityConfigurator<T> c
/// <summary>
/// Sets <paramref name="configurator"/> Required to false
/// </summary>
public static IEntityConfigurator<T> IsOptional<T>(this IEntityConfigurator<T> configurator)
where T : Entity
public static IEntityLayoutConfigurator<TSchema, T> IsOptional<TSchema, T>(this IEntityLayoutConfigurator<TSchema, T> configurator)
where TSchema : Entity
where T : TSchema
{
configurator.Required = false;

Expand Down
4 changes: 2 additions & 2 deletions src/Machete/Configuration/LayoutMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ protected LayoutMap()
Specification = new LayoutSpecification<TLayout, TSchema>();
}

protected void Entity<T>(Expression<Func<TLayout, Entity<T>>> propertyExpression, int position, Action<IEntityConfigurator<T>> configure = null)
protected void Entity<T>(Expression<Func<TLayout, Entity<T>>> propertyExpression, int position, Action<IEntityLayoutConfigurator<TSchema, T>> configure = null)
where T : TSchema
{
var propertyInfo = propertyExpression.GetPropertyInfo();
Expand All @@ -33,7 +33,7 @@ protected void Entity<T>(Expression<Func<TLayout, Entity<T>>> propertyExpression
Specification.Add(propertyInfo.Name, specification);
}

protected void Entity<T>(Expression<Func<TLayout, EntityList<T>>> propertyExpression, int position, Action<IEntityConfigurator<T>> configure = null)
protected void Entity<T>(Expression<Func<TLayout, EntityList<T>>> propertyExpression, int position, Action<IEntityLayoutConfigurator<TSchema, T>> configure = null)
where T : TSchema
{
var propertyInfo = propertyExpression.GetPropertyInfo();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Machete.SchemaConfiguration
{
public delegate IParser<TSchema, TEntity> EntityLayoutCondition<TSchema, TEntity>(IQueryParser<TSchema, TEntity> parser)
where TSchema : Entity
where TEntity : TSchema;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,6 @@
using Builders;


public interface IEntityConfigurator<out TEntity>
where TEntity : Entity
{
bool Required { set; }
}


/// <summary>
/// Entity Configuration
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace Machete.SchemaConfiguration
{
public interface IEntityLayoutConfigurator<TSchema, TEntity>
where TSchema : Entity
where TEntity : TSchema
{
bool Required { set; }

EntityLayoutCondition<TSchema, TEntity> Condition { set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
/// <typeparam name="TProperty"></typeparam>
public class EntityLayoutPropertySpecification<TLayout, TSchema, TEntity, TProperty> :
ILayoutPropertySpecification<TLayout, TSchema>,
IEntityConfigurator<TEntity>
IEntityLayoutConfigurator<TSchema, TEntity>
where TLayout : Layout
where TSchema : Entity
where TEntity : TSchema
Expand Down Expand Up @@ -52,11 +52,12 @@ public IEnumerable<Type> GetReferencedEntityTypes()

public void Apply(ILayoutBuilder<TLayout, TSchema> builder)
{
var property = new EntityLayoutProperty<TLayout, TSchema, TEntity, TProperty>(_property, Required, _propertyConverter);
var property = new EntityLayoutProperty<TLayout, TSchema, TEntity, TProperty>(_property, Required, _propertyConverter, Condition);

builder.Add(property);
}

public bool Required { private get; set; }
public EntityLayoutCondition<TSchema, TEntity> Condition { private get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

public class EntityListLayoutPropertySpecification<TLayout, TSchema, TEntity, TProperty> :
ILayoutPropertySpecification<TLayout, TSchema>,
IEntityConfigurator<TEntity>
IEntityLayoutConfigurator<TSchema, TEntity>
where TLayout : Layout
where TSchema : Entity
where TEntity : TSchema
Expand Down Expand Up @@ -44,11 +44,12 @@ public IEnumerable<Type> GetReferencedEntityTypes()

public void Apply(ILayoutBuilder<TLayout, TSchema> builder)
{
var property = new EntityListLayoutProperty<TLayout, TSchema, TEntity, TProperty>(_property, Required, _propertyConverter);
var property = new EntityListLayoutProperty<TLayout, TSchema, TEntity, TProperty>(_property, Required, _propertyConverter, Condition);

builder.Add(property);
}

public bool Required { get; set; }
public EntityLayoutCondition<TSchema, TEntity> Condition { get; set; }
}
}
10 changes: 8 additions & 2 deletions src/Machete/Layouts/LayoutProperties/EntityLayoutProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Reflection;
using Internals.Reflection;
using Parsers;
using SchemaConfiguration;


public class EntityLayoutProperty<TLayout, TSchema, TEntity, TProperty> :
Expand All @@ -17,17 +18,22 @@ public class EntityLayoutProperty<TLayout, TSchema, TEntity, TProperty> :
readonly bool _required;
readonly Func<Entity<TEntity>, TProperty> _propertyConverter;
readonly IWriteProperty<TLayout, TProperty> _property;
readonly EntityLayoutCondition<TSchema, TEntity> _condition;

public EntityLayoutProperty(PropertyInfo property, bool required, Func<Entity<TEntity>, TProperty> propertyConverter)
public EntityLayoutProperty(PropertyInfo property, bool required, Func<Entity<TEntity>, TProperty> propertyConverter,
EntityLayoutCondition<TSchema, TEntity> condition = null)
{
_required = required;
_propertyConverter = propertyConverter;
_condition = condition;
_property = WritePropertyCache<TLayout>.GetProperty<TProperty>(property.Name);
}

public IParser<TSchema, LayoutMatch<TLayout>> CreateQuery(LayoutParserOptions options, IQueryBuilder<TSchema> queryBuilder)
{
IParser<TSchema, TEntity> parser = queryBuilder.Select<TEntity>().Parser;
IQueryParser<TSchema, TEntity> queryParser = queryBuilder.Select<TEntity>();

IParser<TSchema, TEntity> parser = _condition != null ? _condition(queryParser) : queryParser.Parser;
if (_required == false)
parser = parser.Optional();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Reflection;
using Internals.Reflection;
using Parsers;
using SchemaConfiguration;


public class EntityListLayoutProperty<TLayout, TSchema, TEntity, TProperty> :
Expand All @@ -17,11 +18,13 @@ public class EntityListLayoutProperty<TLayout, TSchema, TEntity, TProperty> :
readonly bool _required;
readonly Func<EntityList<TEntity>, TProperty> _propertyConverter;
readonly IWriteProperty<TLayout, TProperty> _property;
readonly EntityLayoutCondition<TSchema, TEntity> _condition;

public EntityListLayoutProperty(PropertyInfo property, bool required, Func<EntityList<TEntity>, TProperty> propertyConverter)
public EntityListLayoutProperty(PropertyInfo property, bool required, Func<EntityList<TEntity>, TProperty> propertyConverter, EntityLayoutCondition<TSchema, TEntity> condition)
{
_required = required;
_propertyConverter = propertyConverter;
_condition = condition;
_property = WritePropertyCache<TLayout>.GetProperty<TProperty>(property.Name);
}

Expand Down

0 comments on commit 010ac95

Please sign in to comment.