-
Notifications
You must be signed in to change notification settings - Fork 23
/
binary.go
48 lines (41 loc) · 1.14 KB
/
binary.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
// SPDX-FileCopyrightText: 2022 SAP SE or an SAP affiliate company and Open Component Model contributors.
//
// SPDX-License-Identifier: Apache-2.0
package runtime
import (
"encoding/base64"
"encoding/json"
"strings"
"github.com/open-component-model/ocm/pkg/errors"
)
// Binary holds binary data which will be marshaled
// a base64 encoded string.
// If the string starts with a '!', the data is used as string
// byte sequence.
type Binary []byte
// MarshalJSON returns m as the JSON encoding of m.
func (m Binary) MarshalJSON() ([]byte, error) {
if m == nil {
return []byte("null"), nil
}
return json.Marshal(base64.StdEncoding.EncodeToString(m))
}
// UnmarshalJSON sets *m to a copy of data.
func (m *Binary) UnmarshalJSON(data []byte) error {
if m == nil {
return errors.Newf("runtime.Binary: UnmarshalJSON on nil pointer")
}
var s string
err := json.Unmarshal(data, &s)
if err != nil {
return err
}
if !strings.HasPrefix(s, "!") {
*m, err = base64.StdEncoding.DecodeString(s)
return err
}
*m = []byte(s[1:])
return nil
}
var _ json.Marshaler = (*Binary)(nil) //nolint: gofumpt // yes
var _ json.Unmarshaler = (*Binary)(nil)