parse

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 7, 2019 License: BSD-3-Clause Imports: 10 Imported by: 8

README

parse

Simple parsing functions.

Description

This currently supplies two simple objects to help with parsing:

StringToken is used to create a string iterator. It's a small convience over indexing a slice, but results in cleaner code.

Example:

	args := []string{"this", "is", "a", "sequence"}
	token := parse.NewStringToken(args...)
	for a, err := token.Next(); err == nil; a, err = token.Next() {
		switch a {
		}
	}

Matching is used to match two strings. It currently supports MQTT rules.

Example:

	pattern := "a/#"
	cmp := "a/a/d"
	m := NewMqttStringMatch(pattern)
	if m.Matches(cmp) {
		// success
	}

Documentation

Index

Constants

View Source
const (
	TreeSeparator = "/"
)

Variables

View Source
var (
	KeepRule    = KeepRuleT{}
	KeepAllRule = KeepAllRuleT{}
	SkipRule    = SkipRuleT{}
)

Functions

func Advance

func Advance(s string, t StringToken) bool

func Advance() advances the token until it finds the string, returning true if it's found. The token will be left on the next string.

func AsArguments

func AsArguments(s string) []string

AsArguments() parses a single string into a slice based on typical command line argument rules (spaces separate tokens, except when quoted, and in that case quotes are removed). Note: seems very likely there's already something to handle this in go, but I'm not seeing it.

func Extract

func Extract(src []string, rules ...interface{}) ([]string, error)

func Extract() takes a list of strings and returns portions of it, according to the rules. Rules can contain either strings or an ExtractRule. Strings:

The node must match the string exactly. It is not included in the response.

ExtractRules:

KeepRule. Node can be anything. It is returned in the response.
SkipRule. Node can be anything. It is not included in the response.

func ExtractSep

func ExtractSep(path, separator string, rules ...interface{}) ([]string, error)

func ExtractSep() is a wrapper on Extract(), first splitting the string based on the separator.

func FindTreeInt

func FindTreeInt(path string, root interface{}) (int, bool)

FindTreeInt() finds the value at the given path and answers it as an int, if it's convertible.

func FindTreeString

func FindTreeString(path string, root interface{}) (string, bool)

FindTreeString() func follows a path down an arbitrary object to answer a string at the root. The path is "/" separated to enter maps. Note: This is identical to TreeString(), but I'm acknowledging, for all the convenience of that API in certain situations, I should have stuck to a more go-way of doing things.

func FindTreeValue

func FindTreeValue(_path string, _tree interface{}) (interface{}, bool)

func Path

func Path(_path string, opts PathOpts, rules ...interface{}) ([]string, error)

func Path() takes a list of strings and returns portions of it, according to the rules. Rules can contain either strings or a path rule. Strings:

The node must match the string exactly. It is not included in the response.

Path rules:

KeepRule. Node can be anything. It is returned in the response.
KeepAllRule. All remaining nodes are added to the response and returned.
ReplaceRuleT. Replace this item with the Replace.With.
SkipRule. Node can be anything. It is not included in the response.

func PathOne

func PathOne(path string, opts PathOpts, rules ...interface{}) string

PathOne() answers a single item from the path input, reconstructed from the results. There's no error reporting, if you need more complete information use a more complex form.

func PathTwo

func PathTwo(path string, opts PathOpts, rules ...interface{}) (string, string)

PathTwo() answers two items from the path input. There's no error reporting, if you need more complete information use a more complex form.

func ReadJsonFile

func ReadJsonFile(filename string) (interface{}, error)

func ReplacePairs

func ReplacePairs(s string, pairs ...interface{}) string

ReplacePairs() takes a string and applies a series of replace pairs. For each pair in the arguments, the first value is found and replaced with the second. The second argument can be anything that can reduce down to a string, including a function with no arguments that returns a string.

func SetTreeInt

func SetTreeInt(path string, value int, tree interface{}) (interface{}, error)

func SetTreeString

func SetTreeString(path, value string, tree interface{}) (interface{}, error)

func SolveInt

func SolveInt(s string) (int, error)

func TreeBool

func TreeBool(path string, root interface{}, defaultValue bool) bool

func TreeInt

func TreeInt(path string, root interface{}, defaultValue int) int

func TreeString

func TreeString(path string, root interface{}, defaultValue string) string

func TreeStringSlice

func TreeStringSlice(path string, root interface{}) []string

func TreeValue

func TreeValue(path string, tree interface{}) (interface{}, error)

Types

type KeepAllRuleT

type KeepAllRuleT struct{}

type KeepRuleT

type KeepRuleT struct{}

type PathOpts

type PathOpts struct {
	Separator string
}

type ReplaceRuleT

type ReplaceRuleT struct {
	With string
}

type SkipRuleT

type SkipRuleT struct{}

type StringMatch

type StringMatch interface {
	Matches(cmp string) bool
}

interface StringMatch describes an object that can match strings.

func NewMqttStringMatch

func NewMqttStringMatch(_pattern string) StringMatch

type StringToken

type StringToken interface {
	Empty() bool
	Next() (string, error)
	// Answer the remaining items. This leaves the token empty.
	Remainder() ([]string, error)
}

interface StringToken allows iterating over a series of strings.

func NewStringToken

func NewStringToken(tokens ...string) StringToken

Answer a new iterating string token from a list of strings.

Jump to

Keyboard shortcuts

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