#Fuego and typing error with context body

9 messages · Page 1 of 1 (latest)

clear spindle
#

Hi !
I'm trying some things with Fuego and follow their tutorial (I'm in the CRUD part, with code generation)

But I end up with this code:

func (rs BooksResources) postBooks(c fuego.ContextWithBody[BooksCreate]) (Books, error) {
    body, err := c.Body()
    if err != nil {
        return Books{}, err
    }

    new, err := rs.BooksService.CreateBooks(body)
    if err != nil {
        return Books{}, err
    }

    return new, nil
}

And the message from Goland (the IDE): Cannot use 'body' (type B) as the type BooksCreate
(when overing the body variable usage in rs.BooksService.CreateBooks)
Even with this, the code compiles
Am I missing something ?

cyan storm
#
        var rb BooksCreate
    err := json.NewDecoder(r.Body).Decode(&rb)
    if err != nil {
        http.Error(w, "Bad request", http.StatusBadRequest)
        return
    }```
#

so u make a variable of the BooksCreate type, then use json.Decode. Sorry, not Unmarshall, I don't think

clear spindle
#

That's a bit weird that fuego itself generates such code

cyan storm
#

Oh

#

well that's how you do it with the stdlib. Maybe fuego can do it automatically?

#

It's also possible that the request and the struct are different?