Skip to content

Commit

Permalink
Implement implicit ID
Browse files Browse the repository at this point in the history
  • Loading branch information
prdoyle committed Aug 24, 2023
1 parent 157a2f2 commit 0533f9a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
18 changes: 17 additions & 1 deletion bosk-core/src/main/java/io/vena/bosk/SerializationPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,23 @@ public final List<Object> parameterValueList(Class<?> nodeClass, Map<String, Obj
} else if (Phantom.class.equals(type)) {
parameterValues.add(Phantom.empty());
} else {
throw new DeserializationException("Missing field: " + name);
Path path = currentScope.get().path();
if ("id".equals(name) && !path.isEmpty()) {
// If the object is an entry in a Catalog or a key in a SideTable, we can determine its ID
Reference<Object> enclosingRef;
try {
enclosingRef = bosk.rootReference().then(Object.class, path.truncatedBy(1));
} catch (InvalidTypeException e) {
throw new AssertionError("Non-empty path must have an enclosing reference: " + path, e);
}
if (AddressableByIdentifier.class.isAssignableFrom(enclosingRef.targetClass())) {
parameterValues.add(Identifier.from(path.lastSegment()));
} else {
throw new DeserializationException("Missing id field for object at " + path);
}
} else {
throw new DeserializationException("Missing field \"" + name + "\" at " + path);
}
}
} else if (implicitReference == null) {
parameterValues.add(value);
Expand Down
10 changes: 10 additions & 0 deletions docs/USERS.md
Original file line number Diff line number Diff line change
Expand Up @@ -801,6 +801,13 @@ A field of type `Optional<T>` is simply serialized as a `T`, unless the optional

A field of type `Phantom<T>` is not serialized (just like `Optional.empty()`).

The `id` field of a Catalog entry or a SideTable key may be omitted,
and will be inferred during deserialization if possible from context,
including any `@DeserializationPath` annotations.
This inference process takes some time, though,
so for best performance, it's better for the JSON input to include the `id` field,
just as it does when serialized.

#### DeserializationScope

Fields marked as `@Self` or `@Enclosing` are not serialized.
Expand All @@ -825,6 +832,9 @@ try (var __ = jacksonPlugin.newDeserializationScope(ref)) {
For this to work, you will need access to the `JacksonPlugin` object,
typically from your dependency injection framework.

For an object whose _fields_ represent specific nodes of the bosk state,
use the `@DeserializationPath` annotation; see the javadocs for more info.

#### DerivedRecord

In inner-loop, high-performance code, it can be too costly to use `Reference.value()` to access node objects, and it is definitely too costly to create new `Reference` objects.
Expand Down

0 comments on commit 0533f9a

Please sign in to comment.