segment
![Open Source Helpers](https://www.codetriage.com/opentogo/segment/badges/users.svg)
This package was inspired in Michel Martens' seg Crystal library and it offer a segment matcher for paths in Go.
Installation
go get github.com/opentogo/segment
Usage
Consider this interactive session:
package main
import (
"fmt"
"github.com/opentogo/segment"
)
func main() {
seg := segment.NewSegment("/users/42", "")
fmt.Printf("%#v\n", seg)
// => segment.Segment{path:"/users/42", size:9, pos:0}
fmt.Println(seg.Previous())
// =>
fmt.Println(seg.Current())
// => /users/42
fmt.Println(seg.Consume("users"))
// => true
fmt.Println(seg.Previous())
// => /users
fmt.Println(seg.Current())
// => /42
fmt.Println(seg.Consume("42"))
// => true
fmt.Println(seg.Previous())
// => /users/42
fmt.Println(seg.Current())
// =>
}
As you can see, the command fails and the prev
and curr
strings are not altered. Now we'll see how to capture segment values:
package main
import (
"fmt"
"github.com/opentogo/segment"
)
func main() {
var (
captures = map[string]string{}
seg = segment.NewSegment("/users/42", "")
)
fmt.Printf("%#v\n", seg)
// => segment.Segment{path:"/users/42", size:9, pos:0}
fmt.Println(seg.Previous())
// =>
fmt.Println(seg.Current())
// => /users/42
seg.Capture("foo", captures)
fmt.Println(seg.Previous())
// => /users
fmt.Println(seg.Current())
// => /42
seg.Capture("bar", captures)
fmt.Println(seg.Previous())
// => /users/42
fmt.Println(seg.Current())
// =>
fmt.Println(captures)
// => map[bar:42 foo:users]
}
It is also possible to extract
the next segment from the path. The method extract
returns the next segment, if available, or nil otherwise:
package main
import (
"fmt"
"github.com/opentogo/segment"
)
func main() {
seg := segment.NewSegment("/users/42", "")
fmt.Printf("%#v\n", seg)
// => segment.Segment{path:"/users/42", size:9, pos:0}
fmt.Println(seg.Previous())
// =>
fmt.Println(seg.Current())
// => /users/42
fmt.Println(seg.Extract())
// => users
fmt.Println(seg.Previous())
// => /users
fmt.Println(seg.Current())
// => /42
fmt.Println(seg.Extract())
// => 42
fmt.Println(seg.Previous())
// => /users/42
fmt.Println(seg.Current())
// =>
}
You can also go back by using the methods retract
and restore
, which are the antidote to extract
and consume
respectively.
Let's see how retract
works:
package main
import (
"fmt"
"github.com/opentogo/segment"
)
func main() {
seg := segment.NewSegment("/users/42", "")
fmt.Printf("%#v\n", seg)
// => segment.Segment{path:"/users/42", size:9, pos:0}
fmt.Println(seg.Previous())
// =>
fmt.Println(seg.Current())
// => /users/42
fmt.Println(seg.Extract())
// => users
fmt.Println(seg.Previous())
// => /users
fmt.Println(seg.Current())
// => /42
fmt.Println(seg.Retract())
// => users
fmt.Println(seg.Previous())
// =>
fmt.Println(seg.Current())
// => /users/42
}
And now restore
:
package main
import (
"fmt"
"github.com/opentogo/segment"
)
func main() {
seg := segment.NewSegment("/users/42", "")
fmt.Printf("%#v\n", seg)
// => segment.Segment{path:"/users/42", size:9, pos:0}
fmt.Println(seg.Previous())
// =>
fmt.Println(seg.Current())
// => /users/42
fmt.Println(seg.Extract())
// => users
fmt.Println(seg.Previous())
// => /users
fmt.Println(seg.Current())
// => /42
fmt.Println(seg.Restore("foo"))
// => false
fmt.Println(seg.Restore("users"))
// => true
fmt.Println(seg.Previous())
// =>
fmt.Println(seg.Current())
// => /users/42
}
Contributors