Skip to content

Commit

Permalink
Merge pull request #73 from sebastian-toepfer/printable
Browse files Browse the repository at this point in the history
make it printable
  • Loading branch information
sebastian-toepfer authored Jan 13, 2024
2 parents b1acfd3 + 4d59f73 commit a28c5c8
Show file tree
Hide file tree
Showing 80 changed files with 980 additions and 174 deletions.
14 changes: 14 additions & 0 deletions api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@
<name>Json Schema :: api</name>

<dependencies>
<dependency>
<groupId>io.github.sebastian-toepfer.ddd</groupId>
<artifactId>common</artifactId>
</dependency>
<dependency>
<groupId>io.github.sebastian-toepfer.ddd</groupId>
<artifactId>media-json-api</artifactId>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
Expand All @@ -33,6 +42,11 @@
<artifactId>hamcrest</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.github.sebastian-toepfer.ddd</groupId>
<artifactId>media-core</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>jakarta.json</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Collection;
import java.util.Locale;

/**
* see: http://json-schema.org/draft/2020-12/json-schema-core.html#name-instance-data-model
Expand All @@ -41,13 +42,7 @@ public enum InstanceType {
INTEGER() {
@Override
public boolean isInstance(final JsonValue value) {
final boolean result;
if (value instanceof JsonNumber nr) {
result = isIntegral(nr.bigDecimalValue());
} else {
result = false;
}
return result;
return value instanceof JsonNumber nr && isIntegral(nr.bigDecimalValue());
}

@SuppressWarnings("BigDecimalEquals")
Expand All @@ -62,6 +57,10 @@ private boolean isIntegral(final BigDecimal decimal) {
},
STRING(JsonValue.ValueType.STRING);

public static InstanceType fromString(final String name) {
return valueOf(name.toUpperCase(Locale.US));
}

@SuppressWarnings("ImmutableEnumChecker")
private final Collection<JsonValue.ValueType> validTypes;

Expand All @@ -72,4 +71,9 @@ private boolean isIntegral(final BigDecimal decimal) {
public boolean isInstance(final JsonValue value) {
return validTypes.contains(value.getValueType());
}

@Override
public String toString() {
return name().toLowerCase(Locale.US);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@
*/
package io.github.sebastiantoepfer.jsonschema;

import io.github.sebastiantoepfer.ddd.common.Printable;
import io.github.sebastiantoepfer.jsonschema.keyword.Keyword;
import jakarta.json.JsonValue;
import java.util.Optional;

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

