Skip to content

collectors: add Struct collector and StructToMap#63

Open
bigbes wants to merge 1 commit into
masterfrom
bigbes/gh-no-struct-source
Open

collectors: add Struct collector and StructToMap#63
bigbes wants to merge 1 commit into
masterfrom
bigbes/gh-no-struct-source

Conversation

@bigbes
Copy link
Copy Markdown
Collaborator

@bigbes bigbes commented May 12, 2026

Adds a reflection-based collectors.Struct that reads configuration straight from a Go struct, alongside the standalone collectors.StructToMap helper that exposes the same conversion.

Field names come from the config struct tag, falling back to yaml, then the lowercased field name. The - directive skips a field, omitempty skips zero values, and inline merges a struct/map field's keys into the parent (anonymous fields without inline nest under the lowercased type name). Unexported fields are ignored except those promoted from an embedded struct. Internally the struct is turned into a map[string]any and flattened the same way as the Map collector, so it behaves consistently; key order is preserved by default like YamlFormat. Non-struct input yields no values from the collector, and ErrNotStruct from StructToMap.

Includes unit tests, a runnable example, doc updates, and a changelog entry.

@coveralls
Copy link
Copy Markdown

coveralls commented May 12, 2026

Coverage Status

coverage: 85.805% (+0.4%) from 85.423% — bigbes/gh-no-struct-source into master

@bigbes bigbes requested review from Mockird31 and sssciel May 12, 2026 13:55
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.
@bigbes bigbes force-pushed the bigbes/gh-no-struct-source branch from 76c36f0 to a4b5926 Compare May 12, 2026 13:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants