storagepath

package
v0.12.1 Latest Latest
Warning

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

Go to latest
Published: May 11, 2020 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package storagepath provides functions similar to filepath.

Note that storage depends on storagepath, not the other way around.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Base

func Base(path string) string

Base is equivalent to filepath.Base.

Normalizes before returning.

func ByDir

func ByDir(paths ...string) map[string][]string

ByDir maps the paths into a map from directory via Dir to the original paths.

The paths for each value slice will be sorted.

The path is expected to be normalized.

func Components

func Components(path string) []string

Components splits the path into it's components.

This calls filepath.Split repeatedly.

The path is expected to be normalized.

func Dir

func Dir(path string) string

Dir is equivalent to filepath.Dir.

Normalizes before returning.

func ErrorEquals added in v0.4.0

func ErrorEquals(err error, target error) bool

ErrorEquals returns true if err is an Error and err.Error == target.

func Ext

func Ext(path string) string

Ext is equivalent to filepath.Ext.

Can return empty string.

func IsMatch added in v0.12.1

func IsMatch(value string, path string) bool

IsMatch returns true if the value is equal to or contains the path.

The path and value are expected to be normalized and validated.

For a given value:

  • If value == ".", the value contains the path.
  • If value == path, the value is equal to the path.
  • If value is a directory that contains path, this returns true.

The path is normalized within this call.

func Join

func Join(paths ...string) string

Join is equivalent to filepath.Join.

Empty strings are ignored, Can return empty string.

Normalizes before returning otherwise.

func MapContainsMatch

func MapContainsMatch(m map[string]struct{}, path string) bool

MapContainsMatch returns true if the path matches any file or directory in the map.

The path and all keys in m are expected to be normalized and validated.

For a given key x:

  • If x == ".", the path always matches.
  • If x == path, the path matches.
  • If x is a directory that contains path, the path matches.

If the map is empty, returns false.

All files and directories in the map are expected to be normalized. The path is normalized within this call.

func MapMatches

func MapMatches(m map[string]struct{}, path string) map[string]struct{}

MapMatches returns the matching paths in the map.

The path and all keys in m are expected to be normalized and validated.

For a given key x:

  • If x == ".", the path always matches.
  • If x == path, the path matches.
  • If x is a directory that contains path, the path matches.

If the map is empty, returns empty map.

All files and directories in the map are expected to be normalized. The path is normalized within this call.

func Normalize

func Normalize(path string) string

Normalize normalizes the given path.

This calls filepath.Clean and filepath.ToSlash on the path. If the path is "" or ".", this returns ".".

func NormalizeAndValidate

func NormalizeAndValidate(path string) (string, error)

NormalizeAndValidate normalizes and validates the given path.

This calls Normalize on the path. Returns Error if the path is not relative or jumps context. This can be used to validate that paths are valid to use with Buckets. The error message is safe to pass to users.

func Rel

func Rel(basepath string, targpath string) (string, error)

Rel is equivalent to filepath.Rel.

Can return empty string, especially on error.

Normalizes before returning otherwise.

func Unnormalize

func Unnormalize(path string) string

Unnormalize unnormalizes the given path.

This calls filepath.FromSlash on the path. If the path is "", this returns "".

Types

type Error added in v0.4.0

type Error struct {
	Path string
	Err  error
}

Error is a path error.

func NewError added in v0.4.0

func NewError(path string, err error) *Error

NewError returns a new Error.

func (*Error) Error added in v0.4.0

func (e *Error) Error() string

Error implements error.

type Transformer

type Transformer interface {
	// Transform transforms and filters the path.
	//
	// If the path is filtered, this will return empty and false.
	// Returns the potentially transformed path.
	//
	// The path is expected to be normalized.
	Transform(path string) (string, bool)
}

Transformer transforms and filters paths.

func NewTransformer

func NewTransformer(options ...TransformerOption) Transformer

NewTransformer returns a new Transformer.

type TransformerOption

type TransformerOption func(*transformer)

TransformerOption is an option for a new Transformer.

func WithExactPath

func WithExactPath(exactPath string) TransformerOption

WithExactPath returns WithMatcher() for path == exactPath.

exactPath is normalized within this call.

func WithExt

func WithExt(ext string) TransformerOption

WithExt returns WithMatcher() for Ext(path) == ext.

func WithMatcher

func WithMatcher(matcher func(name string) bool) TransformerOption

WithMatcher returns a TransformerOption with the given matcher.

Transform will return false and no path if no matcher returns true. If there are no matchers, there is no processing of match rules. Multiple matchers are or'ed. Applied after strip components. Paths are normalized before passing to a matcher.

The default is no matchers.

func WithStripComponents

func WithStripComponents(stripComponentCount uint32) TransformerOption

WithStripComponents strips the given number of components from a path.

ApplyOptions will return false and no path if a file does not have this many components.

The default is 0.

Jump to

Keyboard shortcuts

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