vnameutil

package
v0.0.67 Latest Latest
Warning

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

Go to latest
Published: Mar 14, 2024 License: Apache-2.0, NCSA Imports: 10 Imported by: 10

Documentation

Overview

Package vnameutil provides utilities for generating consistent VNames from common path-like values (e.g., filenames, import paths).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Compare added in v0.0.27

func Compare(a, b *spb.VName) int

Compare reports the relative order of two vnames, returning -1 if a < b, 0 if a == b, and 1 if a > b.

Types

type Rule

type Rule struct {
	*regexp.Regexp // A pattern to match against an input string
	*spb.VName     // A template to populate with matches from the input
}

A Rule associates a regular expression pattern with a VName template. A Rule can be applied to a string to produce a VName.

func ConvertRule added in v0.0.31

func ConvertRule(r *spb.VNameRewriteRule) (Rule, error)

ConvertRule compiles a VNameRewriteRule proto into a Rule that can be applied to strings.

func (Rule) Apply

func (r Rule) Apply(input string) (*spb.VName, bool)

Apply reports whether input matches the regexp associated with r. If so, it returns a VName whose fields have values taken from r.VName, with submatches populated from the input string.

Submatch replacement is done using regexp.ExpandString, so the same syntax is supported for specifying replacements.

func (Rule) MarshalJSON added in v0.0.49

func (r Rule) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface.

func (Rule) String added in v0.0.49

func (r Rule) String() string

String returns a debug string of r.

func (Rule) ToProto added in v0.0.49

func (r Rule) ToProto() *spb.VNameRewriteRule

ToProto returns an equivalent VNameRewriteRule proto.

func (*Rule) UnmarshalJSON added in v0.0.49

func (r *Rule) UnmarshalJSON(rec []byte) error

UnmarshalJSON implements the json.Unmarshaler interface.

type Rules

type Rules []Rule

Rules are an ordered set of rewriting rules. Applying a group of rules tries each rule in sequence, and returns the result of the first one that matches.

func LoadRules added in v0.0.31

func LoadRules(path string) (Rules, error)

LoadRules loads and parses the vname mapping rules in path. If path == "", this returns nil without error (no rules).

func ParseProtoRules added in v0.0.49

func ParseProtoRules(data []byte) (Rules, error)

ParseProtoRules reads a wire-encoded *spb.VNameRewriteRules.

func ParseRules

func ParseRules(data []byte) (Rules, error)

ParseRules reads Rules from JSON-encoded data in a byte array.

func ReadRules added in v0.0.31

func ReadRules(r io.Reader) (Rules, error)

ReadRules parses Rules from JSON-encoded data in the following format:

[
  {
    "pattern": "re2_regex_pattern",
    "vname": {
      "corpus": "corpus_template",
      "root": "root_template",
      "path": "path_template"
    }
  }, ...
]

Each pattern is an RE2 regexp pattern. Patterns are implicitly anchored at both ends. The template strings may contain markers of the form @n@, that will be replaced by the n'th regexp group on a successful input match.

func (Rules) Apply

func (r Rules) Apply(input string) (*spb.VName, bool)

Apply applies each rule in to the input in sequence, returning the first successful match. If no rules apply, returns (nil, false).

func (Rules) ApplyDefault

func (r Rules) ApplyDefault(input string, v *spb.VName) *spb.VName

ApplyDefault acts as r.Apply, but returns v there is no matching rule.

func (Rules) Marshal added in v0.0.49

func (r Rules) Marshal() ([]byte, error)

Marshal implements the proto.Marshaler interface.

func (Rules) ToProto added in v0.0.49

func (r Rules) ToProto() *spb.VNameRewriteRules

ToProto returns an equivalent VNameRewriteRules proto.

Directories

Path Synopsis
Program test_vname_rules verifies that a set of vname mapping rules behave as expected.
Program test_vname_rules verifies that a set of vname mapping rules behave as expected.

Jump to

Keyboard shortcuts

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