An open API service indexing awesome lists of open source software.

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

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}}
```