-
Notifications
You must be signed in to change notification settings - Fork 19
/
encoding.go
49 lines (35 loc) · 1.13 KB
/
encoding.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
// SPDX-FileCopyrightText: 2022 SAP SE or an SAP affiliate company and Open Component Model contributors.
//
// SPDX-License-Identifier: Apache-2.0
package runtime
// github.com/ghodss/yaml
import (
"encoding/json"
"sigs.k8s.io/yaml"
)
type Marshaler interface {
Marshal(obj interface{}) ([]byte, error)
}
type Unmarshaler interface {
Unmarshal(data []byte, obj interface{}) error
}
type MarshalFunction func(obj interface{}) ([]byte, error)
func (f MarshalFunction) Marshal(obj interface{}) ([]byte, error) { return f(obj) }
type UnmarshalFunction func(data []byte, obj interface{}) error
func (f UnmarshalFunction) Unmarshal(data []byte, obj interface{}) error { return f(data, obj) }
type Encoding interface {
Unmarshaler
Marshaler
}
type EncodingWrapper struct {
Unmarshaler
Marshaler
}
var DefaultJSONEncoding = &EncodingWrapper{
Marshaler: MarshalFunction(json.Marshal),
Unmarshaler: UnmarshalFunction(json.Unmarshal),
}
var DefaultYAMLEncoding = &EncodingWrapper{
Marshaler: MarshalFunction(yaml.Marshal),
Unmarshaler: UnmarshalFunction(func(data []byte, obj interface{}) error { return yaml.Unmarshal(data, obj) }),
}