{"id":13413247,"url":"https://github.com/simonnilsson/ask","last_synced_at":"2025-03-14T19:31:39.106Z","repository":{"id":50192495,"uuid":"295161759","full_name":"simonnilsson/ask","owner":"simonnilsson","description":"A Go package that provides a simple way of accessing nested properties in maps and slices.","archived":false,"fork":false,"pushed_at":"2023-08-28T21:07:01.000Z","size":455,"stargazers_count":44,"open_issues_count":1,"forks_count":3,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-07-31T20:52:03.608Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/simonnilsson.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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}},"created_at":"2020-09-13T13:53:31.000Z","updated_at":"2024-07-24T13:56:42.000Z","dependencies_parsed_at":"2024-01-08T15:34:40.159Z","dependency_job_id":null,"html_url":"https://github.com/simonnilsson/ask","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simonnilsson%2Fask","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simonnilsson%2Fask/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simonnilsson%2Fask/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simonnilsson%2Fask/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/simonnilsson","download_url":"https://codeload.github.com/simonnilsson/ask/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243635274,"owners_count":20322908,"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":[],"created_at":"2024-07-30T20:01:36.033Z","updated_at":"2025-03-14T19:31:38.809Z","avatar_url":"https://github.com/simonnilsson.png","language":"Go","readme":"\u003cp align=\"center\"\u003e\n\u003cimg src=\"logo.svg\" height=\"100\" border=\"0\" alt=\"ASK\"\u003e\n\u003cbr/\u003e\n\u003ca href=\"https://pkg.go.dev/github.com/simonnilsson/ask\"\u003e\u003cimg src=\"https://pkg.go.dev/badge/github.com/simonnilsson/ask\" alt=\"PkgGoDev\" /\u003e\u003c/a\u003e\n\u003ca href=\"https://goreportcard.com/report/github.com/simonnilsson/ask\"\u003e\u003cimg src=\"https://goreportcard.com/badge/github.com/simonnilsson/ask\" alt=\"Go Report Card\" /\u003e\u003c/a\u003e\n\u003ca href=\"https://raw.githack.com/wiki/simonnilsson/ask/coverage.html\"\u003e\u003cimg src=\"https://github.com/simonnilsson/ask/wiki/coverage.svg\" alt=\"Go Coverage\" /\u003e\u003c/a\u003e\n\u003ca href=\"https://github.com/avelino/awesome-go#json\"\u003e\u003cimg src=\"https://awesome.re/mentioned-badge.svg\" alt=\"Mentioned in Awesome Go\" /\u003e\u003c/a\u003e  \n\u003cbr/\u003e\nand you shall receive.\n\u003c/p\u003e\nAsk provides a simple way of accessing nested properties in maps and slices. Works great in combination with encoding/json and other packages that \"Unmarshal\" arbitrary data into Go data-types. Inspired by the get function in the lodash javascript library.\n\u003cbr/\u003e\u003cbr/\u003e\n\n:warning: From version `v0.3.0`, the package requires [Go 1.17+](https://golang.org/doc/install), due to the usage of new additions to the reflect package.\n\n## Use\n\n```go\npackage main\n\nimport \"json\"\nimport \"github.com/simonnilsson/ask\"\n\nfunc main() {\n\n\t// Use parsed JSON as source data\n\tvar object map[string]interface{}\n\tjson.Unmarshal([]byte(`{ \"a\": [{ \"b\": { \"c\": 3 } }] }`), \u0026object)\n\n\t// Extract the 3\n\tres, ok := ask.For(object, \"a[0].b.c\").Int(0)\n\n\tfmt.Println(res, ok)\n\t// Output: 3 true\n\n\t// Attempt extracting a string at path .d that does not exist\n\tres2, ok := ask.ForArgs(object, \"a\", 0, \"b\", \"d\").String(\"nothing\")\n\n\tfmt.Println(res2, ok)\n\t// Output: nothing false\n\n}\n```\n\n## API\n\nInternally ask uses type assertions to traverse down the path supplied. Each invocation starts by calling For() with your data structure **source** and the **path** in this structure to extract. You can also use ForArgs() if want to supply each part of the path as a seperate argument, this can be usefull if your field names contain dots for example. The same accessors used in For can be used in ForArgs, but ForArgs can also take integers for slice indexes.\n\n```go\nFor(source interface{}, path string) *Answer\nForArgs(source interface{}, parts ...interface{}) *Answer\n```\nAdditional paths can be traversed by calling Path()/PathArgs() on the resulting answer.\n```go\n(a *Answer) Path(path string) *Answer\n(a *Answer) PathArgs(parts ...interface{}) *Answer\n```\n\n\n### Type assertion\n\nAfter receiving an `*Answer` from a call to For() it can be asserted to a type. The methods for this is seen below. Each function takes a default value as a parameter that will be returned in case the value can not be asserted from the answer. A second return value is used to indicate if the assertion was successful.\n\n```go\n(a *Answer) String(d string) (string, bool)\n(a *Answer) Bool(d bool) (bool, bool)\n(a *Answer) Int(d int64) (int64, bool)\n(a *Answer) Uint(d uint64) (uint64, bool)\n(a *Answer) Float(d float64) (float64, bool)\n(a *Answer) Slice(d []interface{}) ([]interface{}, bool)\n(a *Answer) Map(d map[string]interface{}) (map[string]interface{}, bool)\n```\n\nIf a number is found but it is of different type than requested it will be casted to desired type and return success. If the value would not fit within the valid range of requested type the operation will fail however and the default parameter will be returned instead.\n\nTwo additional methods are available, one to check if the answer has a value (not nil) and one to return the raw value as a interface{}.\n\n```go\n(a *Answer) Exists() bool\n(a *Answer) Value() interface{}\n```\n\nFor full documentation see [pkg.go.dev](https://pkg.go.dev/github.com/simonnilsson/ask).\n\n## License\n\n[MIT](LICENSE)\n","funding_links":[],"categories":["JSON","Relational Databases"],"sub_categories":["Search and Analytic Databases","检索及分析资料库","Advanced Console UIs"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsimonnilsson%2Fask","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsimonnilsson%2Fask","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsimonnilsson%2Fask/lists"}