Skip to content

Commit

Permalink
Parsing des propriétés dans les from mapper
Browse files Browse the repository at this point in the history
  • Loading branch information
JabX committed Nov 25, 2023
1 parent 073925e commit 3756454
Show file tree
Hide file tree
Showing 15 changed files with 81 additions and 36 deletions.
1 change: 1 addition & 0 deletions TopModel.Core/FileModel/ModelFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ public class ModelFile
.ToList();

public IList<IProperty> Properties => Classes.SelectMany(c => c.Properties)
.Concat(Classes.SelectMany(c => c.FromMapperProperties))
.Concat(Endpoints.SelectMany(e => e.Params))
.Concat(Endpoints.Select(e => e.Returns))
.Concat(Decorators.SelectMany(e => e.Properties))
Expand Down
17 changes: 6 additions & 11 deletions TopModel.Core/Loaders/ClassLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,7 @@ public Class Load(Parser parser)
case "properties":
parser.ConsumeSequence(() =>
{
foreach (var property in _propertyLoader.Load(parser))
{
classe.Properties.Add(property);
}
classe.Properties.Add(_propertyLoader.Load(parser));
});
break;
case "unique":
Expand Down Expand Up @@ -138,7 +135,7 @@ public Class Load(Parser parser)
case "from":
parser.ConsumeSequence(() =>
{
var mapper = new FromMapper();
var mapper = new FromMapper { Class = classe };
classe.FromMappers.Add(mapper);

parser.ConsumeMapping(prop =>
Expand Down Expand Up @@ -196,12 +193,10 @@ public Class Load(Parser parser)
switch (prop.Value)
{
case "property":
foreach (var p in _propertyLoader.Load(parser))
{
var param = new PropertyMapping { Property = p };
mapper.Params.Add(param);
}

var p = _propertyLoader.Load(parser);
var param = new PropertyMapping { Property = p, FromMapper = mapper };
p.PropertyMapping = param;
mapper.Params.Add(param);
break;
case "target":
var targetReference = new Reference(parser.Consume<Scalar>());
Expand Down
5 changes: 1 addition & 4 deletions TopModel.Core/Loaders/DecoratorLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,7 @@ public Decorator Load(Parser parser)
case "properties":
parser.ConsumeSequence(() =>
{
foreach (var property in _propertyLoader.Load(parser))
{
decorator.Properties.Add(property);
}
decorator.Properties.Add(_propertyLoader.Load(parser));
});
break;
default:
Expand Down
11 changes: 4 additions & 7 deletions TopModel.Core/Loaders/EndpointLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,14 @@ public Endpoint Load(Parser parser)
case "params":
parser.ConsumeSequence(() =>
{
foreach (var property in _propertyLoader.Load(parser))
{
property.Endpoint = endpoint;
endpoint.Params.Add(property);
}
var property = _propertyLoader.Load(parser);
property.Endpoint = endpoint;
endpoint.Params.Add(property);
});
break;
case "returns":
endpoint.Returns = _propertyLoader.Load(parser).First();
endpoint.Returns = _propertyLoader.Load(parser);
endpoint.Returns.Endpoint = endpoint;
parser.Consume<MappingEnd>();
break;
case "decorators":
parser.ConsumeSequence(() =>
Expand Down
22 changes: 10 additions & 12 deletions TopModel.Core/Loaders/PropertyLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace TopModel.Core.Loaders;

public class PropertyLoader : ILoader<IEnumerable<IProperty>>
public class PropertyLoader : ILoader<IProperty>
{
private readonly ModelConfig _modelConfig;

Expand All @@ -14,7 +14,7 @@ public PropertyLoader(ModelConfig modelConfig)
}

/// <inheritdoc cref="ILoader{T}.Load" />
public IEnumerable<IProperty> Load(Parser parser)
public IProperty Load(Parser parser)
{
parser.Consume<MappingStart>();
switch (parser.Current)
Expand Down Expand Up @@ -67,8 +67,8 @@ public IEnumerable<IProperty> Load(Parser parser)
rp.Required = true;
}

yield return rp;
break;
parser.Consume<MappingEnd>();
return rp;

case Scalar { Value: "association" } s:
var ap = new AssociationProperty
Expand Down Expand Up @@ -136,8 +136,8 @@ public IEnumerable<IProperty> Load(Parser parser)
ap.Required = true;
}

yield return ap;
break;
parser.Consume<MappingEnd>();
return ap;

case Scalar { Value: "composition" } s:
var cp = new CompositionProperty
Expand Down Expand Up @@ -172,8 +172,8 @@ public IEnumerable<IProperty> Load(Parser parser)
}
}

yield return cp;
break;
parser.Consume<MappingEnd>();
return cp;

case Scalar { Value: "alias" } s:
var aliasReference = new AliasReference();
Expand Down Expand Up @@ -274,13 +274,11 @@ public IEnumerable<IProperty> Load(Parser parser)
}

alp.Reference = aliasReference;
yield return alp;
break;
parser.Consume<MappingEnd>();
return alp;

default:
throw new ModelException($"Type de propriété inconnu.");
}

