Skip to content

Commit

Permalink
Use yaml encoder for encoding resources (#16)
Browse files Browse the repository at this point in the history
* use yaml encoder for encoding resources

* remove separator
  • Loading branch information
colinhoglund authored Feb 9, 2023
1 parent bb87735 commit adba3c0
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 16 deletions.
18 changes: 4 additions & 14 deletions pkg/manifest/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import (
"gopkg.in/yaml.v3"
)

const separator = "\n---\n"

// Decode parses all YAML documents in the input string, unmarshals
// to the appropriate type, and outputs a slice of Resource objects.
func Decode(data string) ([]Resource, error) {
Expand Down Expand Up @@ -63,19 +61,11 @@ func remarshal(src, dst Resource) error {
// a single multi-document YAML string.
func Encode(resources []Resource) (string, error) {
buf := bytes.NewBuffer(nil)
for idx, r := range resources {
if idx != 0 {
if _, err := buf.WriteString(separator); err != nil {
return "", err
}
}

resourceBytes, err := yaml.Marshal(r)
if err != nil {
return "", err
}
enc := yaml.NewEncoder(buf)
defer enc.Close()

if _, err := buf.Write(bytes.TrimSpace(resourceBytes)); err != nil {
for _, r := range resources {
if err := enc.Encode(r); err != nil {
return "", err
}
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/manifest/encoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestDecodeEncode(t *testing.T) {
desc: "test separator prefix",
input: "---\nkind: pipeline\n",
decoded: []manifest.Resource{&manifest.Pipeline{Kind: manifest.KindPipeline}},
encoded: "kind: pipeline",
encoded: "kind: pipeline\n",
},
{
desc: "test multiple resources",
Expand Down Expand Up @@ -75,7 +75,8 @@ inline: secret
kind: signature
hmac: signature
inline:
key: signature`,
key: signature
`,
},
}

Expand Down

0 comments on commit adba3c0

Please sign in to comment.