|
7 | 7 | "regexp" |
8 | 8 | "strings" |
9 | 9 |
|
| 10 | + "github.com/mendixlabs/mxcli/mdl/ast" |
10 | 11 | mdlerrors "github.com/mendixlabs/mxcli/mdl/errors" |
| 12 | + "github.com/mendixlabs/mxcli/mdl/linter" |
11 | 13 | "github.com/mendixlabs/mxcli/model" |
12 | 14 | "github.com/mendixlabs/mxcli/sdk/domainmodel" |
13 | 15 | ) |
@@ -420,3 +422,111 @@ func validateViewAssociationNames(ctx *ExecContext, moduleName, viewEntityName, |
420 | 422 | } |
421 | 423 | return out |
422 | 424 | } |
| 425 | + |
| 426 | +// viewAttributeIsObjectRule refuses a view entity attribute that tries to hold |
| 427 | +// an object: one declared for an association column, or typed with an entity. |
| 428 | +const viewAttributeIsObjectRule = "MDL080" |
| 429 | + |
| 430 | +// ValidateViewAttributeDeclarations reports declared attributes that cannot be |
| 431 | +// attributes (ako/view-entity-examples FINDINGS §4), from the script alone. |
| 432 | +// |
| 433 | +// Writing the association column as a declared attribute — `MeterRef: |
| 434 | +// Trends.Meter` or `MeterRef: Trends.Meter.ID` beside `select m.ID as MeterRef` |
| 435 | +// — parses, because a bare qualified name is how MDL spells an enumeration |
| 436 | +// type. It was stored as exactly that, next to the association the column |
| 437 | +// already declares. Measured on 11.14.0: the two-part form is CE1613 "The |
| 438 | +// selected enumeration 'Trends.Meter' no longer exists"; the three-part form |
| 439 | +// makes mx check throw "An error occurred when trying to set the 'Enumeration' |
| 440 | +// property" before it validates anything. And `check -p` could not say so: the |
| 441 | +// column is not an attribute, so it reported "1 columns but 2 attributes" and |
| 442 | +// compared the declaration against the NEXT column's type. |
| 443 | +// |
| 444 | +// Two shapes are decidable without a project: a declared name that is also an |
| 445 | +// association column's alias, and a three-part type name, which no enumeration |
| 446 | +// has. An entity-typed attribute under any other name needs the project to tell |
| 447 | +// an entity from an enumeration — see viewAttributeEntityTypeErrors. |
| 448 | +func ValidateViewAttributeDeclarations(oql string, attrs []ast.ViewAttribute) []linter.Violation { |
| 449 | + assocByName := map[string]viewAssociationColumn{} |
| 450 | + for _, c := range viewAssociationColumns(oql) { |
| 451 | + assocByName[strings.ToLower(c.Name)] = c |
| 452 | + } |
| 453 | + var out []linter.Violation |
| 454 | + for _, a := range attrs { |
| 455 | + if c, ok := assocByName[strings.ToLower(a.Name)]; ok { |
| 456 | + out = append(out, viewAttributeViolation(fmt.Sprintf( |
| 457 | + "attribute '%s' cannot be declared: the select column '%s as %s' already makes %s an "+ |
| 458 | + "association to %s, and an association is not an attribute", |
| 459 | + a.Name, c.Expr, c.Name, c.Name, c.Entity), |
| 460 | + fmt.Sprintf("Remove '%s' from the attribute list — the column is the whole declaration", a.Name))) |
| 461 | + continue |
| 462 | + } |
| 463 | + if ref := enumRefOf(a.Type); ref != nil && strings.Contains(ref.Name, ".") { |
| 464 | + out = append(out, viewAttributeViolation(fmt.Sprintf( |
| 465 | + "attribute '%s': '%s' is not a type — an attribute cannot hold an object or its id", |
| 466 | + a.Name, ref.String()), |
| 467 | + objectAttributeAdvice(a.Name))) |
| 468 | + } |
| 469 | + } |
| 470 | + return out |
| 471 | +} |
| 472 | + |
| 473 | +// viewAttributeEntityTypeErrors reports attributes typed with a name that is an |
| 474 | +// entity rather than an enumeration. It needs the project (or the script's own |
| 475 | +// entities) to tell the two apart, which the parser cannot. |
| 476 | +func viewAttributeEntityTypeErrors(ctx *ExecContext, oql string, attrs []ast.ViewAttribute, scriptEntities map[string]bool) []string { |
| 477 | + if ctx == nil || !ctx.Connected() { |
| 478 | + return nil |
| 479 | + } |
| 480 | + // An association column's name is already refused by MDL080, whatever its |
| 481 | + // type; reporting it twice would read as two problems. |
| 482 | + associationNames := map[string]bool{} |
| 483 | + for _, c := range viewAssociationColumns(oql) { |
| 484 | + associationNames[strings.ToLower(c.Name)] = true |
| 485 | + } |
| 486 | + var entities map[string]bool |
| 487 | + var out []string |
| 488 | + for _, a := range attrs { |
| 489 | + ref := enumRefOf(a.Type) |
| 490 | + if ref == nil || ref.Module == "" || strings.Contains(ref.Name, ".") || associationNames[strings.ToLower(a.Name)] { |
| 491 | + continue |
| 492 | + } |
| 493 | + qn := ref.String() |
| 494 | + if entities == nil { |
| 495 | + entities = buildEntityQualifiedNames(ctx) |
| 496 | + } |
| 497 | + if !scriptEntities[qn] && !entities[qn] { |
| 498 | + continue |
| 499 | + } |
| 500 | + if enumerationExists(ctx, qn) { |
| 501 | + continue |
| 502 | + } |
| 503 | + out = append(out, fmt.Sprintf( |
| 504 | + "attribute '%s': %s is an entity, not an enumeration — an attribute cannot hold an object "+ |
| 505 | + "(it would be stored as Enumeration(%s), which fails the build with CE1613). %s", |
| 506 | + a.Name, qn, qn, objectAttributeAdvice(a.Name))) |
| 507 | + } |
| 508 | + return out |
| 509 | +} |
| 510 | + |
| 511 | +func enumRefOf(dt ast.DataType) *ast.QualifiedName { |
| 512 | + if dt.Kind != ast.TypeEnumeration { |
| 513 | + return nil |
| 514 | + } |
| 515 | + return dt.EnumRef |
| 516 | +} |
| 517 | + |
| 518 | +func objectAttributeAdvice(name string) string { |
| 519 | + return fmt.Sprintf("For an association, select the entity's id under this name and do not declare it: "+ |
| 520 | + "`select m.ID as %s` (m being the entity's alias in the FROM/JOIN). For the id as text, "+ |
| 521 | + "declare '%s: String' and select `cast(m.ID as string) as %s`", name, name, name) |
| 522 | +} |
| 523 | + |
| 524 | +func viewAttributeViolation(msg, suggestion string) linter.Violation { |
| 525 | + return linter.Violation{ |
| 526 | + RuleID: viewAttributeIsObjectRule, |
| 527 | + Severity: linter.SeverityError, |
| 528 | + Message: msg, |
| 529 | + Location: linter.Location{DocumentType: "viewentity"}, |
| 530 | + Suggestion: suggestion, |
| 531 | + } |
| 532 | +} |
0 commit comments