newsdev / archieml.org

The archieml.org website and hub for specification development
Other
81 stars 5 forks source link

Nesting objects by prepending . #33

Open martinburch opened 7 years ago

martinburch commented 7 years ago

I'd like to write something like this, but it doesn't seem to be supported.

{colors}
{.reds}
crimson: #dc143c
darkred: #8b0000
{.blues}
cornflowerblue: #6495ed
darkblue: #00008b

The reds and blues are ignored and everything becomes properties of the colors object.

The docs show this example, which repeats colors. Any way to avoid this repetition?

{colors.reds}
crimson: #dc143c
darkred: #8b0000
{colors.blues}
cornflowerblue: #6495ed
darkblue: #00008b

The output in both cases would be expected to be:

{
  "colors": {
    "reds": {
      "crimson": "#dc143c",
      "darkred": "#8b0000"
    },
    "blues": {
      "cornflowerblue": "#6495ed",
      "darkblue": "#00008b"
    }
  }
}

Thanks, Martin

martinburch commented 7 years ago

At first I thought I'd resolved my problem, but switching to array brackets creates a needless single-element array.

{colors}
[.reds]
crimson: #dc143c
darkred: #8b0000
[]
[.blues]
cornflowerblue: #6495ed
darkblue: #00008b

The desired way to access my properties would be e.g. colors.reds.crimson but I must use colors.reds[0].crimson with this technique.

{
  "colors": {
    "reds": [
      {
        "crimson": "#dc143c",
        "darkred": "#8b0000"
      }
    ],
    "blues": [
      {
        "cornflowerblue": "#6495ed",
        "darkblue": "#00008b"
      }
    ]
  }
}
martinburch commented 7 years ago

The problem is that, when in an array, using {objectName} causes the parser to lose its index in the array and return to the root object. Using {.objectName} doesn't help. In this way, objects are parsed differently than arrays. The only way I can see to handle this now is to use the full dot notation for each property. This is very repetitive.

Nevertheless, in case this helps others, here is an example:

[universities]
name: Example University
[.students]
name:Alice
major:Business
name:John
major:English
[]
majors.English.dean: Dr. Smith
majors.Business.dean: Dr. Jones
name: Another University
[.students]
name:Bob
major:Business
[]
majors.Business.dean: Dr. Zimmerman

{
  "universities": [
    {
      "name": "Example University",
      "students": [
        {
          "name": "Alice",
          "major": "Business"
        },
        {
          "name": "John",
          "major": "English"
        }
      ],
      "majors": {
        "English": {
          "dean": "Dr. Smith"
        },
        "Business": {
          "dean": "Dr. Jones"
        }
      }
    },
    {
      "name": "Another University",
      "students": [
        {
          "name": "Bob",
          "major": "Business"
        }
      ],
      "majors": {
        "Business": {
          "dean": "Dr. Zimmerman"
        }
      }
    }
  ]
}
brandonkal commented 4 years ago

@martinburch I would like to see something like your first example.

But I would expect

{colors}
{.reds}
crimson: #dc143c
darkred: #8b0000
{.blues}
cornflowerblue: #6495ed
darkblue: #00008b

To be parsed like this:

{
  "colors": {
    "reds": {
      "crimson": "#dc143c",
      "darkred": "#8b0000",
      "blues": {
        "cornflowerblue": "#6495ed",
        "darkblue": "#00008b"
      }
    }
  }
}

Note that blues becomes nested inside reds because you didn't include a {} in the line above.

I would propose a syntax like so: {".blues} as the quote character is common shorthand for "repeat the above".

The quote character could either act as a shorthand for "close the last opened object" or it could act as a reference to the first object that is opened. The benefit of that approach is that you get some idea of nesting level in the absence of significant white space.

What do you think?

john-michael-murphy commented 4 years ago

Here's a PR that addresses the issue: https://github.com/newsdev/archieml-js/pull/28