-
Notifications
You must be signed in to change notification settings - Fork 2
Added support for profiles #7
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
681c7b2
09f66d7
c5e6075
57383bb
0a4eb34
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,87 @@ | ||
| /* | ||
| * MIT License | ||
| * | ||
| * Copyright (c) 2024-2025. Artem Getmanskii | ||
| * | ||
| * Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| * of this software and associated documentation files (the "Software"), to deal | ||
| * in the Software without restriction, including without limitation the rights | ||
| * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| * copies of the Software, and to permit persons to whom the Software is | ||
| * furnished to do so, subject to the following conditions: | ||
| * | ||
| * The above copyright notice and this permission notice shall be included in all | ||
| * copies or substantial portions of the Software. | ||
| * | ||
| * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| * SOFTWARE. | ||
| */ | ||
|
|
||
| package io.github.artemget.entrys.profile; | ||
|
|
||
| import io.github.artemget.entrys.ESafe; | ||
| import io.github.artemget.entrys.Entry; | ||
| import io.github.artemget.entrys.file.EFile; | ||
| import io.github.artemget.entrys.operation.EContains; | ||
| import io.github.artemget.entrys.yaml.EYaml; | ||
|
|
||
| /** | ||
| * Configuration properties entry. | ||
| * By default gets entries from "src/main/resources/application.yaml" | ||
| * Supports all yaml types, default values and envs passed via ${ENV}. | ||
| * | ||
| * @since 0.4.0 | ||
| */ | ||
| public final class EValProf extends ESafe<String> { | ||
|
|
||
| /** | ||
| * From yaml file at default dir. | ||
| * | ||
| * @param key Of Entry | ||
| */ | ||
| public EValProf(final String key) { | ||
| this(key, "src/main/resources/application.yaml"); | ||
| } | ||
|
|
||
| /** | ||
| * From yaml file. | ||
| * | ||
| * @param key Of Entry | ||
| * @param path To Yaml File | ||
| */ | ||
| public EValProf(final String key, final String path) { | ||
| this(key, new EFile(path), path); | ||
| } | ||
|
|
||
| /** | ||
| * Main ctor. | ||
| * | ||
| * @param key Of Entry | ||
| * @param content Yaml Content | ||
| * @param path String Path | ||
| */ | ||
| public EValProf(final String key, final Entry<String> content, final String path) { | ||
| super( | ||
| () -> { | ||
| final String result; | ||
| if (new EContains(new EYaml("entrys.profile", path)).value()) { | ||
|
Owner
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. How would you know either entry.profile mapping not exists or just its value is missed? |
||
| final String profile = new EYaml("entrys.profile", path).value(); | ||
| result = new EYaml(key, parsePath(path, profile)).value(); | ||
| } else { | ||
| result = new EYaml(key, content).value(); | ||
| } | ||
| return result; | ||
| }, | ||
| () -> String.format("Attribute for key '%s' is null", key) | ||
| ); | ||
| } | ||
|
|
||
| private static String parsePath(final String path, final String profile) { | ||
| return path.replace("application", String.format("application-%s", profile)); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| /* | ||
| * MIT License | ||
| * | ||
| * Copyright (c) 2024-2025. Artem Getmanskii | ||
| * | ||
| * Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| * of this software and associated documentation files (the "Software"), to deal | ||
| * in the Software without restriction, including without limitation the rights | ||
| * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| * copies of the Software, and to permit persons to whom the Software is | ||
| * furnished to do so, subject to the following conditions: | ||
| * | ||
| * The above copyright notice and this permission notice shall be included in all | ||
| * copies or substantial portions of the Software. | ||
| * | ||
| * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| * SOFTWARE. | ||
| */ | ||
|
|
||
| /** | ||
| * Profile operation directory. | ||
| */ | ||
| package io.github.artemget.entrys.profile; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,160 @@ | ||
| /* | ||
| * MIT License | ||
| * | ||
| * Copyright (c) 2024-2025. Artem Getmanskii | ||
| * | ||
| * Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| * of this software and associated documentation files (the "Software"), to deal | ||
| * in the Software without restriction, including without limitation the rights | ||
| * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| * copies of the Software, and to permit persons to whom the Software is | ||
| * furnished to do so, subject to the following conditions: | ||
| * | ||
| * The above copyright notice and this permission notice shall be included in all | ||
| * copies or substantial portions of the Software. | ||
| * | ||
| * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| * SOFTWARE. | ||
| */ | ||
|
|
||
| package io.github.artemget.entrys.yaml; | ||
|
|
||
| import com.amihaiemil.eoyaml.Node; | ||
| import com.amihaiemil.eoyaml.Yaml; | ||
| import com.amihaiemil.eoyaml.YamlMapping; | ||
| import com.amihaiemil.eoyaml.YamlNode; | ||
| import io.github.artemget.entrys.ESafe; | ||
| import io.github.artemget.entrys.Entry; | ||
| import io.github.artemget.entrys.EntryException; | ||
| import io.github.artemget.entrys.EntryExceptionUnchecked; | ||
| import io.github.artemget.entrys.file.EFile; | ||
| import io.github.artemget.entrys.operation.EContains; | ||
| import io.github.artemget.entrys.operation.EFork; | ||
| import io.github.artemget.entrys.operation.ESplit; | ||
| import io.github.artemget.entrys.operation.EUnwrap; | ||
| import io.github.artemget.entrys.system.EEnv; | ||
| import java.io.IOException; | ||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| /** | ||
| * Configuration properties entry. | ||
| * By default gets entries from "src/main/resources/application.yaml" | ||
|
Owner
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. I dont think that its a good idea, because we already have EVal etc. |
||
| * Supports all yaml types, default values and envs passed via ${ENV}. | ||
| * | ||
| * @since 0.4.0 | ||
| */ | ||
| public class EYaml extends ESafe<String> { | ||
|
Owner
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. Lets move it to separate pr and add tests. |
||
|
|
||
| /** | ||
| * From yaml file at default dir. | ||
| * | ||
| * @param key Of Entry | ||
| */ | ||
| public EYaml(final String key) { | ||
| this(key, "src/main/resources/application.yaml"); | ||
| } | ||
|
|
||
| /** | ||
| * From yaml file. | ||
| * | ||
| * @param key Of Entry | ||
| * @param path To Yaml File | ||
| */ | ||
| public EYaml(final String key, final String path) { | ||
| this(key, new EFile(path)); | ||
| } | ||
|
|
||
| /** | ||
| * Main ctor. | ||
| * | ||
| * @param key Of Entry | ||
| * @param content Yaml Content | ||
| */ | ||
| public EYaml(final String key, final Entry<String> content) { | ||
| super( | ||
| () -> { | ||
| YamlMapping root = fileToYaml(content, key); | ||
| String res = null; | ||
| final List<String> elements = new ESplit(() -> key, ".").value(); | ||
| for (int element = 0; element < elements.size(); ++element) { | ||
| if (element == elements.size() - 1) { | ||
| final YamlNode value = root.value(elements.get(element)); | ||
| if (value.isEmpty()) { | ||
| throw new EntryExceptionUnchecked( | ||
| String.format("Empty value for key: %s", key) | ||
| ); | ||
| } | ||
| if (Node.SCALAR == value.type()) { | ||
| res = EYaml.ejected(value); | ||
| } else if (Node.SEQUENCE == value.type()) { | ||
| res = value.asSequence() | ||
| .children().stream() | ||
| .map(node -> node.asScalar().value()) | ||
| .collect(Collectors.joining(";")); | ||
| } | ||
| } | ||
| root = root.yamlMapping(elements.get(element)); | ||
| } | ||
| return res; | ||
| }, | ||
| () -> String.format("Attribute for key '%s' is null", key) | ||
| ); | ||
| } | ||
|
|
||
| private static YamlMapping fileToYaml(final Entry<String> content, final String key) | ||
| throws EntryException { | ||
| final YamlMapping root; | ||
| try { | ||
| root = Yaml.createYamlInput(content.value()) | ||
| .readYamlMapping(); | ||
| } catch (final IOException | EntryException exception) { | ||
| throw new EntryException( | ||
| String.format("Failed to read yaml mapping for key: '%s'", key), | ||
| exception | ||
| ); | ||
| } | ||
| return root; | ||
| } | ||
|
|
||
| private static String ejected(final YamlNode node) throws EntryException { | ||
|
Owner
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. Seems like copy-paste from EVal. But I think you would not need this code anymore (read comments above). |
||
| final String scalar = node.asScalar().value(); | ||
| return new EFork<>( | ||
| () -> scalar.startsWith("${") && scalar.endsWith("}"), | ||
| new EFork<>( | ||
| () -> new ESplit(() -> scalar, ":").value().size() >= 2, | ||
| new EFork<>( | ||
| new EContains(new EEnv(() -> EYaml.selectedEnv(scalar).trim())), | ||
| new EEnv(() -> EYaml.selectedEnv(scalar).trim()), | ||
| () -> EYaml.joined(scalar) | ||
| ), | ||
| new EEnv(new EUnwrap(scalar, "${", "}")) | ||
| ), | ||
| () -> scalar | ||
| ).value(); | ||
| } | ||
|
|
||
| private static String selectedEnv(final String scalar) throws EntryException { | ||
| return new ESplit(new EUnwrap(scalar, "${", "}"), ":") | ||
| .value().get(0); | ||
| } | ||
|
|
||
| private static String joined(final String scalar) throws EntryException { | ||
| final List<String> split = new ESplit( | ||
| new EUnwrap(scalar, "${", "}"), ":" | ||
| ).value(); | ||
| final StringBuilder value = new StringBuilder(); | ||
| for (int index = 1; index < split.size(); ++index) { | ||
| value.append(split.get(index)); | ||
| if (index < split.size() - 1) { | ||
| value.append(':'); | ||
| } | ||
| } | ||
| return value.toString(); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.