Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Report errors when trying to using optional ranges #250

Merged
merged 2 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion flatdata-generator/flatdata/generator/tree/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from flatdata.generator.tree.errors import (
InvalidEnumWidthError, InvalidRangeName, InvalidRangeReference,
InvalidConstReference, InvalidConstValueReference, DuplicateInvalidValueReference,
InvalidStructInExplicitReference)
InvalidStructInExplicitReference, OptionalRange)
from flatdata.generator.tree.nodes.explicit_reference import ExplicitReference
from flatdata.generator.tree.nodes.archive import Archive
from flatdata.generator.tree.nodes.node import Node
Expand Down Expand Up @@ -181,6 +181,9 @@ def _check_ranges(root):
for sibling in field.parent.fields:
if sibling.name == name:
raise InvalidRangeName(name)
# Also check that the range is not optional
if field.invalid_value:
raise OptionalRange(name)

# Now check that structs with ranges are only used in vectors
for reference in root.iterate(StructureReference):
Expand Down
6 changes: 6 additions & 0 deletions flatdata-generator/flatdata/generator/tree/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,4 +148,10 @@ class InvalidRangeReference(FlatdataSyntaxError):
def __init__(self, name):
super().__init__(
"Structs with @range can only be used in vectors: {name}"
.format(name=name))

class OptionalRange(FlatdataSyntaxError):
def __init__(self, name):
super().__init__(
"@range cannot be combined with @optional, store empty ranges instead: {name}"
.format(name=name))
1 change: 0 additions & 1 deletion flatdata-generator/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ name = "flatdata-generator"
version = "0.4.6"
description = "Generate source code for C++, Rust, Go or Python from a Flatdata schema file"
readme = "README.md"
license = ""
authors = [
{ name = "Flatdata Developers" },
]
Expand Down
15 changes: 12 additions & 3 deletions flatdata-generator/tests/tree/test_syntax_tree_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
sys.path.insert(0, "..")
from flatdata.generator.tree.errors import MissingSymbol, InvalidRangeName, InvalidRangeReference, \
InvalidConstReference, InvalidConstValueReference, DuplicateInvalidValueReference, \
InvalidStructInExplicitReference
InvalidStructInExplicitReference, OptionalRange
from flatdata.generator.tree.builder import build_ast
from flatdata.generator.tree.nodes.trivial import Namespace, Structure, Field, Constant, Enumeration, EnumerationValue
from flatdata.generator.tree.nodes.archive import Archive
Expand All @@ -21,8 +21,6 @@
FieldReference, ArchiveReference, BuiltinStructureReference, ConstantValueReference, \
EnumerationReference, InvalidValueReference



def test_validating_archive_with_no_structure_defined_raises_missing_symbol_error():
def __test(resource_type):
with assert_raises(MissingSymbol):
Expand Down Expand Up @@ -103,6 +101,17 @@ def test_range_cannot_be_used_in_struct_resource():
}
""")

def test_optional_range():
with assert_raises(OptionalRange):
build_ast("""namespace foo{
const u32 NO_EDGES_REF = 200;
struct Node {
@range(edges_range)
@optional( NO_EDGES_REF )
first_edge_ref : u32;
}
}""")

def test_ranges_can_be_used_in_normally():
build_ast("""namespace foo{
struct A {
Expand Down
1 change: 0 additions & 1 deletion flatdata-py/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ name = "flatdata-py"
version = "0.4.7"
description = "Python 3 implementation of Flatdata"
readme = "README.md"
license = ""
boxdot marked this conversation as resolved.
Show resolved Hide resolved
authors = [
{ name = "Flatdata Developers" },
]
Expand Down
Loading