Skip to content

Commit

Permalink
encoding/toml: implement support for table arrays
Browse files Browse the repository at this point in the history
This is a significant amount of code given that there are effectively
three ways in which table arrays can be used.

First, declaring a new array and repeating its key like:

    [[foo]]
    leaf = "one"
    [[foo]]
    leaf = "two"

resulting in appending to the same array:

    foo: [
        {leaf: "one"},
        {leaf: "two"},
    ]

Second, a table array can be created under an existing table:

    [foo]
    leaf = "one"
    [[foo.bar]]
    leaf = "two"

resulting in:

    foo: {
        leaf: "one"
        bar: [
            {leaf: "two"},
        ]
    }

Third, and perhaps most confusing, a table or table array can be created
under an existing table array, adding to the table's last element:

    [[foo]]
    leaf = "one"
    [foo.bar]
    leaf = "two"
    [[foo.baz]]
    leaf = "three"

resulting in:

    foo: [
        {
            leaf: "one"
            bar: {
                leaf: "two"
            }
            baz: [
                {leaf: "three"}
            ]
        },
    }

We add test cases for all of these edge cases,
as well as test cases to ensure that unique keys are handled correctly,
and that we reject declaring a key as both a table and an array table.

Updates #68.

Signed-off-by: Daniel Martí <mvdan@mvdan.cc>
Change-Id: I2b483a67810b7c42ddf319a04a0f22724877b8a9
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1195025
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
Reviewed-by: Aram Hăvărneanu <aram@cue.works>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
  • Loading branch information
mvdan committed Jun 12, 2024
1 parent 475f692 commit 8a6194a
Show file tree
Hide file tree
Showing 2 changed files with 624 additions and 76 deletions.
Loading

0 comments on commit 8a6194a

Please sign in to comment.