* chore(deps): consolidate dependency and security bumps Dependabot PRs applied: - #12878 qs 6.15.2 -> 6.16.0 (web/web-runtime) - #13013 alpine 3.24.1 -> 3.24.2 - #13061 actions/checkout -> v7.0.1 (missed instance in k8s.yml) - #13062 actions/setup-node -> v7.0.0 (missed instance in k8s.yml) - #13075 simple-git 3.36.0 -> 4.0.1 (security) - #13076 katex 0.16.45 -> 0.18.2+ (security) - #13077 dompurify 3.4.13 -> 3.4.16 (security) - #13079 go deps minor-and-patch group, 18 updates (opa 1.19.0 -> 1.21.1, otel 1.46 -> 1.47, bleve, govips, grpc-gateway, lico, tablewriter, logrus, gjson, x/exp, x/image, genproto, etc.) Skipped (already satisfied on master): #12513, #12624, #12625, #12633 Manual security bumps via pnpm.overrides (no dependabot PR existed): - axios -> 1.20.0 - smol-toml -> 1.9.0 - postcss -> 8.5.23 - postcss-selector-parser -> 7.1.6 - colord -> 2.9.4 - unhead -> 2.1.13 - markdown-it -> 14.3.1 - esbuild -> 0.28.1 - brace-expansion 5.0.11 -> 5.0.12 Not fixable: sprintf-js and elliptic have no patched version released upstream yet; left as-is. go build and pnpm install verified clean. * chore(deps): fix residual transitive vulns found by trivy trivy flagged duplicate/stale resolutions that survived the earlier override pass: dompurify (3.4.12 -> 3.4.16), qs (6.14.2 -> 6.16.0), and fast-uri (3.1.7 -> 3.1.8, CVE-2026-86472). Added/bumped pnpm.overrides accordingly. trivy fs scan of web/pnpm-lock.yaml now clean. * chore(deps): bump reva to bac0e1ee144ab853a81c6b02142032f102dd245a Pins github.com/owncloud/reva/v2 to bac0e1ee144ab853a81c6b02142032f102dd245a, pulling in its go-playground/locales and huandu/xstrings version bumps. Appends this PR to changelog/unreleased/bump-reva.md. go build ./... verified clean after go mod vendor resync. * fix(ci): revert unhead override, breaks web build * revert: keep k8s.yml actions/checkout and setup-node pinned
275 lines
5.6 KiB
Go
275 lines
5.6 KiB
Go
package tw
|
|
|
|
import (
|
|
"fmt"
|
|
"sort"
|
|
)
|
|
|
|
// KeyValuePair represents a single key-value pair from a Mapper.
|
|
type KeyValuePair[K comparable, V any] struct {
|
|
Key K
|
|
Value V
|
|
}
|
|
|
|
// Mapper is a generic map type with comparable keys and any value type.
|
|
// It provides type-safe operations on maps with additional convenience methods.
|
|
type Mapper[K comparable, V any] map[K]V
|
|
|
|
// NewMapper creates and returns a new initialized Mapper.
|
|
func NewMapper[K comparable, V any]() Mapper[K, V] {
|
|
return make(Mapper[K, V])
|
|
}
|
|
|
|
// NewMapperWithKeys creates a Mapper with the given keys and zero values
|
|
func NewMapperWithKeys[K comparable, V any](keys ...K) Mapper[K, V] {
|
|
m := make(Mapper[K, V], len(keys))
|
|
for _, k := range keys {
|
|
m[k] = *new(V)
|
|
}
|
|
return m
|
|
}
|
|
|
|
// NewMapperFromPairs creates a Mapper from alternating key-value pairs
|
|
func NewMapperFromPairs[K comparable, V any](pairs ...any) Mapper[K, V] {
|
|
m := make(Mapper[K, V])
|
|
for i := 0; i < len(pairs); i += 2 {
|
|
if i+1 >= len(pairs) {
|
|
break
|
|
}
|
|
k, ok := pairs[i].(K)
|
|
if !ok {
|
|
continue
|
|
}
|
|
v, ok := pairs[i+1].(V)
|
|
if !ok {
|
|
continue
|
|
}
|
|
m[k] = v
|
|
}
|
|
return m
|
|
}
|
|
|
|
// Get returns the value associated with the key.
|
|
// If the key doesn't exist or the map is nil, it returns the zero value for the value type.
|
|
func (m Mapper[K, V]) Get(key K) V {
|
|
if m == nil {
|
|
var zero V
|
|
return zero
|
|
}
|
|
return m[key]
|
|
}
|
|
|
|
// OK returns the value associated with the key and a boolean indicating whether the key exists.
|
|
func (m Mapper[K, V]) OK(key K) (V, bool) {
|
|
if m == nil {
|
|
var zero V
|
|
return zero, false
|
|
}
|
|
val, ok := m[key]
|
|
return val, ok
|
|
}
|
|
|
|
// Set sets the value for the specified key.
|
|
// Does nothing if the map is nil.
|
|
func (m Mapper[K, V]) Set(key K, value V) Mapper[K, V] {
|
|
if m != nil {
|
|
m[key] = value
|
|
}
|
|
return m
|
|
}
|
|
|
|
// Delete removes the specified key from the map.
|
|
// Does nothing if the key doesn't exist or the map is nil.
|
|
func (m Mapper[K, V]) Delete(key K) Mapper[K, V] {
|
|
if m != nil {
|
|
delete(m, key)
|
|
}
|
|
return m
|
|
}
|
|
|
|
// Has returns true if the key exists in the map, false otherwise.
|
|
func (m Mapper[K, V]) Has(key K) bool {
|
|
if m == nil {
|
|
return false
|
|
}
|
|
_, exists := m[key]
|
|
return exists
|
|
}
|
|
|
|
// Len returns the number of elements in the map.
|
|
// Returns 0 if the map is nil.
|
|
func (m Mapper[K, V]) Len() int {
|
|
if m == nil {
|
|
return 0
|
|
}
|
|
return len(m)
|
|
}
|
|
|
|
// Keys returns a slice containing all keys in the map.
|
|
// Returns nil if the map is nil or empty.
|
|
func (m Mapper[K, V]) Keys() []K {
|
|
if m == nil {
|
|
return nil
|
|
}
|
|
keys := make([]K, 0, len(m))
|
|
for k := range m {
|
|
keys = append(keys, k)
|
|
}
|
|
return keys
|
|
}
|
|
|
|
func (m Mapper[K, V]) Clear() {
|
|
if m == nil {
|
|
return
|
|
}
|
|
for k := range m {
|
|
delete(m, k)
|
|
}
|
|
}
|
|
|
|
// Values returns a slice containing all values in the map.
|
|
// Returns nil if the map is nil or empty.
|
|
func (m Mapper[K, V]) Values() []V {
|
|
if m == nil {
|
|
return nil
|
|
}
|
|
values := make([]V, 0, len(m))
|
|
for _, v := range m {
|
|
values = append(values, v)
|
|
}
|
|
return values
|
|
}
|
|
|
|
// Each iterates over each key-value pair in the map and calls the provided function.
|
|
// Does nothing if the map is nil.
|
|
func (m Mapper[K, V]) Each(fn func(K, V)) {
|
|
for k, v := range m {
|
|
fn(k, v)
|
|
}
|
|
}
|
|
|
|
// Filter returns a new Mapper containing only the key-value pairs that satisfy the predicate.
|
|
func (m Mapper[K, V]) Filter(fn func(K, V) bool) Mapper[K, V] {
|
|
result := NewMapper[K, V]()
|
|
for k, v := range m {
|
|
if fn(k, v) {
|
|
result[k] = v
|
|
}
|
|
}
|
|
return result
|
|
}
|
|
|
|
// MapValues returns a new Mapper with the same keys but values transformed by the provided function.
|
|
func (m Mapper[K, V]) MapValues(fn func(V) V) Mapper[K, V] {
|
|
result := NewMapper[K, V]()
|
|
for k, v := range m {
|
|
result[k] = fn(v)
|
|
}
|
|
return result
|
|
}
|
|
|
|
// Clone returns a shallow copy of the Mapper.
|
|
func (m Mapper[K, V]) Clone() Mapper[K, V] {
|
|
result := NewMapper[K, V]()
|
|
for k, v := range m {
|
|
result[k] = v
|
|
}
|
|
return result
|
|
}
|
|
|
|
// Slicer converts the Mapper to a Slicer of key-value pairs.
|
|
func (m Mapper[K, V]) Slicer() Slicer[KeyValuePair[K, V]] {
|
|
if m == nil {
|
|
return nil
|
|
}
|
|
result := make(Slicer[KeyValuePair[K, V]], 0, len(m))
|
|
for k, v := range m {
|
|
result = append(result, KeyValuePair[K, V]{Key: k, Value: v})
|
|
}
|
|
return result
|
|
}
|
|
|
|
func (m Mapper[K, V]) SortedKeys() []K {
|
|
keys := make([]K, 0, len(m))
|
|
for k := range m {
|
|
keys = append(keys, k)
|
|
}
|
|
|
|
sort.Slice(keys, func(i, j int) bool {
|
|
a, b := any(keys[i]), any(keys[j])
|
|
|
|
switch va := a.(type) {
|
|
case int:
|
|
if vb, ok := b.(int); ok {
|
|
return va < vb
|
|
}
|
|
case int32:
|
|
if vb, ok := b.(int32); ok {
|
|
return va < vb
|
|
}
|
|
case int64:
|
|
if vb, ok := b.(int64); ok {
|
|
return va < vb
|
|
}
|
|
case uint:
|
|
if vb, ok := b.(uint); ok {
|
|
return va < vb
|
|
}
|
|
case uint64:
|
|
if vb, ok := b.(uint64); ok {
|
|
return va < vb
|
|
}
|
|
case float32:
|
|
if vb, ok := b.(float32); ok {
|
|
return va < vb
|
|
}
|
|
case float64:
|
|
if vb, ok := b.(float64); ok {
|
|
return va < vb
|
|
}
|
|
case string:
|
|
if vb, ok := b.(string); ok {
|
|
return va < vb
|
|
}
|
|
}
|
|
|
|
// fallback to string comparison
|
|
return fmt.Sprintf("%v", a) < fmt.Sprintf("%v", b)
|
|
})
|
|
|
|
return keys
|
|
}
|
|
|
|
func NewBoolMapper[K comparable](keys ...K) Mapper[K, bool] {
|
|
if len(keys) == 0 {
|
|
return nil
|
|
}
|
|
mapper := NewMapper[K, bool]()
|
|
for _, key := range keys {
|
|
mapper.Set(key, true)
|
|
}
|
|
return mapper
|
|
}
|
|
|
|
func NewIntMapper[K comparable](keys ...K) Mapper[K, int] {
|
|
if len(keys) == 0 {
|
|
return nil
|
|
}
|
|
mapper := NewMapper[K, int]()
|
|
for _, key := range keys {
|
|
mapper.Set(key, 0)
|
|
}
|
|
return mapper
|
|
}
|
|
|
|
func NewIdentityMapper[K comparable](keys ...K) Mapper[K, K] {
|
|
if len(keys) == 0 {
|
|
return nil
|
|
}
|
|
mapper := NewMapper[K, K]()
|
|
for _, key := range keys {
|
|
mapper.Set(key, key)
|
|
}
|
|
return mapper
|
|
}
|