github.com/graemephi/kahugo@v0.62.3-0.20211121071557-d78c0423784d/tpl/transform/unmarshal.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 transform
    15  
    16  import (
    17  	"io/ioutil"
    18  	"strings"
    19  
    20  	"github.com/gohugoio/hugo/resources/resource"
    21  
    22  	"github.com/gohugoio/hugo/common/types"
    23  
    24  	"github.com/mitchellh/mapstructure"
    25  
    26  	"github.com/gohugoio/hugo/helpers"
    27  	"github.com/gohugoio/hugo/parser/metadecoders"
    28  	"github.com/pkg/errors"
    29  
    30  	"github.com/spf13/cast"
    31  )
    32  
    33  // Unmarshal unmarshals the data given, which can be either a string, json.RawMessage
    34  // or a Resource. Supported formats are JSON, TOML, YAML, and CSV.
    35  // You can optionally provide an options map as the first argument.
    36  func (ns *Namespace) Unmarshal(args ...interface{}) (interface{}, error) {
    37  	if len(args) < 1 || len(args) > 2 {
    38  		return nil, errors.New("unmarshal takes 1 or 2 arguments")
    39  	}
    40  
    41  	var data interface{}
    42  	decoder := metadecoders.Default
    43  
    44  	if len(args) == 1 {
    45  		data = args[0]
    46  	} else {
    47  		m, ok := args[0].(map[string]interface{})
    48  		if !ok {
    49  			return nil, errors.New("first argument must be a map")
    50  		}
    51  
    52  		var err error
    53  
    54  		data = args[1]
    55  		decoder, err = decodeDecoder(m)
    56  		if err != nil {
    57  			return nil, errors.WithMessage(err, "failed to decode options")
    58  		}
    59  	}
    60  
    61  	if r, ok := data.(resource.UnmarshableResource); ok {
    62  		key := r.Key()
    63  
    64  		if key == "" {
    65  			return nil, errors.New("no Key set in Resource")
    66  		}
    67  
    68  		if decoder != metadecoders.Default {
    69  			key += decoder.OptionsKey()
    70  		}
    71  
    72  		return ns.cache.GetOrCreate(key, func() (interface{}, error) {
    73  			f := metadecoders.FormatFromMediaType(r.MediaType())
    74  			if f == "" {
    75  				return nil, errors.Errorf("MIME %q not supported", r.MediaType())
    76  			}
    77  
    78  			reader, err := r.ReadSeekCloser()
    79  			if err != nil {
    80  				return nil, err
    81  			}
    82  			defer reader.Close()
    83  
    84  			b, err := ioutil.ReadAll(reader)
    85  			if err != nil {
    86  				return nil, err
    87  			}
    88  
    89  			return decoder.Unmarshal(b, f)
    90  		})
    91  	}
    92  
    93  	dataStr, err := types.ToStringE(data)
    94  	if err != nil {
    95  		return nil, errors.Errorf("type %T not supported", data)
    96  	}
    97  
    98  	key := helpers.MD5String(dataStr)
    99  
   100  	return ns.cache.GetOrCreate(key, func() (interface{}, error) {
   101  		f := decoder.FormatFromContentString(dataStr)
   102  		if f == "" {
   103  			return nil, errors.New("unknown format")
   104  		}
   105  
   106  		return decoder.Unmarshal([]byte(dataStr), f)
   107  	})
   108  }
   109  
   110  func decodeDecoder(m map[string]interface{}) (metadecoders.Decoder, error) {
   111  	opts := metadecoders.Default
   112  
   113  	if m == nil {
   114  		return opts, nil
   115  	}
   116  
   117  	// mapstructure does not support string to rune conversion, so do that manually.
   118  	// See https://github.com/mitchellh/mapstructure/issues/151
   119  	for k, v := range m {
   120  		if strings.EqualFold(k, "Delimiter") {
   121  			r, err := stringToRune(v)
   122  			if err != nil {
   123  				return opts, err
   124  			}
   125  			opts.Delimiter = r
   126  			delete(m, k)
   127  
   128  		} else if strings.EqualFold(k, "Comment") {
   129  			r, err := stringToRune(v)
   130  			if err != nil {
   131  				return opts, err
   132  			}
   133  			opts.Comment = r
   134  			delete(m, k)
   135  		}
   136  	}
   137  
   138  	err := mapstructure.WeakDecode(m, &opts)
   139  
   140  	return opts, err
   141  }
   142  
   143  func stringToRune(v interface{}) (rune, error) {
   144  	s, err := cast.ToStringE(v)
   145  	if err != nil {
   146  		return 0, err
   147  	}
   148  
   149  	if len(s) == 0 {
   150  		return 0, nil
   151  	}
   152  
   153  	var r rune
   154  
   155  	for i, rr := range s {
   156  		if i == 0 {
   157  			r = rr
   158  		} else {
   159  			return 0, errors.Errorf("invalid character: %q", v)
   160  		}
   161  	}
   162  
   163  	return r, nil
   164  }