tools: Add POC for bottom-up config.toml.example generator
-
Please check this box if this contribution uses AI-generated content (including content generated by GitLab Duo features) as outlined in the GitLab DCO & CLA. As a benefit of being a GitLab Community Contributor, you receive complimentary access to GitLab Duo.
Configuration Extractor: Struct-to-TOML Conversion Tool
Overview
This MR introduces a configuration extraction tool that converts Go structs into TOML format using a bottom-up tree construction algorithm. The tool leverages the visitor pattern with reflectwalk to traverse complex nested structures and generate example configuration files.
Implementation Details
Core Algorithm
The extractor uses a bottom-up tree construction approach with two synchronized stacks:
-
Values stack: Builds nested containers (
map[string]any{}for structs/maps,[]any{}for slices/arrays) - Keys stack: Tracks field names extracted from TOML tags or struct field names
Three-Phase Processing
Phase 1 - Container Creation (Enter):
- Creates empty containers when encountering structs, maps, slices, or arrays
- Extracts field names from
tomltags or uses default field names - Implements extraction level filtering (
minimal/complete) based onexampletag values
Phase 2 - Leaf Processing (Primitives):
- Processes primitive values (strings, numbers, booleans) by popping the current container
- Adds the value using appropriate key (for maps) or index (for slices)
- Pushes the updated container back to maintain stack integrity
Phase 3 - Container Completion (Exit):
- Performs bottom-up assembly when exiting containers
- Attaches completed child containers to their parents
- Maintains proper nesting hierarchy through controlled stack operations
The extracted map structure is used for TOML generation via github.com/pelletier/go-toml/v2.
Features
-
Flexible Extraction Levels: Support for
minimalandcompleteextraction modes -
TOML Tag Support: Honors
tomlstruct tags for field naming - Complex Type Handling: Supports nested structs, pointers, slices, arrays, and maps
- Stack-Based Architecture: Ensures proper hierarchical construction without recursion issues
Related to #6730 (closed)
Edited by 🤖 GitLab Bot 🤖