-
Notifications
You must be signed in to change notification settings - Fork 1.8k
[docs] Document avoiding embedded structs in config structures #14340
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| # Use this changelog template to create an entry for release notes. | ||
|
|
||
| # One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' | ||
| change_type: enhancement | ||
|
|
||
| # The name of the component, or a single word describing the area of concern, (e.g. receiver/otlp) | ||
| component: docs | ||
|
|
||
| # A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). | ||
| note: Add documentation guidelines for avoiding embedded structs in configuration | ||
|
|
||
| # One or more tracking issues or pull requests related to the change | ||
| issues: [12719] | ||
|
|
||
| # (Optional) One or more lines of additional information to render under the primary note. | ||
| # These lines will be padded with 2 spaces and then inserted directly into the document. | ||
| # Use pipe (|) for multiline entries. | ||
| subtext: | | ||
| Added a new section to coding-guidelines.md explaining why embedded structs should be | ||
| avoided in configuration definitions, including examples showing the preferred approach. | ||
| # Optional: The change log or logs in which this entry should be included. | ||
| # e.g. '[user]' or '[user, api]' | ||
| # Include 'user' if the change is relevant to end users. | ||
| # Include 'api' if there is a change to a library API. | ||
| # Default: '[user]' | ||
| change_logs: [] | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -47,6 +47,38 @@ When naming configuration structs, use the following guidelines: | |
| - Use the `Settings` suffix for configuration structs that are set by developers in the code. For example, `component.TelemetrySettings` ends in `Settings` since it is set by developers in the code. | ||
| - Avoid redundant prefixes that are already implied by the package name. For example, use`configgrpc.ClientConfig` instead of `configgrpc.GRPCClientConfig`. | ||
|
|
||
| #### Avoid Embedded Structs | ||
|
|
||
| When defining configuration structs, avoid using embedded (anonymous) struct fields. Instead, use explicitly named fields. | ||
|
|
||
| **Rationale:** | ||
|
|
||
| 1. **Unmarshal Compatibility**: Embedded structs can break custom `Unmarshal` implementations. If an embedded struct requires special unmarshaling logic, it may not function correctly when embedded. | ||
|
|
||
| 2. **Naming Conflicts**: Embedded structs can cause field name collisions. Even if the YAML configuration nests them properly (e.g., under `sending_queue`), having identical field names in embedded structs creates ambiguity in the Go code. | ||
|
|
||
| 3. **Clarity**: Named fields make the configuration structure more explicit and easier to understand. | ||
|
|
||
| **Example:** | ||
|
|
||
| ```go | ||
| // ❌ BAD: Using embedded structs | ||
| type ExporterConfig struct { | ||
| exporterhelper.TimeoutConfig // embedded | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a linter we could enable as well to enforce checking for this? |
||
| exporterhelper.QueueConfig // embedded | ||
| exporterhelper.RetryConfig // embedded | ||
| } | ||
|
|
||
| // ✅ GOOD: Using named fields | ||
| type ExporterConfig struct { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are cases where we want to achieve the same end-user functionality of embedding structs while still naming the field in the struct itself (example here). Can you document this as well? |
||
| Timeout exporterhelper.TimeoutConfig `mapstructure:"timeout"` | ||
| Queue exporterhelper.QueueConfig `mapstructure:"sending_queue"` | ||
| Retry exporterhelper.RetryConfig `mapstructure:"retry_on_failure"` | ||
| } | ||
| ``` | ||
|
|
||
| This practice ensures better maintainability and prevents subtle bugs related to struct composition and configuration unmarshaling. | ||
|
|
||
| ## Module organization | ||
|
|
||
| As usual in Go projects, organize your code into packages grouping related functionality. To ensure | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This kind of change doesn't need a changelog entry.