https://github.com/jobstoit/tags
A simplified golang reflect package for tags
https://github.com/jobstoit/tags
go golang golang-library reflect
Last synced: 2 months ago
JSON representation
A simplified golang reflect package for tags
- Host: GitHub
- URL: https://github.com/jobstoit/tags
- Owner: jobstoit
- License: mit
- Created: 2019-02-07T16:53:59.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2021-07-19T19:12:43.000Z (over 4 years ago)
- Last Synced: 2025-08-14T23:50:09.631Z (8 months ago)
- Topics: go, golang, golang-library, reflect
- Language: Go
- Homepage:
- Size: 43 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Tags
[](https://godoc.org/github.com/jobstoit/tags)
[](https://actions-badge.atrox.dev/jobstoit/tags/goto)
An interface package for parsing tags in structs simplifying tag based golang reflections
## Usage
Create your custom tag reader simply defining a parser:
```go
func main() {
obj := Obj{}
if err := CustomUnmarshaler(&obj); err != nil {
panic(err)
}
if err := CustomScanner(&obj); err != nil {
panic(err)
}
}
type Obj struct {
Val bool `customTag:"yourcase"`
StVal string `customTag:"required"`
}
func CustomUnmarshaler(obj interface{}) error {
return tags.Parse(obj, `customTag`, func(tagVal string) (string, error) {
switch tagVal {
case `yourcase`:
return `true`, nil
default:
return ``, errCustom
}
})
}
func CustomScanner(obj interface{}) error {
return tags.Scan(obj, `customTag`, func(tagVal string, value interface{}) error {
v, ok := value.(string)
if !ok {
return nil
}
if tagVal == `required` && v == `` {
return errRequired
}
return nil
})
}
```
you get the values of a parent value in the string seperated by a dot:
```go
err := tags.Parse(obj, `tagname`, func(val string) (string, error) {
allvalues := strings.Split(val, `.`)
childVal := allvalues[len(allvalues)-1]
...
```
## Install
Get this package using go get:
```bash
$ go get github.com/jobbitz/tags/...
```