-
Notifications
You must be signed in to change notification settings - Fork 0
/
yaml.go
50 lines (35 loc) · 1.02 KB
/
yaml.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
package yaml
import (
errors "github.com/apenella/go-common-utils/error"
"github.com/gostevedore/stevedore/internal/core/domain/credentials"
"gopkg.in/yaml.v3"
)
type YAMLFormater struct{}
func NewYAMLFormater() *YAMLFormater {
return &YAMLFormater{}
}
func (f *YAMLFormater) Marshal(credential *credentials.Credential) (string, error) {
var yamled []byte
var err error
errContext := "(YAMLFormater::Marshal)"
if credential == nil {
return "", errors.New(errContext, "Credential to be formatted must be provided")
}
yamled, err = yaml.Marshal(credential)
if err != nil {
return "", err
}
return string(yamled), nil
}
func (f *YAMLFormater) Unmarshal(data []byte) (*credentials.Credential, error) {
errContext := "(YAMLFormater::Unmarshal)"
if data == nil {
return nil, errors.New(errContext, "Data to be unmarshalled must be provided")
}
credential := &credentials.Credential{}
err := yaml.Unmarshal(data, credential)
if err != nil {
return nil, errors.New(errContext, "", err)
}
return credential, nil
}