{"id":18894563,"url":"https://github.com/ravsii/textra","last_synced_at":"2025-04-15T00:32:10.623Z","repository":{"id":64300851,"uuid":"572060661","full_name":"ravsii/textra","owner":"ravsii","description":"A package designed to extract and work with go structs as values. Types \u0026 Tags export","archived":false,"fork":false,"pushed_at":"2023-05-01T22:57:11.000Z","size":75,"stargazers_count":6,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-28T13:05:34.434Z","etag":null,"topics":["go","golang","struct","structtags","tags"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ravsii.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}},"created_at":"2022-11-29T13:24:24.000Z","updated_at":"2024-08-09T01:36:54.000Z","dependencies_parsed_at":"2024-01-08T16:09:19.221Z","dependency_job_id":"a629897c-6bc8-435c-aea3-21b4340d2af5","html_url":"https://github.com/ravsii/textra","commit_stats":null,"previous_names":["ravcii/textra"],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ravsii%2Ftextra","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ravsii%2Ftextra/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ravsii%2Ftextra/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ravsii%2Ftextra/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ravsii","download_url":"https://codeload.github.com/ravsii/textra/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248984451,"owners_count":21193754,"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":["go","golang","struct","structtags","tags"],"created_at":"2024-11-08T08:23:15.442Z","updated_at":"2025-04-15T00:32:07.053Z","avatar_url":"https://github.com/ravsii.png","language":"Go","funding_links":[],"categories":["Go 工具","Go Tools"],"sub_categories":["路由器","Routers"],"readme":"# Textra\n\n[![Go Reference](https://pkg.go.dev/badge/github.com/ravsii/textra.svg)](https://pkg.go.dev/github.com/ravsii/textra) [![codecov](https://codecov.io/gh/ravsii/textra/branch/main/graph/badge.svg?token=C8WA38GNFV)](https://codecov.io/gh/ravsii/textra) [![Go Report Card](https://goreportcard.com/badge/github.com/ravsii/textra)](https://goreportcard.com/report/github.com/ravsii/textra) [![Mentioned in Awesome Go](https://awesome.re/mentioned-badge.svg)](https://github.com/avelino/awesome-go) \n\nTextra is a zero-dependency, simple and fast struct tags parser library. It also has json tags for all structs, in case of JSON output.\n\nInitially I built it for another private project, but decided to try to open source it, since it could be useful for someone. Because of that, it has some features that feel redundant, like having field type as a part of returned data\n\n## Installation\n\n```shell\ngo get github.com/ravsii/textra\n```\n\n## Examples\n\nBasic usage:\n\n```go\ntype Tester struct {\n NoTags   bool\n WithTag  string `json:\"with_tag,omitempty\"`\n WithTags string `json:\"with_tags\"          sql:\"with_tag\"`\n SqlOnly  string `sql:\"sql_only\"`\n}\n\nfunc main() {\n basic := textra.Extract((*Tester)(nil))\n for _, field := range basic {\n  fmt.Println(field)\n }\n}\n\n```\n\n```text\nNoTags(bool):[]\nWithTag(string):[json:\"with_tag,omitempty\"]\nWithTags(string):[json:\"with_tags\" sql:\"with_tag\"]\nSqlOnly(string):[sql:\"sql_only\"]\n```\n\nYou can look at return types at [pkg.go.dev](https://pkg.go.dev/github.com/Ravcii/textra), but basically it returns a slice of fields with its types (as strings) and a slice of Tags for each field.\n\nNow let's apply some functions:\n\n```go\n removed := basic.RemoveEmpty()\n for _, field := range removed {\n  fmt.Println(field)\n }\n```\n\n```text\nSqlOnly(*[]string):[pg:\"sql_only\" sql:\"sql_only\"]\nWithTag(string):[json:\"with_tag,omitempty\"]\nWithTags(*string):[json:\"with_tags\" sql:\"with_tag\"]\n```\n\nWhat if we care only about SQL tags?\n\n```go\n onlySQL := removed.OnlyTag(\"sql\")\n for _, field := range onlySQL {\n  fmt.Println(field)\n }\n```\n\n```text\nSqlOnly(*[]string):sql:\"sql_only\"\nWithTags(*string):sql:\"with_tag\"\n```\n\n_Only() is a bit special as it returns a Field of a different type, with `Tag` rather than `Tags`(=`[]Tag`)_\n\nAPI is built like standard's `time` package, where chaining function will create new values, instead of modifying them.\n\nAlthough it may be redundant, it also parses types a their string representation (for easier comparison or output, if you need it)\n\n```go\ntype Types struct {\n intType        int\n intPType       *int\n byteType       byte\n bytePType      *byte\n byteArrType    []byte\n byteArrPType   []*byte\n bytePArrPType  *[]*byte\n runeType       rune\n runePType      *rune\n stringType     string\n stringPType    *string\n booleanType    bool\n booleanPType   *bool\n mapType        map[string]string\n mapPType       map[*string]*string\n mapPImportType map[*string]*time.Time\n chanType       chan int\n funcType       func() error\n funcParamsType func(arg1 int, arg2 string, arg3 map[*string]*time.Time) (int, error)\n importType     time.Time\n pointerType    *string\n}\n\nfunc main() {\n fields := textra.Extract((*Types)(nil))\n for _, field := range fields {\n  fmt.Println(field.Name, field.Type)\n }\n}\n```\n\n```text\nintType int\nintPType *int\nbyteType uint8\nbytePType *uint8\nbyteArrType []uint8\nbyteArrPType []*uint8\nbytePArrPType *[]*uint8\nruneType int32\nrunePType *int32\nstringType string\nstringPType *string\nbooleanType bool\nbooleanPType *bool\nmapType map[string]string\nmapPType map[*string]*string\nmapPImportType map[*string]*time.Time\nchanType chan\nfuncType func() error\nfuncParamsType func(int, string, map[*string]*time.Time) int, error\nimportType time.Time\npointerType *string\n```\n\n### TODO\n\n- [ ] Examples for go.dev\n- [ ] ...?\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fravsii%2Ftextra","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fravsii%2Ftextra","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fravsii%2Ftextra/lists"}