cddl-codegen icon indicating copy to clipboard operation
cddl-codegen copied to clipboard

.cbor handling incorrect for standalone types

Open SebastienGllmt opened this issue 3 years ago • 2 comments

#50 adds support for the .cbor notation

It does so by having foo = bytes .cbor bar simply make foo be an alias for bar at the type level and handling the cbor-in-cbor encoding in the (de)serialization logic.

This works fine when embedded as part of a larger object, but when the type is used standalone, it just leaves you with type foo = bar with no (de)serialization wrapping a-la #88

I also noticed the same issue happens with TaggedData (foo = #6.24(bytes) generates the wrong code when standalone)

Proposed Solution

I think probably the best way to fix this is to extend the upstream cddl library to give it parent pointers for all the AST types. Basically we would add a new feature flag that, when enabled, adds a parent member variable that gets filled in on a 2nd pass of the AST using the visitor utilities in the library

Then, from our library, we can use these parent pointers in parsing.rs to know if a type is top-level (needs to be wrapped) or if it's embedded (use current technique)

Having this kind of parent pointer would also improve the logic implemented in https://github.com/dcSpark/cddl-codegen/pull/84 as we could:

  1. Remove the struct I defined in the PR
  2. Crawl up the parent stack to fallback to the root rule name if no @name definition exists

SebastienGllmt avatar Sep 02 '22 13:09 SebastienGllmt

I made https://github.com/anweiss/cddl/pull/134 to start work on the upstream feature we need

SebastienGllmt avatar Sep 12 '22 16:09 SebastienGllmt

FYSA ... https://github.com/anweiss/cddl/pull/147

anweiss avatar Oct 14 '22 16:10 anweiss