Skip to content

Instantly share code, notes, and snippets.

@klokane
Created September 6, 2017 11:03
Show Gist options
  • Save klokane/e56f0be0f65e42a2ec7c35ac6bef746e to your computer and use it in GitHub Desktop.
Save klokane/e56f0be0f65e42a2ec7c35ac6bef746e to your computer and use it in GitHub Desktop.
API Elements - EnumElement Proposals

What is wrong with EnumElement

All element structure descriptions are stored in content (value in C++)

For primitive types it hold final value (number, string, bool) For complex types there is stored definitions:

  • object: members (can be leaf (primitive type) or branch (another level of complex type)
  • array: there are stored all values which user define

In both cases there is stored complet definition of object

For Enum there were "array" and in API Elemements spec was written

Enumeration type. Exclusive list of possible elements. The elements in content's array MUST be interpreted as mutually exclusive.

After longer discussion we decide to move array of possible values into attribute enumerations and content use to store for value choosed by user

There is a few problems which I found while implementation in C++

Inconsistent code

IElement has method bool empty() const. It is typicaly used for check if it shold be rendered as empty element or we shoud render conten But this does not work for EnumElement since it has enumerations attribute.

There are two ways how to solve it:

  1. change empty() to make additional check for enumerations attribute (inconsistent with behavior of other elements)
  2. everywhere where is it required make check if (!e.empty() || hasEnumerationAttr(e)) {...}

Inconsistent behavior

As I mention above

Proposals

In general EnumElement is kind of One of MSON definition. It is mutual exclusive set of values. One of hold members. In refrect it is tored in following way

- object
    - select
        - option
            - m1: v1
            - m2: v2
        - option:
            - m3: v3
            - m4: v4

There is used select elment to store MSON definition.

...the select element is provided to give multiple possibilities for a given content.

In other word it mean it is mutial exclusive by definition

We can use already existing behavior of refract spec

If we mirror this beavior to Enum we can present Enum in following way:

- select
    - option
        - notrh
    - option
        - south
    - option
        - west
    - option
        - east

Implementation

We have two possible ways how to implement proposed behavior

Inherit from Select

- enum
    - content
        - option
            - notrh
        - option
            - south
        - option
            - west
        - option
            - east

Hold Select as content (value) of EnumElement

- enum
    - content
        - select
            - option
                - notrh
            - option
                - south
            - option
                - west
            - option
                - east

Weaknes

There is same problem like select element has. There is no way to say what which option is picked by user. Typicaly:

- A: north (enum)
    - south
    - west
    - east

There is a few ways how to implement it.

Attr on option

- enum
    - option (attr[value])
        - notrh
    - option
        - south
    - option
        - west
    - option
        - east

Attr on enum

- enum (attr[value]:ref(option/0))
    - option 
        - notrh
    - option
        - south
    - option
        - west
    - option
        - east
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment