简体   繁体   中英

Unmarshalling neo4j results to a nested struct in Golang

I'm using the neoism library ( https://github.com/jmcvetta/neois ) to talk to a local neo4j database - I'm fairly new to go and am new to neo4j so the gap in my understanding could be on either side of the problem.

I have a simple database, a single "page" node which is related to a single "template" node. I was hoping to be able to have struct representing each node and nest them inside one another but I'm strugging to get this to work.

Creating the simple DB:

template, err := ioutil.ReadFile(viewPath + "templates/default.mustache")

if err != nil{
    panic(err)
}

defaultTemplate, _ := db.CreateNode(neoism.Props{
    "name": "default",
    "content": string(template),
})

defaultTemplate.AddLabel("Template")

n0, _ := db.CreateNode(neoism.Props{
    "name": "Home",
    "slug": "home",
    "title": "Home Page",
    "content" : "here I am",
})

n0.AddLabel("Page")
n0.Relate("TEMPLATE", template.Id(), neoism.Props{}) 

Now to the business of trying to get the data back out...

Here is my query which works just fine:

type PageStruct struct{
    Name string `json:"p.name"`
    Slug string `json:"p.slug"`
    Title string `json:"p.title"`
    Content string `json:"p.content"`
    TemplateName string `json:"t.name"`
    TemplateContent string `json:"t.content"`

}    

res := []PageStruct{}

    cq := neoism.CypherQuery{
        Statement: `
            MATCH (p:Page)-[r:TEMPLATE]->(t:Template)
            WHERE p.slug = {slug}
            RETURN p.name, p.slug, p.title, p.content, t.name, t.content
        `,
        Parameters: neoism.Props{"slug": pageSlug},
        Result:     &res,
    }

    db.Cypher(&cq)
    page := res[0]

But ideally what I want is to unmarshall into a nested struct, something like this:

    type PageStruct struct{
        Name string `json:"p.name"`
        Slug string `json:"p.slug"`
        Title string `json:"p.title"`
        Content string `json:"p.content"`
        Template struct {
            Name string `json:"t.name"`
            Content string `json:"t.content"`
                 } `json:"t"`

}

I've been trying various things with no success, could anyone give me any advice on how to achieve this abitious feat of computer engineering...

Additionally I'm slightly unclear as to how to handle relationships in neo4j in the sense of having no enforcement (that I'm aware of) as to the type of relationship (eg one-to-one, one-to-many) so how to handle this - do we always just assume an array of data?

Any help/advice is greatly appreciated.

Use embedding .

type Page struct {
    Name string `json:"p.name"`
    // ...
    Template
}

type Template struct {
    Name string `json:"t.name"`
    // ...
}

Playground: http://play.golang.org/p/B3ro3wgsGS .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM