{"id":13412894,"url":"https://github.com/sonh/qs","last_synced_at":"2025-03-14T18:32:37.056Z","repository":{"id":52075655,"uuid":"300570540","full_name":"sonh/qs","owner":"sonh","description":"Go module for encoding structs into URL query parameters","archived":false,"fork":false,"pushed_at":"2024-03-05T06:22:38.000Z","size":40,"stargazers_count":75,"open_issues_count":0,"forks_count":5,"subscribers_count":3,"default_branch":"main","last_synced_at":"2024-07-31T20:51:37.276Z","etag":null,"topics":["encoder","encoding","form","form-data","go","query-parameters","querystring","url-parameters"],"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/sonh.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":"2020-10-02T09:50:35.000Z","updated_at":"2024-07-03T06:27:39.000Z","dependencies_parsed_at":"2024-06-18T18:30:55.395Z","dependency_job_id":"65575939-92a3-4e6a-92e8-a4c3da517414","html_url":"https://github.com/sonh/qs","commit_stats":null,"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sonh%2Fqs","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sonh%2Fqs/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sonh%2Fqs/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sonh%2Fqs/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sonh","download_url":"https://codeload.github.com/sonh/qs/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":221495313,"owners_count":16832458,"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","encoding","form","form-data","go","query-parameters","querystring","url-parameters"],"created_at":"2024-07-30T20:01:30.743Z","updated_at":"2024-10-26T04:31:23.318Z","avatar_url":"https://github.com/sonh.png","language":"Go","readme":"# qs #\n[![Build](https://github.com/sonh/qs/actions/workflows/go.yml/badge.svg?branch=main)](https://github.com/sonh/qs/actions)\n[![Codecov](https://codecov.io/gh/sonh/qs/branch/main/graph/badge.svg)](https://codecov.io/gh/sonh/qs)\n[![GoReportCard](https://goreportcard.com/badge/github.com/sonh/qs)](https://goreportcard.com/report/github.com/sonh/qs)\n[![Release](https://img.shields.io/github/release/sonh/qs.svg?color=brightgreen)](https://github.com/sonh/qs/releases/)\n[![PkgGoDev](https://pkg.go.dev/badge/github.com/sonh/qs)](https://pkg.go.dev/github.com/sonh/qs)\n[![MIT License](https://img.shields.io/badge/License-MIT-blue.svg)](https://github.com/sonh/qs/blob/main/LICENSE)\n\nPackage sonh/qs encodes structs into url.Values.\n\n## Installation\n```bash\ngo get github.com/sonh/qs\n```\n\n## Usage\n```go\nimport (\n    \"github.com/sonh/qs\"\n)\n```\nPackage qs exports `NewEncoder()` function to create an encoder. \n\nEncoder caches struct info to speed up encoding process, use a single instance is highly recommended. \n\nUse `WithTagAlias()` func to register custom tag alias (default is `qs`)\n```go\nencoder = qs.NewEncoder(\n    qs.WithTagAlias(\"myTag\"),\n)\n```\n\nEncoder has `Values()` and `Encode()` functions to encode structs into `url.Values`.\n\n### Supported data types:\n- all basic types (`bool`, `uint`, `string`, `float64`,...)\n- `struct`\n- `slice`, `array`\n- `pointer`\n- `time.Time`   \n- custom type\n\n### Example\n```go\ntype Query struct {\n    Tags   []string  `qs:\"tags\"`\n    Limit  int       `qs:\"limit\"`\n    From   time.Time `qs:\"from\"`\n    Active bool      `qs:\"active,omitempty\"`  //omit empty value\n    Ignore float64   `qs:\"-\"`                 //ignore\n}\n\nquery := \u0026Query{\n    Tags:   []string{\"docker\", \"golang\", \"reactjs\"},\n    Limit:  24,\n    From:   time.Unix(1580601600, 0).UTC(),\n    Ignore: 0,\n}\n\nencoder := qs.NewEncoder()\nvalues, err := encoder.Values(query)\nif err != nil {\n    // Handle error\n}\nfmt.Println(values.Encode()) //(unescaped) output: \"from=2020-02-02T00:00:00Z\u0026limit=24\u0026tags=docker\u0026tags=golang\u0026tags=reactjs\"\n```\n### Bool format\nUse `int` option to encode bool to integer\n```go\ntype Query struct {\n    DefaultFmt bool `qs:\"default_fmt\"`\n    IntFmt     bool `qs:\"int_fmt,int\"`\n}\n\nquery := \u0026Query{\n    DefaultFmt: true, \n    IntFmt:     true,\n}\nvalues, _ := encoder.Values(query)\nfmt.Println(values.Encode()) // (unescaped) output: \"default_fmt=true\u0026int_fmt=1\"\n```\n### Time format\nBy default, package encodes time.Time values as RFC3339 format. \n\nIncluding the `\"second\"` or `\"millis\"` option to signal that the field should be encoded as second or millisecond.\n```go\ntype Query struct {\n    Default time.Time   `qs:\"default_fmt\"`\n    Second  time.Time   `qs:\"second_fmt,second\"` //use `second` option\n    Millis  time.Time   `qs:\"millis_fmt,millis\"` //use `millis` option\n}\n\nt := time.Unix(1580601600, 0).UTC()\nquery := \u0026Query{\n    Default: t,\n    Second:  t,\n    Millis:  t,\n}\n\nencoder := qs.NewEncoder()\nvalues, _ := encoder.Values(query)\nfmt.Println(values.Encode()) // (unescaped) output: \"default_fmt=2020-02-02T00:00:00Z\u0026millis_fmt=1580601600000\u0026second_fmt=1580601600\"\n```\n\n### Slice/Array Format\nSlice and Array default to encoding into multiple URL values of the same value name.\n```go\ntype Query struct {\n    Tags []string `qs:\"tags\"`\n}\n\nvalues, _ := encoder.Values(\u0026Query{Tags: []string{\"foo\",\"bar\"}})\nfmt.Println(values.Encode()) //(unescaped) output: \"tags=foo\u0026tags=bar\"\n```\n\nIncluding the `comma` option to signal that the field should be encoded as a single comma-delimited value.\n```go\ntype Query struct {\n    Tags []string `qs:\"tags,comma\"`\n}\n\nvalues, _ := encoder.Values(\u0026Query{Tags: []string{\"foo\",\"bar\"}})\nfmt.Println(values.Encode()) //(unescaped) output: \"tags=foo,bar\"\n```\n\nIncluding the `bracket` option to signal that the multiple URL values should have \"[]\" appended to the value name.\n```go\ntype Query struct {\n    Tags []string `qs:\"tags,bracket\"`\n}\n\nvalues, _ := encoder.Values(\u0026Query{Tags: []string{\"foo\",\"bar\"}})\nfmt.Println(values.Encode()) //(unescaped) output: \"tags[]=foo\u0026tags[]=bar\"\n```\n\nThe `index` option will append an index number with brackets to value name.\n```go\ntype Query struct {\n    Tags []string `qs:\"tags,index\"`\n}\n\nvalues, _ := encoder.Values(\u0026Query{Tags: []string{\"foo\",\"bar\"}})\nfmt.Println(values.Encode()) //(unescaped) output: \"tags[0]=foo\u0026tags[1]=bar\"\n```\n\n### Nested structs\nAll nested structs are encoded including the parent value name with brackets for scoping.\n```go\ntype User struct {\n    Verified bool      `qs:\"verified\"`\n    From     time.Time `qs:\"from,millis\"`\n}\n\ntype Query struct {\n    User User `qs:\"user\"`\n}\n\nquery := Query{\n    User: User{\n        Verified: true,\n        From: time.Now(),\n    },\n}\nvalues, _ := encoder.Values(query)\nfmt.Println(values.Encode()) //(unescaped) output: \"user[from]=1601623397728\u0026user[verified]=true\"\n```\n\n### Custom Type\nImplement funcs:\n* `EncodeParam` to encode itself into query param.\n* `IsZero` to check whether an object is zero to determine whether it should be omitted when encoding.\n```go\ntype NullableName struct {\n\tFirst string\n\tLast  string\n}\n\nfunc (n NullableName) EncodeParam() (string, error) {\n\treturn n.First + n.Last, nil\n}\n\nfunc (n NullableName) IsZero() bool {\n\treturn n.First == \"\" \u0026\u0026 n.Last == \"\"\n}\n\ntype Struct struct {\n    User  NullableName `qs:\"user\"`\n    Admin NullableName `qs:\"admin,omitempty\"`\n}\n\ns := Struct{\n    User: NullableName{\n        First: \"son\",\n        Last:  \"huynh\",\n    },\n}\nencoder := qs.NewEncoder()\n\nvalues, err := encoder.Values(\u0026s)\nif err != nil {\n    // Handle error\n    fmt.Println(\"failed\")\n    return\n}\nfmt.Println(values.Encode()) //(unescaped) output: \"user=sonhuynh\"\n```\n\n### Limitation\n- if elements in `slice/array` are `struct` data type, multi-level nesting are limited\n- no decoder yet\n\n_Will improve in future versions_ \n\n## License\nDistributed under MIT License, please see license file in code for more details.\n","funding_links":[],"categories":["表单","Forms","表单`表单解析与绑定`","Go","Relational Databases"],"sub_categories":["SQL 查询语句构建库","Search and Analytic Databases","检索及分析资料库","Advanced Console UIs"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsonh%2Fqs","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsonh%2Fqs","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsonh%2Fqs/lists"}