Skip to content

xml: make -q honor TableMapping when -n is not set#67

Open
kroexov wants to merge 3 commits into
vmkteam:masterfrom
kroexov:fix/quiet-respects-table-mapping
Open

xml: make -q honor TableMapping when -n is not set#67
kroexov wants to merge 3 commits into
vmkteam:masterfrom
kroexov:fix/quiet-respects-table-mapping

Conversation

@kroexov

@kroexov kroexov commented May 14, 2026

Copy link
Copy Markdown

Problem

in v 0.3.0. the -q flag was added to mfd-generator

-q, --quiet string quiet mode. ignored when --namespaces (-n) flag is set. possible values:
- all - will use namespace entity mapping from mfd, entities not present in mfd file will be ignored
- new - generator will prompt namespace for entities not present in mfd file

I expected to use this flag to generate new tables, which do not exist in tableMapping, but found that this does not work:

mfd-xml-new:
@mfd-generator xml -c "...connection..." -m ./docs/model/apisrv.mfd -t public.* -q new

Summary

The generator has two ways to learn which namespace a table belongs to:

  • -n (--namespaces) — an explicit mapping you pass on the command line.
  • TableMapping — a mapping already stored in the .mfd file.

The old code merged both into one Packages field and only checked whether that field was empty. On a real project the .mfd file almost always has a TableMapping, so this check was always true and -q new behaved exactly like -q all: it quietly reused the existing mapping and never prompted for new tables.

This PR keeps the two sources apart:

  • options.Packages now means strictly "came from -n".
  • TableMapping from the .mfd file is read separately and combined with the -q mode.

The resulting behaviour:

flags what happens
-n set -q is ignored (as the help promises): use the -n mapping, skip everything else.
-q all, no -n use TableMapping and entities already in the project, skip unmapped tables.
-q new, no -n use TableMapping and existing entities, and prompt for the rest.
neither prompt for every table.

Table selection for the database read was adjusted too: with -q new or the default mode and no -t, the generator now reads public.*. Without this the prompt could never run when TableMapping was not empty.

All of these rules now live in one small pure function, decideNamespace, which makes them easy to read and to test.

The xml generator's README is also updated: the -q/--quiet flag is now documented, and the CLI help snapshot is refreshed (it was missing -g, -q and --custom-types).

Notes

A note on testing.

The decision logic is fully covered by decideNamespace: it takes plain inputs (the flags, the table mapping, the current table) and returns one of three outcomes — assign a namespace, skip the table, or prompt. The tests call this function directly, so they need no database and no fake prompt. The interactive PromptNS stays a thin wrapper around promptui with no logic worth mocking, and no test-only code is left in the production path.

While moving that logic out, the long Generate method was split as well — the per-entity loop is now fillNamespaces. As a side effect this clears a pre-existing golangci-lint complaint about Generate being too complex, so the package lints cleanly.

kroexov added 3 commits May 14, 2026 13:23
The help for -q states it is ignored only when --namespaces (-n) is set,
but the generator collapsed both sources of Packages — explicit -n and
implicit TableMapping from mfd — into a single nil-check, so -q new
effectively never prompted on real projects whose TableMapping is populated.

Split the two sources: Packages stays "from -n", TableMapping is consulted
separately and combined with --quiet. -q new now prompts for tables that
are absent from TableMapping; -q all still skips them; -n keeps full
precedence over TableMapping as the help promises.

Add integration tests covering all three combinations using a fake prompt
hook injected into the generator.
Move namespace selection into a pure decideNamespace function that is
tested directly, and split Generate's per-entity loop into fillNamespaces.
Drops the promptNS field that existed only for tests; the package now
passes golangci-lint (Generate no longer trips gocognit).
@kroexov kroexov force-pushed the fix/quiet-respects-table-mapping branch from 06bbbef to 42d084d Compare June 21, 2026 15:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant