NordicSemiconductor / zcbor

Low footprint C/C++ CBOR library and Python tool providing code generation from CDDL descriptions.
Apache License 2.0
111 stars 34 forks source link

tdate is not defined #438

Open dgpetrie opened 1 month ago

dgpetrie commented 1 month ago

RFC 8610 defines the CDDL standard prelude in appendix D. This defines tdate. zcbor yields an exception: KeyError: 'tdate' when is is reference in CDDL.

import zcbor cddl_str = """ date_type = { creation: tdate } """ cddl_res = zcbor.zcbor.zcbor.DataTranslator.from_cddl(cddl_str, 100) Traceback (most recent call last): File "", line 1, in File "/usr/local/lib/python3.8/dist-packages/zcbor/zcbor/zcbor.py", line 293, in from_cddl my_types[my_type].post_validate() File "/usr/local/lib/python3.8/dist-packages/zcbor/zcbor/zcbor.py", line 937, in post_validate none_keys = [child for child in self.value if not child.elem_has_key()] File "/usr/local/lib/python3.8/dist-packages/zcbor/zcbor/zcbor.py", line 937, in none_keys = [child for child in self.value if not child.elem_has_key()] File "/usr/local/lib/python3.8/dist-packages/zcbor/zcbor/zcbor.py", line 928, in elem_has_key or (self.type == "OTHER" and self.my_types[self.value].elem_has_key())\ KeyError: 'tdate'

If I add the RFC8610 prelude definition of tdate the following exception is raise:

import zcbor cddl_str = """ date_type = { creation: tdate }

tdate = #6.0(tstr) """ cddl_res = zcbor.zcbor.zcbor.DataTranslator.from_cddl(cddl_str, 100) Traceback (most recent call last): File "", line 1, in File "/usr/local/lib/python3.8/dist-packages/zcbor/zcbor/zcbor.py", line 293, in from_cddl my_types[my_type].post_validate() File "/usr/local/lib/python3.8/dist-packages/zcbor/zcbor/zcbor.py", line 940, in post_validate raise TypeError( TypeError: Map member(s) must have key: [creation:OTHER'tdate'] pointing to [#6.0TSTR]

I have not tested other types defined in the RFC 8610 standard prelude. Perhaps none are defined?

dgpetrie commented 1 month ago

The following approach for tdate does not work either:

cddl_str = """ date_type = { creation: #6.0(tstr) } """

cddl_res = zcbor.zcbor.zcbor.DataTranslator.from_cddl(cddl_str, 100) Traceback (most recent call last): File "", line 1, in File "/usr/local/lib/python3.8/dist-packages/zcbor/zcbor/zcbor.py", line 293, in from_cddl my_types[my_type].post_validate() File "/usr/local/lib/python3.8/dist-packages/zcbor/zcbor/zcbor.py", line 940, in post_validate raise TypeError( TypeError: Map member(s) must have key: [creation:#6.0TSTR] pointing to []

dgpetrie commented 1 month ago

Testing was performed in Python 3.8 using zcbor version 0.8.1

oyvindronningstad commented 1 month ago

Thanks.

The prelude is added by default when using the CLI, but not when using the Python module. The prelude can be found in the repo at zcbor/prelude.cddl, you can programmatically append it to the cddl_str. I will look into making it easier to add the when using the python module.

Your second comment seems to fail because you are defining a map, but the member has no well-formed key. Try e.g.

cddl_str = """
date_type = {
    "creation" => #6.0(tstr)
}
"""