{"id":22813336,"url":"https://github.com/southclaws/qstring","last_synced_at":"2025-04-22T16:54:51.298Z","repository":{"id":57497499,"uuid":"277883592","full_name":"Southclaws/qstring","owner":"Southclaws","description":"This package provides an easy way to marshal and unmarshal url query string data to and from structs.","archived":false,"fork":false,"pushed_at":"2020-07-07T17:41:54.000Z","size":34,"stargazers_count":3,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-29T16:51:13.930Z","etag":null,"topics":["http","query-string","reflection","struct-tags"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Southclaws.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}},"created_at":"2020-07-07T17:41:45.000Z","updated_at":"2021-12-21T03:47:42.000Z","dependencies_parsed_at":"2022-09-03T23:52:28.168Z","dependency_job_id":null,"html_url":"https://github.com/Southclaws/qstring","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Southclaws%2Fqstring","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Southclaws%2Fqstring/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Southclaws%2Fqstring/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Southclaws%2Fqstring/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Southclaws","download_url":"https://codeload.github.com/Southclaws/qstring/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250283765,"owners_count":21405217,"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":["http","query-string","reflection","struct-tags"],"created_at":"2024-12-12T12:26:39.789Z","updated_at":"2025-04-22T16:54:51.234Z","avatar_url":"https://github.com/Southclaws.png","language":"Go","readme":"# qstring\n\nThis package provides an easy way to marshal and unmarshal url query string data to and from structs.\n\nThis was originally forked from [dyninc/qstring](https://github.com/dyninc/qstring) but it seems to have become inactive\nand I wanted some extra features so I am now maintaining this fork.\n\n## Installation\n\n```bash\n$ go get github.com/Southclaws/qstring\n```\n\n## Examples\n\n### Unmarshaling\n\n```go\npackage main\n\nimport (\n\t\"net/http\"\n\n\t\"github.com/Southclaws/qstring\"\n)\n\n// Query is the http request query struct.\ntype Query struct {\n\tNames    []string\n\tLimit     int\n\tPage      int\n}\n\nfunc handler(w http.ResponseWriter, req *http.Request) {\n\tquery := \u0026Query{}\n\terr := qstring.Unmarshal(req.Url.Query(), query)\n\tif err != nil {\n\t\thttp.Error(w, err.Error(), http.StatusBadRequest)\n\t}\n\n\t// ... run conditional logic based on provided query parameters\n}\n```\n\nThe above example will unmarshal the query string from an http.Request and unmarshal it into the provided struct. This\nmeans that a query of `?names=foo\u0026names=bar\u0026limit=50\u0026page=1` would be unmarshaled into a struct similar to the\nfollowing:\n\n```go\nQuery{\n\tNames: []string{\"foo\", \"bar\"},\n\tLimit: 50,\n\tPage: 1,\n}\n```\n\n### Marshalling\n\n`qstring` also exposes two methods of Marshaling structs _into_ Query parameters, one will Marshal the provided struct\ninto a raw query string, the other will Marshal a struct into a `url.Values` type. Some Examples of both follow.\n\n### Marshal Raw Query String\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\t\"net/http\"\n\n\t\"github.com/Southclaws/qstring\"\n)\n\n// Query is the http request query struct.\ntype Query struct {\n\tNames    []string\n\tLimit     int\n\tPage      int\n}\n\nfunc main() {\n\tquery := \u0026Query{\n\t\tNames: []string{\"foo\", \"bar\"},\n\t\tLimit: 50,\n\t\tPage: 1,\n\t}\n\tq, err := qstring.MarshalString(query)\n\tfmt.Println(q)\n\t// Output: names=foo\u0026names=bar\u0026limit=50\u0026page=1\n}\n```\n\n### Marshal url.Values\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\t\"net/http\"\n\n\t\"github.com/Southclaws/qstring\"\n)\n\n// Query is the http request query struct.\ntype Query struct {\n\tNames    []string\n\tLimit     int\n\tPage      int\n}\n\nfunc main() {\n\tquery := \u0026Query{\n\t\tNames: []string{\"foo\", \"bar\"},\n\t\tLimit: 50,\n\t\tPage: 1,\n\t}\n\tq, err := qstring.Marshal(query)\n\tfmt.Println(q)\n\t// Output: map[names:[foo, bar] limit:[50] page:[1]]\n}\n```\n\n### Nested\n\nIn the same spirit as other Unmarshaling libraries, `qstring` allows you to Marshal/Unmarshal nested structs\n\n```go\npackage main\n\nimport (\n\t\"net/http\"\n\n\t\"github.com/Southclaws/qstring\"\n)\n\n// PagingParams represents common pagination information for query strings\ntype PagingParams struct {\n\tPage int\n\tLimit int\n}\n\n// Query is the http request query struct.\ntype Query struct {\n\tNames    []string\n\tPageInfo PagingParams\n}\n```\n\n### Complex Structures\n\nAgain, in the spirit of other Unmarshaling libraries, `qstring` allows for some more complex types, such as pointers and\ntime.Time fields. A more complete example might look something like the following code snippet\n\n```go\npackage main\n\nimport (\n\t\"time\"\n)\n\n// PagingParams represents common pagination information for query strings\ntype PagingParams struct {\n\tPage int\t`qstring:\"page\"`\n\tLimit int `qstring:\"limit\"`\n}\n\n// Query is the http request query struct.\ntype Query struct {\n\tNames    []string\n\tIDs      []int\n\tPageInfo *PagingParams\n\tCreated  time.Time\n\tModified time.Time\n}\n```\n\n## Additional Notes\n\n- All Timestamps are assumed to be in RFC3339 format\n- A struct field tag of `qstring` is supported and supports all of the features you've come to know and love from Go\n  (un)marshalers.\n  - A field tag with a value of `qstring:\"-\"` instructs `qstring` to ignore the field.\n  - A field tag with an the `omitempty` option set will be ignored if the field being marshaled has a zero value.\n    `qstring:\"name,omitempty\"`\n  - A slice field tag with the `comma` option set will produce a comma-separated list of items in a single query\n    parameter instead of multiple instances of the parameter. `qstring:\"names,comma\"` will produce `?names=a,b,c`\n    instead of `?names=a\u0026names=b\u0026names=c`.\n\n### Custom Fields\n\nIn order to facilitate more complex queries `qstring` also provides some custom fields to save you a bit of headache\nwith custom marshal/unmarshaling logic. Currently the following custom fields are provided:\n\n- `qstring.ComparativeTime` - Supports timestamp query parameters with optional logical operators (\u003c, \u003e, \u003c=, \u003e=) such as\n  `?created\u003c=2006-01-02T15:04:05Z`\n\n## Benchmarks\n\n```\nBenchmarkUnmarshall-4 \t  500000\t      2711 ns/op\t     448 B/op\t      23 allocs/op\nBenchmarkRawPLiteral-4\t 1000000\t      1675 ns/op\t     448 B/op\t      23 allocs/op\nok  \tgithub.com/Southclaws/qstring\t3.163s\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsouthclaws%2Fqstring","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsouthclaws%2Fqstring","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsouthclaws%2Fqstring/lists"}