github.com/anakojm/hugo-katex@v0.0.0-20231023141351-42d6f5de9c0b/identity/identityhash.go (about)

     1  // Copyright 2023 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 identity
    15  
    16  import (
    17  	"strconv"
    18  
    19  	"github.com/mitchellh/hashstructure"
    20  )
    21  
    22  // HashString returns a hash from the given elements.
    23  // It will panic if the hash cannot be calculated.
    24  // Note that this hash should be used primarily for identity, not for change detection as
    25  // it in the more complex values (e.g. Page) will not hash the full content.
    26  func HashString(vs ...any) string {
    27  	hash := HashUint64(vs...)
    28  	return strconv.FormatUint(hash, 10)
    29  }
    30  
    31  // HashUint64 returns a hash from the given elements.
    32  // It will panic if the hash cannot be calculated.
    33  // Note that this hash should be used primarily for identity, not for change detection as
    34  // it in the more complex values (e.g. Page) will not hash the full content.
    35  func HashUint64(vs ...any) uint64 {
    36  	var o any
    37  	if len(vs) == 1 {
    38  		o = toHashable(vs[0])
    39  	} else {
    40  		elements := make([]any, len(vs))
    41  		for i, e := range vs {
    42  			elements[i] = toHashable(e)
    43  		}
    44  		o = elements
    45  	}
    46  
    47  	hash, err := hashstructure.Hash(o, nil)
    48  	if err != nil {
    49  		panic(err)
    50  	}
    51  	return hash
    52  }
    53  
    54  type keyer interface {
    55  	Key() string
    56  }
    57  
    58  // For structs, hashstructure.Hash only works on the exported fields,
    59  // so rewrite the input slice for known identity types.
    60  func toHashable(v any) any {
    61  	switch t := v.(type) {
    62  	case Provider:
    63  		return t.GetIdentity()
    64  	case keyer:
    65  		return t.Key()
    66  	default:
    67  		return v
    68  	}
    69  }