utils

package
v1.7.1 Latest Latest
Warning

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

Go to latest
Published: Sep 17, 2025 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var StringBuilderPool = sync.Pool{
	New: func() interface{} {
		return &strings.Builder{}
	},
}

StringBuilderPool provides a pool of string builders to reduce allocations when building strings, especially for repeated operations like reference resolution.

Functions

func BuildAbsoluteReference added in v1.0.0

func BuildAbsoluteReference(baseRef, jsonPtr string) string

BuildAbsoluteReference efficiently builds an absolute reference string by combining a base reference with a JSON pointer. For this specific 3-string concatenation pattern, Go's optimized string concatenation is faster than string builders.

func BuildString added in v1.0.0

func BuildString(parts ...string) string

BuildString efficiently builds a string from multiple parts using a pooled string builder. This is useful for any string concatenation operations that happen frequently.

func ClearGlobalURLCache added in v1.0.0

func ClearGlobalURLCache()

ClearGlobalURLCache clears the global URL cache

func IsFilePath added in v1.0.0

func IsFilePath(ref string) bool

IsFilePath returns true if the reference string represents a file path

func IsFragment added in v1.0.0

func IsFragment(ref string) bool

IsFragment returns true if the reference string represents a JSON Pointer fragment

func IsURL added in v1.0.0

func IsURL(ref string) bool

IsURL returns true if the reference string represents a URL

func JoinReference added in v1.0.0

func JoinReference(base, relative string) (string, error)

JoinReference is a convenience function that classifies the base reference and joins it with a relative reference. For better performance when you already have a classification, use ReferenceClassification.JoinWith() instead.

func JoinWithSeparator added in v1.0.0

func JoinWithSeparator(separator string, parts ...string) string

JoinWithSeparator efficiently joins strings with a separator using a pooled string builder. This is more efficient than strings.Join for frequently called operations.

func MapSlice added in v0.2.2

func MapSlice[T any, U any](slice []T, fn func(T) U) []U

func ParseURLCached added in v1.0.0

func ParseURLCached(rawURL string) (*url.URL, error)

ParseURLCached parses a URL string using a cache to avoid repeated parsing of the same URLs. This is particularly beneficial when the same base URLs are parsed thousands of times.

func ParseVersion

func ParseVersion(version string) (int, int, int, error)

Types

type ReferenceClassification added in v1.0.0

type ReferenceClassification struct {
	Type       ReferenceType
	IsURL      bool
	IsFile     bool
	IsFragment bool
	Original   string
	ParsedURL  *url.URL // Cached parsed URL to avoid re-parsing
}

ReferenceClassification holds the result of classifying a reference string

func ClassifyReference added in v1.0.0

func ClassifyReference(ref string) (*ReferenceClassification, error)

ClassifyReference determines if a string represents a URL, file path, or JSON Pointer fragment. It returns detailed classification information and any parsing errors.

func (*ReferenceClassification) JoinWith added in v1.0.0

func (rc *ReferenceClassification) JoinWith(relative string) (string, error)

JoinWith joins this classified reference with a relative reference. It uses the cached classification and parsed URL (if available) to avoid re-parsing. For URLs, it uses the cached ParsedURL and ResolveReference. For file paths, it uses filepath.Join. Fragments are handled specially and can be combined with both URLs and file paths.

type ReferenceType added in v1.0.0

type ReferenceType int

ReferenceType represents the type of reference string

const (
	ReferenceTypeUnknown ReferenceType = iota
	ReferenceTypeURL
	ReferenceTypeFilePath
	ReferenceTypeFragment
)

type URLCache added in v1.0.0

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

URLCache provides a thread-safe cache for parsed URLs to avoid repeated parsing

func (*URLCache) Clear added in v1.0.0

func (c *URLCache) Clear()

Clear clears all cached URLs. Useful for testing or memory management.

func (*URLCache) Parse added in v1.0.0

func (c *URLCache) Parse(rawURL string) (*url.URL, error)

Parse parses a URL string using the cache. If the URL has been parsed before, it returns a copy of the cached result. Otherwise, it parses the URL, caches it, and returns the result.

type URLCacheStats added in v1.0.0

type URLCacheStats struct {
	Size int64
}

Stats returns basic statistics about the cache

func GetURLCacheStats added in v1.0.0

func GetURLCacheStats() URLCacheStats

GetStats returns statistics about the global URL cache

Jump to

Keyboard shortcuts

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