Documentation
¶
Overview ¶
Package util provides common utility functions used across the Packhorse codebase.
Index ¶
- func DeferClose(logger *slog.Logger, closer io.Closer, msg string) func()
- func RepoFromPath(path string) string
- func WrapHandler(handler http.Handler) http.Handler
- func WrapTransport(transport http.RoundTripper) http.RoundTripper
- type OrderedMap
- func (om *OrderedMap[K, V]) All() iter.Seq2[K, V]
- func (om *OrderedMap[K, V]) Delete(key K)
- func (om *OrderedMap[K, V]) Get(key K) (V, bool)
- func (om *OrderedMap[K, V]) Has(key K) bool
- func (om *OrderedMap[K, V]) Keys() []K
- func (om *OrderedMap[K, V]) Len() int
- func (om *OrderedMap[K, V]) Range(fn func(key K, value V) bool)
- func (om *OrderedMap[K, V]) Set(key K, value V)
- func (om *OrderedMap[K, V]) ToMap() map[K]V
- type Pair
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DeferClose ¶
DeferClose returns a function that closes the given io.Closer and logs any errors. This is useful for defer statements where you want to ensure close errors are logged but don't want to handle them explicitly.
Example usage:
file, err := os.Open("file.txt")
if err != nil {
return err
}
defer DeferClose(logger, file, "failed to close file")()
func RepoFromPath ¶
RepoFromPath extracts a repository name like "gitlab-org/gitlab" from a Git Smart HTTP request path like "/gitlab-org/gitlab.git/git-upload-pack" or "/gitlab-org/gitlab.git/info/refs".
func WrapHandler ¶
WrapHandler wraps an HTTP server handler with debug logging when PACKHORSE_DEBUG_HTTP=1. If debug logging is not enabled, returns the original handler unchanged.
func WrapTransport ¶
func WrapTransport(transport http.RoundTripper) http.RoundTripper
WrapTransport wraps an HTTP client transport with debug logging when PACKHORSE_DEBUG_HTTP=1. If debug logging is not enabled, returns the original transport unchanged.
Types ¶
type OrderedMap ¶
type OrderedMap[K comparable, V any] struct { // contains filtered or unexported fields }
OrderedMap is a map that preserves insertion order. It stores key-value pairs and maintains the order in which keys were first inserted.
func NewOrderedMap ¶
func NewOrderedMap[K comparable, V any]() *OrderedMap[K, V]
NewOrderedMap creates a new OrderedMap.
func NewOrderedMapFromPairs ¶
func NewOrderedMapFromPairs[K comparable, V any](pairs ...Pair[K, V]) *OrderedMap[K, V]
NewOrderedMapFromPairs creates a new OrderedMap with initial key-value pairs. The pairs are inserted in the order provided.
func (*OrderedMap[K, V]) All ¶
func (om *OrderedMap[K, V]) All() iter.Seq2[K, V]
All returns an iterator over key-value pairs in insertion order. This enables using OrderedMap with Go's range over function syntax:
for key, value := range om.All() {
// process key and value
}
func (*OrderedMap[K, V]) Delete ¶
func (om *OrderedMap[K, V]) Delete(key K)
Delete removes a key-value pair from the map.
func (*OrderedMap[K, V]) Get ¶
func (om *OrderedMap[K, V]) Get(key K) (V, bool)
Get retrieves the value for a key and whether it exists.
func (*OrderedMap[K, V]) Has ¶
func (om *OrderedMap[K, V]) Has(key K) bool
Has checks if a key exists in the map.
func (*OrderedMap[K, V]) Keys ¶
func (om *OrderedMap[K, V]) Keys() []K
Keys returns a slice of all keys in insertion order.
func (*OrderedMap[K, V]) Len ¶
func (om *OrderedMap[K, V]) Len() int
Len returns the number of key-value pairs in the map.
func (*OrderedMap[K, V]) Range ¶
func (om *OrderedMap[K, V]) Range(fn func(key K, value V) bool)
Range iterates over the map in insertion order, calling the function for each key-value pair. If the function returns false, iteration stops.
func (*OrderedMap[K, V]) Set ¶
func (om *OrderedMap[K, V]) Set(key K, value V)
Set sets a key-value pair. If the key already exists, the value is updated but the key position in the order is not changed.
func (*OrderedMap[K, V]) ToMap ¶
func (om *OrderedMap[K, V]) ToMap() map[K]V
ToMap returns a regular map with all key-value pairs (order information is lost).
type Pair ¶
type Pair[K comparable, V any] struct { Key K Value V }
Pair represents a key-value pair for OrderedMap construction.
func P ¶
func P[K comparable, V any](key K, value V) Pair[K, V]
P creates a Pair with type inference from the provided key and value. This is a convenience function to avoid explicitly specifying type parameters.