[go: up one dir, main page]

Skip to content

tools: Add POC for bottom-up config.toml.example generator

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 toml tags or uses default field names
  • Implements extraction level filtering (minimal/complete) based on example tag 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 minimal and complete extraction modes
  • TOML Tag Support: Honors toml struct 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 🤖

Merge request reports

Loading