Some checks are pending
Docs Deploy / build_and_deploy (push) Waiting to run
Generate Docs / cli (push) Waiting to run
Generate Config Doc / cli (push) Waiting to run
Go formatting / go-formatting (push) Waiting to run
Check links / markdown-link-check (push) Waiting to run
Integration / pre-test (push) Waiting to run
Integration / test on (push) Blocked by required conditions
Integration / status (push) Blocked by required conditions
Lint / Lint Go code (push) Waiting to run
Test / test (ubuntu-latest) (push) Waiting to run
44 lines
1.1 KiB
Go
44 lines
1.1 KiB
Go
package xyaml
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
|
|
"github.com/goccy/go-yaml"
|
|
"github.com/goccy/go-yaml/parser"
|
|
|
|
"github.com/ignite/cli/v29/ignite/pkg/errors"
|
|
)
|
|
|
|
// Marshal converts an object to a string in a YAML format and transforms
|
|
// the byte slice fields from the path to string to be more readable.
|
|
func Marshal(ctx context.Context, obj interface{}, paths ...string) (string, error) {
|
|
requestYaml, err := yaml.MarshalContext(ctx, obj)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
file, err := parser.ParseBytes(requestYaml, 0)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
// normalize the structure converting the byte slice fields to string
|
|
for _, path := range paths {
|
|
pathString, err := yaml.PathString(path)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
var byteSlice []byte
|
|
err = pathString.Read(strings.NewReader(string(requestYaml)), &byteSlice)
|
|
if err != nil && !errors.Is(err, yaml.ErrNotFoundNode) {
|
|
return "", err
|
|
}
|
|
if err := pathString.ReplaceWithReader(file,
|
|
strings.NewReader(string(byteSlice)),
|
|
); err != nil {
|
|
return "", err
|
|
}
|
|
}
|
|
|
|
return file.String(), nil
|
|
}
|