github.com/anakojm/hugo-katex@v0.0.0-20231023141351-42d6f5de9c0b/common/types/types.go (about) 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 // KeyValue is a interface{} tuple. 32 type KeyValue struct { 33 Key any 34 Value any 35 } 36 37 // KeyValueStr is a string tuple. 38 type KeyValueStr struct { 39 Key string 40 Value string 41 } 42 43 // KeyValues holds an key and a slice of values. 44 type KeyValues struct { 45 Key any 46 Values []any 47 } 48 49 // KeyString returns the key as a string, an empty string if conversion fails. 50 func (k KeyValues) KeyString() string { 51 return cast.ToString(k.Key) 52 } 53 54 func (k KeyValues) String() string { 55 return fmt.Sprintf("%v: %v", k.Key, k.Values) 56 } 57 58 // NewKeyValuesStrings takes a given key and slice of values and returns a new 59 // KeyValues struct. 60 func NewKeyValuesStrings(key string, values ...string) KeyValues { 61 iv := make([]any, len(values)) 62 for i := 0; i < len(values); i++ { 63 iv[i] = values[i] 64 } 65 return KeyValues{Key: key, Values: iv} 66 } 67 68 // Zeroer, as implemented by time.Time, will be used by the truth template 69 // funcs in Hugo (if, with, not, and, or). 70 type Zeroer interface { 71 IsZero() bool 72 } 73 74 // IsNil reports whether v is nil. 75 func IsNil(v any) bool { 76 if v == nil { 77 return true 78 } 79 80 value := reflect.ValueOf(v) 81 switch value.Kind() { 82 case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice: 83 return value.IsNil() 84 } 85 86 return false 87 } 88 89 // DevMarker is a marker interface for types that should only be used during 90 // development. 91 type DevMarker interface { 92 DevOnly() 93 } 94 95 // This is only used for debugging purposes. 96 var InvocationCounter atomic.Int64