-
-
Notifications
You must be signed in to change notification settings - Fork 321
Expand file tree
/
Copy pathoutput.go
More file actions
43 lines (35 loc) · 1.04 KB
/
output.go
File metadata and controls
43 lines (35 loc) · 1.04 KB
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
package ferret
import (
"github.com/MontFerret/ferret/v2/pkg/encoding"
"github.com/MontFerret/ferret/v2/pkg/runtime"
"github.com/MontFerret/ferret/v2/pkg/vm"
)
// Output is the encoded result returned from session or engine execution.
type Output struct {
// ContentType is the MIME type of the encoded content (e.g. "application/json").
ContentType string
// Content holds the encoded output bytes.
Content []byte
}
func newOutput(registry *encoding.Registry, contentType string, res *vm.Result) (*Output, error) {
codec, err := registry.Codec(contentType)
if err != nil {
return nil, err
}
return vm.Materialize[*Output](res, func(value runtime.Value) (vm.Materialized[*Output], error) {
enc := codec.EncodeWith().PreHook(func(value runtime.Value) error {
res.AdoptValue(value)
return nil
}).Encoder()
data, err := enc.Encode(value)
if err != nil {
return vm.Materialized[*Output]{}, err
}
return vm.Materialized[*Output]{
Value: &Output{
ContentType: codec.ContentType(),
Content: data,
},
}, nil
})
}