You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[docs] Document avoiding embedded structs in config structures
Add guidelines to coding-guidelines.md explaining why embedded structs
should be avoided in configuration definitions. This helps prevent
unmarshal issues, naming conflicts, and improves code clarity.
Fixes#12719
Copy file name to clipboardExpand all lines: docs/coding-guidelines.md
+32Lines changed: 32 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -47,6 +47,38 @@ When naming configuration structs, use the following guidelines:
47
47
- 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.
48
48
- Avoid redundant prefixes that are already implied by the package name. For example, use`configgrpc.ClientConfig` instead of `configgrpc.GRPCClientConfig`.
49
49
50
+
#### Avoid Embedded Structs
51
+
52
+
When defining configuration structs, avoid using embedded (anonymous) struct fields. Instead, use explicitly named fields.
53
+
54
+
**Rationale:**
55
+
56
+
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.
57
+
58
+
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.
59
+
60
+
3.**Clarity**: Named fields make the configuration structure more explicit and easier to understand.
0 commit comments