Progressed on deserializing schemas.

This commit is contained in:
David Noble
2019-09-17 21:38:19 -07:00
parent 4c77aef689
commit a19fa1f8a0
3 changed files with 17 additions and 10 deletions

View File

@@ -40,7 +40,7 @@ public final class Json {
try { try {
return Optional.of(reader.forType(type).readValue(stream)); return Optional.of(reader.forType(type).readValue(stream));
} catch (IOException error) { } catch (IOException error) {
logger.error("", error); logger.error("failed to parse %s due to ", type.getName(), error);
return Optional.empty(); return Optional.empty();
} }
} }

View File

@@ -6,24 +6,22 @@ package com.azure.data.cosmos.serialization.hybridrow.layouts;
import com.azure.data.cosmos.serialization.hybridrow.SchemaId; import com.azure.data.cosmos.serialization.hybridrow.SchemaId;
import com.azure.data.cosmos.serialization.hybridrow.schemas.Namespace; import com.azure.data.cosmos.serialization.hybridrow.schemas.Namespace;
import com.google.common.base.Suppliers; import com.google.common.base.Suppliers;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.URL; import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.security.CodeSource; import java.security.CodeSource;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.Optional; import java.util.Optional;
import java.util.function.Supplier; import java.util.function.Supplier;
import static com.google.common.base.Preconditions.checkState;
import static com.google.common.base.Strings.lenientFormat; import static com.google.common.base.Strings.lenientFormat;
public final class SystemSchema { public final class SystemSchema {
public static final String specificationTitle = "HybridRow serialization library";
/** /**
* SchemaId of the empty schema. This schema has no defined cells but can accomodate * SchemaId of the empty schema. This schema has no defined cells but can accomodate
* unschematized sparse content. * unschematized sparse content.
@@ -46,15 +44,16 @@ public final class SystemSchema {
final Optional<Namespace> namespace; final Optional<Namespace> namespace;
try (final InputStream stream = getResourceAsStream("SystemSchema.json")) { try (final InputStream stream = getResourceAsStream("SystemSchema.json")) {
namespace = Namespace.parse(stream); namespace = Namespace.parse(stream);
} catch (IOException cause) { } catch (IOException cause) {
String message = lenientFormat("Failed to load SystemSchema.json due to %s", cause); String message = lenientFormat("failed to initialize %s due to %s", cause);
throw new IllegalStateException(message, cause); throw new IllegalStateException(message, cause);
} }
return new LayoutResolverNamespace(namespace.get()); return new LayoutResolverNamespace(namespace.orElseThrow(() -> {
String message = lenientFormat("failed to initialize %s due to system schema parse error");
return new IllegalStateException(message);
}));
}); });
private SystemSchema() { private SystemSchema() {

View File

@@ -5,6 +5,8 @@ package com.azure.data.cosmos.serialization.hybridrow.schemas;
import com.azure.data.cosmos.core.Json; import com.azure.data.cosmos.core.Json;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.InputStream; import java.io.InputStream;
import java.util.ArrayList; import java.util.ArrayList;
@@ -13,6 +15,8 @@ import java.util.Optional;
public class Namespace { public class Namespace {
private static final Logger logger = LoggerFactory.getLogger(Json.class);
@JsonProperty(required = true) @JsonProperty(required = true)
private String name; private String name;
@@ -102,7 +106,11 @@ public class Namespace {
*/ */
public static Optional<Namespace> parse(InputStream stream) { public static Optional<Namespace> parse(InputStream stream) {
Optional<Namespace> namespace = Json.parse(stream, Namespace.class); Optional<Namespace> namespace = Json.parse(stream, Namespace.class);
namespace.ifPresent(SchemaValidator::validate); try {
namespace.ifPresent(SchemaValidator::validate);
} catch (SchemaException error) {
logger.error("failed to parse {} due to ", Namespace.class, error);
}
return namespace; return namespace;
} }
} }