Skip to content
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

refactor keyword creation #25

Merged
merged 4 commits into from
Dec 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,13 @@
import java.util.Optional;

public interface JsonSchema extends JsonValue {
public Validator validator();
Validator validator();

Optional<Keyword> keywordByName(String name);

Optional<JsonSubSchema> asSubSchema(String name);

default JsonSchema rootSchema() {
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,9 @@
*/
public interface JsonSubSchema extends JsonSchema {
JsonSchema owner();

@Override
default JsonSchema rootSchema() {
return owner().rootSchema();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
package io.github.sebastiantoepfer.jsonschema.keyword;

import io.github.sebastiantoepfer.jsonschema.JsonSchema;
import jakarta.json.JsonValue;
import java.util.Objects;

public interface KeywordType {
Expand All @@ -34,5 +33,5 @@ default boolean hasName(String name) {

String name();

Keyword createKeyword(JsonSchema schema, JsonValue value);
Keyword createKeyword(JsonSchema schema);
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,29 @@ public final class FakeJsonSchemaFactory implements JsonSchemaFactory {

@Override
public JsonSchema create(final JsonValue schema) {
return new JsonSchema() {
@Override
public Validator validator() {
throw new UnsupportedOperationException("Not supported yet.");
}

@Override
public JsonValue.ValueType getValueType() {
throw new UnsupportedOperationException("Not supported yet.");
}

@Override
public Optional<Keyword> keywordByName(String name) {
throw new UnsupportedOperationException("Not supported yet.");
}
};
return new FakeJsonSchema();
}

static class FakeJsonSchema implements JsonSchema {

@Override
public Validator validator() {
throw new UnsupportedOperationException("Not supported yet.");
}

@Override
public Optional<Keyword> keywordByName(String name) {
throw new UnsupportedOperationException("Not supported yet.");
}

@Override
public JsonValue.ValueType getValueType() {
throw new UnsupportedOperationException("Not supported yet.");
}

@Override
public Optional<JsonSubSchema> asSubSchema(String name) {
throw new UnsupportedOperationException("Not supported yet.");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,20 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package io.github.sebastiantoepfer.jsonschema.core;
package io.github.sebastiantoepfer.jsonschema;

import io.github.sebastiantoepfer.jsonschema.JsonSchema;
import io.github.sebastiantoepfer.jsonschema.keyword.Keyword;
import io.github.sebastiantoepfer.jsonschema.keyword.KeywordType;
import java.util.Objects;
import java.util.Optional;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.sameInstance;

final class KeywordSearch {
import io.github.sebastiantoepfer.jsonschema.FakeJsonSchemaFactory.FakeJsonSchema;
import org.junit.jupiter.api.Test;

private final KeywordType keywordType;
class JsonSchemaTest {

public KeywordSearch(final KeywordType keywordType) {
this.keywordType = Objects.requireNonNull(keywordType);
}

public Optional<Keyword> searchForKeywordIn(final JsonSchema schema) {
return Optional
.ofNullable(schema.asJsonObject().get(keywordType.name()))
.map(keywordValue -> keywordType.createKeyword(schema, keywordValue));
@Test
void should_return_itself_as_root_schema() {
final JsonSchema schema = new FakeJsonSchema();
assertThat(schema.rootSchema(), is(sameInstance(schema)));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* The MIT License
*
* Copyright 2023 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;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.sameInstance;

import io.github.sebastiantoepfer.jsonschema.keyword.Keyword;
import jakarta.json.JsonValue;
import java.util.Optional;
import org.junit.jupiter.api.Test;

class JsonSubSchemaTest {

@Test
void should_return_its_owner_as_root() {
final JsonSchema root = new FakeJsonSchemaFactory.FakeJsonSchema();
assertThat(
new JsonSubSchema() {
@Override
public JsonSchema owner() {
return root;
}

@Override
public Validator validator() {
throw new UnsupportedOperationException("Not supported yet.");
}

@Override
public Optional<Keyword> keywordByName(String name) {
throw new UnsupportedOperationException("Not supported yet.");
}

@Override
public JsonValue.ValueType getValueType() {
throw new UnsupportedOperationException("Not supported yet.");
}

@Override
public Optional<JsonSubSchema> asSubSchema(String name) {
throw new UnsupportedOperationException("Not supported yet.");
}
}
.rootSchema(),
is(sameInstance(root))
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import static org.hamcrest.Matchers.is;

import io.github.sebastiantoepfer.jsonschema.JsonSchema;
import jakarta.json.JsonValue;
import org.junit.jupiter.api.Test;

class KeywordTypeTest {
Expand All @@ -46,7 +45,7 @@ public String name() {
}

@Override
public Keyword createKeyword(final JsonSchema schema, final JsonValue value) {
public Keyword createKeyword(final JsonSchema schema) {
throw new UnsupportedOperationException("Not supported yet.");
}
}
Expand Down
31 changes: 31 additions & 0 deletions api/src/test/java/module-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* The MIT License
*
* Copyright 2023 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.
*/

open module io.github.sebastiantoepfer.jsonschema {
requires jakarta.json;

requires org.junit.jupiter.api;
requires org.junit.jupiter.params;
requires org.hamcrest;
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,16 @@
import static java.util.stream.Collectors.collectingAndThen;
import static java.util.stream.Collectors.toList;

import io.github.sebastiantoepfer.jsonschema.InstanceType;
import io.github.sebastiantoepfer.jsonschema.JsonSubSchema;
import io.github.sebastiantoepfer.jsonschema.Validator;
import io.github.sebastiantoepfer.jsonschema.core.codition.AllOfCondition;
import io.github.sebastiantoepfer.jsonschema.core.codition.ApplicatorBasedCondtion;
import io.github.sebastiantoepfer.jsonschema.core.codition.AssertionBasedCondition;
import io.github.sebastiantoepfer.jsonschema.core.codition.Condition;
import io.github.sebastiantoepfer.jsonschema.core.vocab.core.VocabularyKeywordType;
import io.github.sebastiantoepfer.jsonschema.keyword.Keyword;
import io.github.sebastiantoepfer.jsonschema.keyword.KeywordType;
import io.github.sebastiantoepfer.jsonschema.vocabulary.spi.VocabularyDefinition;
import io.github.sebastiantoepfer.jsonschema.vocabulary.spi.VocabularyDefinitions;
import jakarta.json.JsonObject;
Expand All @@ -41,13 +44,10 @@
import java.util.Optional;
import java.util.stream.Stream;

final class DefaultJsonSchema extends AbstractJsonValueSchema {
final class DefaultJsonObjectSchema extends AbstractJsonValueSchema {

private final Keywords keywords;

public DefaultJsonSchema(final JsonObject value) {
public DefaultJsonObjectSchema(final JsonObject value) {
super(value);
keywords = new Keywords(vocabulary());
}

@Override
Expand All @@ -65,18 +65,32 @@ public Optional<Keyword> keywordByName(final String name) {
return keywords().filter(k -> k.hasName(name)).findFirst();
}

private Stream<Keyword> keywords() {
final Keywords keywords = new Keywords(vocabulary());
return asJsonObject().keySet().stream().map(propertyName -> keywords.createKeywordFor(this, propertyName));
}

private Collection<VocabularyDefinition> vocabulary() {
return new KeywordSearch(new VocabularyKeywordType())
.searchForKeywordIn(this)
final KeywordType keywordType = new VocabularyKeywordType();
return Optional
.ofNullable(asJsonObject().get(keywordType.name()))
.map(keywordValue -> keywordType.createKeyword(this))
.filter(VocabularyDefinitions.class::isInstance)
.map(VocabularyDefinitions.class::cast)
.stream()
.flatMap(VocabularyDefinitions::definitions)
.toList();
}

private Stream<Keyword> keywords() {
return asJsonObject().entrySet().stream().map(property -> keywords.createKeywordFor(this, property));
@Override
public Optional<JsonSubSchema> asSubSchema(final String name) {
return Optional
.ofNullable(asJsonObject().get(name))
.filter(value ->
Stream.of(InstanceType.BOOLEAN, InstanceType.OBJECT).anyMatch(type -> type.isInstance(value))
)
.map(new DefaultJsonSchemaFactory()::create)
.map(subSchema -> new DefaultJsonSubSchema(this, subSchema));
}

private Optional<Condition<JsonValue>> asContraint(final Keyword keyword) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public JsonSchema create(final JsonValue schema) {
} else if (schema.equals(JsonValue.EMPTY_JSON_OBJECT)) {
result = new EmptyJsonSchema();
} else {
result = new DefaultJsonSchema(schema.asJsonObject());
result = new DefaultJsonObjectSchema(schema.asJsonObject());
}
return result;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* The MIT License
*
* Copyright 2023 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;

import io.github.sebastiantoepfer.jsonschema.JsonSchema;
import io.github.sebastiantoepfer.jsonschema.JsonSubSchema;
import io.github.sebastiantoepfer.jsonschema.Validator;
import io.github.sebastiantoepfer.jsonschema.keyword.Keyword;
import jakarta.json.JsonObject;
import java.util.Objects;
import java.util.Optional;

final class DefaultJsonSubSchema implements JsonSubSchema {

private final JsonSchema owner;
private final JsonSchema schema;

public DefaultJsonSubSchema(final JsonSchema owner, final JsonSchema schema) {
this.owner = Objects.requireNonNull(owner);
this.schema = Objects.requireNonNull(schema);
}

@Override
public JsonSchema owner() {
return owner;
}

@Override
public Validator validator() {
return schema.validator();
}

@Override
public Optional<Keyword> keywordByName(final String name) {
return schema.keywordByName(name);
}

@Override
public Optional<JsonSubSchema> asSubSchema(final String name) {
return schema.asSubSchema(name);
}

@Override
public ValueType getValueType() {
return schema.getValueType();
}

@Override
public JsonObject asJsonObject() {
return schema.asJsonObject();
}
}
Loading