From d51c93a867dbcb5bf9b9d79c816f0ad2bfc0b3a6 Mon Sep 17 00:00:00 2001 From: Damien Date: Thu, 12 Dec 2024 16:17:05 +0100 Subject: [PATCH] `className` sur association (fix #374) --- TopModel.Core/Loaders/PropertyLoader.cs | 3 +++ TopModel.Core/Model/AssociationProperty.cs | 20 +++++++++++++++----- TopModel.Core/schema.json | 4 ++++ docs/model/properties.md | 2 +- samples/model/angular.topmodel.lock | 2 +- samples/model/focus.topmodel.lock | 2 +- 6 files changed, 25 insertions(+), 8 deletions(-) diff --git a/TopModel.Core/Loaders/PropertyLoader.cs b/TopModel.Core/Loaders/PropertyLoader.cs index a5b3fb91..3ec00ac6 100644 --- a/TopModel.Core/Loaders/PropertyLoader.cs +++ b/TopModel.Core/Loaders/PropertyLoader.cs @@ -129,6 +129,9 @@ public IProperty Load(Parser parser) case "trigram": ap.Trigram = new LocatedString(value); break; + case "className": + ap.ClassName = value!.Value; + break; case "customProperties": parser.ConsumeMapping(prop => ap.CustomProperties.Add(prop.Value, parser.Consume().Value)); break; diff --git a/TopModel.Core/Model/AssociationProperty.cs b/TopModel.Core/Model/AssociationProperty.cs index a8f7a15a..46192b1e 100644 --- a/TopModel.Core/Model/AssociationProperty.cs +++ b/TopModel.Core/Model/AssociationProperty.cs @@ -10,6 +10,8 @@ public class AssociationProperty : IProperty public LocatedString? Trigram { get; set; } + public string? ClassName { get; set; } + #nullable disable public Class Association { get; set; } @@ -77,7 +79,11 @@ public string Name var name = new StringBuilder(); - if (Type == AssociationType.OneToMany || Type == AssociationType.ManyToMany) + if (ClassName != null) + { + name.Append(ClassName); + } + else if (Type == AssociationType.OneToMany || Type == AssociationType.ManyToMany) { name.Append(Association.PluralName); } @@ -116,7 +122,11 @@ public string NameCamel var name = new StringBuilder(); - if (Type == AssociationType.OneToMany || Type == AssociationType.ManyToMany) + if (ClassName != null) + { + name.Append(ClassName.ToCamelCase(strictIfUppercase: true)); + } + else if (Type == AssociationType.OneToMany || Type == AssociationType.ManyToMany) { name.Append(Association.PluralNameCamel); } @@ -127,7 +137,7 @@ public string NameCamel if (Type == AssociationType.ManyToOne || Type == AssociationType.OneToOne) { - if (Association.Extends == null || !Association.PrimaryKey.Any()) + if (Association.Extends == null || !Association.PrimaryKey.Any() || ClassName != null) { name.Append(Property?.NameCamel.ToFirstUpper()); } @@ -148,9 +158,9 @@ public string NameCamel public string NamePascal => ((IProperty)this).Parent.PreservePropertyCasing ? Name : NameCamel.ToFirstUpper(); - public string NameByClassPascal => Type.IsToMany() ? $"{NamePascal}" : $"{Association.NamePascal}{Role?.ToPascalCase() ?? string.Empty}"; + public string NameByClassPascal => Type.IsToMany() ? $"{NamePascal}" : $"{ClassName?.ToPascalCase(strictIfUppercase: true) ?? Association.NamePascal}{Role?.ToPascalCase() ?? string.Empty}"; - public string NameByClassCamel => Type.IsToMany() ? $"{NameCamel}" : $"{Association.NameCamel}{Role?.ToPascalCase() ?? string.Empty}"; + public string NameByClassCamel => Type.IsToMany() ? $"{NameCamel}" : $"{ClassName?.ToCamelCase(strictIfUppercase: true) ?? Association.NameCamel}{Role?.ToPascalCase() ?? string.Empty}"; public Domain Domain => Type.IsToMany() && (Property?.Domain?.AsDomains.TryGetValue(As, out var ld) ?? false) ? ld : Property?.Domain!; diff --git a/TopModel.Core/schema.json b/TopModel.Core/schema.json index 063b83b4..570c8970 100644 --- a/TopModel.Core/schema.json +++ b/TopModel.Core/schema.json @@ -158,6 +158,10 @@ "type": "string", "description": "Surcharge locale du trigram" }, + "className": { + "type": "string", + "description": "Surcharge du nom de la classe cible dans le nom de la propriété d'association." + }, "property": { "type": "string", "description": "Propriété de la classe cible à utiliser pour la clé étrangère (au lieu de la clé primaire)." diff --git a/docs/model/properties.md b/docs/model/properties.md index 1717c396..2aa28374 100644 --- a/docs/model/properties.md +++ b/docs/model/properties.md @@ -53,7 +53,7 @@ comment: C'est une FK obligatoire role: Exemple ``` -La propriété qui en découlera sera `ClasseCibleExempleId` (si la `primaryKey` de `ClasseCible` est `Id`). +La propriété qui en découlera sera `ClasseCibleExempleId` (si la `primaryKey` de `ClasseCible` est `Id`). Il est possible de surcharger le nom de la classe cible dans la propriété (donc ici `ClasseCible`) via la propriété `className`. Une association peut référencer une classe non persistée, dans ce cas il faut identifier la propriété de la classe cible à utiliser via `property` (puisqu'une telle classe ne peut pas avoir de clé primaire par définition). diff --git a/samples/model/angular.topmodel.lock b/samples/model/angular.topmodel.lock index d9b1a778..b22934d1 100644 --- a/samples/model/angular.topmodel.lock +++ b/samples/model/angular.topmodel.lock @@ -4,7 +4,7 @@ version: 2.2.1 custom: - ../../../TopModel.Generator.Javascript: 427ee5726835e07eed75a535e18ef04c + ../../../TopModel.Generator.Javascript: 2532588a5ca4f3312ebc30f105cc84fc generatedFiles: - ../generators/angular/src/appgenerated/api/securite/profil/profil.service.ts - ../generators/angular/src/appgenerated/api/securite/utilisateur/utilisateur.service.ts diff --git a/samples/model/focus.topmodel.lock b/samples/model/focus.topmodel.lock index d1d304b5..de8367e5 100644 --- a/samples/model/focus.topmodel.lock +++ b/samples/model/focus.topmodel.lock @@ -4,7 +4,7 @@ version: 2.2.1 custom: - ../../../TopModel.Generator.Javascript: 427ee5726835e07eed75a535e18ef04c + ../../../TopModel.Generator.Javascript: 2532588a5ca4f3312ebc30f105cc84fc generatedFiles: - ../generators/focus/src/locale/common.ts - ../generators/focus/src/locale/securite.ts