-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
introduce a more flexible way to define affects relationships of keyword
instead of define a affects relationship with list of strings we us a list of new introduces object Affects with a name (the old string) and a new AbsenceStrategy. The AbsenceStrategy can be use to define what should be happens if the keyword is missing. At default is exists two: Provide a default value -> useful if the affected works without the affectedBy one Replace -> useful if the affected doesn't works without the affectedBy one
- Loading branch information
1 parent
a37f02b
commit fa76f02
Showing
12 changed files
with
357 additions
and
97 deletions.
There are no files selected for viewing
This file contains 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
94 changes: 94 additions & 0 deletions
94
core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/keyword/type/Affects.java
This file contains 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,94 @@ | ||
/* | ||
* The MIT License | ||
* | ||
* Copyright 2024 sebastian. | ||
* | ||
* 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.sebastiantoepfer.jsonschema.core.keyword.type; | ||
|
||
import io.github.sebastiantoepfer.jsonschema.JsonSchema; | ||
import io.github.sebastiantoepfer.jsonschema.keyword.Annotation; | ||
import io.github.sebastiantoepfer.jsonschema.keyword.Keyword; | ||
import io.github.sebastiantoepfer.jsonschema.keyword.StaticAnnotation; | ||
import jakarta.json.JsonValue; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.Optional; | ||
import java.util.function.Function; | ||
|
||
public final class Affects { | ||
|
||
private final String name; | ||
private final AbsenceStrategy strategy; | ||
|
||
public Affects(final String name, final JsonValue answerInAbsence) { | ||
this(name, new ProvideDefaultValue(answerInAbsence)); | ||
} | ||
|
||
public Affects(final String name, final AbsenceStrategy strategy) { | ||
this.name = Objects.requireNonNull(name); | ||
this.strategy = Objects.requireNonNull(strategy); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Affects{" + "name=" + name + ", strategy=" + strategy.getClass() + '}'; | ||
} | ||
|
||
Map.Entry<Annotation, Function<Keyword, Keyword>> findAffectsKeywordIn(final JsonSchema schema) { | ||
final Map.Entry<Annotation, Function<Keyword, Keyword>> result; | ||
final Optional<Annotation> annotation = schema | ||
.keywordByName(name) | ||
.filter(k -> k.hasCategory(Keyword.KeywordCategory.ANNOTATION)) | ||
.map(Keyword::asAnnotation); | ||
if (annotation.isPresent()) { | ||
result = Map.entry(annotation.get(), k -> k); | ||
} else { | ||
result = strategy.create(name); | ||
} | ||
return result; | ||
} | ||
|
||
public interface AbsenceStrategy { | ||
Map.Entry<Annotation, Function<Keyword, Keyword>> create(String name); | ||
} | ||
|
||
public static final class ReplaceKeyword implements AbsenceStrategy { | ||
|
||
@Override | ||
public Map.Entry<Annotation, Function<Keyword, Keyword>> create(final String name) { | ||
return Map.entry(new StaticAnnotation(name, JsonValue.NULL), ReplacingKeyword::new); | ||
} | ||
} | ||
|
||
public static final class ProvideDefaultValue implements AbsenceStrategy { | ||
|
||
private final JsonValue answerInAbsence; | ||
|
||
public ProvideDefaultValue(final JsonValue answerInAbsence) { | ||
this.answerInAbsence = Objects.requireNonNullElse(answerInAbsence, JsonValue.NULL); | ||
} | ||
|
||
@Override | ||
public Map.Entry<Annotation, Function<Keyword, Keyword>> create(final String name) { | ||
return Map.entry(new StaticAnnotation(name, answerInAbsence), k -> k); | ||
} | ||
} | ||
} |
This file contains 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 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 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 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.