globber

package module
v0.0.0-...-da4e583 Latest Latest
Warning

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

Go to latest
Published: Mar 19, 2019 License: MIT Imports: 4 Imported by: 0

README

globber

GoDoc

Package globber facilitates glob-style pattern matching for configuration. It provides concrete types that are serializable as JSON and usable as flags via flag.Var()

This is a small package that provides some convenient wrappers. The hard work is performed by Sergey Kamardin's glob package, available here: https://github.com/gobwas/glob

Please see documentation of the glob package for examples of patterns and performance details.

Both globber and glob are MIT licensed.

Documentation

Overview

Package globber facilitates glob-style pattern matching for configuration. It provides concrete types that are serializable as JSON and usable as flags via flag.Var()

This is a small package that provides some convenient wrappers. The hard work is performed by Sergey Kamardin's glob package, available here: https://github.com/gobwas/glob

Please see documentation of the glob package for examples of patterns and performance details.

Both globber and glob are MIT licensed.

Example
package main

import (
	"flag"
	"fmt"
	"strings"

	"github.com/gentlemanautomaton/globber"
)

type Matcher interface {
	Match(string) bool
}

type Book struct {
	Title   string
	Authors []string
}

type BookSet []Book

func (books BookSet) ByTitle(matcher Matcher) (matched BookSet) {
	for _, book := range books {
		if matcher.Match(book.Title) {
			matched = append(matched, book)
		}
	}
	return
}

func (books BookSet) ByAuthor(matcher Matcher) (matched BookSet) {
	for _, book := range books {
		for _, author := range book.Authors {
			if matcher.Match(author) {
				matched = append(matched, book)
				break
			}
		}
	}
	return
}

var books = BookSet{
	// Freshman year
	{"Marmots 100: Orientation to marmotology", []string{"John Bramble", "T. Jones"}},
	{"Marmots 101: Native habitats and habits", []string{"John Bramble", "Grandpa George"}},
	{"Marmots 102: Typical predators in lakes", []string{"John Bramble", "T. Jones"}},
	// Sophomore year
	{"Marmots 200: Basic principals + methods", []string{"Smith", "Wesson", "McCloud"}},
	{"Marmots 201: Five 21st century policies", []string{"Don Juan the Iconoclast", "The Queen"}},
	{"Marmots 205: Marmot mascots and beyond?", []string{"Franky Frank Frankness", "Koala McGoo"}},
	// Comics
	{"No one messes with Marmotron or friends", []string{"John Dooey"}},
	{"How to spar with marmots, and feel okay", []string{"John Dooey", "Franky Dooey"}},
}

func main() {
	// Defaults
	var (
		title  = globber.New("Marmots*")
		author = globber.NewSet("John Bramble", "T. Jones", "*Queen*")
	)

	// Inputs
	args := []string{"-title", "Marmots 20*", "-author", "*Bramble,*Franky*"}

	// Flag processing
	fs := flag.NewFlagSet("", flag.ContinueOnError)
	fs.Var(&title, "title", "title search")
	fs.Var(&author, "author", "author search (separated by commas)")
	fs.Parse(args)

	// Title match by glob
	fmt.Printf("Books matching \"%s\" in the title:\n", title)
	for _, book := range books.ByTitle(title) {
		fmt.Printf("%s: %s\n", book.Title, strings.Join(book.Authors, ", "))
	}

	// Author match by glob set
	fmt.Printf("Books written by %s:\n", author)
	for _, book := range books.ByAuthor(author) {
		fmt.Printf("%s: %s\n", book.Title, strings.Join(book.Authors, ", "))
	}

}
Output:

Books matching "Marmots 20*" in the title:
Marmots 200: Basic principals + methods: Smith, Wesson, McCloud
Marmots 201: Five 21st century policies: Don Juan the Iconoclast, The Queen
Marmots 205: Marmot mascots and beyond?: Franky Frank Frankness, Koala McGoo
Books written by *Bramble,*Franky*:
Marmots 100: Orientation to marmotology: John Bramble, T. Jones
Marmots 101: Native habitats and habits: John Bramble, Grandpa George
Marmots 102: Typical predators in lakes: John Bramble, T. Jones
Marmots 205: Marmot mascots and beyond?: Franky Frank Frankness, Koala McGoo
How to spar with marmots, and feel okay: John Dooey, Franky Dooey

Index

Examples

Constants

View Source
const Separator = ','

Separator is the delimiter used when converting sets to and from strings.

Variables

This section is empty.

Functions

This section is empty.

Types

type Glob

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

Glob matches string literals and glob patterns.

func New

func New(value string) Glob

New returns a glob for the given string literal or pattern.

If value cannot be compiled as a pattern, the result will only match literal values.

func (Glob) MarshalJSON

func (g Glob) MarshalJSON() ([]byte, error)

MarshalJSON marshals the glob as a JSON-encoded string.

func (Glob) Match

func (g Glob) Match(value string) bool

Match returns true if the glob matches the given value exactly or as a pattern match.

func (Glob) MatchInsensitive

func (g Glob) MatchInsensitive(value string) bool

MatchInsensitive returns true if the glob case-insensitively matches the given value exactly or as a pattern match.

func (*Glob) Set

func (g *Glob) Set(value string) error

Set applies the given value or pattern to g. It facilitates use in the flag package.

func (Glob) String

func (g Glob) String() string

String returns a string representation of the value or pattern.

func (*Glob) UnmarshalJSON

func (g *Glob) UnmarshalJSON(b []byte) error

UnmarshalJSON unmarshals the glob from a JSON-encoded string.

type Matcher

type Matcher interface {
	Match(string) bool
}

Matcher is a generic matching interface satisfied by globs and sets of globs.

type Set

type Set []Glob

Set is a slice of globs.

func NewSet

func NewSet(values ...string) (set Set)

NewSet returns a set of globs for the given pattern. Commas and whitespace will divide individual globs.

func Split

func Split(values string) (set Set)

Split returns a set of globs for the given string. The string is split on commas and whitespace.

func (Set) Match

func (s Set) Match(value string) bool

Match returns true if any member of the set matches the given value.

func (Set) MatchInsensitive

func (s Set) MatchInsensitive(value string) bool

MatchInsensitive returns true if any member of the set case-insensitively matches the given value.

func (*Set) Set

func (s *Set) Set(values string) error

Set applies the given value or pattern to s. It facilitates use in the flag package.

Set assumes that values are delimited by whitespace or commas.

func (Set) String

func (s Set) String() string

String returns a string representation of the set. Individual values are joined together by commas.

Jump to

Keyboard shortcuts

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