collectors: add Struct collector and StructToMap#63
Open
bigbes wants to merge 1 commit into
Open
Conversation
Add a reflection-based collector that reads configuration straight from a Go struct, plus a standalone helper exposing the same conversion. - collectors.Struct streams a struct's fields as configuration values, using `config` struct tags (falling back to `yaml`), the `-` skip directive, and the `omitempty` and `inline` options; key order is preserved by default. - collectors.StructToMap converts a struct (or a pointer to one) into a map[string]any with the same tag rules, returning ErrNotStruct for non-struct input.
76c36f0 to
a4b5926
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.
Adds a reflection-based
collectors.Structthat reads configuration straight from a Go struct, alongside the standalonecollectors.StructToMaphelper that exposes the same conversion.Field names come from the
configstruct tag, falling back toyaml, then the lowercased field name. The-directive skips a field,omitemptyskips zero values, andinlinemerges a struct/map field's keys into the parent (anonymous fields withoutinlinenest under the lowercased type name). Unexported fields are ignored except those promoted from an embedded struct. Internally the struct is turned into amap[string]anyand flattened the same way as theMapcollector, so it behaves consistently; key order is preserved by default likeYamlFormat. Non-struct input yields no values from the collector, andErrNotStructfromStructToMap.Includes unit tests, a runnable example, doc updates, and a changelog entry.