https://github.com/jeppech/recap
Small package for mapping regex capture groups to a struct
https://github.com/jeppech/recap
Last synced: 9 months ago
JSON representation
Small package for mapping regex capture groups to a struct
- Host: GitHub
- URL: https://github.com/jeppech/recap
- Owner: jeppech
- License: mit
- Created: 2021-02-08T20:43:22.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2021-12-29T18:26:05.000Z (over 4 years ago)
- Last Synced: 2024-06-21T03:17:25.043Z (almost 2 years ago)
- Language: Go
- Homepage:
- Size: 25.4 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# recap
This library will map NAMED regex capture groups, to a tagged struct
```golang
package main
import "github.com/jeppech/recap"
type Person struct {
Name string `recap:"name"`
Age int `recap:"age"`
Public bool `recap:"public;default=false"`
Fruits Fruits
}
type Fruits struct {
Apple bool `recap:"fruit;contains=apple"`
Orange bool `recap:"fruit;contains=orange"`
Banana bool `recap:"fruit;contains=banana"`
}
func main() {
pers := Person{}
rx := regexp.MustCompile(`(?P[A-Za-z]+) (?P\d+) (?:(?P(true|false)))?\W?\[(?P.*)\]`)
err, match := recap.Parse(&pers, rx, "Jeppe 31 [orange banana]")
if err != nil {
log.Fatal(err)
}
if (match) {
fmt.Printf("%+v\n", pers)
} else {
// did not match
}
}
// {Name:Jeppe Age:31 Public:false Fruits:{Apple:false Orange:true Banana:true}}
```