pattern

package
v0.0.0-...-df570b3 Latest Latest
Warning

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

Go to latest
Published: Jul 12, 2018 License: MIT Imports: 4 Imported by: 2

README

Pattern

GoDoc

Pattern provides a simple URI pattern matcher, useful for constructing url matchers.

Example


package main

import "github.com/influx6/faux/pattern"
import "fmt"

func main(){

	r := pattern.New(`/name/{id:[\d+]}/`)

	params, state := r.Validate(`/name/12/d`)
  if !state {
    panic("No match found")
  }

  fmt.Printf("URL Params: %+s",params)
}

Documentation

Overview

Package pattern provides a simple regexp pattern matching library majorly for constructing URL matchers.

Patterns in this package follow the follow approach in declaring custom match

segments.

pattern: /name/{id:[/\d+/]}/log/{date:[/\w+\W+/]}
pattern: /name/:id

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CheckPriority

func CheckPriority(patt string) int

CheckPriority is used to return the priority of a pattern. 0 for highest(when no parameters). 1 for restricted parameters({id:[]}). 2 for no paramters. The first parameter catched is used for rating. The ratings go from highest to lowest .i.e (0-2).

func CleanPath

func CleanPath(p string) string

CleanPath provides a public path cleaner

func CleanSlashes

func CleanSlashes(p string) string

CleanSlashes cleans all double forward slashes into one

func HasKeyParam

func HasKeyParam(p string) bool

HasKeyParam returns true/false if the special pattern {:[..]} exists in the string

func HasParam

func HasParam(p string) bool

HasParam returns true/false if the special pattern {:[..]} exists in the string

func HasPick

func HasPick(p string) bool

HasPick matches string of type :id,:name

func IsEndless

func IsEndless(s string) bool

IsEndless returns true/false if the pattern as a /*

func RemoveBracket

func RemoveBracket(s string) string

RemoveBracket removes '[' and ']' from any string

func RemoveCurly

func RemoveCurly(s string) string

RemoveCurly removes '{' and '}' from any string

func SplitPattern

func SplitPattern(c string) []string

SplitPattern splits a pattern with the '/'

func SplitPatternAndRemovePrefix

func SplitPatternAndRemovePrefix(c string) []string

SplitPatternAndRemovePrefix splits a pattern with the '/'

func TrimEndSlashe

func TrimEndSlashe(c string) string

TrimEndSlashe removes the '/' at the end of string.

func TrimSlashes

func TrimSlashes(c string) string

TrimSlashes removes the '/' at the beginning and end of string.

func YankSpecial

func YankSpecial(val string) (string, string, bool)

YankSpecial provides a means of extracting parts of form `{id:[\d+]}`

Types

type Matchable

type Matchable interface {
	IsParam() bool
	HasHash() bool
	Segment() string
	Validate(string) bool
}

Matchable defines an interface for matchers.

func Segment

func Segment(segment string) Matchable

Segment returns a Matchable for a specific part of a pattern eg. :name, age, {id:[\\d+]}.

type Matchers

type Matchers []Matchable

Matchers defines a list of machers for validating patterns with.

func SegmentList

func SegmentList(pattern string) Matchers

SegmentList returns list of SegmentMatcher which implements the Matchable interface, with each made of each segment of the pattern.

type Params

type Params map[string]string

Params defines a map of stringed keys and values.

type SegmentMatcher

type SegmentMatcher struct {
	*regexp.Regexp
	// contains filtered or unexported fields
}

SegmentMatcher defines a single piece of pattern to be matched against.

func (*SegmentMatcher) HasHash

func (s *SegmentMatcher) HasHash() bool

HasHash returns true/false if this segment hash the hash.

func (*SegmentMatcher) IsParam

func (s *SegmentMatcher) IsParam() bool

IsParam returns true/false if the segment is also a paramter.

func (*SegmentMatcher) Segment

func (s *SegmentMatcher) Segment() string

Segment returns the original string that makes up this segment matcher.

func (*SegmentMatcher) Validate

func (s *SegmentMatcher) Validate(m string) bool

Validate validates the value against the matcher.

type URIMatcher

type URIMatcher interface {
	Validate(string) (Params, string, bool)
	Pattern() string
	Priority() int
}

URIMatcher defines an interface for a URI matcher.

func New

func New(pattern string) URIMatcher

New returns a new instance of a URIMatcher.

Jump to

Keyboard shortcuts

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