-
-
Notifications
You must be signed in to change notification settings - Fork 7.4k
/
unmarshal.go
201 lines (161 loc) · 4.21 KB
/
unmarshal.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// Copyright 2019 The Hugo Authors. All rights reserved.
//
// 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 (
"errors"
"fmt"
"io"
"strings"
"github.com/gohugoio/hugo/resources"
"github.com/gohugoio/hugo/resources/resource"
"github.com/gohugoio/hugo/common/types"
"github.com/mitchellh/mapstructure"
"github.com/gohugoio/hugo/helpers"
"github.com/gohugoio/hugo/parser/metadecoders"
"github.com/spf13/cast"
)
// Unmarshal unmarshals the data given, which can be either a string, json.RawMessage
// or a Resource. Supported formats are JSON, TOML, YAML, and CSV.
// You can optionally provide an options map as the first argument.
func (ns *Namespace) Unmarshal(args ...any) (any, error) {
if len(args) < 1 || len(args) > 2 {
return nil, errors.New("unmarshal takes 1 or 2 arguments")
}
var data any
decoder := metadecoders.Default
if len(args) == 1 {
data = args[0]
} else {
m, ok := args[0].(map[string]any)
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, fmt.Errorf("failed to decode options: %w", err)
}
}
if r, ok := data.(resource.UnmarshableResource); ok {
key := r.Key()
if key == "" {
return nil, errors.New("no Key set in Resource")
}
if decoder != metadecoders.Default {
key += decoder.OptionsKey()
}
v, err := ns.cache.GetOrCreate(key, func(string) (*resources.StaleValue[any], error) {
f := metadecoders.FormatFromStrings(r.MediaType().Suffixes()...)
if f == "" {
return nil, fmt.Errorf("MIME %q not supported", r.MediaType())
}
reader, err := r.ReadSeekCloser()
if err != nil {
return nil, err
}
defer reader.Close()
b, err := io.ReadAll(reader)
if err != nil {
return nil, err
}
v, err := decoder.Unmarshal(b, f)
if err != nil {
return nil, err
}
return &resources.StaleValue[any]{
Value: v,
IsStaleFunc: func() bool {
return resource.IsStaleAny(r)
},
}, nil
})
if err != nil {
return nil, err
}
return v.Value, nil
}
dataStr, err := types.ToStringE(data)
if err != nil {
return nil, fmt.Errorf("type %T not supported", data)
}
if dataStr == "" {
return nil, errors.New("no data to transform")
}
key := helpers.MD5String(dataStr)
v, err := ns.cache.GetOrCreate(key, func(string) (*resources.StaleValue[any], error) {
f := decoder.FormatFromContentString(dataStr)
if f == "" {
return nil, errors.New("unknown format")
}
v, err := decoder.Unmarshal([]byte(dataStr), f)
if err != nil {
return nil, err
}
return &resources.StaleValue[any]{
Value: v,
IsStaleFunc: func() bool {
return false
},
}, nil
})
if err != nil {
return nil, err
}
return v.Value, nil
}
func decodeDecoder(m map[string]any) (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 {
if strings.EqualFold(k, "Delimiter") {
r, err := stringToRune(v)
if err != nil {
return opts, err
}
opts.Delimiter = r
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 any) (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, fmt.Errorf("invalid character: %q", v)
}
}
return r, nil
}