Skip to content

Commit

Permalink
introduce a more flexible way to define affects relationships of keyword
Browse files Browse the repository at this point in the history
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
sebastian-toepfer committed Jun 5, 2024
1 parent a37f02b commit fa76f02
Show file tree
Hide file tree
Showing 12 changed files with 357 additions and 97 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import java.util.TreeSet;
import java.util.function.Function;

public class AffectedByKeywordType implements KeywordType {
public final class AffectedByKeywordType implements KeywordType {

private final String name;
private final Collection<AffectedBy> affectedBy;
Expand All @@ -44,6 +44,9 @@ public AffectedByKeywordType(
final Collection<AffectedBy> affectedBy,
final Function<JsonSchema, Keyword> keywordCreator
) {
if (affectedBy.isEmpty()) {
throw new IllegalArgumentException("affectedBy can not be empty!");
}
this.name = Objects.requireNonNull(name);
this.affectedBy = List.copyOf(affectedBy);
this.keywordCreator = Objects.requireNonNull(keywordCreator);
Expand Down
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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,27 @@
import io.github.sebastiantoepfer.jsonschema.keyword.Annotation;
import io.github.sebastiantoepfer.jsonschema.keyword.Keyword;
import io.github.sebastiantoepfer.jsonschema.keyword.KeywordType;
import io.github.sebastiantoepfer.jsonschema.keyword.StaticAnnotation;
import jakarta.json.JsonValue;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.BiFunction;
import java.util.function.Function;

public class AffectsKeywordType implements KeywordType {

private final String name;
private final String affects;
private final BiFunction<Annotation, JsonSchema, Keyword> keywordCreator;
private final Collection<Affects> affects;
private final BiFunction<Collection<Annotation>, JsonSchema, Keyword> keywordCreator;

public AffectsKeywordType(
final String name,
final String affects,
final BiFunction<Annotation, JsonSchema, Keyword> keywordCreator
final Collection<Affects> affects,
final BiFunction<Collection<Annotation>, JsonSchema, Keyword> keywordCreator
) {
this.name = name;
this.affects = affects;
this.name = Objects.requireNonNull(name);
this.affects = List.copyOf(affects);
this.keywordCreator = keywordCreator;
}

Expand All @@ -55,36 +58,46 @@ public String name() {

@Override
public Keyword createKeyword(final JsonSchema schema) {
return new AffectsKeyword(schema, name, affects, keywordCreator);
return new AffectsKeyword(schema, name, List.copyOf(affects), keywordCreator);
}

static final class AffectsKeyword extends KeywordRelationship {

private final JsonSchema schema;
private final String affects;
private final BiFunction<Annotation, JsonSchema, Keyword> keywordCreator;
private final Collection<Affects> affects;
private final BiFunction<Collection<Annotation>, JsonSchema, Keyword> keywordCreator;

public AffectsKeyword(
final JsonSchema schema,
final String name,
final String affects,
final BiFunction<Annotation, JsonSchema, Keyword> keywordCreator
final List<Affects> affects,
final BiFunction<Collection<Annotation>, JsonSchema, Keyword> keywordCreator
) {
super(name);
this.schema = Objects.requireNonNull(schema);
this.affects = affects;
this.affects = List.copyOf(affects);
this.keywordCreator = Objects.requireNonNull(keywordCreator);
}

@Override
protected Keyword delegate() {
return keywordCreator.apply(
schema
.keywordByName(affects)
.map(Keyword::asAnnotation)
.orElseGet(() -> new StaticAnnotation(affects, JsonValue.NULL)),
schema
);
//ugly ... map.entry is not the right structure ...
final Map.Entry<List<Annotation>, Function<Keyword, Keyword>> p = affects
.stream()
.map(a -> a.findAffectsKeywordIn(schema))
.reduce(
Map.entry(List.of(), k -> k),
(
Map.Entry<List<Annotation>, Function<Keyword, Keyword>> t,
Map.Entry<Annotation, Function<Keyword, Keyword>> u
) -> {
final ArrayList<Annotation> newAnnotations = new ArrayList<>(t.getKey());
newAnnotations.add(u.getKey());
return Map.entry(newAnnotations, t.getValue().andThen(u.getValue()));
},
(l, r) -> null
);
return p.getValue().apply(keywordCreator.apply(p.getKey(), schema));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@

final class ReplacingKeyword implements Keyword {

private final Keyword affectedKeyword;
private final Keyword keywordToReplace;
private final Collection<KeywordCategory> categoriesToReplace;

public ReplacingKeyword(final Keyword affectedKeyword) {
this(affectedKeyword, EnumSet.of(KeywordCategory.APPLICATOR, KeywordCategory.ASSERTION));
public ReplacingKeyword(final Keyword keywordToReplace) {
this(keywordToReplace, EnumSet.of(KeywordCategory.APPLICATOR, KeywordCategory.ASSERTION));
}

public ReplacingKeyword(final Keyword affectedKeyword, final Collection<KeywordCategory> categoriesToReplace) {
this.affectedKeyword = Objects.requireNonNull(affectedKeyword);
public ReplacingKeyword(final Keyword keywordToReplace, final Collection<KeywordCategory> categoriesToReplace) {
this.keywordToReplace = Objects.requireNonNull(keywordToReplace);
this.categoriesToReplace = List.copyOf(categoriesToReplace);
}

Expand All @@ -57,7 +57,7 @@ public Identifier asIdentifier() {
if (categoriesToReplace.contains(KeywordCategory.IDENTIFIER)) {
throw new UnsupportedOperationException();
} else {
return affectedKeyword.asIdentifier();
return keywordToReplace.asIdentifier();
}
}

Expand All @@ -66,7 +66,7 @@ public Assertion asAssertion() {
if (categoriesToReplace.contains(KeywordCategory.ASSERTION)) {
throw new UnsupportedOperationException();
} else {
return affectedKeyword.asAssertion();
return keywordToReplace.asAssertion();
}
}

Expand All @@ -75,7 +75,7 @@ public Annotation asAnnotation() {
if (categoriesToReplace.contains(KeywordCategory.ANNOTATION)) {
throw new UnsupportedOperationException();
} else {
return affectedKeyword.asAnnotation();
return keywordToReplace.asAnnotation();
}
}

Expand All @@ -84,7 +84,7 @@ public Applicator asApplicator() {
if (categoriesToReplace.contains(KeywordCategory.APPLICATOR)) {
throw new UnsupportedOperationException();
} else {
return affectedKeyword.asApplicator();
return keywordToReplace.asApplicator();
}
}

Expand All @@ -93,22 +93,22 @@ public ReservedLocation asReservedLocation() {
if (categoriesToReplace.contains(KeywordCategory.RESERVED_LOCATION)) {
throw new UnsupportedOperationException();
} else {
return affectedKeyword.asReservedLocation();
return keywordToReplace.asReservedLocation();
}
}

@Override
public Collection<Keyword.KeywordCategory> categories() {
return affectedKeyword.categories().stream().filter(not(categoriesToReplace::contains)).collect(toSet());
return keywordToReplace.categories().stream().filter(not(categoriesToReplace::contains)).collect(toSet());
}

@Override
public boolean hasName(final String string) {
return affectedKeyword.hasName(string);
return keywordToReplace.hasName(string);
}

@Override
public <T extends Media<T>> T printOn(final T media) {
return affectedKeyword.printOn(media);
return keywordToReplace.printOn(media);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public ApplicatorVocabulary() {
new NamedJsonSchemaKeywordType(PatternPropertiesKeyword.NAME, PatternPropertiesKeyword::new),
new SubSchemaKeywordType(ItemsKeyword.NAME, ItemsKeyword::new),
new SchemaArrayKeywordType(PrefixItemsKeyword.NAME, PrefixItemsKeyword::new),
new ArraySubSchemaKeywordType(PrefixItemsKeyword.NAME, PrefixItemsKeyword::new),
new SchemaArrayKeywordType(PrefixItemsKeyword.NAME, PrefixItemsKeyword::new),
new AffectedByKeywordType(
ContainsKeyword.NAME,
List.of(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
import jakarta.json.JsonArray;
import jakarta.json.JsonValue;
import java.math.BigInteger;
import java.util.Collection;
import java.util.List;
import java.util.Objects;

/**
Expand All @@ -47,11 +49,11 @@
final class MaxContainsKeyword implements Assertion {

static final String NAME = "maxContains";
private final Annotation affects;
private final Collection<Annotation> affects;
private final BigInteger maxContains;

public MaxContainsKeyword(final Annotation affects, final BigInteger maxContains) {
this.affects = Objects.requireNonNull(affects);
public MaxContainsKeyword(final Collection<Annotation> affects, final BigInteger maxContains) {
this.affects = List.copyOf(affects);
this.maxContains = Objects.requireNonNull(maxContains);
}

Expand All @@ -67,20 +69,21 @@ public <T extends Media<T>> T printOn(final T media) {

@Override
public boolean isValidFor(final JsonValue instance) {
return (
!InstanceType.ARRAY.isInstance(instance) || isValidFor(affects.valueFor(instance), instance.asJsonArray())
return (!InstanceType.ARRAY.isInstance(instance) || isValidFor(instance.asJsonArray()));
}

private boolean isValidFor(final JsonArray instance) {
return isValidFor(
affects
.stream()
.map(a -> a.valueFor(instance))
.map(v -> new NumberOfMatches(instance, v))
.mapToInt(NumberOfMatches::count)
.sum()
);
}

private boolean isValidFor(final JsonValue containing, final JsonArray values) {
final boolean result;
if (JsonValue.NULL.equals(containing)) {
result = true;
} else if (JsonValue.TRUE.equals(containing)) {
result = values.size() <= maxContains.intValue();
} else {
result = containing.asJsonArray().size() <= maxContains.intValue();
}
return result;
private boolean isValidFor(final int containing) {
return containing <= maxContains.intValue();
}
}
Loading

0 comments on commit fa76f02

Please sign in to comment.