github.com/kovansky/hugo@v0.92.3-0.20220224232819-63076e4ff19f/tpl/internal/resourcehelpers/helpers.go (about) 1 // Copyright 2020 The Hugo Authors. All rights reserved. 2 // 3 // Portions Copyright The Go Authors. 4 5 // Licensed under the Apache License, Version 2.0 (the "License"); 6 // you may not use this file except in compliance with the License. 7 // You may obtain a copy of the License at 8 // http://www.apache.org/licenses/LICENSE-2.0 9 // 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 16 package resourcehelpers 17 18 import ( 19 "errors" 20 "fmt" 21 22 _errors "github.com/pkg/errors" 23 24 "github.com/gohugoio/hugo/common/maps" 25 "github.com/gohugoio/hugo/resources" 26 ) 27 28 // We allow string or a map as the first argument in some cases. 29 func ResolveIfFirstArgIsString(args []interface{}) (resources.ResourceTransformer, string, bool) { 30 if len(args) != 2 { 31 return nil, "", false 32 } 33 34 v1, ok1 := args[0].(string) 35 if !ok1 { 36 return nil, "", false 37 } 38 v2, ok2 := args[1].(resources.ResourceTransformer) 39 40 return v2, v1, ok2 41 } 42 43 // This roundabout way of doing it is needed to get both pipeline behaviour and options as arguments. 44 func ResolveArgs(args []interface{}) (resources.ResourceTransformer, map[string]interface{}, error) { 45 if len(args) == 0 { 46 return nil, nil, errors.New("no Resource provided in transformation") 47 } 48 49 if len(args) == 1 { 50 r, ok := args[0].(resources.ResourceTransformer) 51 if !ok { 52 return nil, nil, fmt.Errorf("type %T not supported in Resource transformations", args[0]) 53 } 54 return r, nil, nil 55 } 56 57 r, ok := args[1].(resources.ResourceTransformer) 58 if !ok { 59 if _, ok := args[1].(map[string]interface{}); !ok { 60 return nil, nil, fmt.Errorf("no Resource provided in transformation") 61 } 62 return nil, nil, fmt.Errorf("type %T not supported in Resource transformations", args[0]) 63 } 64 65 m, err := maps.ToStringMapE(args[0]) 66 if err != nil { 67 return nil, nil, _errors.Wrap(err, "invalid options type") 68 } 69 70 return r, m, nil 71 }