Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Golang : type conversion between slices of structs

This questions follows another question of mine.

I don't exactly get what is wrong with my attempt to convert res to a ListSociete in the following test code :

import (
    "errors"
    "fmt"
    "github.com/jmcvetta/neoism"
)

type Societe struct {
    Name string
}

type ListSociete []Societe

func loadListSociete(name string) (ListSociete, error) {
    db, err := neoism.Connect("http://localhost:7474/db/data")
    if err != nil {
        return nil, err
    }
    res := []struct {
        Name string `json:"a.name"`
    }{}
    cq := neoism.CypherQuery{
        Statement: `
            MATCH (a:Societe)
            WHERE a.name = {name}
            RETURN a.name
            `,
        Parameters: neoism.Props{"name": name},
        Result:     &res,
    }
    db.Cypher(&cq)
    if len(res) == 0 {
        return nil, errors.New("Page duz not exists")
    }
    r := res[0]
    return ListSociete(res), nil
}

Is a []struct{Name string} different from a []struct{Name string json:"a.name" } ?

Or is a ListSociete different from a []struct{Name string} ?

Thanks.

like image 755
Nicolas Marshall Avatar asked Jul 08 '14 21:07

Nicolas Marshall


1 Answers

You are currently dealing with two different types:

type Societe struct {
    Name string
}

and the anonymous one:

struct {
    Name string `json:"a.name"`
}

These two would be identical if it wasn't for the tag. The Go Specifications states (my emphasis):

Two struct types are identical if they have the same sequence of fields, and if corresponding fields have the same names, and identical types, and identical tags. Two anonymous fields are considered to have the same name. Lower-case field names from different packages are always different.

So, you can't do a simple conversion between the two. Also, the fact that you are converting slices of the two types makes the conversion problematic. I can see two options for you:

Copy through iteration:

This is the safe and recommended solution, but it is also more verbose and slow.

ls := make(ListSociete, len(res))
for i := 0; i < len(res); i++ { 
    ls[i].Name = res[i].Name
}
return ls, nil

Unsafe conversion:

Since both types have the same underlying data structure, it is possible to do an unsafe conversion.
This might however blow up in your face later on. Be warned!

return *(*ListSociete)(unsafe.Pointer(&res)), nil

Playground Example: http://play.golang.org/p/lfk7qBp2Gb

like image 187
ANisus Avatar answered Oct 12 '22 22:10

ANisus