Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Encode simple slice type results in error #94

Closed
frankMilde opened this issue Nov 22, 2017 · 2 comments
Closed

Encode simple slice type results in error #94

frankMilde opened this issue Nov 22, 2017 · 2 comments

Comments

@frankMilde
Copy link

Huge fan of your product. I seem unable to figure out, how to encode a simple slice type type People []Person. Using

package main

import (
        "fmt"

        "github.com/gorilla/schema"
)

type Person struct {
        Name string `schema:"name"`
        Age  int
}

type People []Person

func main() {
        jay := Person{"Jay", 34}
        may := Person{"May", 43}
        peeps := People{jay, may}

        values := make(map[string][]string)
        encoder := schema.NewEncoder()
        err := encoder.Encode(peeps, values)
        if err != nil {
                panic(err)
        }
        fmt.Printf("Encoded people: %v\n", values)
}

I get the error panic: schema: interface must be a struct.
Wrapping People into a struct now called Group also does not work:

package main

import (
        "fmt"

        "github.com/gorilla/schema"
)

type Person struct {
        Name string `schema:"name"`
        Age  int
}

type Group struct {
        People []Person
}

func main() {
        jay := Person{"Jay", 34}
        may := Person{"May", 43}
        peeps := Group{[]Person{jay, may}}

        values := make(map[string][]string)
        encoder := schema.NewEncoder()
        err := encoder.Encode(peeps, values)
        if err != nil {
                panic(err)
        }
        fmt.Printf("Encoded group %v\n", values)
}

Exits with error:
panic: schema: encoder not found for [{Jay 34} {May 43}]

What am I doing wrong here? Ideally I would like to work with the first version of type People []Person.

@elithrar
Copy link
Contributor

A slice of structs isn't a supported type to encode from. You can either:

@stale
Copy link

stale bot commented Dec 9, 2018

This issue has been automatically marked as stale because it hasn't seen a recent update. It'll be automatically closed in a few days.

@stale stale bot added the stale label Dec 9, 2018
@stale stale bot closed this as completed Dec 17, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants