xml: make -q honor TableMapping when -n is not set#67
Open
kroexov wants to merge 3 commits into
Open
Conversation
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).
06bbbef to
42d084d
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
in v 0.3.0. the -q flag was added to mfd-generator
I expected to use this flag to generate new tables, which do not exist in tableMapping, but found that this does not work:
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.mfdfile.The old code merged both into one
Packagesfield and only checked whether that field was empty. On a real project the.mfdfile almost always has aTableMapping, so this check was always true and-q newbehaved exactly like-q all: it quietly reused the existing mapping and never prompted for new tables.This PR keeps the two sources apart:
options.Packagesnow means strictly "came from-n".TableMappingfrom the.mfdfile is read separately and combined with the-qmode.The resulting behaviour:
-nset-qis ignored (as the help promises): use the-nmapping, skip everything else.-q all, no-nTableMappingand entities already in the project, skip unmapped tables.-q new, no-nTableMappingand existing entities, and prompt for the rest.Table selection for the database read was adjusted too: with
-q newor the default mode and no-t, the generator now readspublic.*. Without this the prompt could never run whenTableMappingwas 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/--quietflag is now documented, and the CLI help snapshot is refreshed (it was missing-g,-qand--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 interactivePromptNSstays a thin wrapper aroundpromptuiwith no logic worth mocking, and no test-only code is left in the production path.While moving that logic out, the long
Generatemethod was split as well — the per-entity loop is nowfillNamespaces. As a side effect this clears a pre-existinggolangci-lintcomplaint aboutGeneratebeing too complex, so the package lints cleanly.