• Stars
    star
    1,351
  • Rank 34,773 (Top 0.7 %)
  • Language
    Go
  • License
    BSD 3-Clause "New...
  • Created about 12 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Package gorilla/schema fills a struct with form values.

gorilla/schema

testing codecov godoc sourcegraph

Gorilla Logo

Package gorilla/schema converts structs to and from form values.

Example

Here's a quick example: we parse POST form values and then decode them into a struct:

// Set a Decoder instance as a package global, because it caches
// meta-data about structs, and an instance can be shared safely.
var decoder = schema.NewDecoder()

type Person struct {
    Name  string
    Phone string
}

func MyHandler(w http.ResponseWriter, r *http.Request) {
    err := r.ParseForm()
    if err != nil {
        // Handle error
    }

    var person Person

    // r.PostForm is a map of our POST form values
    err = decoder.Decode(&person, r.PostForm)
    if err != nil {
        // Handle error
    }

    // Do something with person.Name or person.Phone
}

Conversely, contents of a struct can be encoded into form values. Here's a variant of the previous example using the Encoder:

var encoder = schema.NewEncoder()

func MyHttpRequest() {
    person := Person{"Jane Doe", "555-5555"}
    form := url.Values{}

    err := encoder.Encode(person, form)

    if err != nil {
        // Handle error
    }

    // Use form values, for example, with an http client
    client := new(http.Client)
    res, err := client.PostForm("http://my-api.test", form)
}

To define custom names for fields, use a struct tag "schema". To not populate certain fields, use a dash for the name and it will be ignored:

type Person struct {
    Name  string `schema:"name,required"`  // custom name, must be supplied
    Phone string `schema:"phone"`          // custom name
    Admin bool   `schema:"-"`              // this field is never set
}

The supported field types in the struct are:

  • bool
  • float variants (float32, float64)
  • int variants (int, int8, int16, int32, int64)
  • string
  • uint variants (uint, uint8, uint16, uint32, uint64)
  • struct
  • a pointer to one of the above types
  • a slice or a pointer to a slice of one of the above types

Unsupported types are simply ignored, however custom types can be registered to be converted.

More examples are available on the Gorilla website: https://www.gorillatoolkit.org/pkg/schema

License

BSD licensed. See the LICENSE file for details.

More Repositories

1

websocket

Package gorilla/websocket is a fast, well-tested and widely used WebSocket implementation for Go.
Go
21,832
star
2

mux

Package gorilla/mux is a powerful HTTP router and URL matcher for building Go web servers with 🦍
Go
20,537
star
3

sessions

Package gorilla/sessions provides cookie and filesystem sessions and infrastructure for custom session backends.
Go
2,829
star
4

handlers

Package gorilla/handlers is a collection of useful middleware for Go HTTP services & web applications 🛃
Go
1,640
star
5

csrf

Package gorilla/csrf provides Cross Site Request Forgery (CSRF) prevention middleware for Go web applications & services 🔒
Go
1,024
star
6

feeds

Package gorilla/feeds is a golang rss/atom generator library
Go
732
star
7

securecookie

Package gorilla/securecookie encodes and decodes authenticated and optionally encrypted cookie values for Go web applications.
Go
682
star
8

rpc

Package gorilla/rpc is a golang foundation for RPC over HTTP services.
Go
581
star
9

context

Package gorilla/context is a golang registry for global request variables.
Go
431
star
10

http

Package gorilla/http is an alternative HTTP client implementation for Go.
Go
267
star
11

pat

Package gorilla/pat is a pretty simple HTTP router for Go.
Go
153
star
12

css

Package gorilla/css is a CSS3 tokenizer.
Go
86
star
13

muxy

Package gorilla/muxy takes gorilla/mux to the next level
Go
74
star
14

gorilla.github.io

Gorilla web toolkit's website.
HTML
59
star
15

reverse

Package gorilla/reverse is a set of utilities to create request routers.
Go
53
star
16

i18n

Package gorilla/i18n groups packages related to internationalization
Go
49
star
17

template

A fork of the standard template packages.
Go
46
star
18

.github

The .github repository for the @gorilla organization.
9
star