Skip to content

Commit

Permalink
className sur association (fix #374)
Browse files Browse the repository at this point in the history
  • Loading branch information
JabX committed Dec 12, 2024
1 parent 0ed5388 commit d51c93a
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 8 deletions.
3 changes: 3 additions & 0 deletions TopModel.Core/Loaders/PropertyLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Scalar>().Value));
break;
Expand Down
20 changes: 15 additions & 5 deletions TopModel.Core/Model/AssociationProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }

Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand All @@ -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());
}
Expand All @@ -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!;

Expand Down
4 changes: 4 additions & 0 deletions TopModel.Core/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -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)."
Expand Down
2 changes: 1 addition & 1 deletion docs/model/properties.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).

Expand Down
2 changes: 1 addition & 1 deletion samples/model/angular.topmodel.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion samples/model/focus.topmodel.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d51c93a

Please sign in to comment.