From 8c83903114aeb248fe6ce181ebee94e4af96904d Mon Sep 17 00:00:00 2001 From: Christian Vetter Date: Thu, 14 Nov 2024 14:42:33 +0100 Subject: [PATCH] Report errors when trying to using optional ranges (#250) * Report errors when trying to using optional ranges Signed-off-by: Christian Vetter * Fix build Signed-off-by: Christian Vetter --------- Signed-off-by: Christian Vetter --- .../flatdata/generator/tree/builder.py | 5 ++++- .../flatdata/generator/tree/errors.py | 6 ++++++ flatdata-generator/pyproject.toml | 1 - .../tests/tree/test_syntax_tree_builder.py | 15 ++++++++++++--- flatdata-py/pyproject.toml | 1 - 5 files changed, 22 insertions(+), 6 deletions(-) diff --git a/flatdata-generator/flatdata/generator/tree/builder.py b/flatdata-generator/flatdata/generator/tree/builder.py index ee837def..bc53a9de 100644 --- a/flatdata-generator/flatdata/generator/tree/builder.py +++ b/flatdata-generator/flatdata/generator/tree/builder.py @@ -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 @@ -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): diff --git a/flatdata-generator/flatdata/generator/tree/errors.py b/flatdata-generator/flatdata/generator/tree/errors.py index 0c4754f6..22c8d4fa 100644 --- a/flatdata-generator/flatdata/generator/tree/errors.py +++ b/flatdata-generator/flatdata/generator/tree/errors.py @@ -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)) \ No newline at end of file diff --git a/flatdata-generator/pyproject.toml b/flatdata-generator/pyproject.toml index dc537248..f83f8e19 100644 --- a/flatdata-generator/pyproject.toml +++ b/flatdata-generator/pyproject.toml @@ -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" }, ] diff --git a/flatdata-generator/tests/tree/test_syntax_tree_builder.py b/flatdata-generator/tests/tree/test_syntax_tree_builder.py index 2062897b..ccd025fa 100644 --- a/flatdata-generator/tests/tree/test_syntax_tree_builder.py +++ b/flatdata-generator/tests/tree/test_syntax_tree_builder.py @@ -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 @@ -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): @@ -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 { diff --git a/flatdata-py/pyproject.toml b/flatdata-py/pyproject.toml index 9874ecc0..94da4a24 100644 --- a/flatdata-py/pyproject.toml +++ b/flatdata-py/pyproject.toml @@ -7,7 +7,6 @@ name = "flatdata-py" version = "0.4.7" description = "Python 3 implementation of Flatdata" readme = "README.md" -license = "" authors = [ { name = "Flatdata Developers" }, ]