Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nesting objects by prepending . #33

Open
martinburch opened this issue Feb 28, 2017 · 4 comments
Open

Nesting objects by prepending . #33

martinburch opened this issue Feb 28, 2017 · 4 comments

Comments

@martinburch
Copy link

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
Copy link
Author

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
Copy link
Author

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
Copy link

brandonkal commented Jan 19, 2020

@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
Copy link
Contributor

Here's a PR that addresses the issue: newsdev/archieml-js#28

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants