Add Hiera defaults support for module documentation#27
Add Hiera defaults support for module documentation#27bastelfreak merged 10 commits intovoxpupuli:mainfrom
Conversation
This enhancement allows openvox-strings to automatically extract and display parameter defaults from Hiera data (common.yaml) in addition to code defaults. Features: - New Hiera parser class that reads hiera.yaml and data/common.yaml - Automatic module root detection from manifest file paths - Seamless integration with existing markdown generator - Code defaults take precedence over Hiera defaults - Support for all Puppet data types (strings, integers, booleans, hashes, arrays, undef) Implementation: - lib/openvox-strings/hiera.rb: Core Hiera parsing and value conversion - lib/openvox-strings/markdown/base.rb: Extended defaults() method to merge Hiera data - spec/unit/openvox-strings/hiera_spec.rb: Comprehensive unit tests This allows module maintainers to define defaults in data/common.yaml without duplicating them in the Puppet class parameters, while still generating complete documentation.
Empty hashes and arrays should be formatted as {} and [] (without spaces)
to match the formatting of code defaults, ensuring identical REFERENCE.md
generation.
Arrays must not have spaces around brackets to match the format used by puppet-strings when reading defaults from code: - Code format: ['a', 'b', 'c'] - Hiera format was: [ 'a', 'b', 'c' ] (WRONG) - Hiera format now: ['a', 'b', 'c'] (CORRECT) This ensures byte-for-byte identical REFERENCE.md output whether defaults come from code or from Hiera data.
During development we discovered that arrays must be formatted without spaces around brackets to match puppet-strings code defaults format: - Correct: ['a', 'b', 'c'] - Wrong: [ 'a', 'b', 'c' ] Added explicit tests to ensure this formatting is preserved: - String arrays without spaces - Integer arrays without spaces - Nested arrays in hashes without spaces
|
@ekohl I think you will like this |
- Combine duplicate branches for Integer/Float/TrueClass/FalseClass - Use %w[] notation for word arrays in tests - All tests still pass with the same behavior
ekohl
left a comment
There was a problem hiding this comment.
puppetlabs#273 was my attempt a few years ago but I never found the time to finish it. It took a bigger approach of also expanding facts so you got a complete table.
use Pathname to check file/folder existence - metadata.json, manifests, hiera.yaml Co-authored-by: Ewoud Kohl van Wijngaarden <ewoud@kohlvanwijngaarden.nl>
- Use dynamic hierarchy parsing instead of hardcoded common.yaml
- Find first hierarchy layer without interpolations (%{...})
- Support various file names (common.yaml, common.yml, defaults.yaml)
- Fix RuboCop indentation violation in find_module_root method
|
Instead of hardcoding |
Split load_common_data into two focused methods for better testability: - find_first_static_layer_path: Finds the first static hierarchy layer - load_yaml_data: Loads and parses YAML data files Added comprehensive unit tests for both new methods covering edge cases like interpolated paths, custom datadirs, and invalid YAML files.
Replace regex pattern with simpler string check in production code
(path.include?('%{') instead of regex) and add RuboCop disable
comments for test fixtures that contain Hiera interpolation syntax.
- Use .include?('%{') for cleaner interpolation detection
- Add Style/FormatStringToken disable in test fixtures
- Fix string literal quote style in tests
|
Feel free to squash the commits when merging. |
|
@ekohl any feedback on the latest changes? |
ekohl
left a comment
There was a problem hiding this comment.
From reading the code I think this looks correct. There are 2 minor things inline, but that shouldn't hold back the merge.
Anyone else would like to weigh in before we merge this?
- Reuse load_yaml_data in load_hiera_config to reduce code duplication - Check all entries in paths array for interpolations, not just the first - Add test for mixed paths array scenario
|
How should we proceed with this? Since this is my first contribution to this repo, I can't merge it myself. |
|
Hi @slauger . I gave this a test in voxpupuli/puppet-systemd#607. I regenerated the REFERENCE.md with |
|
@bastelfreak the first match in the https://github.com/voxpupuli/puppet-systemd/blob/master/hiera.yaml#L15 But there is no |
|
Ah right, it just parses the static paths. I tested it in voxpupuli/puppet-yum#375. thanks for the great work! |
|
I need to think a minute if we want to release this as breaking change or enhancement. breaking changes aren't automatically rolled out to our modules. We have a CI check that validates the REFERENCE.md. This change would break the pipeline for modules with a REFERENCE.md + a static hiera data file. |
Summary
This PR adds support for reading parameter defaults from Hiera data files (
data/common.yaml) when generating module documentation with openvox-strings.Previously, openvox-strings could only document parameter defaults that were explicitly defined in Puppet code. With this feature, defaults can now be maintained in Hiera data files while still appearing correctly in the generated REFERENCE.md documentation.
Motivation
Many Puppet modules use Hiera for default values to enable easier customization and separation of data from code. However, this created a documentation gap - parameters without code defaults wouldn't show any default value in the generated documentation, even though they had defaults in
data/common.yaml.Implementation
Core Changes
New
OpenvoxStrings::Hieraclass (lib/openvox-strings/hiera.rb)hiera.yamlconfigurationdata/common.yamllookup_default(class_name, param_name)methodIntegration in
OpenvoxStrings::Markdown::Base(lib/openvox-strings/markdown/base.rb)defaultsmethod to merge code defaults with Hiera defaultshiera.yamlexistsKey Features
hiera.yamlexistsData Type Conversions
The implementation correctly handles all Puppet data types:
'value'(quoted)42,3.14(unquoted)true,false(lowercase, unquoted)undef[],{}['a', 'b', 'c'](no spaces around brackets to match puppet-strings format){ 'key' => 'value' }(spaces inside braces)Testing
Unit Tests
Comprehensive test suite in
spec/unit/openvox-strings/hiera_spec.rbcovering:Integration Tests
Three-stage integration test in
slauger/puppet-github_actions_runner:Test 1 - Baseline (PR#4)
Test 2 - Backwards Compatibility (PR#5)
Test 3 - Full Hiera (PR#6)
data/common.yamlResult: All three tests produce byte-for-byte identical REFERENCE.md documentation.
Test Data Types Covered
The integration tests validate all common Puppet data types:
Integer:42Float:3.14Array(empty):[]Array[String]:['alpha', 'beta', 'gamma']Array[Integer]:[1, 2, 3, 5, 8]Hash(nested):{ 'config' => { 'timeout' => 30, 'retry' => true }, 'tags' => ['prod', 'eu'] }Breaking Changes
None. This is a fully backwards-compatible addition:
Documentation
The implementation is self-documenting through:
Related Issues
This addresses the common pain point where modules using Hiera v5 data-in-modules pattern couldn't document their defaults properly.