match

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Mar 12, 2025 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package match provides functionality to evaluate and match assets against specific platform requirements and name-based hints. It enables scoring assets based on their compatibility with target platforms (e.g., OS, architecture) and their matching patterns, which can include regex or string comparisons.

The core types in this package include Asset, which represents a downloadable file or package, and Requirements, which defines the criteria for matching assets. These criteria can include platform specifications and pattern hints. The Results type facilitates sorting and evaluating the best-matching assets.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNoMatch is returned when no matches are found.
	ErrNoMatch = errors.New("no matches found")
	// ErrAmbiguous is returned when multiple equally good matches are found.
	ErrAmbiguous = errors.New("ambiguous matches found")
	// ErrNoQualified is returned when no qualified matches are found.
	ErrNoQualified = errors.New("no qualified matches found")
)

Functions

This section is empty.

Types

type Asset

type Asset struct {
	Name     string          // Name is the file name or identifier of the asset.
	Platform detect.Platform // Platform describes the OS, architecture, and other relevant details for compatibility.
}

Asset represents a downloadable file, typically for a specific platform. It includes metadata like the asset name and the platform it targets.

func (*Asset) Extension

func (a *Asset) Extension() Extension

Extension returns the file extension of the asset based on its name. It maps common file extensions to predefined constants.

func (Asset) Match

func (a Asset) Match(req Requirements) (int, bool)

Match evaluates if the asset satisfies the given requirements. It aggregates scores from both platform compatibility and matching hints.

func (Asset) MatchHint

func (a Asset) MatchHint(hint Hint) bool

MatchHint checks if the asset's name matches the provided hint. The hint can be a regular expression or a simple substring match.

func (Asset) NameLower

func (a Asset) NameLower() string

NameLower returns the asset's name in lowercase. This is useful for case-insensitive matching operations.

func (*Asset) Parse

func (a *Asset) Parse()

Parse invokes the platform's parsing logic on the asset's name. It populates or derives the platform information from the asset's name.

func (Asset) PlatformMatch

func (a Asset) PlatformMatch(req Requirements) (int, bool)

PlatformMatch evaluates whether the asset's platform matches the required platform. It calculates a score based on the degree of compatibility and returns whether the asset is qualified.

type Assets

type Assets []Asset

Assets is a slice of Asset, representing a collection of downloadable files.

func (Assets) FromNames

func (as Assets) FromNames(names ...string) Assets

FromNames creates a collection of assets from the provided names. It initializes each asset with the given name.

func (Assets) Match

func (as Assets) Match(req Requirements) Results

Match evaluates all assets against the provided requirements and returns a list of results. Each result contains the asset's name, its matching score, and whether it qualifies.

func (Assets) Select

func (as Assets) Select(req Requirements) Results

Select filters and sorts the assets based on the provided requirements. It returns the best matching assets, sorted by score, if any are qualified.

type Extension

type Extension int

Extension represents a file extension.

const (
	// None represents no file extension.
	None Extension = iota
	// EXE represents the ".exe" file extension.
	EXE
	// GZ represents the ".gz" file extension.
	GZ
	// ZIP represents the ".zip" file extension.
	ZIP
	// Other represents any other file extension.
	Other
)

type Hint

type Hint struct {
	Pattern string // Pattern to match against the asset's name.
	Weight  string // Weight used to adjust the score for non-mandatory hints.
	Must    bool   // Indicates if the hint is mandatory for a match.
	// contains filtered or unexported fields
}

Hint represents a pattern used to match asset names. It can be a regular expression or a simple string pattern.

func (Hint) GetWeight

func (h Hint) GetWeight() int

GetWeight returns the weight of the hint.

func (*Hint) SetWeight

func (h *Hint) SetWeight() error

SetWeight converts the weight string to an integer.

type Hints

type Hints []Hint

Hints represents a collection of Hint objects used to evaluate asset matches.

func (*Hints) Add

func (h *Hints) Add(hints ...Hint)

Add appends a set of hints to the current collection.

func (*Hints) Append

func (h *Hints) Append(hints Hints)

Append appends a set of hints to the current collection.

type Requirements

type Requirements struct {
	Platform detect.Platform // Platform specifies the required OS, architecture, and library compatibility.
	Hints    []Hint          // Hints contains patterns used to match the asset's name.
}

Requirements represents the criteria an asset must meet. It includes platform compatibility and a list of hints for name matching.

type Result

type Result struct {
	Asset     Asset // Asset being evaluated.
	Score     int   // Score representing how well the asset matches the requirements.
	Qualified bool  // Qualified indicates whether the asset meets the necessary criteria.
}

Result represents the outcome of matching an asset. It contains the asset, its score, and whether it is qualified.

type Results

type Results []Result

Results is a collection of Result objects.

func (Results) Best

func (m Results) Best() Results

Best returns the best qualified results based on the highest score. If multiple results have the same best score, they are all returned.

func (Results) HasQualified

func (m Results) HasQualified() bool

HasQualified returns true if there's at least one qualified result in the set.

func (Results) IsAmbigious

func (m Results) IsAmbigious() bool

IsAmbigious returns true if there are multiple qualified results.

func (Results) Sorted

func (m Results) Sorted() Results

Sorted returns a new sorted instance of Results. It sorts first by qualification status and then by score in descending order.

func (Results) Status

func (m Results) Status() (err error)

Status evaluates the state of the results and returns an appropriate error if needed.

func (Results) Success

func (m Results) Success() bool

Success returns true if there is exactly one result.

func (Results) ToString

func (m Results) ToString() string

ToString converts the results into a formatted string for output.

func (Results) WithoutZero

func (m Results) WithoutZero() Results

WithoutZero returns a new instance of Results without zero scores.

Jump to

Keyboard shortcuts

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