Documentation ¶
Overview ¶
Package normalpath provides functions similar to filepath.
A normalized path is a cleaned and to-slash'ed path. A validated path validates that a path is relative and does not jump context.
Index ¶
- func Base(path string) string
- func ByDir(paths ...string) map[string][]string
- func Components(path string) []string
- func ContainsPath(dirPath string, path string, pathType PathType) bool
- func Dir(path string) string
- func EqualsOrContainsPath(value string, path string, pathType PathType) bool
- func Ext(path string) string
- func Join(paths ...string) string
- func MapAllEqualOrContainingPathMap(m map[string]struct{}, path string, pathType PathType) map[string]struct{}
- func MapAllEqualOrContainingPaths(m map[string]struct{}, path string, pathType PathType) []string
- func MapHasEqualOrContainingPath(m map[string]struct{}, path string, pathType PathType) bool
- func Normalize(path string) string
- func NormalizeAndAbsolute(path string) (string, error)
- func NormalizeAndTransformForPathType(path string, pathType PathType) (string, error)
- func NormalizeAndValidate(path string) (string, error)
- func Rel(basepath string, targpath string) (string, error)
- func StripComponents(path string, countUint32 uint32) (string, bool)
- func Unnormalize(path string) string
- func ValidatePathComponent(component string) error
- func ValidatePathComponents(components ...string) error
- func ValidatePathsNormalizedValidatedUnique(paths []string) error
- type Error
- type PathType
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ByDir ¶
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 ¶
Components splits the path into it's components.
This calls filepath.Split repeatedly.
The path is expected to be normalized.
func ContainsPath ¶
ContainsPath returns true if the dirPath contains the path.
The path and value are expected to be normalized and validated if Relative is used. The path and value are expected to be normalized and absolute if Absolute is used.
For a given dirPath:
- If path == PathType, dirPath does not contain the path.
- If dirPath == PathType, the dirPath contains the path.
- If dirPath is a directory that contains path, this returns true.
func EqualsOrContainsPath ¶
EqualsOrContainsPath returns true if the value is equal to or contains the path.
The path and value are expected to be normalized and validated if Relative is used. The path and value are expected to be normalized and absolute if Absolute is used.
func Join ¶
Join is equivalent to filepath.Join.
Empty strings are ignored, Can return empty string.
Normalizes before returning otherwise.
func MapAllEqualOrContainingPathMap ¶
func MapAllEqualOrContainingPathMap(m map[string]struct{}, path string, pathType PathType) map[string]struct{}
MapAllEqualOrContainingPathMap returns the paths in m that are equal to, or contain path, in a new map.
The path and keys in m are expected to be normalized and validated if Relative is used. The path and keys in m are expected to be normalized and absolute if Absolute is used.
If the map is empty, returns nil.
func MapAllEqualOrContainingPaths ¶
MapAllEqualOrContainingPaths returns the matching paths in the map in a sorted slice.
The path and keys in m are expected to be normalized and validated if Relative is used. The path and keys in m are expected to be normalized and absolute if Absolute is used.
If the map is empty, returns nil.
func MapHasEqualOrContainingPath ¶
MapHasEqualOrContainingPath returns true if the path matches any file or directory in the map.
The path and keys in m are expected to be normalized and validated if Relative is used. The path and keys in m are expected to be normalized and absolute if Absolute is used.
If the map is empty, returns false.
func Normalize ¶
Normalize normalizes the given path.
This calls filepath.Clean and filepath.ToSlash on the path. If the path is "" or ".", this returns ".".
func NormalizeAndAbsolute ¶
NormalizeAndAbsolute normalizes the path and makes it absolute.
func NormalizeAndTransformForPathType ¶
NormalizeAndTransformForPathType calls NormalizeAndValidate for relative paths, and NormalizeAndAbsolute for absolute paths.
func NormalizeAndValidate ¶
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 ¶
Rel is equivalent to filepath.Rel.
Can return empty string, especially on error.
Normalizes before returning otherwise.
func StripComponents ¶
StripComponents strips the specified number of components.
Path expected to be normalized. Returns false if the path does not have more than the specified number of components.
func Unnormalize ¶
Unnormalize unnormalizes the given path.
This calls filepath.FromSlash on the path. If the path is "", this returns "".
func ValidatePathComponent ¶
ValidatePathComponent validates that the string is a valid component of a path, e.g. it can be Joined and form a valid path.
func ValidatePathComponents ¶
ValidatePathComponents validates that all the strings are valid components of a path, e.g. they can be Joined and form a valid path.
func ValidatePathsNormalizedValidatedUnique ¶
ValidatePathsNormalizedValidatedUnique validates the file or diretory paths are normalized and validated, and not duplicated.
Types ¶
type PathType ¶
type PathType int
PathType is a terminate type for path comparisons.