Skip to content

Commit

Permalink
chore: redo EncodeString and EncodeBytes using buffer interface
Browse files Browse the repository at this point in the history
Remove the need for additional allocation in case we use `EncodeString` and overall looks nicer.

Signed-off-by: Dmitriy Matrenichev <dmitry.matrenichev@siderolabs.com>
  • Loading branch information
DmitriyMV committed Jul 8, 2024
1 parent d9db360 commit 963612b
Showing 1 changed file with 26 additions and 8 deletions.
34 changes: 26 additions & 8 deletions pkg/machinery/config/container/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"errors"
"fmt"
"slices"
"strings"

"github.com/hashicorp/go-multierror"
"github.com/siderolabs/gen/xslices"
Expand Down Expand Up @@ -203,41 +204,58 @@ func (container *Container) Bytes() ([]byte, error) {

// EncodeString configuration to YAML using the provided options.
func (container *Container) EncodeString(encoderOptions ...encoder.Option) (string, error) {
b, err := container.EncodeBytes(encoderOptions...)
var buf strings.Builder

err := container.encodeToBuf(&buf, encoderOptions...)
if err != nil {
return "", err
}

return string(b), nil
return buf.String(), nil
}

// EncodeBytes configuration to YAML using the provided options.
func (container *Container) EncodeBytes(encoderOptions ...encoder.Option) ([]byte, error) {
var buf bytes.Buffer

err := container.encodeToBuf(&buf, encoderOptions...)
if err != nil {
return nil, err
}

return buf.Bytes(), nil
}

type buffer interface {
Len() int
Write(p []byte) (int, error)
WriteString(s string) (int, error)
}

func (container *Container) encodeToBuf(buf buffer, encoderOptions ...encoder.Option) error {
if container.v1alpha1Config != nil {
b, err := encoder.NewEncoder(container.v1alpha1Config, encoderOptions...).Encode()
if err != nil {
return nil, err
return err
}

buf.Write(b)
buf.Write(b) //nolint:errcheck
}

for _, doc := range container.documents {
if buf.Len() > 0 {
buf.WriteString("---\n")
buf.WriteString("---\n") //nolint:errcheck
}

b, err := encoder.NewEncoder(doc, encoderOptions...).Encode()
if err != nil {
return nil, err
return err
}

buf.Write(b)
buf.Write(b) //nolint:errcheck
}

return buf.Bytes(), nil
return nil
}

// Validate checks configuration and returns warnings and fatal errors (as multierror).
Expand Down

0 comments on commit 963612b

Please sign in to comment.