github.com/neohugo/neohugo@v0.123.8/resources/resource/resource_helpers.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 resource 15 16 import ( 17 "strings" 18 "time" 19 20 "github.com/neohugo/neohugo/helpers" 21 "github.com/pelletier/go-toml/v2" 22 23 "github.com/spf13/cast" 24 ) 25 26 // GetParam will return the param with the given key from the Resource, 27 // nil if not found. 28 func GetParam(r Resource, key string) any { 29 return getParam(r, key, false) 30 } 31 32 // GetParamToLower is the same as GetParam but it will lower case any string 33 // result, including string slices. 34 func GetParamToLower(r Resource, key string) any { 35 return getParam(r, key, true) 36 } 37 38 func getParam(r Resource, key string, stringToLower bool) any { 39 v := r.Params()[strings.ToLower(key)] 40 41 if v == nil { 42 return nil 43 } 44 45 switch val := v.(type) { 46 case bool: 47 return val 48 case string: 49 if stringToLower { 50 return strings.ToLower(val) 51 } 52 return val 53 case int64, int32, int16, int8, int: 54 return cast.ToInt(v) 55 case float64, float32: 56 return cast.ToFloat64(v) 57 case time.Time: 58 return val 59 case toml.LocalDate: 60 return val.AsTime(time.UTC) 61 case toml.LocalDateTime: 62 return val.AsTime(time.UTC) 63 case []string: 64 if stringToLower { 65 return helpers.SliceToLower(val) 66 } 67 return v 68 case map[string]any: 69 return v 70 case map[any]any: 71 return v 72 } 73 return nil 74 }