segment

package module
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 15, 2020 License: MIT Imports: 1 Imported by: 0

README

segment

Build Status GoDoc codecov Go Report Card Open Source Helpers

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Segment

type Segment struct {
	// contains filtered or unexported fields
}

Segment ...

func NewSegment

func NewSegment(path, toIgnore string) Segment

NewSegment ...

func (*Segment) Capture

func (s *Segment) Capture(key string, store map[string]string)

Capture ...

func (*Segment) Consume

func (s *Segment) Consume(value string) bool

Consume ...

func (Segment) Current

func (s Segment) Current() string

Current ...

func (*Segment) Extract

func (s *Segment) Extract() string

Extract ...

func (Segment) Init

func (s Segment) Init() bool

Init ...

func (Segment) Previous

func (s Segment) Previous() string

Previous ...

func (*Segment) Restore

func (s *Segment) Restore(value string) bool

Restore ...

func (*Segment) Retract

func (s *Segment) Retract() string

Retract ...

func (Segment) Root

func (s Segment) Root() bool

Root ...

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL