Skip to content
Carsten Bormann edited this page Jun 11, 2022 · 5 revisions

This wiki page collects useful CDDL snippets that could turn into CDDL idioms.

one-or-more

   one-or-more<T> = T / [ 2* T ]

(From draft-ietf-sacm-coswid-21.txt.)

This describes a type that can be a single T or two or more T items in an array. Note that this only works really well if T cannot be an array; while it may be possible to distinguish the cases if T cannot be an array of T-like items, this requires additional implementation effort.

non-empty

   non-empty<M> = (M) .and ({ + any => any })

(From RFC 9115.)

This describes a map that has at least one entry. It can be used with M being a CDDL rule name, or directly in a map definition:

   problem-details = non-empty<{
     ? &(title: -1) => oltext
     ? &(detail: -2) => oltext
   }>

(Note that this example from draft-ietf-core-problem-details-04.txt also uses the named-number idiom, below.)

named numbers

Many CDDL specifications use numbered labels, as in:

   concise-swid-tag = {
     tag-id => text / bstr .size 16,
     tag-version => integer,
   ...

   tag-id = 0
   software-name = 1

(Example extracted from draft-ietf-sacm-coswid-21.txt.)

A shorter snippet with similar semantics is:

   concise-swid-tag = {
     &(tag-id: 0) => text / bstr .size 16,
     &(tag-version: 1) => integer,
   ...

Obviously, this does not define a rule tag-id or tag-version that could be used in other places in the specification, but it associates the number 0 with the label tag-id in a way that tools can access this association.

JC<J, C>

   JC<J,C> = J .feature "json" / C .feature "cbor"

(From RFC 9165.)

This marks the branches of a choice with feature json and feature cbor. Tools may be able to apply the right subset to a JSON and a CBOR file, for instance to use numeric labels in CBOR and string labels (which are the only ones available) in JSON:

   SenML-Record = {
   ; ...
     ? v => number
   ; ...
   }
   v = JC<"v", 2>

A snippet that may also be useful for a combined JSON and CBOR model:

   base64-string = tstr  ; add regex or ABNF for base64url without padding as needed
   binary-value = JC<base64-string, bstr>

When some parts of the mode are available only for JSON or only for CBOR, the definition of JC can be expanded to include CBOR-ONLY and JSON-ONLY rules:

   JSON-ONLY<J> = J .feature "json"
   CBOR-ONLY<C> = C .feature "cbor"
   JC<J,C> = JSON-ONLY<J> / CBOR-ONLY<C>

(From draft-ietf-rats-uccs-02.txt. Note that versions of the cddl tool before 0.8.29 do not process this correctly; please upgrade to get the bug fix.)

Clone this wiki locally