mukan-ignite/ignite/pkg/xyaml/yaml.go
Mukan Erkin Törük c32551b6f7
Some checks failed
Docs Deploy / build_and_deploy (push) Has been cancelled
Generate Docs / cli (push) Has been cancelled
Generate Config Doc / cli (push) Has been cancelled
Go formatting / go-formatting (push) Has been cancelled
Check links / markdown-link-check (push) Has been cancelled
Integration / pre-test (push) Has been cancelled
Integration / test on (push) Has been cancelled
Integration / status (push) Has been cancelled
Lint / Lint Go code (push) Has been cancelled
Test / test (ubuntu-latest) (push) Has been cancelled
refactor: replace all github.com upstream refs with git.cw.tr/mukan-network
2026-05-11 03:36:24 +03:00

44 lines
1.1 KiB
Go

package xyaml
import (
"context"
"strings"
"github.com/goccy/go-yaml"
"github.com/goccy/go-yaml/parser"
"git.cw.tr/mukan-network/mukan-ignite/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
}