Skip to content

Commit

Permalink
Mappers 'from' : Retrait mapping "this"
Browse files Browse the repository at this point in the history
  • Loading branch information
JabX committed Nov 25, 2023
1 parent fde101e commit 073925e
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 40 deletions.
2 changes: 1 addition & 1 deletion TopModel.Core/FileModel/ModelFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public class ModelFile
.Concat(Classes.SelectMany(c => c.UniqueKeyReferences.SelectMany(uk => uk).Select(propRef => (propRef, (object)c.Properties.FirstOrDefault(p => p.Name == propRef.ReferenceName)))))
.Concat(Classes.SelectMany(c => c.ValueReferences.SelectMany(rv => rv.Value).Select(prop => (prop.Key, (object)c.ExtendedProperties.FirstOrDefault(p => p.Name == prop.Key.ReferenceName)))))
.Concat(Classes.SelectMany(c => c.FromMappers.SelectMany(m => m.ClassParams).Concat(c.ToMappers)).Select(p => (p.ClassReference as Reference, (object)p.Class)))
.Concat(Classes.SelectMany(c => c.FromMappers.SelectMany(m => m.ClassParams).Concat(c.ToMappers).SelectMany(m => m.MappingReferences.SelectMany(mr => new[] { (mr.Key, (object)c.ExtendedProperties.FirstOrDefault(k => k.Name == mr.Key.ReferenceName)), (mr.Value, mr.Value.ReferenceName == "this" || mr.Value.ReferenceName == "false" ? new Keyword { ModelFile = c.ModelFile } : m.Mappings.Values.FirstOrDefault(k => k.Name == mr.Value.ReferenceName)) }))))
.Concat(Classes.SelectMany(c => c.FromMappers.SelectMany(m => m.ClassParams).Concat(c.ToMappers).SelectMany(m => m.MappingReferences.SelectMany(mr => new[] { (mr.Key, (object)c.ExtendedProperties.FirstOrDefault(k => k.Name == mr.Key.ReferenceName)), (mr.Value, mr.Value.ReferenceName == "false" ? new Keyword { ModelFile = c.ModelFile } : m.Mappings.Values.FirstOrDefault(k => k.Name == mr.Value.ReferenceName)) }))))
.Concat(Converters.SelectMany(c => c.DomainsFromReferences.Select(d => (d as Reference, c.From.FirstOrDefault(dom => dom.Name == d.ReferenceName) as object))))
.Concat(Converters.SelectMany(c => c.DomainsToReferences.Select(d => (d as Reference, c.To.FirstOrDefault(dom => dom.Name == d.ReferenceName) as object))))
.Concat(DataFlows.Select(d => (d.ClassReference as Reference, d.Class as object)))
Expand Down
2 changes: 1 addition & 1 deletion TopModel.Core/Model/ClassMappings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class ClassMappings
#nullable enable
public string? Comment { get; set; }

public Dictionary<IProperty, IFieldProperty?> Mappings { get; } = new();
public Dictionary<IProperty, IFieldProperty> Mappings { get; } = new();

public Dictionary<Reference, Reference> MappingReferences { get; } = new();
}
16 changes: 1 addition & 15 deletions TopModel.Core/ModelStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1099,20 +1099,6 @@ IEnumerable<ModelError> ResolveAliases(IEnumerable<AliasProperty> alps)
continue;
}

if (currentProperty != null && mapping.Value.ReferenceName == "this")
{
if (currentProperty is CompositionProperty cp && cp.Composition == mappedClass)
{
mappings.Mappings.Add(currentProperty, null);
}
else
{
yield return new ModelError(classe, $"La classe '{mappedClass.Name}' ne peut pas être mappée sur la propriété '{currentProperty.Name}' car ce n'est pas une composition de cette classe.", mapping.Value) { ModelErrorType = ModelErrorType.TMD1020 };
}

continue;
}

var mappedProperty = mappedClass.ExtendedProperties.OfType<IFieldProperty>().FirstOrDefault(p => p.Name == mapping.Value.ReferenceName);
if (mappedProperty == null)
{
Expand Down Expand Up @@ -1287,7 +1273,7 @@ IEnumerable<ModelError> ResolveAliases(IEnumerable<AliasProperty> alps)
{
foreach (var mapper in classe.FromMappers)
{
if (!mapper.ClassParams.SelectMany(p => p.Mappings).Any())
if (!mapper.ClassParams.SelectMany(p => p.Mappings).Any() && !mapper.PropertyParams.Any())
{
yield return new ModelError(classe, "Aucun mapping n'a été trouvé sur ce mapper.", mapper.GetLocation()) { ModelErrorType = ModelErrorType.TMD1025 };
}
Expand Down
37 changes: 15 additions & 22 deletions TopModel.Generator.Csharp/MapperGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,29 +127,22 @@ protected override void HandleFile(string fileName, string tag, IList<(Class Cla
w.Write(3, $"{mapping.Key.NamePascal} = ");
}

if (mapping.Value == null)
var value = $"{param.Name}{(!param.Required && mapping.Key is not CompositionProperty ? "?" : string.Empty)}.{mapping.Value.NamePascal}";

if (mapping.Key is CompositionProperty cp)
{
w.Write(param.Name);
w.Write($"{(!param.Required ? $"{param.Name} is null ? null : " : string.Empty)}new() {{ {cp.Composition.PrimaryKey.SingleOrDefault()?.NamePascal} = ");
}
else
{
var value = $"{param.Name}{(!param.Required && mapping.Key is not CompositionProperty ? "?" : string.Empty)}.{mapping.Value.NamePascal}";

if (mapping.Key is CompositionProperty cp)
{
w.Write($"{(!param.Required ? $"{param.Name} is null ? null : " : string.Empty)}new() {{ {cp.Composition.PrimaryKey.SingleOrDefault()?.NamePascal} = ");
}
else
{
value = Config.GetConvertedValue(value, mapping.Value?.Domain, (mapping.Key as IFieldProperty)?.Domain);
}

w.Write(value);

if (mapping.Key is CompositionProperty)
{
w.Write(" }");
}
value = Config.GetConvertedValue(value, mapping.Value.Domain, (mapping.Key as IFieldProperty)?.Domain);
}

w.Write(value);

if (mapping.Key is CompositionProperty)
{
w.Write(" }");
}

if (mapper.Params.IndexOf(param) < mapper.ClassParams.Count() - 1 || mappings.IndexOf(mapping) < mappings.Count - 1)
Expand Down Expand Up @@ -227,11 +220,11 @@ static string GetSourceMapping(IProperty property)
var mappings = mapper.Mappings.ToList();
foreach (var mapping in mapper.Mappings)
{
var value = Config.GetConvertedValue($"source.{GetSourceMapping(mapping.Key)}", (mapping.Key as IFieldProperty)?.Domain, mapping.Value?.Domain);
var value = Config.GetConvertedValue($"source.{GetSourceMapping(mapping.Key)}", (mapping.Key as IFieldProperty)?.Domain, mapping.Value.Domain);

if (mapper.Class.Abstract)
{
w.Write(3, $"{mapping.Value?.NameCamel}: {value}");
w.Write(3, $"{mapping.Value.NameCamel}: {value}");

if (mappings.IndexOf(mapping) < mappings.Count - 1)
{
Expand All @@ -244,7 +237,7 @@ static string GetSourceMapping(IProperty property)
}
else
{
w.WriteLine(2, $"dest.{mapping.Value?.NamePascal} = {value};");
w.WriteLine(2, $"dest.{mapping.Value.NamePascal} = {value};");
}
}

Expand Down
2 changes: 1 addition & 1 deletion TopModel.LanguageServer/SemanticTokensHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ protected override Task Tokenize(SemanticTokensBuilder builder, ITextDocumentIde
ClassReference or DecoratorReference => SemanticTokenType.Class,
DataFlowReference => SemanticTokenType.Operator,
DomainReference => SemanticTokenType.EnumMember,
Reference r when r.ReferenceName == "this" || r.ReferenceName == "false" => SemanticTokenType.Keyword,
Reference r when r.ReferenceName == "false" => SemanticTokenType.Keyword,
_ => SemanticTokenType.Function
};

Expand Down

0 comments on commit 073925e

Please sign in to comment.