github.com/anakojm/hugo-katex@v0.0.0-20231023141351-42d6f5de9c0b/config/namespace.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 config
    15  
    16  import (
    17  	"encoding/json"
    18  
    19  	"github.com/gohugoio/hugo/identity"
    20  )
    21  
    22  func DecodeNamespace[S, C any](configSource any, buildConfig func(any) (C, any, error)) (*ConfigNamespace[S, C], error) {
    23  
    24  	// Calculate the hash of the input (not including any defaults applied later).
    25  	// This allows us to introduce new config options without breaking the hash.
    26  	h := identity.HashString(configSource)
    27  
    28  	// Build the config
    29  	c, ext, err := buildConfig(configSource)
    30  	if err != nil {
    31  		return nil, err
    32  	}
    33  
    34  	if ext == nil {
    35  		ext = configSource
    36  	}
    37  
    38  	if ext == nil {
    39  		panic("ext is nil")
    40  	}
    41  
    42  	ns := &ConfigNamespace[S, C]{
    43  		SourceStructure: ext,
    44  		SourceHash:      h,
    45  		Config:          c,
    46  	}
    47  
    48  	return ns, nil
    49  }
    50  
    51  // ConfigNamespace holds a Hugo configuration namespace.
    52  // The construct looks a little odd, but it's built to make the configuration elements
    53  // both self-documenting and contained in a common structure.
    54  type ConfigNamespace[S, C any] struct {
    55  	// SourceStructure represents the source configuration with any defaults applied.
    56  	// This is used for documentation and printing of the configuration setup to the user.
    57  	SourceStructure any
    58  
    59  	// SourceHash is a hash of the source configuration before any defaults gets applied.
    60  	SourceHash string
    61  
    62  	// Config is the final configuration as used by Hugo.
    63  	Config C
    64  }
    65  
    66  // MarshalJSON marshals the source structure.
    67  func (ns *ConfigNamespace[S, C]) MarshalJSON() ([]byte, error) {
    68  	return json.Marshal(ns.SourceStructure)
    69  }
    70  
    71  // Signature returns the signature of the source structure.
    72  // Note that this is for documentation purposes only and SourceStructure may not always be cast to S (it's usually just a map).
    73  func (ns *ConfigNamespace[S, C]) Signature() S {
    74  	var s S
    75  	return s
    76  }