Skip to content

Commit

Permalink
Inline deprecated Bosk reference methods (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
prdoyle authored Jul 18, 2023
2 parents 4d747f5 + 399447b commit 73502ce
Show file tree
Hide file tree
Showing 23 changed files with 84 additions and 79 deletions.
28 changes: 14 additions & 14 deletions bosk-core/src/main/java/io/vena/bosk/Bosk.java
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@ private <S> void triggerCascade(Reference<S> effectiveScope, @Nullable R priorRo
// could correspond to changed objects and then recursing.
//
Path containerPath = effectiveScope.path().truncatedTo(effectiveScope.path().firstParameterIndex());
Reference<EnumerableByIdentifier<?>> containerRef = reference(enumerableByIdentifierClass(), containerPath);
Reference<EnumerableByIdentifier<?>> containerRef = rootReference().then(enumerableByIdentifierClass(), containerPath);
EnumerableByIdentifier<?> priorContainer = refValueIfExists(containerRef, priorRoot);
EnumerableByIdentifier<?> newContainer = refValueIfExists(containerRef, newRoot);

Expand Down Expand Up @@ -777,25 +777,25 @@ public <U> Reference<U> then(Class<U> requestedClass, Path path) throws InvalidT

@Override
public <E extends Entity> CatalogReference<E> thenCatalog(Class<E> entryClass, Path path) throws InvalidTypeException {
Reference<Catalog<E>> ref = reference(Classes.catalog(entryClass), path);
Reference<Catalog<E>> ref = this.then(Classes.catalog(entryClass), path);
return new CatalogRef<>(ref, entryClass);
}

@Override
public <E extends Entity> ListingReference<E> thenListing(Class<E> entryClass, Path path) throws InvalidTypeException {
Reference<Listing<E>> ref = reference(Classes.listing(entryClass), path);
Reference<Listing<E>> ref = this.then(Classes.listing(entryClass), path);
return new ListingRef<>(ref);
}

@Override
public <K extends Entity, V> SideTableReference<K, V> thenSideTable(Class<K> keyClass, Class<V> valueClass, Path path) throws InvalidTypeException {
Reference<SideTable<K,V>> ref = reference(Classes.sideTable(keyClass, valueClass), path);
Reference<SideTable<K,V>> ref = this.then(Classes.sideTable(keyClass, valueClass), path);
return new SideTableRef<>(ref, keyClass, valueClass);
}

@Override
public <TT> Reference<Reference<TT>> thenReference(Class<TT> targetClass, Path path) throws InvalidTypeException {
return reference(Classes.reference(targetClass), path);
return this.then(Classes.reference(targetClass), path);
}

@Override
Expand Down Expand Up @@ -827,27 +827,27 @@ public RootReference<?> root() {

@Override
public final <U> Reference<U> then(Class<U> targetClass, String... segments) throws InvalidTypeException {
return reference(targetClass, path.then(segments));
return rootReference().then(targetClass, path.then(segments));
}

@Override
public final <U extends Entity> CatalogReference<U> thenCatalog(Class<U> entryClass, String... segments) throws InvalidTypeException {
return catalogReference(entryClass, path.then(segments));
return rootReference().thenCatalog(entryClass, path.then(segments));
}

@Override
public final <U extends Entity> ListingReference<U> thenListing(Class<U> entryClass, String... segments) throws InvalidTypeException {
return listingReference(entryClass, path.then(segments));
return rootReference().thenListing(entryClass, path.then(segments));
}

@Override
public final <K extends Entity, V> SideTableReference<K, V> thenSideTable(Class<K> keyClass, Class<V> valueClass, String... segments) throws InvalidTypeException {
return sideTableReference(keyClass, valueClass, path.then(segments));
return rootReference().thenSideTable(keyClass, valueClass, path.then(segments));
}

@Override
public final <TT> Reference<Reference<TT>> thenReference(Class<TT> targetClass, String... segments) throws InvalidTypeException {
return referenceReference(targetClass, path.then(segments));
return rootReference().thenReference(targetClass, path.then(segments));
}

@SuppressWarnings("unchecked")
Expand All @@ -859,13 +859,13 @@ public final <TT> Reference<TT> enclosingReference(Class<TT> targetClass) throws
for (Path p = this.path.truncatedBy(1); !p.isEmpty(); p = p.truncatedBy(1)) try {
Type targetType = pathCompiler.targetTypeOf(p);
if (targetClass.isAssignableFrom(rawClass(targetType))) {
return reference(targetClass, p);
return rootReference().then(targetClass, p);
}
} catch (InvalidTypeException e) {
throw new InvalidTypeException("Error looking up enclosing " + targetClass.getSimpleName() + " from " + path);
}
// Might be the root
if (targetClass.isAssignableFrom(rawClass(rootRef.targetType()))) {
if (targetClass.isAssignableFrom(rootRef.targetClass())) {
return (Reference<TT>) rootReference();
} else {
throw new InvalidTypeException("No enclosing " + targetClass.getSimpleName() + " from " + path);
Expand Down Expand Up @@ -901,7 +901,7 @@ public final boolean equals(Object obj) {
}

private Type rootType() {
return Bosk.this.rootRef.targetType();
return Bosk.this.rootRef.targetType;
}

@Override
Expand Down Expand Up @@ -976,7 +976,7 @@ public void forEachValue(BiConsumer<T, BindingEnvironment> action, BindingEnviro
Path containerPath = path.truncatedTo(firstParameterIndex);
Reference<EnumerableByIdentifier<?>> containerRef;
try {
containerRef = reference(enumerableByIdentifierClass(), containerPath);
containerRef = rootReference().then(enumerableByIdentifierClass(), containerPath);
} catch (InvalidTypeException e) {
throw new AssertionError("Parameter reference must come after a " + EnumerableByIdentifier.class, e);
}
Expand Down
8 changes: 4 additions & 4 deletions bosk-core/src/main/java/io/vena/bosk/ReferenceBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,16 @@ static <T, R extends StateTreeNode> T buildReferences(Class<T> refsClass, Bosk<R
Path path = Path.parseParameterized(referencePath.value());
if (returnClass.equals(CatalogReference.class)) {
Type entryType = parameterType(returnType, CatalogReference.class, 0);
result = bosk.catalogReference((Class) rawClass(entryType), path);
result = bosk.rootReference().thenCatalog((Class) rawClass(entryType), path);
} else if (returnClass.equals(ListingReference.class)) {
Type entryType = parameterType(returnType, ListingReference.class, 0);
result = bosk.listingReference((Class) rawClass(entryType), path);
result = bosk.rootReference().thenListing((Class) rawClass(entryType), path);
} else if (returnClass.equals(SideTableReference.class)) {
Type keyType = parameterType(returnType, SideTableReference.class, 0);
Type valueType = parameterType(returnType, SideTableReference.class, 1);
result = bosk.sideTableReference((Class) rawClass(keyType), (Class) rawClass(valueType), path);
result = bosk.rootReference().thenSideTable((Class) rawClass(keyType), (Class) rawClass(valueType), path);
} else {
result = bosk.reference(rawClass(targetType), path);
result = bosk.rootReference().then(rawClass(targetType), path);
}
} catch (InvalidTypeException e) {
// Add some troubleshooting info for the user
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ private Reference<?> findImplicitReferenceIfAny(Class<?> nodeClass, Parameter pa
private <T> Reference<T> selfReference(Class<T> targetClass, Bosk<?> bosk) throws AssertionError {
Path currentPath = currentScope.get().path();
try {
return bosk.reference(targetClass, currentPath);
return bosk.rootReference().then(targetClass, currentPath);
} catch (InvalidTypeException e) {
throw new UnexpectedPathException("currentDeserializationPath should be valid: \"" + currentPath + "\"", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public void flush() throws InterruptedException, IOException {
@SuppressWarnings("unchecked")
private <T> Reference<T> correspondingReference(Reference<T> original) {
try {
return (Reference<T>)mirror.reference(Object.class, original.path());
return (Reference<T>) mirror.rootReference().then(Object.class, original.path());
} catch (InvalidTypeException e) {
throw new AssertionError("References are expected to be compatible: " + original, e);
}
Expand Down
43 changes: 22 additions & 21 deletions bosk-core/src/test/java/io/vena/bosk/BoskLocalReferenceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,23 +67,23 @@ public interface Refs {
void initializeBosk() throws InvalidTypeException {
Root initialRoot = new Root(1, Catalog.empty());
bosk = new Bosk<>(BOSK_NAME, Root.class, initialRoot, Bosk::simpleDriver);
refs = bosk.buildReferences(Refs.class);
refs = bosk.rootReference().buildReferences(Refs.class);
Identifier ernieID = Identifier.from("ernie");
Identifier bertID = Identifier.from("bert");
TestEntity ernie = new TestEntity(ernieID, 1,
bosk.reference(TestEntity.class, Path.of(Root.Fields.entities, bertID.toString())),
Catalog.empty(),
Listing.of(refs.entities(), bertID),
SideTable.of(refs.entities(), bertID, "buddy"),
ListValue.empty(),
Optional.empty());
refs.entity(bertID),
Catalog.empty(),
Listing.of(refs.entities(), bertID),
SideTable.of(refs.entities(), bertID, "buddy"),
ListValue.empty(),
Optional.empty());
TestEntity bert = new TestEntity(bertID, 1,
bosk.reference(TestEntity.class, Path.of(Root.Fields.entities, ernieID.toString())),
Catalog.empty(),
Listing.of(refs.entities(), ernieID),
SideTable.of(refs.entities(), ernieID, "pal"),
ListValue.empty(),
Optional.empty());
refs.entity(ernieID),
Catalog.empty(),
Listing.of(refs.entities(), ernieID),
SideTable.of(refs.entities(), ernieID, "pal"),
ListValue.empty(),
Optional.empty());
bosk.driver().submitReplacement(refs.entities(), Catalog.of(ernie, bert));
root = bosk.currentRoot();
}
Expand Down Expand Up @@ -121,9 +121,10 @@ void testCatalogReference() throws Exception {
Class<Catalog<TestEntity>> catalogClass = (Class<Catalog<TestEntity>>)(Class)Catalog.class;
Path entitiesPath = Path.just(Root.Fields.entities);
List<Reference<Catalog<TestEntity>>> testRefs = asList(
bosk.reference(catalogClass, entitiesPath),
bosk.catalogReference(TestEntity.class, entitiesPath),
bosk.rootReference().thenCatalog(TestEntity.class, Root.Fields.entities));
bosk.rootReference().then(catalogClass, entitiesPath),
bosk.rootReference().thenCatalog(TestEntity.class, entitiesPath),
bosk.rootReference().thenCatalog(TestEntity.class, Root.Fields.entities),
refs.entities());
for (Reference<Catalog<TestEntity>> catalogRef: testRefs) {
checkReferenceProperties(catalogRef, entitiesPath, root.entities());
for (Identifier id: root.entities.ids()) {
Expand All @@ -143,7 +144,7 @@ void testListingReference() throws Exception {
for (Identifier id: root.entities.ids()) {
Path listingPath = Path.of(Root.Fields.entities, id.toString(), TestEntity.Fields.listing);
List<ListingReference<TestEntity>> testRefs = asList(
bosk.listingReference(TestEntity.class, listingPath),
bosk.rootReference().thenListing(TestEntity.class, listingPath),
bosk.rootReference().thenListing(TestEntity.class, Root.Fields.entities, id.toString(), TestEntity.Fields.listing),
bosk.rootReference().thenListing(TestEntity.class, Root.Fields.entities, "-entity-", TestEntity.Fields.listing).boundTo(id)
);
Expand Down Expand Up @@ -191,7 +192,7 @@ void testSideTableReference() throws InvalidTypeException {
for (Identifier id: root.entities.ids()) {
Path sideTablePath = Path.of(Root.Fields.entities, id.toString(), TestEntity.Fields.sideTable);
List<SideTableReference<TestEntity,String>> testRefs = asList(
bosk.sideTableReference(TestEntity.class, String.class, sideTablePath),
bosk.rootReference().thenSideTable(TestEntity.class, String.class, sideTablePath),
bosk.rootReference().thenSideTable(TestEntity.class, String.class, Root.Fields.entities, id.toString(), TestEntity.Fields.sideTable),
bosk.rootReference().thenSideTable(TestEntity.class, String.class, Root.Fields.entities, "-entity-", TestEntity.Fields.sideTable).boundTo(id)
);
Expand Down Expand Up @@ -225,7 +226,7 @@ void testReferenceReference() throws Exception {
for (Identifier id: root.entities.ids()) {
Path refPath = Path.of(Root.Fields.entities, id.toString(), TestEntity.Fields.refField);
List<Reference<Reference<TestEntity>>> testRefs = asList(
bosk.referenceReference(TestEntity.class, refPath),
bosk.rootReference().thenReference(TestEntity.class, refPath),
bosk.rootReference().thenReference(TestEntity.class, Root.Fields.entities, id.toString(), TestEntity.Fields.refField),
bosk.rootReference().thenReference(TestEntity.class, Root.Fields.entities, "-entity-", TestEntity.Fields.refField).boundTo(id)
);
Expand All @@ -248,7 +249,7 @@ void testReferenceReference() throws Exception {
@Test
void testBogusReferenceReference() {
assertThrows(InvalidTypeException.class, ()->
bosk.reference(Classes.reference(String.class), Path.empty())); // Root object isn't a reference to a String
bosk.rootReference().then(Classes.reference(String.class), Path.empty())); // Root object isn't a reference to a String
}

@Test
Expand Down Expand Up @@ -425,7 +426,7 @@ private Reference<TestEntity> refUpdater(Reference<TestEntity> ref) {
List<String> pathSegments = ref.path().segmentStream().collect(toList());
pathSegments.set(1, "REPLACED_ID"); // second segment is the entity ID
try {
return bosk.reference(ref.targetClass(), Path.of(pathSegments));
return bosk.rootReference().then(ref.targetClass(), Path.of(pathSegments));
} catch (InvalidTypeException e) {
throw new AssertionError("Unexpected!", e);
}
Expand Down
2 changes: 1 addition & 1 deletion bosk-core/src/test/java/io/vena/bosk/HooksTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public interface Refs {
@BeforeEach
void setupBosk() throws InvalidTypeException {
bosk = setUpBosk(Bosk::simpleDriver);
refs = bosk.buildReferences(Refs.class);
refs = bosk.rootReference().buildReferences(Refs.class);
try (val __ = bosk.readContext()) {
originalRoot = bosk.rootReference().value();
originalParent = refs.parent().value();
Expand Down
12 changes: 8 additions & 4 deletions bosk-core/src/test/java/io/vena/bosk/ListingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

/*
* TODO: This test is written in a mighty weird style. Change it to set up
* the bosk in a more normal manner, and try to use buildReferences too.
*/
class ListingTest {

static class ListingArgumentProvider implements ArgumentsProvider {
Expand All @@ -53,7 +57,7 @@ public Stream<? extends Arguments> provideArguments(ExtensionContext context) {
Bosk<TestEntity> bosk = new Bosk<>("Test Bosk", TestEntity.class, root, Bosk::simpleDriver);
CatalogReference<TestEntity> catalog;
try {
catalog = bosk.catalogReference(TestEntity.class, Path.just(TestEntity.Fields.children));
catalog = bosk.rootReference().thenCatalog(TestEntity.class, Path.just(TestEntity.Fields.children));
} catch (InvalidTypeException e) {
throw new AssertionError(e);
}
Expand All @@ -70,7 +74,7 @@ public Stream<? extends Arguments> provideArguments(ExtensionContext context) th
List<TestEntity> children = singletonList(child);
TestEntity root = new TestEntity(Identifier.unique("parent"), Catalog.of(children));
Bosk<TestEntity> bosk = new Bosk<>("Test Bosk", TestEntity.class, root, Bosk::simpleDriver);
CatalogReference<TestEntity> childrenRef = bosk.catalogReference(TestEntity.class, Path.just(TestEntity.Fields.children));
CatalogReference<TestEntity> childrenRef = bosk.rootReference().thenCatalog(TestEntity.class, Path.just(TestEntity.Fields.children));
return idStreams().map(list -> Arguments.of(list.map(Identifier::from).collect(toList()), childrenRef, bosk));
}

Expand Down Expand Up @@ -245,7 +249,7 @@ void testEmpty() throws InvalidTypeException {
List<TestEntity> children = singletonList(child);
TestEntity root = new TestEntity(Identifier.unique("parent"), Catalog.of(children));
Bosk<TestEntity> bosk = new Bosk<>("Test Bosk", TestEntity.class, root, Bosk::simpleDriver);
CatalogReference<TestEntity> childrenRef = bosk.catalogReference(TestEntity.class, Path.just(TestEntity.Fields.children));
CatalogReference<TestEntity> childrenRef = bosk.rootReference().thenCatalog(TestEntity.class, Path.just(TestEntity.Fields.children));

Listing<TestEntity> actual = Listing.empty(childrenRef);
assertTrue(actual.isEmpty());
Expand Down Expand Up @@ -378,7 +382,7 @@ void testWithoutID(Listing<TestEntity> listing, List<TestEntity> children, Bosk<
@ParameterizedTest
@ArgumentsSource(ListingArgumentProvider.class)
void testScope(Listing<TestEntity> listing, List<TestEntity> children, Bosk<TestEntity> bosk) throws InvalidTypeException {
Reference<Catalog<TestEntity>> expected = bosk.catalogReference(TestEntity.class, Path.just(TestEntity.Fields.children));
Reference<Catalog<TestEntity>> expected = bosk.rootReference().thenCatalog(TestEntity.class, Path.just(TestEntity.Fields.children));
assertEquals(expected, listing.domain());
}

Expand Down
4 changes: 2 additions & 2 deletions bosk-core/src/test/java/io/vena/bosk/OptionalRefsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class OptionalRefsTest extends AbstractRoundTripTest {
@Test
void testReferenceOptionalNotAllowed() {
Bosk<OptionalString> bosk = new Bosk<>("optionalNotAllowed", OptionalString.class, new OptionalString(ID, Optional.empty()), Bosk::simpleDriver);
InvalidTypeException e = assertThrows(InvalidTypeException.class, () -> bosk.reference(Optional.class, Path.just("field")));
InvalidTypeException e = assertThrows(InvalidTypeException.class, () -> bosk.rootReference().then(Optional.class, Path.just("field")));
assertThat(e.getMessage(), containsString("not supported"));
}

Expand Down Expand Up @@ -134,7 +134,7 @@ private <E extends Entity, V> void doTest(E initialRoot, ValueFactory<E, V> valu

// Try other ways of getting the same reference
@SuppressWarnings("unchecked")
Reference<V> ref2 = bosk.reference((Class<V>)value.getClass(), Path.just("field"));
Reference<V> ref2 = bosk.rootReference().then((Class<V>) value.getClass(), Path.just("field"));
assertEquals(optionalRef, ref2);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void setup() throws InvalidTypeException {
rootRef = bosk.rootReference();
TestRoot localRoot = root = rootRef.value();
threadLocalRoot = ThreadLocal.withInitial(() -> localRoot);
ref5Segments = bosk.reference(TestEnum.class, Path.of(
ref5Segments = bosk.rootReference().then(TestEnum.class, Path.of(
TestRoot.Fields.entities, "parent",
TestEntity.Fields.children, "child1",
TestChild.Fields.testEnum
Expand Down
4 changes: 2 additions & 2 deletions bosk-core/src/test/java/io/vena/bosk/ReferenceErrorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ void setupBosk() {

@Test
void referenceGet_brokenGetter_propagatesException() throws InvalidTypeException {
Reference<Identifier> idRef = bosk.reference(Identifier.class, Path.just("id"));
Reference<Identifier> idRef = bosk.rootReference().then(Identifier.class, Path.just("id"));
try (val __ = bosk.readContext()) {
assertThrows(UnsupportedOperationException.class, idRef::value,
"Reference.value() should propagate the exception as-is");
Expand All @@ -34,7 +34,7 @@ void referenceGet_brokenGetter_propagatesException() throws InvalidTypeException

@Test
void referenceUpdate_brokenGetter_propagatesException() throws InvalidTypeException {
Reference<String> stringRef = bosk.reference(String.class, Path.of(BadGetters.Fields.nestedObject, NestedObject.Fields.string));
Reference<String> stringRef = bosk.rootReference().then(String.class, Path.of(BadGetters.Fields.nestedObject, NestedObject.Fields.string));
assertThrows(UnsupportedOperationException.class, ()->
bosk.driver().submitReplacement(stringRef, "newValue"));
assertThrows(UnsupportedOperationException.class, ()->
Expand Down
Loading

0 comments on commit 73502ce

Please sign in to comment.