Repository navigation
feat(cli): support nested maps in rc config file #9578
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
Merged
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
bb4815f
feat(cli): support nested maps in rc config file
lodekeeper 5684d86
docs(cli): add rcConfig example to CLI reference
lodekeeper 7f8349a
fix(cli): harden flattenObject against non-plain and prototype-pollut…
lodekeeper 6900079
docs(cli): note rcConfig flag+prefix nesting caveat
lodekeeper 54b1316
docs(cli): clarify rcConfig flag+prefix caveat with a concrete example
lodekeeper c2828d4
feat(cli): accept nested `enabled` for on/off flags in rcConfig
lodekeeper d7943c4
docs(cli): trim rcConfig example per review
lodekeeper 440318e
fix(cli): repair rcConfig docs rendering, keep example minimal
lodekeeper 2f496f0
docs(cli): render rcConfig example as a bare yaml block
lodekeeper a4db343
docs(cli): keep rcConfig example inline, drop docsgen change
lodekeeper 5307c3e
docs(cli): use beacon-config.yaml in rcConfig example
lodekeeper c8a47f1
docs(cli): drop "example:" label from rcConfig docs, keep command
lodekeeper f801040
docs(cli): keep "example:" label, revert docsgen tweak
lodekeeper 5f96ad3
docs(cli): drop command from rcConfig example, keep "example:" label
lodekeeper 60b6648
docs(cli): use command-agnostic options in rcConfig example
lodekeeper bf161a1
refactor(cli): drop unused command from option examples
lodekeeper b5b9c93
docs(cli): show dotted-key form in rcConfig example
lodekeeper f035e9d
Apply suggestion from @nflaig
nflaig 657b5c5
Merge branch 'unstable' into feat/nested-rc-config
nflaig b151a88
chore: re-trigger CI
lodekeeper File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| import {describe, expect, it} from "vitest"; | ||
| import {flattenObject, translateEnabledKeys} from "../../../src/util/object.js"; | ||
|
|
||
| describe("util / flattenObject", () => { | ||
| it("flattens nested maps into dot-notation keys", () => { | ||
| expect(flattenObject({rest: {address: "0.0.0.0", port: 9596}})).toEqual({ | ||
| "rest.address": "0.0.0.0", | ||
| "rest.port": 9596, | ||
| }); | ||
| }); | ||
|
|
||
| it("leaves already-dotted keys unchanged (backwards compatible)", () => { | ||
| expect(flattenObject({"rest.address": "0.0.0.0", "rest.port": 9596})).toEqual({ | ||
| "rest.address": "0.0.0.0", | ||
| "rest.port": 9596, | ||
| }); | ||
| }); | ||
|
|
||
| it("preserves arrays as values instead of flattening them by index", () => { | ||
| expect(flattenObject({rest: {namespace: ["beacon", "config"]}, bootnodes: ["enr:-a", "enr:-b"]})).toEqual({ | ||
| "rest.namespace": ["beacon", "config"], | ||
| bootnodes: ["enr:-a", "enr:-b"], | ||
| }); | ||
| }); | ||
|
|
||
| it("preserves primitive value types (boolean, number, null)", () => { | ||
| expect(flattenObject({metrics: {enabled: true, port: 8008, host: null}})).toEqual({ | ||
| "metrics.enabled": true, | ||
| "metrics.port": 8008, | ||
| "metrics.host": null, | ||
| }); | ||
| }); | ||
|
|
||
| it("supports nested and dotted keys mixed in the same object", () => { | ||
| expect(flattenObject({rest: {address: "0.0.0.0"}, "rest.port": 9596, network: "hoodi"})).toEqual({ | ||
| "rest.address": "0.0.0.0", | ||
| "rest.port": 9596, | ||
| network: "hoodi", | ||
| }); | ||
| }); | ||
|
|
||
| it("flattens arbitrarily deep nesting", () => { | ||
| expect(flattenObject({a: {b: {c: 1}}})).toEqual({"a.b.c": 1}); | ||
| }); | ||
|
|
||
| it("returns top-level scalars untouched", () => { | ||
| expect(flattenObject({network: "hoodi", port: 9000, rest: true})).toEqual({ | ||
| network: "hoodi", | ||
| port: 9000, | ||
| rest: true, | ||
| }); | ||
| }); | ||
|
|
||
| it("last definition wins when a key is given in both nested and dotted form", () => { | ||
| expect(flattenObject({"rest.address": "first", rest: {address: "second"}})).toEqual({ | ||
| "rest.address": "second", | ||
| }); | ||
| }); | ||
|
|
||
| it("keeps arrays of objects intact as a single value", () => { | ||
| expect(flattenObject({chain: {trustedSetup: [{index: 0}, {index: 1}]}})).toEqual({ | ||
| "chain.trustedSetup": [{index: 0}, {index: 1}], | ||
| }); | ||
| }); | ||
|
|
||
| it("drops empty nested maps and keeps sibling keys", () => { | ||
| expect(flattenObject({rest: {}, network: "hoodi"})).toEqual({network: "hoodi"}); | ||
| }); | ||
|
|
||
| it("returns an empty object for non-plain input instead of throwing", () => { | ||
| // e.g. an empty or scalar rc config file makes readFile return undefined / a primitive | ||
| expect(flattenObject(undefined as unknown as Record<string, unknown>)).toEqual({}); | ||
| expect(flattenObject(null as unknown as Record<string, unknown>)).toEqual({}); | ||
| expect(flattenObject("network: hoodi" as unknown as Record<string, unknown>)).toEqual({}); | ||
| expect(flattenObject(42 as unknown as Record<string, unknown>)).toEqual({}); | ||
| }); | ||
|
|
||
| it("skips the __proto__ key and does not pollute Object.prototype", () => { | ||
| const malicious = JSON.parse('{"__proto__": {"polluted": true}, "network": "hoodi"}'); | ||
| expect(flattenObject(malicious)).toEqual({network: "hoodi"}); | ||
| expect(({} as Record<string, unknown>).polluted).toBeUndefined(); | ||
| }); | ||
|
|
||
| it("keeps non-plain object values (e.g. Date) as leaves instead of dropping them", () => { | ||
| const genesisTime = new Date("2024-01-01T00:00:00Z"); | ||
| expect(flattenObject({chain: {genesisTime}})).toEqual({"chain.genesisTime": genesisTime}); | ||
| }); | ||
| }); | ||
|
|
||
| describe("util / translateEnabledKeys", () => { | ||
| it("rewrites `{prefix}.enabled` to the bare `{prefix}` flag", () => { | ||
| expect(translateEnabledKeys({"metrics.enabled": true, "metrics.port": 8008})).toEqual({ | ||
| metrics: true, | ||
| "metrics.port": 8008, | ||
| }); | ||
| }); | ||
|
|
||
| it("translates the nested form produced by flattenObject (metrics: {enabled, port})", () => { | ||
| expect(translateEnabledKeys(flattenObject({metrics: {enabled: true, port: 8008}}))).toEqual({ | ||
| metrics: true, | ||
| "metrics.port": 8008, | ||
| }); | ||
| }); | ||
|
|
||
| it("handles multiple prefixes independently", () => { | ||
| expect( | ||
| translateEnabledKeys({"metrics.enabled": true, "builder.enabled": false, "builder.url": "http://localhost"}) | ||
| ).toEqual({ | ||
| metrics: true, | ||
| builder: false, | ||
| "builder.url": "http://localhost", | ||
| }); | ||
| }); | ||
|
|
||
| it("leaves `{prefix}.enabled` untouched when the bare `{prefix}` is already set (conflict surfaces later)", () => { | ||
| expect(translateEnabledKeys({metrics: false, "metrics.enabled": true})).toEqual({ | ||
| metrics: false, | ||
| "metrics.enabled": true, | ||
| }); | ||
| }); | ||
|
|
||
| it("is a no-op when there are no `.enabled` keys", () => { | ||
| expect(translateEnabledKeys({"rest.port": 9596, network: "hoodi"})).toEqual({ | ||
| "rest.port": 9596, | ||
| network: "hoodi", | ||
| }); | ||
| }); | ||
|
|
||
| it("does not translate a top-level key literally named `enabled`", () => { | ||
| expect(translateEnabledKeys({enabled: true})).toEqual({enabled: true}); | ||
| }); | ||
|
|
||
| it("strips `.enabled` from a deeply-prefixed key", () => { | ||
| expect(translateEnabledKeys({"a.b.enabled": true})).toEqual({"a.b": true}); | ||
| }); | ||
| }); |
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.