{"id":13692968,"url":"https://github.com/gorilla/schema","last_synced_at":"2025-05-13T21:04:00.254Z","repository":{"id":4896196,"uuid":"6051827","full_name":"gorilla/schema","owner":"gorilla","description":"Package gorilla/schema fills a struct with form values.","archived":false,"fork":false,"pushed_at":"2024-08-19T14:00:33.000Z","size":162,"stargazers_count":1424,"open_issues_count":19,"forks_count":237,"subscribers_count":28,"default_branch":"main","last_synced_at":"2025-04-28T11:56:31.133Z","etag":null,"topics":["encoder","forms","go","golang","gorilla","gorilla-web-toolkit","http","schema"],"latest_commit_sha":null,"homepage":"https://gorilla.github.io","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"sudar/Arduino-Makefile","license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/gorilla.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2012-10-02T21:33:46.000Z","updated_at":"2025-04-22T13:19:46.000Z","dependencies_parsed_at":"2023-01-13T13:17:11.925Z","dependency_job_id":"5603fe0a-c26a-42a2-9c34-019d6553b4de","html_url":"https://github.com/gorilla/schema","commit_stats":{"total_commits":124,"total_committers":49,"mean_commits":"2.5306122448979593","dds":0.7983870967741935,"last_synced_commit":"180f71e625073e210314dc8ca83dfbbbd0096dfe"},"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gorilla%2Fschema","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gorilla%2Fschema/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gorilla%2Fschema/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gorilla%2Fschema/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gorilla","download_url":"https://codeload.github.com/gorilla/schema/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254028185,"owners_count":22002204,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["encoder","forms","go","golang","gorilla","gorilla-web-toolkit","http","schema"],"created_at":"2024-08-02T17:01:04.008Z","updated_at":"2025-05-13T21:04:00.168Z","avatar_url":"https://github.com/gorilla.png","language":"Go","readme":"# gorilla/schema\n\n![testing](https://github.com/gorilla/schema/actions/workflows/test.yml/badge.svg)\n[![codecov](https://codecov.io/github/gorilla/schema/branch/main/graph/badge.svg)](https://codecov.io/github/gorilla/schema)\n[![godoc](https://godoc.org/github.com/gorilla/schema?status.svg)](https://godoc.org/github.com/gorilla/schema)\n[![sourcegraph](https://sourcegraph.com/github.com/gorilla/schema/-/badge.svg)](https://sourcegraph.com/github.com/gorilla/schema?badge)\n\n\n![Gorilla Logo](https://github.com/gorilla/.github/assets/53367916/d92caabf-98e0-473e-bfbf-ab554ba435e5)\n\nPackage gorilla/schema converts structs to and from form values.\n\n## Example\n\nHere's a quick example: we parse POST form values and then decode them into a struct:\n\n```go\n// Set a Decoder instance as a package global, because it caches\n// meta-data about structs, and an instance can be shared safely.\nvar decoder = schema.NewDecoder()\n\ntype Person struct {\n    Name  string\n    Phone string\n}\n\nfunc MyHandler(w http.ResponseWriter, r *http.Request) {\n    err := r.ParseForm()\n    if err != nil {\n        // Handle error\n    }\n\n    var person Person\n\n    // r.PostForm is a map of our POST form values\n    err = decoder.Decode(\u0026person, r.PostForm)\n    if err != nil {\n        // Handle error\n    }\n\n    // Do something with person.Name or person.Phone\n}\n```\n\nConversely, contents of a struct can be encoded into form values. Here's a variant of the previous example using the Encoder:\n\n```go\nvar encoder = schema.NewEncoder()\n\nfunc MyHttpRequest() {\n    person := Person{\"Jane Doe\", \"555-5555\"}\n    form := url.Values{}\n\n    err := encoder.Encode(person, form)\n\n    if err != nil {\n        // Handle error\n    }\n\n    // Use form values, for example, with an http client\n    client := new(http.Client)\n    res, err := client.PostForm(\"http://my-api.test\", form)\n}\n\n```\n\nTo 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:\n\n```go\ntype Person struct {\n    Name  string `schema:\"name,required\"`  // custom name, must be supplied\n    Phone string `schema:\"phone\"`          // custom name\n    Admin bool   `schema:\"-\"`              // this field is never set\n}\n```\n\nThe supported field types in the struct are:\n\n* bool\n* float variants (float32, float64)\n* int variants (int, int8, int16, int32, int64)\n* string\n* uint variants (uint, uint8, uint16, uint32, uint64)\n* struct\n* a pointer to one of the above types\n* a slice or a pointer to a slice of one of the above types\n\nUnsupported types are simply ignored, however custom types can be registered to be converted.\n\n## Setting Defaults\n\nIt is possible to set default values when encoding/decoding by using the `default` tag option. The value of `default` is applied when a field has a zero value, a pointer has a nil value, or a slice is empty.\n\n```go\ntype Person struct {\n    Phone string `schema:\"phone,default:+123456\"`          // custom name\n    Age int     `schema:\"age,default:21\"`\n\tAdmin bool    `schema:\"admin,default:false\"`\n\tBalance float64 `schema:\"balance,default:10.0\"`\n    Friends []string `schema:friends,default:john|bob`\n}\n```\n\nThe `default` tag option is supported for the following types:\n\n* bool\n* float variants (float32, float64)\n* int variants (int, int8, int16, int32, int64)\n* uint variants (uint, uint8, uint16, uint32, uint64)\n* string\n* a slice of the above types. As shown in the example above, `|` should be used to separate between slice items. \n* a pointer to one of the above types (pointer to slice and slice of pointers are not supported).\n\n\u003e [!NOTE]  \n\u003e Because primitive types like int, float, bool, unint and their variants have their default (or zero) values set by Golang, it is not possible to distinguish them from a provided value when decoding/encoding form values. In this case, the value provided by the `default` option tag will be always applied. For example, let's assume that the value submitted in the form for `balance` is `0.0` then the default of `10.0` will be applied, even if `0.0` is part of the form data for the `balance` field. In such cases, it is highly recommended to use pointers to allow schema to distinguish between when a form field has no provided value and when a form has a value equal to the corresponding default set by Golang for a particular type. If the type of the `Balance` field above is changed to `*float64`, then the zero value would be `nil`. In this case, if the form data value for `balance` is `0.0`, then the default will not be applied.\n\n## License\n\nBSD licensed. See the LICENSE file for details.\n","funding_links":[],"categories":["开源类库","Go","Open source library","Repositories"],"sub_categories":["路由","Redirect"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgorilla%2Fschema","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgorilla%2Fschema","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgorilla%2Fschema/lists"}