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

add flag to flatten a single-value array #21

Closed
flowchartsman opened this issue Aug 3, 2016 · 6 comments
Closed

add flag to flatten a single-value array #21

flowchartsman opened this issue Aug 3, 2016 · 6 comments

Comments

@flowchartsman
Copy link

I'm currently using this library to create plists for launchd. In a couple of locations, the specification says <dictionary [of something] or array of dictionaries [of the same thing]>. I can't imagine launchd is the only place to do this. It would be nice to be able to have a flag flattensingle such that, given

type Foo map[string]string

type Dict struct {
    Bar []Foo `plist:",omitempty,flattensingle"`
}

func main() {
    d := Dict{
        Bar: []Foo{
            Foo{
                "name": "one",
                "num":  "two",
            },
        },
    }

    o, e := plist.MarshalIndent(d, plist.XMLFormat, "    ")
    fmt.Println(e)
    fmt.Println(string(o))
}

The output would be

<plist version="1.0">
    <dict>
        <key>Bar</key>
        <dict>
            <key>name</key>
            <string>one</string>
            <key>num</key>
            <string>two</string>
        </dict>
    </dict>
</plist>

Rather than what it is now:

<plist version="1.0">
    <dict>
        <key>Bar</key>
        <array>
            <dict>
                <key>name</key>
                <string>one</string>
                <key>num</key>
                <string>two</string>
            </dict>
        </array>
    </dict>
</plist>
@DHowett
Copy link
Owner

DHowett commented Aug 3, 2016 via email

@flowchartsman
Copy link
Author

A great question, and one I intend to test shortly. If it does, that's your use-case right there. If not, this could still be useful, but could also be backlogged.

@DHowett
Copy link
Owner

DHowett commented Mar 6, 2017

@alaska Did you ever test how launchd reacts to single-element arrays? I'm looking at working on plist package improvements, and I'd like to prioritize my open issues. 😄

@flowchartsman
Copy link
Author

I haven't had the time to dig into this. I'll try and break some time off this weekend.

DHowett added a commit that referenced this issue Mar 20, 2017
@DHowett
Copy link
Owner

DHowett commented Mar 20, 2017

The changes in 221fc98 should address this. I added an example (well, a test case) for an array that is serialized as a single object or an array depending on how long it is.

@DHowett DHowett closed this as completed Mar 20, 2017
@DHowett
Copy link
Owner

DHowett commented Mar 20, 2017

That example is here.

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

2 participants