parser.Consume<MappingEnd>();
}
}
2 changes: 2 additions & 0 deletions TopModel.Core/Model/AliasProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public IFieldProperty Property

public Decorator Decorator { get; set; }

public PropertyMapping PropertyMapping { get; set; }

#nullable enable

public LocatedString? Trigram { get; set; }
Expand Down
2 changes: 2 additions & 0 deletions TopModel.Core/Model/AssociationProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public IFieldProperty Property
public Endpoint Endpoint { get; set; }

public Decorator Decorator { get; set; }

public PropertyMapping PropertyMapping { get; set; }
#nullable enable

public string? Role { get; set; }
Expand Down
2 changes: 2 additions & 0 deletions TopModel.Core/Model/Class.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ public class Class : IPropertyContainer

public List<FromMapper> FromMappers { get; } = new();

public IEnumerable<IProperty> FromMapperProperties => FromMappers.SelectMany(fm => fm.PropertyParams.Select(pp => pp.Property));

public List<ClassMappings> ToMappers { get; } = new();

public string PluralName
Expand Down
2 changes: 2 additions & 0 deletions TopModel.Core/Model/CompositionProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ public class CompositionProperty : IProperty

public Decorator Decorator { get; set; }

public PropertyMapping PropertyMapping { get; set; }

public string Label => Name;

public bool PrimaryKey => false;
Expand Down
3 changes: 2 additions & 1 deletion TopModel.Core/Model/FromMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ namespace TopModel.Core;

public class FromMapper
{
#nullable enable
public string? Comment { get; set; }

public List<OneOf<ClassMappings, PropertyMapping>> Params { get; } = new();
Expand All @@ -14,5 +13,7 @@ public class FromMapper
public IEnumerable<PropertyMapping> PropertyParams => Params.Where(p => p.IsT1).Select(p => p.AsT1);

#nullable disable
public Class Class { get; set; }

internal LocatedString Reference { get; set; }
}
2 changes: 2 additions & 0 deletions TopModel.Core/Model/IProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public interface IProperty

Decorator Decorator { get; set; }

PropertyMapping PropertyMapping { get; set; }

IPropertyContainer Parent => Class ?? (IPropertyContainer)Endpoint ?? Decorator;

IProperty CloneWithClassOrEndpoint(Class? classe = null, Endpoint? endpoint = null);
Expand Down
2 changes: 2 additions & 0 deletions TopModel.Core/Model/PropertyMapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ public class PropertyMapping
public IProperty TargetProperty { get; set; }

public Reference TargetPropertyReference { get; set; }

public FromMapper FromMapper { get; set; }
}
2 changes: 2 additions & 0 deletions TopModel.Core/Model/RegularProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public class RegularProperty : IFieldProperty

public Decorator Decorator { get; set; }

public PropertyMapping PropertyMapping { get; set; }

public DomainReference DomainReference { get; set; }

#nullable enable
Expand Down
3 changes: 3 additions & 0 deletions TopModel.Core/ModelExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public static class ModelExtensions
public static IEnumerable<(ClassReference Reference, ModelFile File)> GetClassReferences(this ModelStore modelStore, Class classe)
{
return modelStore.Classes.SelectMany(c => c.Properties)
.Concat(modelStore.Classes.SelectMany(c => c.FromMapperProperties))
.Concat(modelStore.Endpoints.SelectMany(e => e.Properties))
.Concat(modelStore.Decorators.SelectMany(d => d.Properties))
.Where(p =>
Expand Down Expand Up @@ -54,6 +55,7 @@ public static class ModelExtensions
public static IEnumerable<(DomainReference Reference, ModelFile File)> GetDomainReferences(this ModelStore modelStore, Domain domain)
{
return modelStore.Classes.SelectMany(c => c.Properties)
.Concat(modelStore.Classes.SelectMany(c => c.FromMapperProperties))
.Concat(modelStore.Decorators.SelectMany(c => c.Properties))
.Concat(modelStore.Endpoints.SelectMany(e => e.Properties))
.Where(p =>
Expand Down Expand Up @@ -86,6 +88,7 @@ public static ModelFile GetFile(this object? objet)
IProperty { Decorator: Decorator decorator } => decorator.ModelFile,
IProperty { Class: Class classe } => classe.ModelFile,
IProperty { Endpoint: Endpoint endpoint } => endpoint.ModelFile,
IProperty { PropertyMapping: PropertyMapping param } => param.FromMapper.Class.ModelFile,
Domain domain => domain.ModelFile,
Converter converter => converter.ModelFile,
Decorator decorator => decorator.ModelFile,
Expand Down
41 changes: 40 additions & 1 deletion TopModel.Core/ModelStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -670,13 +670,32 @@ private IEnumerable<ModelError> ResolveReferences(ModelFile modelFile)
if (alp.OriginalAliasProperty is not null)
{
var index = classe.Properties.IndexOf(alp);
classe.Properties.RemoveAt(classe.Properties.IndexOf(alp));
classe.Properties.RemoveAt(index);
if (!classe.Properties.Contains(alp.OriginalAliasProperty))
{
classe.Properties.Insert(index, alp.OriginalAliasProperty);
}
}
}

foreach (var alp in classe.FromMapperProperties.OfType<AliasProperty>().ToList())
{
if (alp.OriginalAliasProperty is not null)
{
var index = alp.PropertyMapping.FromMapper.Params.IndexOf(alp.PropertyMapping);
alp.PropertyMapping.FromMapper.Params.RemoveAt(index);
if (!alp.PropertyMapping.FromMapper.Params.Contains(alp.OriginalAliasProperty.PropertyMapping))
{
alp.PropertyMapping.FromMapper.Params.Insert(index, new PropertyMapping
{
FromMapper = alp.PropertyMapping.FromMapper,
Property = alp.OriginalAliasProperty,
TargetProperty = alp.PropertyMapping.TargetProperty,
TargetPropertyReference = alp.PropertyMapping.TargetPropertyReference
});
}
}
}
}

// Reset des alias déjà résolus sur les endpoints.
Expand Down Expand Up @@ -803,6 +822,22 @@ IEnumerable<ModelError> ResolveAliases(IEnumerable<AliasProperty> alps)
alp.Decorator.Properties.Insert(index + 1, prop);
}
}
else if (alp.PropertyMapping != null)
{
var index = alp.PropertyMapping.FromMapper.Params.IndexOf(alp.PropertyMapping);
if (index >= 0)
{
var mapping = new PropertyMapping
{
FromMapper = alp.PropertyMapping.FromMapper,
Property = prop,
TargetProperty = alp.PropertyMapping.TargetProperty,
TargetPropertyReference = alp.PropertyMapping.TargetPropertyReference,
};
prop.PropertyMapping = mapping;
alp.PropertyMapping.FromMapper.Params.Insert(index + 1, mapping);
}
}
}

if (alp.Class != null)
Expand All @@ -813,6 +848,10 @@ IEnumerable<ModelError> ResolveAliases(IEnumerable<AliasProperty> alps)
{
alp.Endpoint.Params.Remove(alp);
}
else if (alp.PropertyMapping != null)
{
alp.PropertyMapping.FromMapper.Params.Remove(alp.PropertyMapping);
}
else
{
alp.Decorator?.Properties.Remove(alp);
Expand Down

0 comments on commit 3756454

Please sign in to comment.