Skip to content

Commit

Permalink
fix (core): Meta 0.3
Browse files Browse the repository at this point in the history
  • Loading branch information
vorburger committed Jul 28, 2024
1 parent 886a2c5 commit ffd8dcd
Show file tree
Hide file tree
Showing 19 changed files with 397 additions and 299 deletions.
27 changes: 27 additions & 0 deletions java/dev/enola/common/io/ByteSourcer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright 2024 The Enola <https://enola.dev> Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dev.enola.common.io;

import com.google.common.io.ByteSource;

public interface ByteSourcer {

// TODO See also https://github.com/enola-dev/enola/pull/478/files

ByteSource asByteSource();
}
8 changes: 6 additions & 2 deletions java/dev/enola/model/enola/meta/Datatype.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@ public interface Datatype extends Type {

String CLASS_IRI = "https://enola.dev/meta/Datatype";

String java();

// TODO @IRI(KIRI.E.META.PARENT)
// Intentionally only singular instead of multiple
Datatype parent();

String java();

String proto();

URI xsd();

// TODO Pattern regExp();
Expand All @@ -45,6 +47,8 @@ interface Builder<B extends Datatype> extends Datatype, Type.Builder<B> { // ski

Datatype.Builder<B> java(String datatype);

Datatype.Builder<B> proto(String proto);

Datatype.Builder<B> xsd(URI xsd);
}
}
22 changes: 17 additions & 5 deletions java/dev/enola/model/enola/meta/bootstrap/MutableDatatype.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@
// NB: This hand-written class may eventually get replaced by a code-generated one!
public class MutableDatatype extends MutableType implements Datatype, Datatype.Builder {

private String java;
private URI xsd;
private String java;
private String proto;

// TODO Use String IDs instead of Datatype, and use TLC/TP get() in accessors?!
private Datatype parent;
Expand All @@ -39,6 +40,17 @@ public Class type() {
return TLC.get(ThingProvider.class).get(Datatype.CLASS_IRI, Class.class);
}

@Override
public Datatype parent() {
return parent;
}

@Override
public Datatype.Builder parent(Datatype datatype) {
this.parent = datatype;
return this;
}

@Override
public String java() {
return java;
Expand All @@ -51,13 +63,13 @@ public Datatype.Builder java(String java) {
}

@Override
public Datatype parent() {
return parent;
public String proto() {
return proto;
}

@Override
public Datatype.Builder parent(Datatype datatype) {
this.parent = datatype;
public Datatype.Builder proto(String proto) {
this.proto = proto;
return this;
}

Expand Down
27 changes: 20 additions & 7 deletions java/dev/enola/model/enola/meta/io/MetaTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,42 @@
*/
package dev.enola.model.enola.meta.io;

import static com.google.common.truth.Truth.assertThat;

import dev.enola.common.io.resource.ClasspathResource;
import dev.enola.common.yamljson.test.TestYaml;
import dev.enola.model.enola.meta.Schema;

import org.junit.Test;

import java.io.IOException;

public class MetaTest {

private void check(String name) throws IOException {
new SchemaIO()
.readYAML(
new ClasspathResource("enola.dev/" + name + ".yaml"),
schema -> TestYaml.assertEqualsToResource(schema, name + ".expected.yaml"));
private Schema read(String name) throws IOException {
return new SchemaIO().readYAML(new ClasspathResource(name + ".yaml"));
}

private Schema expected(String name) throws IOException {
var schema = read(name);
TestYaml.assertEqualsToResource(schema, name + ".expected.yaml");
return schema;
}

@Test
public void readCommonYAML() throws IOException {
check("common");
read("enola.dev/common");
}

@Test
public void readMetaSchemaYAML() throws IOException {
check("meta");
read("enola.dev/meta");
}

@Test
public void testSchemaYAML() throws IOException {
var test = expected("test.esch");
assertThat(test.name()).isEqualTo("Test");
// NB: We're not asserting on every attribute, because test.esch.expected.yaml did!
}
}
18 changes: 14 additions & 4 deletions java/dev/enola/model/enola/meta/io/SchemaIO.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,21 @@
import java.net.URI;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

public class SchemaIO {

// TODO Use dev.enola.thing.validation instead of throw new IllegalArgumentException
// TODO Use dev.enola.thing.validation and errors.esch.yaml instead of IllegalArgumentException

public Schema readYAML(ReadableResource resource) throws IOException {
// TODO Avoid AtomicReference
AtomicReference<Schema> schema = new AtomicReference<>();
readYAML(resource, schema1 -> schema.set(schema1));
return schema.get();
}

// TODO Schema readYAML(ReadableResource resource)
public void readYAML(ReadableResource resource, Consumer<Schema> schemay) throws IOException {
var repo = new MetaThingByIdProvider.Builder();
YAML.readSingleMap(
Expand Down Expand Up @@ -126,6 +133,7 @@ private void readDatatypes(
var datatype = repo.datatype(schema, name);
readCommon(datatypeMap, datatype, schema.iri());
datatype.java(getRemoveString(datatypeMap, "java:type"));
datatype.proto(getRemoveString(datatypeMap, "proto"));

var xsd = getRemoveString(datatypeMap, "xsd");
// TODO Resolve CURIE, if it is one - but how do we know?
Expand Down Expand Up @@ -201,6 +209,8 @@ private void readCommon(Map<?, ?> map, Common.Builder<?> common, String schemaIR

common.label(getRemoveString(map, "label"));
common.description(getRemoveString(map, "description"));
// TODO Handle description-md VS description...
common.description(getRemoveString(map, "description-md"));
// TODO Resolve enola:emoji vs label / description (without enola:) inconsistency...
common.emoji(getRemoveString(map, "enola:emoji"));
}
Expand Down Expand Up @@ -251,10 +261,10 @@ private void setIRI(HasName.Builder named, String schemaIRI) {
private @Nullable List<?> asList(@Nullable Object object) {
if (object == null) return null;
if (object instanceof List<?> list) return list;
throw new IllegalArgumentException(
"Should be List, but is: " + object.getClass() + " " + object);
return List.of(object);
}

// TODO Remove when finally fully switching to Thing, where it's *OK* to have extra!
private void checkEmpty(Map<?, ?> map) {
if (!map.isEmpty()) {
throw new IllegalArgumentException("Unknown properties: " + map);
Expand Down
2 changes: 2 additions & 0 deletions java/dev/enola/thing/java/test/TestThing.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
@JThing("https://enola.dev/TestThing")
public interface TestThing extends Thing {

// TODO Replace with code gen. from test.esch.yaml

// NB: This is only here like this for TestThingTest; otherwise this would be inlined in @IRI!
String NUMBER_URI = "https://enola.dev/test/number";

Expand Down
120 changes: 113 additions & 7 deletions models/enola.dev/common.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ datatypes:
types!
iri: https://enola.dev/String
java:type: java.lang.String
proto: string
xsd: xsd:string
Text:
description:
Expand All @@ -41,28 +42,28 @@ datatypes:
iri: https://enola.dev/Text1
parent: Text
Markdown:
description: Text which is interpreted as Markdown.
description-md: Text which is interpreted as [Common Markdown](https://commonmark.org) syntax.
iri: https://enola.dev/Markdown
parent: Text
Identifier:
description: String which identifies things to machines; not necessarily globally unique.
iri: https://enola.dev/Identifier
parent: String
Name:
description: Camel Case, with underscores OK, but no spaces or dots or colons or hyphens.
description: Identifier with CamelCase, with underscores OK, but no spaces or dots or colons or hyphens etc.
iri: https://enola.dev/Name
parent: Identifier
# TODO regexp: "^[a-zA-Z][a-zA-Z0-9_]*$"
xsd: xsd:NCName
# TODO Having both Names & ID is a bit confusing... can we get rid of one of them?!
Names:
description: With underscores and dots, but no spaces or colons or hyphens.
description: Identifier with underscores and dots, but no spaces or colons or hyphens etc.
iri: https://enola.dev/Names
parent: Identifier
# TODO regexp: "^[a-zA-Z][a-zA-Z0-9_.]*$"
xsd: xsd:NMTOKEN
ID:
description: With underscores and dots and colons, but no spaces or hyphens or slashes.
description: Identifier with underscores and dots and colons, but no spaces or hyphens or slashes.
iri: https://enola.dev/ID
parent: Identifier
# TODO regexp: "^[a-zA-Z][a-zA-Z0-9_.:]*$"
Expand All @@ -84,19 +85,109 @@ datatypes:
xsd: xsd:anyURI
MediaType:
iri: https://enola.dev/MediaType
description: Internet Media Type, also known as a MIME Type or Content Type.
java:type: com.google.common.net.MediaType
# TODO enola:rfc: 6838
Binary:
java:type: dev.enola.common.io.ByteSourcer
proto: bytes
xsd: xsd:base64Binary
Boolean:
java:type: java.lang.Boolean
proto: bool
xsd: xsd:boolean
Long:
description: Number between -9223372036854776000 (-2^63) and 9223372036854776000 (2^63 - 1)
java:type: java.lang.Long
proto: sint64
xsd: xsd:long
UnsignedLong:
description: Number between 0 and 18446744073709551615 (2^64 − 1).
java:type: java.lang.Long
proto: uint64
xsd: xsd:unsignedLong
Integer32:
description: Number between -2147483648 (-2^31) and 2147483647 (2^31 - 1). Not to confuse with BigInteger.
java:type: java.lang.Integer
proto: sint32
xsd: xsd:int # NOT xsd:integer!
UnsignedInteger32:
description: Number between 0 and 4294967295 (2^32 - 1). Not to confuse with BigInteger.
java:type: java.lang.Integer
proto: uint32
xsd: xsd:unsignedInt # NOT xsd:negativeInteger!
Short:
description: Number between -32768 (-2^15) and 32767 (2^15 - 1).
java:type: java.lang.Short
proto: sint32 # ProtoBuf has no Short
xsd: xsd:short
UnsignedShort:
description: Number between 0 and 65536 (2^16).
java:type: java.lang.Short
proto: unit32 # ProtoBuf has no Short
xsd: xsd:unsignedShort
Byte:
description: Number between -256 (-2^8) and 255 (2^8 - 1).
java:type: java.lang.Byte
proto: sint32 # ProtoBuf has no Byte
xsd: xsd:byte
UnsignedByte:
description: Number between 0 and 256 (2^8).
java:type: java.lang.Byte
proto: uint32 # ProtoBuf has no Byte
xsd: xsd:unsignedByte
BigInteger:
description:
Number without dot (.) without any lower or upper bound; AKA an arbitrary-precision signed integer number. E.g.
-123456789012345678901234567890.
java:type: java.math.BigInteger
# ProtoBuf does not have this!
xsd: xsd:integer
Decimal:
description:
Number possibly with dot (.) without any lower or upper bound; AKA an arbitrary-precision signed decimal
number. E.g. -123456789012345678901234567890.123.
java:type: java.math.BigDecimal
# ProtoBuf does not have this!
xsd: xsd:decimal
Double:
description:
Number that's a double-precision 64-bit IEEE 754 floating point, including Not-a-Number (NaN), both -0 and +1, and
+/- ♾️ infinity.
java:type: java.lang.Double
proto: double
xsd: xsd:double
Float:
description:
Number that's a single-precision 32-bit IEEE 754 floating point, including Not-a-Number (NaN), both -0 and +1, and
+/- ♾️ infinity.
java:type: java.lang.Float
proto: float
xsd: xsd:float
Timestamp:
description: Date and Time; global, without timezone.
iri: https://enola.dev/Timestamp
java:type: java.time.Instant
proto: google.protobuf.Timestamp
xsd: xsd:dateTime
enola:emoji: 🕑
HTML:
parent: rdf:HTML
description: HTML data.
java:type: org.jsoup.nodes.Document
JSON:
description: JSON data.
YAML:
description: YAML data.
XML:
parent: rdf:XMLLiteral
description: XML data.
java:type: org.w3c.dom.Document

properties:
name:
type: Name
description: Name of Thing. When part of an ID, often its (and IRI's) last segment.
iri: https://enola.dev/name
label:
type: String
iri: https://www.w3.org/TR/rdf-schema/#ch_label
Expand All @@ -106,12 +197,27 @@ properties:
type: String
iri: https://schema.org/description
description: Description of Thing. Basically a longer label.
description_md:
type: Markdown
parent: description
iri: https://enola.dev/description_md
description: Description of Thing, in Markdown format. Basically a longer label.
emoji:
type: String
iri: https://enola.dev/emoji
iri_template:
type: String # Templates are *NOT* valid IRI (or URL)
iri: https://enola.org/IRITemplate
example:
type: IRI
iri: https://enola.dev/example
enid:
description:
Enola Numeric ID. Identifier of Thing which is unique within a given Enola instance (only, not globally).
type: UnsignedLong
origin:
description: What something is "based on", e.g. where it "comes from".
enola:emoji: "🏺"
type: IRI
origins:
description: What something is "based on", e.g. where it "comes from".
enola:emoji: "🏺"
type: IRI*
Loading

0 comments on commit ffd8dcd

Please sign in to comment.