Delphi-RAG-Lint

drag-lint help

Enum helper generator: repo enum-gen

Since: 1.64.0-alpha

Generates Delphi enums with record helpers and DUnitX tests from the enum candidates; ordinal = stored code, fillers for gaps, out of range raises.

Read this to turn the enum-candidates report into code. drag-lint repo enum-gen --in candidates.json --output Orm3Enums.pas writes Unit.pas (an enum and a record helper per closed code set: ToByte/FromByte, ToInteger/FromInteger, ToString/FromString, ToCaption, IsValid, TryFrom*) and UnitTests.pas (a DUnitX round trip). The ordinal is the stored code, gaps become named fillers, a value out of range raises E<Unit>EnumRange (one exception class per generated unit), existing legacy enums are skipped and database values outside the set are reported.

How to run it

drag-lint repo enum-gen [--in <enum-candidates.json> | --db <index> ... | --project <x.dproj>] [--repo <file.dlrepo>] [--connection "<firebird>"] [--fields "A,B"] [--legacy <file.pas|dir> ...] [--output <Unit.pas>]

Full flag list: repo in the CLI reference.

Flags

FlagArgumentMeaningDefault
--in<enum-candidates.json>
--db<index>
--project<x.dproj>
--repo<file.dlrepo>
--connection"<firebird>"
--fields"A,B"
--legacy<file.pas|dir>
--output<Unit.pas>

Where to click

No IDE surface: this feature runs from the CLI (see How to run it).

Details

Since 1.64.0-alpha. ORM3 enum tool: turns the closed code sets that repo enum-candidates finds into Delphi enum types with record helpers, plus DUnitX round-trip tests. Read only on every input; it writes only the unit you name and its Tests unit.

drag-lint repo enum-gen --in <enum-candidates.json> [--legacy <file.pas|dir> ...] --output <Unit.pas>
drag-lint repo enum-gen --db <client.sqlite> [--db <server.sqlite> ...] [--repo x.dlrepo] [--connection "<firebird>"]
                        [--fields "STATUS,KIND"] [--legacy <file.pas|dir> ...] --output <Unit.pas>

--in is a file saved from repo enum-candidates --json; without it the verb collects the candidates now from --db / --project (same engine, same options). Without --output the unit goes to standard output and the report to standard error. With it, <Unit>.pas and <Unit>Tests.pas are written next to each other.

What is generated

Per accepted candidate, in one unit:

  • TStatus = (Status_Undefined, Status_Filler1, ..., Status_Active, ...) declared WITHOUT explicit values (explicit values lose RTTI). The ordinal is the stored code: a CHAR(1) code 'A' is ordinal 65 (Ord('A')); integers are decimal; a negative code is its two's-complement byte (-1 = 255). Ordinal 0 not used by a code is <Name>_Undefined; every other gap is <Name>_Filler<n>. Fillers are never valid.
  • Member names: the aligned DFM caption (Status_Active), else the code (Status_A, Kind_N3, Level_M1, Sign_C43 for a non-alphanumeric character); a clash (names are case-insensitive) gets _<ordinal>.
  • TStatusHelper = record helper for TStatus: FromByte, TryFromByte, FromInteger, FromString, TryFromString (class, static), ToByte, ToInteger, ToString, ToCaption, IsValid. ToString / FromString is the one-character code (text) or the decimal code (integer). ToCaption is the DFM caption of the code, else the code text.
  • Out of range raises. FromByte / FromInteger / FromString raise EEnumRange (declared once in the unit) for a filler, _Undefined or a value past the last member; TryFrom* return False. Nothing maps to Low(T) (owner rule: a value outside the enum is a bug). Database values outside the code set are listed in the report (OUT OF SET in the database (a bug, not added): Z=1), never added to the enum.
  • <Unit>Tests.pas: per enum a DUnitX fixture: byte / integer round trip, text round trip, fillers invalid, out of range raises, and a TComponent stream round trip (WriteComponent / ReadComponent of a published property of the enum).
  • Output is 7-bit ASCII, CRLF, DocInsight-commented.

What is skipped (listed with the reason, exit 0)

  • low-confidence candidates;
  • a text code that is not one printable 7-bit character ('OP', 'CL'): it needs an owner map to a byte;
  • an integer code outside -128..255, or two codes with the same byte (-1 and 255);
  • a type that already exists in --legacy as an enum or a record helper for (e.g. TTktVerd in MSCTYPES.PAS): the legacy helper is kept, nothing is generated. The type name is the repository ENUMTYPE of the column when it has one, else derived from the identity (FIELD:STATUS -> TStatus, SYM:U.TC.FMode -> TMode).

Not decided here (open owner questions): per-enum range / undefined / policy metadata for legacy enums, and the parameter name (pValue is used, the project rule).

Source: src\sql\DRagLint.FieldRepo.EnumGen.pas (pure), src\cli\DRagLint.CLI.RepoEnumGen.pas. Test: tests\autotest\run_v164_enumgen.ps1 (compiles the generator, compiles the GENERATED unit with its generated DUnitX tests and runs them).


All features | CLI reference | IDE menus