Optional<Keyword> keywordByName(String name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@
*/
package io.github.sebastiantoepfer.jsonschema.keyword;

import io.github.sebastiantoepfer.ddd.common.Printable;
import java.util.Collection;

/**
* see: https://json-schema.org/draft/2020-12/json-schema-core.html#name-json-schema-objects-and-key
**/
public interface Keyword {
public interface Keyword extends Printable {
default Identifier asIdentifier() {
return (Identifier) this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
*/
package io.github.sebastiantoepfer.jsonschema.keyword;

import io.github.sebastiantoepfer.ddd.common.Media;
import io.github.sebastiantoepfer.ddd.media.json.JsonObjectPrintable;
import jakarta.json.Json;
import jakarta.json.JsonValue;
import java.util.Objects;

Expand Down Expand Up @@ -51,4 +54,9 @@ public boolean hasName(final String name) {
public JsonValue valueFor(final JsonValue instance) {
return value;
}

@Override
public <T extends Media<T>> T printOn(final T media) {
return new JsonObjectPrintable(Json.createObjectBuilder().add(name, value).build()).printOn(media);
}
}
2 changes: 2 additions & 0 deletions api/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,7 @@
exports io.github.sebastiantoepfer.jsonschema.keyword;
exports io.github.sebastiantoepfer.jsonschema.spi;

requires io.github.sebastiantoepfer.ddd.common;
requires io.github.sebastiantoepfer.ddd.media.json;
requires jakarta.json;
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/
package io.github.sebastiantoepfer.jsonschema;

import io.github.sebastiantoepfer.ddd.common.Media;
import io.github.sebastiantoepfer.jsonschema.keyword.Keyword;
import io.github.sebastiantoepfer.jsonschema.spi.JsonSchemaFactory;
import jakarta.json.JsonValue;
Expand Down Expand Up @@ -56,5 +57,10 @@ public JsonValue.ValueType getValueType() {
public Optional<JsonSubSchema> asSubSchema(String name) {
throw new UnsupportedOperationException("Not supported yet.");
}

@Override
public <T extends Media<T>> T printOn(final T media) {
throw new UnsupportedOperationException("Not supported yet.");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,13 @@

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.params.provider.Arguments.arguments;

import jakarta.json.Json;
import jakarta.json.JsonValue;
import java.util.stream.Stream;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
Expand All @@ -43,15 +46,15 @@ void should_be_true_if_instancetype_is_correct(final InstanceType instanceType,

static Stream<? extends Arguments> provideWithValidCombinations() {
return Stream.of(
Arguments.of(InstanceType.NULL, JsonValue.NULL),
Arguments.of(InstanceType.BOOLEAN, JsonValue.FALSE),
Arguments.of(InstanceType.BOOLEAN, JsonValue.TRUE),
Arguments.of(InstanceType.OBJECT, JsonValue.EMPTY_JSON_OBJECT),
Arguments.of(InstanceType.ARRAY, JsonValue.EMPTY_JSON_ARRAY),
Arguments.of(InstanceType.NUMBER, Json.createValue(23L)),
Arguments.of(InstanceType.INTEGER, Json.createValue(23L)),
Arguments.of(InstanceType.INTEGER, Json.createValue(10L)),
Arguments.of(InstanceType.INTEGER, Json.createValue(0.0))
arguments(InstanceType.NULL, JsonValue.NULL),
arguments(InstanceType.BOOLEAN, JsonValue.FALSE),
arguments(InstanceType.BOOLEAN, JsonValue.TRUE),
arguments(InstanceType.OBJECT, JsonValue.EMPTY_JSON_OBJECT),
arguments(InstanceType.ARRAY, JsonValue.EMPTY_JSON_ARRAY),
arguments(InstanceType.NUMBER, Json.createValue(23L)),
arguments(InstanceType.INTEGER, Json.createValue(23L)),
arguments(InstanceType.INTEGER, Json.createValue(10L)),
arguments(InstanceType.INTEGER, Json.createValue(0.0))
);
}

Expand All @@ -63,14 +66,46 @@ void should_be_false_if_instancetype_is_correct(final InstanceType instanceType,

static Stream<? extends Arguments> provideWithInvalidCombinations() {
return Stream.of(
Arguments.of(InstanceType.NULL, JsonValue.TRUE),
Arguments.of(InstanceType.BOOLEAN, JsonValue.EMPTY_JSON_OBJECT),
Arguments.of(InstanceType.BOOLEAN, JsonValue.EMPTY_JSON_ARRAY),
Arguments.of(InstanceType.OBJECT, JsonValue.EMPTY_JSON_ARRAY),
Arguments.of(InstanceType.ARRAY, JsonValue.TRUE),
Arguments.of(InstanceType.NUMBER, Json.createValue("string")),
Arguments.of(InstanceType.INTEGER, Json.createValue(23.2)),
Arguments.of(InstanceType.INTEGER, Json.createValue("test"))
arguments(InstanceType.NULL, JsonValue.TRUE),
arguments(InstanceType.BOOLEAN, JsonValue.EMPTY_JSON_OBJECT),
arguments(InstanceType.BOOLEAN, JsonValue.EMPTY_JSON_ARRAY),
arguments(InstanceType.OBJECT, JsonValue.EMPTY_JSON_ARRAY),
arguments(InstanceType.ARRAY, JsonValue.TRUE),
arguments(InstanceType.NUMBER, Json.createValue("string")),
arguments(InstanceType.INTEGER, Json.createValue(23.2)),
arguments(InstanceType.INTEGER, Json.createValue("test"))
);
}

@ParameterizedTest
@MethodSource("provideWithName")
void should_return_his_name(final InstanceType instanceType, final String value) {
assertThat(instanceType, Matchers.hasToString(value));
}

static Stream<? extends Arguments> provideWithName() {
return Stream.of(
arguments(InstanceType.NULL, "null"),
arguments(InstanceType.BOOLEAN, "boolean"),
arguments(InstanceType.OBJECT, "object"),
arguments(InstanceType.ARRAY, "array"),
arguments(InstanceType.NUMBER, "number"),
arguments(InstanceType.INTEGER, "integer")
);
}

@Test
void should_be_createable_from_lowercase_value() {
assertThat(InstanceType.fromString("object"), is(InstanceType.OBJECT));
}

@Test
void should_be_createable_from_uppercase_value() {
assertThat(InstanceType.fromString("NUMBER"), is(InstanceType.NUMBER));
}

@Test
void should_be_createable_from_mixcase_value() {
assertThat(InstanceType.fromString("Null"), is(InstanceType.NULL));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.sameInstance;

import io.github.sebastiantoepfer.ddd.common.Media;
import io.github.sebastiantoepfer.jsonschema.keyword.Keyword;
import jakarta.json.JsonValue;
import java.util.Optional;
Expand All @@ -50,7 +51,7 @@ public Validator validator() {
}

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

Expand All @@ -60,7 +61,12 @@ public JsonValue.ValueType getValueType() {
}

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

@Override
public <T extends Media<T>> T printOn(final T media) {
throw new UnsupportedOperationException("Not supported yet.");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;

import io.github.sebastiantoepfer.ddd.common.Media;
import jakarta.json.JsonValue;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test;
Expand All @@ -52,12 +53,17 @@ void should_return_this_as_annotation() {
private static class TestAnnotation implements Annotation {

@Override
public JsonValue valueFor(JsonValue value) {
public JsonValue valueFor(final JsonValue value) {
throw new UnsupportedOperationException("Not supported yet.");
}

@Override
public boolean hasName(String name) {
public boolean hasName(final String name) {
throw new UnsupportedOperationException("Not supported yet.");
}

@Override
public <T extends Media<T>> T printOn(final T media) {
throw new UnsupportedOperationException("Not supported yet.");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;

import io.github.sebastiantoepfer.ddd.common.Media;
import jakarta.json.JsonValue;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test;
Expand All @@ -52,12 +53,17 @@ void should_return_this_as_applicator() {
private static class TestApplicator implements Applicator {

@Override
public boolean applyTo(JsonValue instance) {
public boolean applyTo(final JsonValue instance) {
throw new UnsupportedOperationException("Not supported yet.");
}

@Override
public boolean hasName(String name) {
public boolean hasName(final String name) {
throw new UnsupportedOperationException("Not supported yet.");
}

@Override
public <T extends Media<T>> T printOn(final T media) {
throw new UnsupportedOperationException("Not supported yet.");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;

import io.github.sebastiantoepfer.ddd.common.Media;
import jakarta.json.JsonValue;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test;
Expand All @@ -52,12 +53,17 @@ void should_return_this_as_assertion() {
private static class TestAssertion implements Assertion {

@Override
public boolean isValidFor(JsonValue instance) {
public boolean isValidFor(final JsonValue instance) {
throw new UnsupportedOperationException("Not supported yet.");
}

@Override
public boolean hasName(String name) {
public boolean hasName(final String name) {
throw new UnsupportedOperationException("Not supported yet.");
}

@Override
public <T extends Media<T>> T printOn(final T media) {
throw new UnsupportedOperationException("Not supported yet.");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;

import io.github.sebastiantoepfer.ddd.common.Media;
import io.github.sebastiantoepfer.jsonschema.keyword.Keyword.KeywordCategory;
import java.net.URI;
import org.hamcrest.Matchers;
Expand Down Expand Up @@ -58,7 +59,12 @@ public URI asUri() {
}

@Override
public boolean hasName(String name) {
public boolean hasName(final String name) {
throw new UnsupportedOperationException("Not supported yet.");
}

@Override
public <T extends Media<T>> T printOn(final T media) {
throw new UnsupportedOperationException("Not supported yet.");
}
}
Expand Down
Loading

0 comments on commit a28c5c8

Please sign in to comment.