hugo

Fork of github.com/gohugoio/hugo with reverse pagination support

git clone git://git.shimmy1996.com/hugo.git

types.go (2239B)

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