wrpparser

package
v0.2.3 Latest Latest
Warning

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

Go to latest
Published: Aug 31, 2021 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Classifier

type Classifier interface {
	Label(*wrp.Message) (string, bool)
}

Classifier is an object that Labels a wrp message. The Classifier provides the label associated with the wrp upon labeling it, as well as a boolean that describes whether or not the Classifier could label the message. The message is not modified in any way.

type ConstClassifier

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

ConstClassifier returns the same label and ok value for every message the struct is asked to label.

func NewConstClassifier

func NewConstClassifier(label string, ok bool) *ConstClassifier

NewConstClassifier creates the classifier that consistently labels every wrp message the same way.

func (ConstClassifier) Label

func (c ConstClassifier) Label(_ *wrp.Message) (string, bool)

Label returns the saved label and boolean value without any checks to the message it receives. Even if the message is nil, the same result is provided.

type DeviceFinder

type DeviceFinder interface {
	FindDeviceID(msg *wrp.Message) (string, error)
}

DeviceFinder extracts a device id from a wrp message.

type Field

type Field int

Field is an enum that describes a specific field in a wrp message. Further docs on wrps can be found here: https://pkg.go.dev/github.com/xmidt-org/wrp-go@v1.3.4/wrp?tab=doc#Message

const (
	// Source is a wrp message's Source field.
	Source Field = iota
	// Destination is a wrp message's Destination field.
	Destination
)

func GetField

func GetField(f string) Field

GetField translates a string into a Field enum. The default is Source.

func (Field) String

func (f Field) String() string

String translates the Field enum into a string.

type FieldFinder

type FieldFinder struct {
	Field Field
}

FieldFinder returns the full value of the given field in a wrp message.

func (FieldFinder) FindDeviceID

func (f FieldFinder) FindDeviceID(msg *wrp.Message) (string, error)

FindDeviceID expects to find the device ID as the only value in a specific field in the wrp message. It returns a lowercase transform of this.

type MultClassifier

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

MultClassifier runs multiple classifiers on each message it receives, returning the first label that is successfully applied to the message.

func NewMultClassifier

func NewMultClassifier(classifiers ...Classifier) (*MultClassifier, error)

NewMultClassifier builds the MultClassifier struct. It requires at least one valid Classifier.

func (*MultClassifier) Label

func (m *MultClassifier) Label(msg *wrp.Message) (string, bool)

Label attempts to label the wrp message with each Classifier in the MultClassifier's list, returning the first successful label. If no Classifier labels the message, an empty string and false boolean is returned.

type ParserOption

type ParserOption func(*StrParser)

ParserOption is a function used to configure the StrParser.

func WithDeviceFinder

func WithDeviceFinder(label string, finder DeviceFinder) ParserOption

WithDeviceFinder adds a DeviceFinder that the StrParser will use to find the device id of a wrp message. Each DeviceFinder is associated with a label. If the label already has a DeviceFinder associated with it, it will be replaced by the new one (as long as the DeviceFinder is not nil).

type RegexpClassifier

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

RegexpLabel labels wrp messages if a regular expression matches a specified field.

func NewRegexpClassifier

func NewRegexpClassifier(label string, regex *regexp.Regexp, field Field) (*RegexpClassifier, error)

NewRegexpClassifier creates a new RegexpClassifier struct as long as the regular expression provided is valid.

func NewRegexpClassifierFromStr

func NewRegexpClassifierFromStr(label, regexStr string, field Field) (*RegexpClassifier, error)

NewRegexpClassifierFromStr takes a string representation of a regular expression and compiles it, then creates a RegexpClassifier struct.

func (*RegexpClassifier) Label

func (r *RegexpClassifier) Label(msg *wrp.Message) (string, bool)

Label checks a field in the message given and if the regular expression matches against it, returns the stored label. Otherwise, it returns an empty string. If a label is returned, the boolean is true. Otherwise, false.

type RegexpFinder

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

RegexpFinder uses a regular expression to find the device id within a field of a wrp message.

func NewRegexpFinder

func NewRegexpFinder(field Field, regex *regexp.Regexp, deviceLabel string) (*RegexpFinder, error)

NewRegexpFinder returns a new RegexpFinder that checks the field given using the regular expression provided. It will extract the device id from the regular expression result at the label given.

func NewRegexpFinderFromStr

func NewRegexpFinderFromStr(field Field, regexStr string, deviceLabel string) (*RegexpFinder, error)

NewRegexpFinderFromStr compiles a string representation of a regular expression and returns a new RegexpFinder that checks the field given using that regular expression. It will extract the device id from the regular expression result at the label given.

func (*RegexpFinder) FindDeviceID

func (r *RegexpFinder) FindDeviceID(msg *wrp.Message) (string, error)

FindDeviceID applies a regular expression to a specific field in the wrp message. If that is successful, it extracts the device id from the expected place in the regular expression submatch.

type StrParser

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

StrParser finds the label for a wrp message and then uses the device finder associated with that label to get the device ID associated with the message.

func NewStrParser

func NewStrParser(classifier Classifier, defaultFinder DeviceFinder, options ...ParserOption) (*StrParser, error)

NewStrParser sets up the StrParser with a valid classifier and defaultFinder. Options need to be provided in order to add more DeviceFinders for different labels.

func (*StrParser) Parse

func (p *StrParser) Parse(msg *wrp.Message) (string, error)

Parse takes a message and parses the device ID from it. It uses the Classifier to determine the label associated with the wrp message. The device ID is found using the device finder associated with the message's label. If there is a problem, the default DeviceFinder is used.

Jump to

Keyboard shortcuts

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