feat(interactive): add reflection-driven interactive prompt engine#249
Open
ben-kalmus wants to merge 1 commit into
Open
feat(interactive): add reflection-driven interactive prompt engine#249ben-kalmus wants to merge 1 commit into
ben-kalmus wants to merge 1 commit into
Conversation
Up to standards ✅🟢 Issues
|
| Metric | Results |
|---|---|
| Complexity | 228 |
| Duplication | 9 |
TIP This summary will be updated as you push new changes.
A generic package that builds a request body by walking any struct via reflection and prompting for each field through a small Prompter interface (survey-backed in production, a scripted fake for tests). Handles scalars, optional pointers, enums (validated through the SDK's own IsValid method), oneOf unions, parameter bags, slices and maps, with per-field input validation and re-prompt on invalid entries. No SDK or command coupling.
b28c05b to
c17e3a6
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.
Summary
Adds
pkg/interactive, a generic package that builds a request body by walking any Go struct with reflection and prompting for each field. It is the foundation for--interactivemodes across the CLI.This PR introduces the engine only. No command is wired to it yet (see the follow-up PR adding
--interactiveto compositions upsert).Demo
Stacked PRs
How it works
Builder.Build(&v)walks the struct and prompts for inputs: optional pointer fields can be skipped, large optional-only parameter objects let you multi-select which fields to fill.Enums (named string types the SDK generates with an
IsValid() boolmethod) are validated against the SDK's own check via reflection.Bad input does not abort the build. It re-prompts in place (non-numeric integer, malformed JSON, empty required field) using validators.
A depth/cycle guard degrades self-referential structures to a raw-JSON prompt instead of looping forever.
Design notes
SDK-agnostic: the package imports only
pkg/iostreamsandpkg/prompt, no SDK or command packages, so any command can reuse it.Input is gathered through a small
Prompterinterface (Input/Confirm/Select/MultiSelect). Production usesSurveyPrompter; tests useScriptedPrompter, a label-keyed fake whose answers match by field label rather than call order, so tests do not break when SDK changes.Changes
pkg/interactive/prompter.go:Prompterinterface andSurveyPrompter(survey/v2 viapkg/prompt).pkg/interactive/scripted_prompter.go:ScriptedPrompter, the reusable label-keyed test fake.pkg/interactive/builder.go: the reflection walk (scalars, optional pointers, unions, parameter bags, slices, maps, enum and input validation).pkg/interactive/classify.go: reflection helpers (union, parameter-bag, required-field detection).pkg/interactive/validators.go: input validator builders (required string, integer, number, boolean, JSON).Tests
Unit tests under
pkg/interactivecovering:map[string]string