-
Notifications
You must be signed in to change notification settings - Fork 2
/
value.go
62 lines (46 loc) · 1010 Bytes
/
value.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
// Copyright 2023 Contributors to the Veraison project.
// SPDX-License-Identifier: Apache-2.0
package cmw
import (
"encoding/base64"
"encoding/json"
"fmt"
"github.com/fxamacker/cbor/v2"
)
type Value []byte
func (o *Value) Set(v []byte) error {
*o = v
return nil
}
func (o Value) IsSet() bool {
return len(o) != 0
}
func (o *Value) UnmarshalJSON(b []byte) error {
var (
v []byte
err error
)
if v, err = base64.RawURLEncoding.DecodeString(string(b[1 : len(b)-1])); err != nil {
return fmt.Errorf("cannot base64 url-safe decode: %w", err)
}
*o = v
return nil
}
func (o Value) MarshalJSON() ([]byte, error) {
s := base64.RawURLEncoding.EncodeToString([]byte(o))
return json.Marshal(s)
}
func (o *Value) UnmarshalCBOR(b []byte) error {
var (
v []byte
err error
)
if err = cbor.Unmarshal(b, &v); err != nil {
return fmt.Errorf("cannot decode value: %w", err)
}
*o = v
return nil
}
func (o Value) MarshalCBOR() ([]byte, error) {
return cbor.Marshal([]byte(o))
}