-
Notifications
You must be signed in to change notification settings - Fork 367
Added snakeyaml to allow more complicated config reading #9390
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
4 commits
Select commit
Hold shift + click to select a range
3b6def0
Added snakeyaml to allow more complicated config reading
rolfyone 6859743
Merge remote-tracking branch 'upstream/master' into yaml-config-reader
rolfyone ff2963d
review feedback
rolfyone b05e428
Merge remote-tracking branch 'upstream/master' into yaml-config-reader
rolfyone 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
61 changes: 61 additions & 0 deletions
61
ethereum/spec/src/main/java/tech/pegasys/teku/spec/config/YamlConfigReader.java
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,61 @@ | ||
| /* | ||
| * Copyright Consensys Software Inc., 2025 | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
| * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations under the License. | ||
| */ | ||
|
|
||
| package tech.pegasys.teku.spec.config; | ||
|
|
||
| import java.io.InputStream; | ||
| import java.util.Map; | ||
| import java.util.NoSuchElementException; | ||
| import org.yaml.snakeyaml.DumperOptions; | ||
| import org.yaml.snakeyaml.LoaderOptions; | ||
| import org.yaml.snakeyaml.Yaml; | ||
| import org.yaml.snakeyaml.constructor.Constructor; | ||
| import org.yaml.snakeyaml.nodes.Tag; | ||
| import org.yaml.snakeyaml.representer.Representer; | ||
| import org.yaml.snakeyaml.resolver.Resolver; | ||
|
|
||
| public class YamlConfigReader { | ||
| private final Yaml yaml; | ||
|
|
||
| public YamlConfigReader() { | ||
| final Resolver resolver = new CustomResolver(); | ||
| yaml = | ||
| new Yaml( | ||
| new Constructor(new LoaderOptions()), | ||
| new Representer(new DumperOptions()), | ||
| new DumperOptions(), | ||
| resolver); | ||
| } | ||
|
|
||
| public Map<String, Object> readValues(final InputStream source) { | ||
| final Map<String, Object> values = yaml.load(source); | ||
| if (values == null) { | ||
| throw new NoSuchElementException(); | ||
| } | ||
| return values; | ||
| } | ||
|
|
||
| /* | ||
| * For the purposes of reading config, we just want strings, we don't want numeric values. | ||
| * This will ensure that we get hex values through as unparsed, and ensure we're getting | ||
| * things such as CRC checks through without them being altered. | ||
| * The implicit resolver below is basically saying just match numerics as string. | ||
| */ | ||
| public static class CustomResolver extends Resolver { | ||
| @Override | ||
| protected void addImplicitResolvers() { | ||
| addImplicitResolver(Tag.STR, INT, "+-0123456789."); | ||
|
rolfyone marked this conversation as resolved.
|
||
| super.addImplicitResolvers(); | ||
| } | ||
| } | ||
| } | ||
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
73 changes: 73 additions & 0 deletions
73
ethereum/spec/src/test/java/tech/pegasys/teku/spec/config/YamlConfigReaderTest.java
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,73 @@ | ||
| /* | ||
| * Copyright Consensys Software Inc., 2025 | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
| * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations under the License. | ||
| */ | ||
|
|
||
| package tech.pegasys.teku.spec.config; | ||
|
|
||
| import static java.nio.charset.StandardCharsets.UTF_8; | ||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Map; | ||
| import org.apache.commons.io.IOUtils; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| @SuppressWarnings("unchecked") | ||
| public class YamlConfigReaderTest { | ||
| private final YamlConfigReader reader = new YamlConfigReader(); | ||
|
|
||
| @Test | ||
| void shouldReadSimpleObjectToStringMap() { | ||
| final String testData = | ||
| """ | ||
| version: 250 | ||
| info: the info | ||
| """; | ||
| final Map<String, Object> objData = reader.readValues(IOUtils.toInputStream(testData, UTF_8)); | ||
| assertThat(objData.get("version")).isInstanceOf(String.class).isEqualTo("250"); | ||
| assertThat(objData.get("info")).isInstanceOf(String.class).isEqualTo("the info"); | ||
| } | ||
|
|
||
| @Test | ||
| void shouldReadObjectWithListOfObjects() { | ||
| final String testData = | ||
| """ | ||
| data: | ||
| - a: 1 | ||
| aa: 11 | ||
| - b: two | ||
| bb: three | ||
| """; | ||
|
|
||
| final Map<String, Object> objData = reader.readValues(IOUtils.toInputStream(testData, UTF_8)); | ||
| assertThat(objData.get("data")).isInstanceOf(List.class); | ||
| final List<Map<String, Object>> list = (List<Map<String, Object>>) objData.get("data"); | ||
| for (Object o : list) { | ||
| assertThat(o).isInstanceOf(Map.class); | ||
| assertThat(((Map) o).size()).isEqualTo(2); | ||
| } | ||
| assertThat(list) | ||
| .containsExactly(Map.of("a", "1", "aa", "11"), Map.of("b", "two", "bb", "three")); | ||
| } | ||
|
|
||
| @Test | ||
| void shouldReadEth1Address() { | ||
| final String testData = | ||
| """ | ||
| DEPOSIT_CONTRACT_ADDRESS: 0x4242424242424242424242424242424242424242 | ||
| """; | ||
| final Map<String, Object> objData = reader.readValues(IOUtils.toInputStream(testData, UTF_8)); | ||
| assertThat(objData.get("DEPOSIT_CONTRACT_ADDRESS")) | ||
| .isInstanceOf(String.class) | ||
| .isEqualTo("0x4242424242424242424242424242424242424242"); | ||
| } | ||
| } |
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
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.