Debian Codegraph @

resources/internal/key.go

raw
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 internal
15
16 import (
17 "github.com/gohugoio/hugo/common/hashing"
18 )
19
20 // ResourceTransformationKey are provided by the different transformation implementations.
21 // It identifies the transformation (name) and its configuration (elements).
22 // We combine this in a chain with the rest of the transformations
23 // with the target filename and a content hash of the origin to use as cache key.
24 type ResourceTransformationKey struct {
25 Name string
26 elements []any
27 }
28
29 // NewResourceTransformationKey creates a new ResourceTransformationKey from the transformation
30 // name and elements. We will create a 64 bit FNV hash from the elements, which when combined
31 // with the other key elements should be unique for all practical applications.
32 func NewResourceTransformationKey(name string, elements ...any) ResourceTransformationKey {
33 return ResourceTransformationKey{Name: name, elements: elements}
34 }
35
36 // Value returns the Key as a string.
37 // Do not change this without good reasons.
38 func (k ResourceTransformationKey) Value() string {
39 if len(k.elements) == 0 {
40 return k.Name
41 }
42
43 return k.Name + "_" + hashing.HashString(k.elements...)
44 }