one-data-model / language

(Old repo:) Simple Definition Format (SDF) for One Data Model definitions
7 stars 4 forks source link

Illustrate Enums as a Numerical Type #29

Open gerickson opened 5 years ago

gerickson commented 5 years ago

From https://github.com/mjkoster/ODM-Examples/issues/3:

Less a schema issue; however, from a data modeling policy issue, I think it’d be best if we illustrated enums as some number type.

gerickson commented 5 years ago

From @mjkoster:

What's an example of this? In the example there is a string enum [ "on", "off" ] using a JSON Schema construct. How would we apply a number type to this?

gerickson commented 5 years ago

What's an example of this? In the example there is a string enum [ "on", "off" ] using a JSON Schema construct. How would we apply a number type to this?

One of the WDL traits, for example, is PowerSourceCapabilitiesTrait and two of the properties of that Trait are the enumerations PowerSourceType and PowerSourceCurrentType:

        /**
         *  This represents an indication of the type or class of power source
         *  for the trait instance published by a resource.
         *
         *  The type power source property enumeration should be extended by
         *  outside entities by adding the vendor identifier to the high order
         *  16-bits of the enumeration and then using the low order 16-bits
         *  as the extended power source type.
         */
        enum PowerSourceType {
                option (wdl.enumopts) = {
                        extendable: true,
                        reserved_tag_min: 1,
                        reserved_tag_max: 31
                };

                POWER_SOURCE_TYPE_UNSPECIFIED         = 0;
                POWER_SOURCE_TYPE_BATTERY             = 1;             ///< Battery (see the Battery Power Source trait)
        }

and:

        /**
         *  This represents the current type of the power source.
         */
        enum PowerSourceCurrentType {
                option (wdl.enumopts) = {
                        extendable: false
                };

                POWER_SOURCE_CURRENT_TYPE_UNSPECIFIED = 0;
                POWER_SOURCE_CURRENT_TYPE_DC          = 1; ///< Direct Current
                POWER_SOURCE_CURRENT_TYPE_AC          = 2; ///< Alternating Current
        }

I'm unclear how I'd model these in the proposed ODM DSL.

gerickson commented 5 years ago

From @WAvdBeek:

may be this should be explicit like using unkown in the (string) enumeration of https://oneiota.org/revisions/5265 note i think this is more like a good practice than a requirement on the language.

gerickson commented 5 years ago

On Fri, May 10, 2019 at 12:17 AM Wouter notifications@github.com wrote:

may be this should be explicit like using unkown in the (string) enumeration of https://oneiota.org/revisions/5265 note i think this is more like a good practice than a requirement on the language.

Precisely, this is data modeling convention and best practice rather than a language feature.

gerickson commented 5 years ago

Provide a fixed mapping between strings and numeric codes to be used in enums. e.g.

"type": "object" "enum": [ { "POWER_SOURCE_CURRENT_TYPE_UNSPECIFIED": 0 }, { "POWER_SOURCE_CURRENT_TYPE_DC": 1 }, { "POWER_SOURCE_CURRENT_TYPE_AC": 2 } ] ...or can we assign in some consistent way? e.g. ODM registry

mjkoster commented 5 years ago

Enums are now defined in the schema and document as an array of maps, with string to number mapping, e.g. [ { "on": 1 }, { "off": 0 } ]

mjkoster commented 5 years ago

3 is closed, we should be able to also close this one