2019-01-02 05:58:32 -05:00
|
|
|
// Copyright 2019 The Hugo Authors. All rights reserved.
|
2018-12-21 10:21:13 -05:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package transform
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
2018-12-23 04:40:32 -05:00
|
|
|
"strings"
|
2018-12-21 10:21:13 -05:00
|
|
|
|
2020-06-30 10:11:05 -04:00
|
|
|
"github.com/gohugoio/hugo/resources/resource"
|
|
|
|
|
|
|
|
"github.com/gohugoio/hugo/common/types"
|
|
|
|
|
2018-12-23 04:40:32 -05:00
|
|
|
"github.com/mitchellh/mapstructure"
|
2018-12-21 10:21:13 -05:00
|
|
|
|
|
|
|
"github.com/gohugoio/hugo/helpers"
|
|
|
|
"github.com/gohugoio/hugo/parser/metadecoders"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
|
|
|
"github.com/spf13/cast"
|
|
|
|
)
|
|
|
|
|
2020-06-30 10:11:05 -04:00
|
|
|
// Unmarshal unmarshals the data given, which can be either a string, json.RawMessage
|
2018-12-23 04:40:32 -05:00
|
|
|
// or a Resource. Supported formats are JSON, TOML, YAML, and CSV.
|
2018-12-23 10:43:04 -05:00
|
|
|
// You can optionally provide an options map as the first argument.
|
2022-03-17 17:03:27 -04:00
|
|
|
func (ns *Namespace) Unmarshal(args ...any) (any, error) {
|
2018-12-23 04:40:32 -05:00
|
|
|
if len(args) < 1 || len(args) > 2 {
|
|
|
|
return nil, errors.New("unmarshal takes 1 or 2 arguments")
|
|
|
|
}
|
|
|
|
|
2022-03-17 17:03:27 -04:00
|
|
|
var data any
|
2020-12-02 07:23:25 -05:00
|
|
|
decoder := metadecoders.Default
|
2018-12-23 04:40:32 -05:00
|
|
|
|
|
|
|
if len(args) == 1 {
|
|
|
|
data = args[0]
|
|
|
|
} else {
|
2022-03-17 17:03:27 -04:00
|
|
|
m, ok := args[0].(map[string]any)
|
2018-12-23 04:40:32 -05:00
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("first argument must be a map")
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
|
|
|
|
data = args[1]
|
|
|
|
decoder, err = decodeDecoder(m)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.WithMessage(err, "failed to decode options")
|
|
|
|
}
|
|
|
|
}
|
2018-12-21 10:21:13 -05:00
|
|
|
|
2020-06-30 10:11:05 -04:00
|
|
|
if r, ok := data.(resource.UnmarshableResource); ok {
|
2018-12-23 10:43:04 -05:00
|
|
|
key := r.Key()
|
2018-12-21 10:21:13 -05:00
|
|
|
|
|
|
|
if key == "" {
|
2018-12-23 10:43:04 -05:00
|
|
|
return nil, errors.New("no Key set in Resource")
|
2018-12-21 10:21:13 -05:00
|
|
|
}
|
|
|
|
|
2018-12-23 15:34:17 -05:00
|
|
|
if decoder != metadecoders.Default {
|
|
|
|
key += decoder.OptionsKey()
|
|
|
|
}
|
|
|
|
|
2022-03-17 17:03:27 -04:00
|
|
|
return ns.cache.GetOrCreate(key, func() (any, error) {
|
2018-12-21 10:21:13 -05:00
|
|
|
f := metadecoders.FormatFromMediaType(r.MediaType())
|
|
|
|
if f == "" {
|
|
|
|
return nil, errors.Errorf("MIME %q not supported", r.MediaType())
|
|
|
|
}
|
|
|
|
|
2018-12-23 10:43:04 -05:00
|
|
|
reader, err := r.ReadSeekCloser()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2018-12-21 10:21:13 -05:00
|
|
|
}
|
2018-12-23 10:43:04 -05:00
|
|
|
defer reader.Close()
|
2018-12-21 10:21:13 -05:00
|
|
|
|
|
|
|
b, err := ioutil.ReadAll(reader)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-12-23 04:40:32 -05:00
|
|
|
return decoder.Unmarshal(b, f)
|
2018-12-21 10:21:13 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-06-30 10:11:05 -04:00
|
|
|
dataStr, err := types.ToStringE(data)
|
2018-12-21 10:21:13 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Errorf("type %T not supported", data)
|
|
|
|
}
|
|
|
|
|
2021-12-16 09:12:13 -05:00
|
|
|
if dataStr == "" {
|
|
|
|
return nil, errors.New("no data to transform")
|
|
|
|
}
|
|
|
|
|
2018-12-21 10:21:13 -05:00
|
|
|
key := helpers.MD5String(dataStr)
|
|
|
|
|
2022-03-17 17:03:27 -04:00
|
|
|
return ns.cache.GetOrCreate(key, func() (any, error) {
|
2018-12-23 04:40:32 -05:00
|
|
|
f := decoder.FormatFromContentString(dataStr)
|
2018-12-21 10:21:13 -05:00
|
|
|
if f == "" {
|
|
|
|
return nil, errors.New("unknown format")
|
|
|
|
}
|
|
|
|
|
2018-12-23 04:40:32 -05:00
|
|
|
return decoder.Unmarshal([]byte(dataStr), f)
|
2018-12-21 10:21:13 -05:00
|
|
|
})
|
|
|
|
}
|
2018-12-23 04:40:32 -05:00
|
|
|
|
2022-03-17 17:03:27 -04:00
|
|
|
func decodeDecoder(m map[string]any) (metadecoders.Decoder, error) {
|
2018-12-23 04:40:32 -05:00
|
|
|
opts := metadecoders.Default
|
|
|
|
|
|
|
|
if m == nil {
|
|
|
|
return opts, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// mapstructure does not support string to rune conversion, so do that manually.
|
|
|
|
// See https://github.com/mitchellh/mapstructure/issues/151
|
|
|
|
for k, v := range m {
|
2018-12-23 15:08:12 -05:00
|
|
|
if strings.EqualFold(k, "Delimiter") {
|
2018-12-23 04:40:32 -05:00
|
|
|
r, err := stringToRune(v)
|
|
|
|
if err != nil {
|
|
|
|
return opts, err
|
|
|
|
}
|
2018-12-23 15:08:12 -05:00
|
|
|
opts.Delimiter = r
|
2018-12-23 04:40:32 -05:00
|
|
|
delete(m, k)
|
|
|
|
|
|
|
|
} else if strings.EqualFold(k, "Comment") {
|
|
|
|
r, err := stringToRune(v)
|
|
|
|
if err != nil {
|
|
|
|
return opts, err
|
|
|
|
}
|
|
|
|
opts.Comment = r
|
|
|
|
delete(m, k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err := mapstructure.WeakDecode(m, &opts)
|
|
|
|
|
|
|
|
return opts, err
|
|
|
|
}
|
|
|
|
|
2022-03-17 17:03:27 -04:00
|
|
|
func stringToRune(v any) (rune, error) {
|
2018-12-23 04:40:32 -05:00
|
|
|
s, err := cast.ToStringE(v)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(s) == 0 {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var r rune
|
|
|
|
|
|
|
|
for i, rr := range s {
|
|
|
|
if i == 0 {
|
|
|
|
r = rr
|
|
|
|
} else {
|
|
|
|
return 0, errors.Errorf("invalid character: %q", v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return r, nil
|
|
|
|
}
|