module test-union { namespace "urn:ietf:params:xml:ns:yang:test-union"; prefix ext; revision 2019-03-25 { description "Draft."; } // -------------------------------------------------- // Proposed extensions extension union-tag { argument tag { yin-element true; } description "In some cases, the interpretation of a value of a union type can differ when encoded using the different data format. This difference comes from the level of type information supported by the different data formats. In the case of XML, all elements and attributes are encoded as string and no type information are included. JSON supports string, number and boolean. Finally, CBOR supports all YANG basic types. This extension can be used to add a tag to a type within a union to guarantee its proper interpretation when decoded."; } // -------------------------------------------------- // This example shows a case for which a tag is not required leaf multiple-enumerations-test-1 { type union { type enumeration { enum "Monday" { value 0; } enum "Tuesday" { value 1; } enum "Wednesday" { value 2; } enum "Thursday" { value 3; } enum "Friday" { value 4; } } type enumeration { enum "Saturday" { value 5; } enum "Sunday" { value 6; } } } } // -------------------------------------------------- // The following two examples require the use of at least one // union tag to guarantee the proper interpretation of those two // enumerations. This ability to tag only one enumeration can be // used to keep the backward compatibility when updating a module // with a second enumeration, see second example. leaf multiple-enumerations-test-2 { type union { type enumeration { ext:union-tag weekdays; enum "Monday"; enum "Tuesday"; enum "Wednesday"; enum "Thursday"; enum "Friday"; } type enumeration { ext:union-tag weekend; enum "Saturday"; enum "Sunday"; } } } typedef weekdays { type enumeration { enum "Monday"; enum "Tuesday"; enum "Wednesday"; enum "Thursday"; enum "Friday"; } } typedef weekend { type enumeration { enum "Saturday"; enum "Sunday"; } } leaf multiple-enumerations-test-3 { type union { type weekdays; type weekend { ext:union-tag weekend; } } } // -------------------------------------------------- // This example shows a case for which a tag is not required leaf multiple-bits-test-1 { type union { type bits { bit "Monday" { position 0; } bit "Tuesday" { position 1; } bit "Wednesday" { position 2; } bit "Thursday" { position 3; } bit "Friday" { position 4; } } type bits { bit "Saturday" { position 5; } bit "Sunday" { position 6; } } } } // -------------------------------------------------- // The following two examples require the use of at least one // union tag to guarantee the proper interpretation of those two // enumerations. leaf multiple-bits-test-2 { type union { type bits { ext:union-tag weekdays; bit "Monday"; bit "Tuesday"; bit "Wednesday"; bit "Thursday"; bit "Friday"; } type bits { ext:union-tag weekend; bit "Saturday"; bit "Sunday"; } } } typedef weekdays-flags { type bits { bit "Monday"; bit "Tuesday"; bit "Wednesday"; bit "Thursday"; bit "Friday"; } } typedef weekend-flags { type bits { bit "Saturday"; bit "Sunday"; } } leaf multiple-bits-test-3 { type union { type weekdays-flags; type weekend-flags { ext:union-tag weekend; } } } }