cbor-wg / cddl

The draft leading up to RFC8610
https://tools.ietf.org/html/rfc8610
Other
22 stars 4 forks source link

Clarification of group in structure #24

Open oliviermartin opened 5 years ago

oliviermartin commented 5 years ago

Considering this example from "Figure 4: Using a group for factorization":

dog = {
    identity,
    leash-length: float,
}

identity = (
    age: int,
    name: tstr,
)

Is it equivalent to?

dog = {
    age: int,
    name: tstr,
    identity,
    leash-length: float,
}

{...} being a map, I would think so. But the syntax is a bit confusing. I find the (...) a bit confusing. It can represent a group, choice/enum, substitution (in the case above).

It would have been more logic to define identity as:

identity = {
    age: int,
    name: tstr,
}

A CDDL tool could check if we could only substitute map inside a map. Or group inside a group.

oliviermartin commented 5 years ago

I might have understood where my confusion comes from. But it is still unclear...

The CDDL specification mentions "group" in a "map" or "array" context. So I guess in this case the "group" is a kind of virtual representation that could be used independently in a map or array context and would adapt to the parent type (ie: "map" or "array").

But I also interpreted "group" could potentially be used by itself and representing a serialised data representation.

Example:

identity = (
    age: int,
    name: tstr,
)

would be represented in CBOR as CBORTypeInt|IntValue|CBorTypeTstr|TstrValue. So not encapsulated in an array and no key.

cabo commented 5 years ago

Ultimately, a CDDL spec defines a type (which is named by the name of the first rule in the spec). While top-level groups have been discussed, they are not part of CDDL 1.0. A potential extension for the next CDDL version might allow a top-level group to specify not a type for a single data items but a group for a sequence of data items, which would be interpreted as in array context.

(A related issue is whether there should be a way to use a group in an array as a sequence of pairs. Again, a potential extension for the next CDDL version.)

cabo commented 5 years ago

Oh, and your specific example has a spurious "identity" in the second code block. If you define identity as a map as in the third code block, you could write

dog = {
    ~identity,
    leash-length: float,
}

to achieve the same effect the example has now (the map type identity is "unwrapped" by the ~ operator). This gets quite close to what inheritance/mixins are often used for in object-oriented languages.