https://github.com/a-poor/tags
Go struct tags helper library
https://github.com/a-poor/tags
go golang reflect struct struct-tags tags
Last synced: 2 months ago
JSON representation
Go struct tags helper library
- Host: GitHub
- URL: https://github.com/a-poor/tags
- Owner: a-poor
- License: mit
- Created: 2021-10-01T22:03:35.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2021-10-02T16:39:35.000Z (over 3 years ago)
- Last Synced: 2025-03-26T06:36:00.168Z (2 months ago)
- Topics: go, golang, reflect, struct, struct-tags, tags
- Language: Go
- Homepage: https://pkg.go.dev/github.com/a-poor/tags
- Size: 10.7 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# tags
[](https://pkg.go.dev/github.com/a-poor/tags)
[](https://pkg.go.dev/github.com/a-poor/tags)
[](https://github.com/a-poor/tags/actions/workflows/go.yml)
[](https://goreportcard.com/report/github.com/a-poor/tags)
[](https://github.com/a-poor/tags/blob/main/LICENSE)

[](https://sourcegraph.com/github.com/a-poor/tags?badge)
[](https://www.codefactor.io/repository/github/a-poor/tags/overview/main)_created by Austin Poor_
A micro-helper-library for working with Go struct tags.
## Installation
Install with `go get`:
```sh
go get github.com/a-poor/tags
```## Example
Here's a quick example of working with the `tags` library.
```go
// Define a struct that we'll be getting the tags from
user := struct {
ID int `app:"user_id"`
Name string `app:",omitempty"`
Email string `app:"user_email,omitempty"`
NotMe bool
ImEmpty bool `app:""`
}{}// Parse the struct's tags
fields := tags.ParseStructTags("app", user)// Print out the results as JSON
data, _ := json.MarshalIndent(fields, "", " ")
fmt.Println(string(data))
// Output: {
// "Email": [
// "user_email",
// "omitempty"
// ],
// "ID": [
// "user_id"
// ],
// "ImEmpty": [
// ""
// ],
// "Name": [
// "",
// "omitempty"
// ]
// }
```## Usage
The `tags` library is _very_ small. At least for now.
There's only one struct, `TagParser`, which has one field, `TagName`, and one method, `Parse`.
Say, for example, we have a struct that looks like this:
```go
type User struct {
ID int `myTag:"user_id"`
Name string `myTag:"name" otherTag"abc123"`
Balance float32 `myTag:"balance,omitempty"`
IsActive bool `myTag:",hello"`
}
```If we wanted to get the struct tag values for `myTag`, we could create a new `TagParser` like this:
```go
tp := tags.TagParser{TagName: "myTag"}
```and then parse the struct's tags like this:
```go
u := User{} // Create a blank user
ut := tp.Parse(u)
````ut` is of the type `map[string][]string`, where each of the map's keys are fields of the struct (with tags), and the map's values are arrays of tag values corresponding to the chosen tag, split on commas.
In our example, we would have the following result (formatted as JSON):
```json
{
"Balance": [
"balance",
"omitempty"
],
"ID": [
"user_id"
],
"IsActive": [
"",
"hello"
],
"Name": [
"name"
]
}
```## To Do
- Should untagged fields appear in the returned result?
- Add more error checks
- ie Catch panics caused by `tags` and return them rather than letting the panic propagate
- Check that the passed value is a struct (not a basic type)
- Be able to pass a pointer to a struct (without panicing)
- Fill a struct with struct tag values?
- ie struct would have fields `name`, `omitempty`, etc. and would be filled by position or value (like flags).## License
[MIT](./LICENSE)
## Contributing
Go ahead and create an issue or submit a pull request! I'd love to hear from you.