NordicSemiconductor / zcbor

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

Making a map item optional worsens it's structure format #416

Open jnz86 opened 2 months ago

jnz86 commented 2 months ago
base = (map_base_t)

map_base_t = {
    first     : ("a" => bstr),
    second    : ("b" => bstr),
    ?third    : ("c" => bstr), 
}

I didn't have time to make it a more simple example. I suspect this is still an issue when removing the sub-types.

In C:

base.first.value = some_pointer;       // OK
base.second.len = 100;                 // OK
base.third_present == true             // OK
base.third.third.value = some_pointer; // Why third.third.?

In types.h:

struct map_base_t_third {
    struct zcbor_string third;
};

struct map_base_t {
    struct zcbor_string first;
    struct zcbor_string second;
    struct map_envelope_t_third third;
    bool third_present;
};

As soon as a map member is made optional, instead of a member like the others, it becomes a map type itself.

Am I doing something wrong with the CDDL? Is this intended behavior? Workaround to get better structure naming?

oyvindronningstad commented 2 months ago

Thanks for the report and testcase. Will look into this a bit to see if I can improve it.