Skip to content

Commit

Permalink
Require that protocol versions are contiguous
Browse files Browse the repository at this point in the history
  • Loading branch information
io7m committed Dec 20, 2020
1 parent 530f9b8 commit 85826f5
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
import com.io7m.cedarbridge.schema.binder.api.CBBindFailedException;
import com.io7m.cedarbridge.schema.binder.api.CBBindingType;

import java.math.BigInteger;
import java.util.Objects;
import java.util.TreeSet;

public final class CBProtocolDeclarationBinder
implements CBElementBinderType<CBASTProtocolDeclaration>
Expand All @@ -40,13 +42,37 @@ private static void bindDeclaration(
final var exceptions = new CBExceptionTracker<CBBindFailedException>();

try (var subContext = context.openBindingScope()) {
final var numbers = new TreeSet<BigInteger>();
for (final var version : item.versions()) {
try {
bindVersionDeclaration(subContext, version);
} catch (final CBBindFailedException e) {
exceptions.addException(e);
}
numbers.add(version.version());
}

final var min =
numbers.stream().min(BigInteger::compareTo).orElseThrow();
final var max =
numbers.stream().max(BigInteger::compareTo).orElseThrow();

for (var current = min;
current.compareTo(max) <= 0;
current = current.add(BigInteger.ONE)) {
if (!numbers.contains(current)) {
exceptions.addException(
context.failed(
item.lexical(),
"errorProtocolVersionMissing",
min,
max,
current
));
break;
}
}

exceptions.throwIfNecessary();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,10 @@
<entry key="errorPackageShortNameUsed">Package short name "{0}" has already been defined.</entry>
<entry key="errorBindingConflict">Name "{0}" has already been defined.</entry>
<entry key="errorBindingMissing">Name "{0}" is not defined or is not in scope.</entry>
<entry key="errorProtocolVersionMissing"><![CDATA[The range of protocol versions is not contiguous.
Minimum: {0}
Maximum: {1}
Missing: {2} (Only one missing value is shown)
]]></entry>

</properties>
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,17 @@
<Term type="expression">P(w)</Term> is the set of types in version <Term type="expression">w</Term>, then
it is not an error if <Term type="expression">P(v) ∩ P(w) ≠ ∅</Term>.
</Paragraph>
<Paragraph>
The declaration order of <Term type="term">types</Term> within a <Term type="term">version</Term> is significant;
changing the order of <Term type="statement">type</Term> declarations will change the semantics and serialized
form of the protocol.
</Paragraph>
<Paragraph>
The declaration order of <Term type="term">versions</Term> within a <Term type="term">protocol</Term> is
NOT significant; changing the order of <Term type="statement">version</Term> declarations will not change the
semantics or serialized form of the protocol as the declarations are expected to be internally ordered by their
declared version number.
</Paragraph>
</Subsection>

<Subsection title="Example">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,18 @@ public void testProto1()
assertEquals(0, this.errors.size());
}

@Test
public void testProto2()
throws Exception
{
assertThrows(CBBindFailedException.class, () -> {
this.bind("errorBindProto2.cbs");
});

assertEquals("errorProtocolVersionMissing", this.takeError().errorCode());
assertEquals(0, this.errors.size());
}

@Test
public void testOK0()
throws Exception
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
(package x.y.z)

(record U)

(protocol T
[version 0 U]
[version 2 U])

0 comments on commit 85826f5

Please sign in to comment.