1

Recently I was trying to an implementation of a Mutation Request using GoLang as a Graphql Server, Basically this is the query that i send: As you can see its an array of object that contains name and an array of strings

mutation{
    CellTest(cells:[{name:"lero",child:["1","2"]},{name:"lero2",child:["12","22"]}]){
            querybody
    }
}

In my Go code I have a type object that is gonna set the values sent

type Cell struct {
    name  string   `json:"name"`
    child []string `json:"child"`
}

and a custom array that is gonna be []Cell

type Cells []*Cell

However when the request is received by GO I get this: Note that this is the print of cellsInterface

[map[child:[1 2] name:lero] map[child:[12 22] name:lero2]]

How can i get each value and assign those in my Array Cells something like this:

Cells[0] = {name="first",child={"1","2"}}

Cells[1] = {name="second",child={"hello","good"}}

this is my current attempt:

var resolvedCells Cells
cellsInterface := params.Args["cells"].([]interface{})
cellsByte, err := json.Marshal(cellsInterface)
if err != nil {
    fmt.Println("marshal the input json", err)
    return resolvedCells, err
}

if err := json.Unmarshal(cellsByte, &resolvedCells); err != nil {
    fmt.Println("unmarshal the input json to Data.Cells", err)
    return resolvedCells, err
}

for cell := range resolvedCells {
    fmt.Println(cellsInterface[cell].([]interface{}))
}

However this only split the cells array into 0 and 1.

Himanshu
  • 12,071
  • 7
  • 46
  • 61
Luis Cardoza Bird
  • 1,265
  • 4
  • 24
  • 43

1 Answers1

1

Range through the map values in the result and append those values to Cell slice. If you are getting an object from json. Then you can unmarshall the bytes into Cell.

The result when unmarshalling should be a slice of Cell struct as

var resolvedCells []Cell
if err := json.Unmarshal(cellsByte, &resolvedCells); err != nil {
                fmt.Println("unmarshal the input json to Data.Cells", err)
    }
fmt.Println(resolvedCells)

Working Code on Go playground

Or if you want to use pointers loop over the resolvedCell as

type Cells []*Cell

func main() {
    var resolvedCells Cells
    if err := json.Unmarshal(cellsByte, &resolvedCells); err != nil {
                    fmt.Println("unmarshal the input json to Data.Cells", err)
        }
    fmt.Println(*resolvedCells[1])
    for _, value := range resolvedCells{
        fmt.Println(value)
        fmt.Printf("%+v",value.Child) // access child struct value of array
    }
}

Playground example

Himanshu
  • 12,071
  • 7
  • 46
  • 61
  • 1
    Now i receive an error: cannot use value (type interface {}) as type *Cell in append: need type assertion – Luis Cardoza Bird Aug 11 '18 at 16:35
  • @LuisCardozaBird Please check edited code you need type assertion to get the underlying value – Himanshu Aug 11 '18 at 17:37
  • 1
    I checked with this: https://stackoverflow.com/questions/14289256/cannot-convert-data-type-interface-to-type-string-need-type-assertion but they take in consideration just one type of value (string), in this case is String and an Array :/ @Himanshu – Luis Cardoza Bird Aug 11 '18 at 17:45
  • @LuisCardozaBird Its ok just provide me the output of json bytes `fmt.Println(string(cellsByte))` – Himanshu Aug 11 '18 at 17:55
  • this is: cells byte output: [{"child":["sasaa","sasa"],"name":"lero"},{"child":["xzxz","ffafa"],"name":"lero2"}] – Luis Cardoza Bird Aug 11 '18 at 17:57
  • 1
    @LuisCardozaBird that's what I needed give me sometime. – Himanshu Aug 11 '18 at 17:57
  • 1
    Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/177878/discussion-between-himanshu-and-luis-cardoza-bird). – Himanshu Aug 11 '18 at 18:10