Phones Example

94 views
Skip to first unread message

andrew

unread,
Oct 18, 2011, 1:59:30 PM10/18/11
to Gorilla web toolkit
I think this schema package is very cool; something that is so
necessary I was about to build myself until I found this project.
Thanks for the work!!

Just wanted to let you know, I don't think the "Phones" slice example
works I created some test code and it never populates the slice with
any information. Here is the sample code did I miss something or is
this a bug?:


package main

import (
"gorilla.googlecode.com/hg/gorilla/schema"
"http"
"fmt"
)

type Phone struct {
Label string
Number string
}

type Person struct {
Name string
Phones []Phone
}

func handler(w http.ResponseWriter, r *http.Request) {
r.FormValue("") // forces the form parser
person := &Person{}
err := schema.Load(person, r.Form)
if err != nil {
panic(err)
}
fmt.Printf("%s", person)
fmt.Printf("Number of phones : %d\n", len(person.Phones))
}

func main() {
http.HandleFunc("/", handler)
http.ListenAndServe(":8888", nil)
}

Rodrigo Moraes

unread,
Oct 18, 2011, 2:06:00 PM10/18/11
to Gorilla web toolkit
Hi,

On Oct 18, 3:59 pm, andrew wrote:
> I think this schema package is very cool; something that is so
> necessary I was about to build myself until I found this project.
> Thanks for the work!!

Thanks. :)

> Just wanted to let you know, I don't think the "Phones" slice example
> works I created some test code and it never populates the slice with
> any information. Here is the sample code did I miss something or is
> this a bug?:

I believe the problem is that you are using request.Form before
calling request.ParseForm(), so form data is not available yet. See
the doc note in Form:

http://golang.org/pkg/http/#Request

Let me know if this was the issue.

-- rodrigo

andrew

unread,
Oct 18, 2011, 2:55:13 PM10/18/11
to Gorilla web toolkit


> I believe the problem is that you are using request.Form before
> calling request.ParseForm(), so form data is not available yet. See
> the doc note in Form:
>
>    http://golang.org/pkg/http/#Request
>
> Let me know if this was the issue.
>

I was calling r.FormValue("") at the beginning of the handler
"FormValue returns the first value for the named component of the
query. FormValue calls ParseMultipartForm and ParseForm if necessary."

I replaced r.FormValue("") w/ r.ParseForm() and I get the exact same
result, a Person object with an empty slice of Phones.

Digging around I found this comment in load.go:
// TODO support struct values in maps and slices at some point.
// Currently maps and slices can be of the basic types only.

So, maybe this isn't supported yet??

Rodrigo Moraes

unread,
Oct 18, 2011, 3:06:33 PM10/18/11
to Gorilla web toolkit
On Oct 18, 4:55 pm, andrew wrote:
> Digging around I found this comment in load.go:
> // TODO support struct values in maps and slices at some point.
> // Currently maps and slices can be of the basic types only.
>
> So, maybe this isn't supported yet??

Hmmm, maybe it is. Sometimes I write usage examples before writing any
actual code ("how it should work"), and I think that example was never
actually implemented. :/

I'll implement it now or change the example. Thanks for finding it.

-- rodrigo

andrew

unread,
Oct 18, 2011, 3:40:34 PM10/18/11
to Gorilla web toolkit

> Hmmm, maybe it is. Sometimes I write usage examples before writing any
> actual code ("how it should work"), and I think that example was never
> actually implemented. :/
>
> I'll implement it now or change the example. Thanks for finding it.
>

Thanks! Now I can stop scratching my head. :)

Rodrigo Moraes

unread,
Oct 19, 2011, 3:17:21 AM10/19/11
to Gorilla web toolkit
On Oct 18, 5:40 pm, andrew wrote:
> Thanks! Now I can stop scratching my head. :)

I'll comment out the example for now and add this to the issue
tracker. I want to refactor the recursive logic before adding support
for []Struct and map[string]Struct. Notice that nested struct fields
are already supported, so right now the workaround for that example is
to use:

type Phones struct {
Label []string
Number []string
}

type Person struct {
Name string
Phones Phones
}

-- rodrigo

andrew

unread,
Oct 19, 2011, 5:02:37 PM10/19/11
to Gorilla web toolkit

> I'll comment out the example for now and add this to the issue
> tracker.

Okay, I'm watching the issue. Any rough estimate on when this might be
implemented? Let me know if I can help.

andrew

Rodrigo Moraes

unread,
Oct 19, 2011, 6:39:29 PM10/19/11
to Gorilla web toolkit
On Oct 19, 7:02 pm, andrew wrote:
> Okay, I'm watching the issue. Any rough estimate on when this might be
> implemented? Let me know if I can help.

If you have a simple solution, please send a patch. Or if you want to
start looking for one, I'll forget it. :)

Otherwise I will try to look at it during the weekend, but no promise.

-- rodrigo

andrew

unread,
Oct 24, 2011, 3:18:01 PM10/24/11
to Gorilla web toolkit
A patch for a very simple solution was added to the issue:
http://code.google.com/p/gorilla/issues/detail?id=22

Andrew

Rodrigo Moraes

unread,
Oct 24, 2011, 8:16:25 PM10/24/11
to Gorilla web toolkit, andrew
On Oct 24, 5:18 pm, andrew wrote:
> A patch for a very simple solution was added to the issue:http://code.google.com/p/gorilla/issues/detail?id=22

Woot! Thanks a lot. Applied, works perfectly. :)

-- rodrigo

andrew

unread,
Oct 27, 2011, 1:31:25 PM10/27/11
to Gorilla web toolkit
My pleasure; thanks for the rest of the code! :)
Reply all
Reply to author
Forward
0 new messages