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