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
|
|
|
|
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"
|
2019-01-02 05:58:32 -05:00
|
|
|
"github.com/gohugoio/hugo/resources/resource"
|
2018-12-21 10:21:13 -05:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
|
|
|
"github.com/spf13/cast"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Unmarshal unmarshals the data given, which can be either a string
|
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.
|
2018-12-23 04:40:32 -05:00
|
|
|
func (ns *Namespace) Unmarshal(args ...interface{}) (interface{}, error) {
|
|
|
|
if len(args) < 1 || len(args) > 2 {
|
|
|
|
return nil, errors.New("unmarshal takes 1 or 2 arguments")
|
|
|
|
}
|
|
|
|
|
|
|
|
var data interface{}
|
|
|
|
var decoder = metadecoders.Default
|
|
|
|
|
|
|
|
if len(args) == 1 {
|
|
|
|
data = args[0]
|
|
|
|
} else {
|
|
|
|
m, ok := args[0].(map[string]interface{})
|
|
|
|
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
|
|
|
|
2018-12-23 10:43:04 -05:00
|
|
|
if r, ok := data.(unmarshableResource); ok {
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2018-12-21 10:21:13 -05:00
|
|
|
return ns.cache.GetOrCreate(key, func() (interface{}, error) {
|
|
|
|
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
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
dataStr, err := cast.ToStringE(data)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Errorf("type %T not supported", data)
|
|
|
|
}
|
|
|
|
|
|
|
|
key := helpers.MD5String(dataStr)
|
|
|
|
|
|
|
|
return ns.cache.GetOrCreate(key, func() (interface{}, 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
|
|
|
|
2018-12-23 10:43:04 -05:00
|
|
|
// All the relevant resources implements this interface.
|
|
|
|
type unmarshableResource interface {
|
|
|
|
resource.ReadSeekCloserResource
|
|
|
|
resource.Identifier
|
|
|
|
}
|
|
|
|
|
2018-12-23 04:40:32 -05:00
|
|
|
func decodeDecoder(m map[string]interface{}) (metadecoders.Decoder, error) {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func stringToRune(v interface{}) (rune, error) {
|
|
|
|
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
|
|
|
|
}
|