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.

5
  • 1
    The child is not a map its a slice of integer values. You can loop through them, it is unclear from the question you have asked what actually you want. Commented Aug 10, 2018 at 18:08
  • 1
    alright what i actually want is to obtain each value that is sended in the Mutation and saved it in a array of Cell (is the type Cell struct at the end) Commented Aug 10, 2018 at 18:11
  • 1
    i hope that my comment make it a little more clear @Himanshu Commented Aug 10, 2018 at 18:14
  • 1
    I edit the question so now it'll be more clear Commented Aug 10, 2018 at 18:31
  • 1
    Yeah we can do that just print the output of cellsInterface and resolvedCells in your question. Commented Aug 11, 2018 at 6:36

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

7 Comments

Now i receive an error: cannot use value (type interface {}) as type *Cell in append: need type assertion
@LuisCardozaBird Please check edited code you need type assertion to get the underlying value
I checked with this: stackoverflow.com/questions/14289256/… but they take in consideration just one type of value (string), in this case is String and an Array :/ @Himanshu
@LuisCardozaBird that's what I needed give me sometime.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.