{"id":23328860,"url":"https://github.com/tkrajina/go-reflector","last_synced_at":"2025-05-16T18:06:42.710Z","repository":{"id":46831441,"uuid":"59272054","full_name":"tkrajina/go-reflector","owner":"tkrajina","description":"Go reflection simplified","archived":false,"fork":false,"pushed_at":"2024-10-24T06:25:54.000Z","size":86,"stargazers_count":101,"open_issues_count":0,"forks_count":14,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-05-16T18:06:33.362Z","etag":null,"topics":["golang","library","reflection"],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tkrajina.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","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":"2016-05-20T06:53:42.000Z","updated_at":"2025-04-18T10:06:21.000Z","dependencies_parsed_at":"2024-10-27T09:02:20.727Z","dependency_job_id":null,"html_url":"https://github.com/tkrajina/go-reflector","commit_stats":null,"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tkrajina%2Fgo-reflector","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tkrajina%2Fgo-reflector/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tkrajina%2Fgo-reflector/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tkrajina%2Fgo-reflector/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tkrajina","download_url":"https://codeload.github.com/tkrajina/go-reflector/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254582905,"owners_count":22095518,"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":["golang","library","reflection"],"created_at":"2024-12-20T21:28:48.549Z","updated_at":"2025-05-16T18:06:42.687Z","avatar_url":"https://github.com/tkrajina.png","language":"Go","funding_links":[],"categories":["Go"],"sub_categories":[],"readme":"# Golang reflector\n\nFirst of all, don't use reflection if you don't have to.\n\nBut if you really have to... This library offers a simplified Golang reflection abstraction.\n\n## Getting and setting fields\n\nLet's suppose we have structs like:\n\n    type Address struct {\n        Street string `tag:\"be\" tag2:\"1,2,3\"`\n        Number int    `tag:\"bi\"`\n    }\n\n    type Person struct {\n        Name string `tag:\"bu\"`\n        Address\n    }\n\n    func (p Person) Hi(name string) string {\n        return fmt.Sprintf(\"Hi %s my name is %s\", name, p.Name)\n    }\n\nInitialize the **reflector**'s object wrapper:\n\n    import \"github.com/tkrajina/go-reflector/reflector\"\n\n\tp := Person{}\n\tobj := reflector.New(p)\n\nCheck if a field is valid:\n\n    obj.Field(\"Name\").IsValid()\n\nGet field value:\n\n    val, err := obj.Field(\"Name\").Get()\n\nSet field value:\n\n\tp := Person{}\n\tobj := reflector.New(\u0026p)\n    err := obj.Field(\"Name\").Set(\"Something\")\n\nDon't forget to use a pointer in `New()`, otherwise setters won't work. Field \"settability\" can be checked by using `field.IsSettable()`.\n\n## Tags\n\nGet a tag:\n\n    jsonTag := obj.Field(\"Name\").Tag(\"json\")\n\nGet tag values array (exploded with \",\" as a delimiter):\n\n    jsonTag := obj.Field(\"Name\").TagExpanded(\"json\")\n\nOr get a map with all field tags:\n\n    fieldTagsMap := obj.Field(\"Name\").Tags()\n\n## Listing fields\n\nThere are three ways to list fields:\n\n * List all fields: This will include anonymous structs **and** fields declared in  anonymous structs (`Name`, `Address`, `Street`, `Number`).\n * List flattened fields: Includes fields declared in anonymous structs **without**  anonymous structs (`Name`, `Street`, `Number`).\n * List nonflattened fields: Includes anonymous structs **without** their fields (`Name`, `Address`). This is the way fields are actually declared in the code.\n\nDepending on which listing you want, you can use:\n\n    fields := obj.FieldsAll()\n    fields := obj.FieldsFlattened()\n    fields := obj.Fields()\n\nYou can only get the list of anonymous fields with `obj.FieldsAnonymous()`.\n\nBe aware that because of anonymous structs, some field names can be returned twice!\nIn most cases this is not a desired situation, but you can use **reflector** to detect such situations in your code:\n\n    doubleDeclaredFields := obj.FindDoubleFields()\n    if len(doubleDeclaredFields) \u003e 0 {\n        fmt.Println(\"Detected multiple fields with same name:\", doubleDeclaredFields)\n    }\n\nThe field listing will contain both exported and unexported fields. Unexported fields are not gettable/settable, but their tags are readable.\n\n## Calling methods\n\n\tobj := reflector.New(\u0026Person{})\n    resp, err := obj.Method(\"Hi\").Call(\"John\", \"Smith\")\n\nThe `err` is not `nil` only if something was wrong with the method (for example invalid method name, or wrong argument number/types), not with the actual method call.\nIf the call finished, `err` will be `nil`.\nIf the method call returned an error, you can check it with:\n\n    if resp.IsError() {\n        fmt.Println(\"Got an error:\", resp.Error.Error())\n    } else {\n        fmt.Println(\"Method call response:\", resp.Result)\n    }\n\n## Listing methods\n\n    for _, method := range obj.Methods() {\n        fmt.Println(\"Method\", method.Name(), \"with input types\", method.InTypes(), \"and output types\", method.OutTypes())\n    }\n\n## Getting length, getting and setting slice/array/string/map elements\n\nMap:\n\n    m := map[string]interface{}{\"aaa\", 17}\n    o := reflector.New(m)\n    fmt.Println(\"Length\", o.Len())\n    val, found := o.GetByKey(\"aaa\")\n    o.SetByKey(\"bbb\", \"new value\")\n    fmt.Println(\"keys:\", o.Keys())\n\nSlice, string:\n\n    l := []int{1, 2, 3}\n    o := reflector.New(o)\n    fmt.Println(\"Length\", o.Len())\n    val, found := o.GetByIndex(0)\n    o.SetByIndex(0, 19)\n\n## Performance\n\nWhen reflecting the same type multiple times, **reflector** will cache as much reflection metadata as possible **only once** and use that in future.\n\nIf you make any changes to the library, run `make test-performance` to check performance improvement/deterioration before/after your change.\n\n    $ make test-performance\n    N=1000000 go test -v ./... -run=TestPerformance\n    === RUN   TestPerformance\n    WITH REFLECTION\n        n= 1000000\n        started: 2016-05-25 08:35:15.5258\n        ended: 2016-05-25 08:35:19.5258\n        duration: 4.269112s\n    --- PASS: TestPerformance (4.27s)\n    === RUN   TestPerformancePlain\n    WITHOUT REFLECTION\n        n= 1000000\n        started: 2016-05-25 08:35:19.5258\n        ended: 2016-05-25 08:35:19.5258\n        duration: 0.005237s\n    --- PASS: TestPerformancePlain (0.01s)\n    PASS\n    ok      github.com/tkrajina/go-reflector/reflector      4.285s\n\nKeep those numbers in mind before deciding to use reflection :)\n\nLicense\n-------\n\n**Reflector** is licensed under the [Apache License, Version 2.0](http://www.apache.org/licenses/LICENSE-2.0)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftkrajina%2Fgo-reflector","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftkrajina%2Fgo-reflector","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftkrajina%2Fgo-reflector/lists"}