| 1 |
// Copyright 2019 The Hugo Authors. All rights reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
// Package types contains types shared between packages in Hugo. |
| 15 |
package types |
| 16 |
|
| 17 |
import ( |
| 18 |
"fmt" |
| 19 |
"reflect" |
| 20 |
"sync/atomic" |
| 21 |
|
| 22 |
"github.com/spf13/cast" |
| 23 |
) |
| 24 |
|
| 25 |
// RLocker represents the read locks in sync.RWMutex. |
| 26 |
type RLocker interface { |
| 27 |
RLock() |
| 28 |
RUnlock() |
| 29 |
} |
| 30 |
|
| 31 |
type Locker interface { |
| 32 |
Lock() |
| 33 |
Unlock() |
| 34 |
} |
| 35 |
|
| 36 |
type RWLocker interface { |
| 37 |
RLocker |
| 38 |
Locker |
| 39 |
} |
| 40 |
|
| 41 |
// KeyValue is a interface{} tuple. |
| 42 |
type KeyValue struct { |
| 43 |
Key any |
| 44 |
Value any |
| 45 |
} |
| 46 |
|
| 47 |
// KeyValueStr is a string tuple. |
| 48 |
type KeyValueStr struct { |
| 49 |
Key string |
| 50 |
Value string |
| 51 |
} |
| 52 |
|
| 53 |
// KeyValues holds an key and a slice of values. |
| 54 |
type KeyValues struct { |
| 55 |
Key any |
| 56 |
Values []any |
| 57 |
} |
| 58 |
|
| 59 |
// KeyString returns the key as a string, an empty string if conversion fails. |
| 60 |
func (k KeyValues) KeyString() string { |
| 61 |
return cast.ToString(k.Key) |
| 62 |
} |
| 63 |
|
| 64 |
func (k KeyValues) String() string { |
| 65 |
return fmt.Sprintf("%v: %v", k.Key, k.Values) |
| 66 |
} |
| 67 |
|
| 68 |
// NewKeyValuesStrings takes a given key and slice of values and returns a new |
| 69 |
// KeyValues struct. |
| 70 |
func NewKeyValuesStrings(key string, values ...string) KeyValues { |
| 71 |
iv := make([]any, len(values)) |
| 72 |
for i := range values { |
| 73 |
iv[i] = values[i] |
| 74 |
} |
| 75 |
return KeyValues{Key: key, Values: iv} |
| 76 |
} |
| 77 |
|
| 78 |
// Zeroer, as implemented by time.Time, will be used by the truth template |
| 79 |
// funcs in Hugo (if, with, not, and, or). |
| 80 |
type Zeroer interface { |
| 81 |
IsZero() bool |
| 82 |
} |
| 83 |
|
| 84 |
// IsNil reports whether v is nil. |
| 85 |
func IsNil(v any) bool { |
| 86 |
if v == nil { |
| 87 |
return true |
| 88 |
} |
| 89 |
|
| 90 |
value := reflect.ValueOf(v) |
| 91 |
switch value.Kind() { |
| 92 |
case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Pointer, reflect.Slice: |
| 93 |
return value.IsNil() |
| 94 |
} |
| 95 |
|
| 96 |
return false |
| 97 |
} |
| 98 |
|
| 99 |
// DevMarker is a marker interface for types that should only be used during |
| 100 |
// development. |
| 101 |
type DevMarker interface { |
| 102 |
DevOnly() |
| 103 |
} |
| 104 |
|
| 105 |
// Unwrapper is implemented by types that can unwrap themselves. |
| 106 |
type Unwrapper interface { |
| 107 |
// Unwrapv is for internal use only. |
| 108 |
// It got its slightly odd name to prevent collisions with user types. |
| 109 |
Unwrapv() any |
| 110 |
} |
| 111 |
|
| 112 |
// Unwrap returns the underlying value of v if it implements Unwrapper, otherwise v is returned. |
| 113 |
func Unwrapv(v any) any { |
| 114 |
if u, ok := v.(Unwrapper); ok { |
| 115 |
return u.Unwrapv() |
| 116 |
} |
| 117 |
return v |
| 118 |
} |
| 119 |
|
| 120 |
// LowHigh represents a byte or slice boundary. |
| 121 |
type LowHigh[S ~[]byte | string] struct { |
| 122 |
Low int |
| 123 |
High int |
| 124 |
} |
| 125 |
|
| 126 |
func (l LowHigh[S]) IsZero() bool { |
| 127 |
return l.Low < 0 || (l.Low == 0 && l.High == 0) |
| 128 |
} |
| 129 |
|
| 130 |
func (l LowHigh[S]) Value(source S) S { |
| 131 |
return source[l.Low:l.High] |
| 132 |
} |
| 133 |
|
| 134 |
// This is only used for debugging purposes. |
| 135 |
var InvocationCounter atomic.Int64 |
| 136 |
|
| 137 |
// WeightProvider provides a weight. |
| 138 |
type WeightProvider interface { |
| 139 |
Weight() int |
| 140 |
} |
| 141 |
|
| 142 |
// Weight0Provider provides a weight that's considered before the WeightProvider in sorting. |
| 143 |
// This allows the weight set on a given term to win. |
| 144 |
type Weight0Provider interface { |
| 145 |
Weight0() int |
| 146 |
} |
| 147 |
|
| 148 |
// PrintableValueProvider is implemented by types that can provide a printable value. |
| 149 |
type PrintableValueProvider interface { |
| 150 |
PrintableValue() any |
| 151 |
} |
| 152 |
|
| 153 |
type ( |
| 154 |
Strings2 [2]string |
| 155 |
Strings3 [3]string |
| 156 |
Ints2 [2]int |
| 157 |
Ints3 [3]int |
| 158 |
) |