Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(config): implement custom string unmarshaler #232

Merged
merged 1 commit into from
Dec 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func getRemoteConfig(url string) (*Aliae, error) {
func parseConfig(data []byte) (*Aliae, error) {
var aliae Aliae

decoder := yaml.NewDecoder(bytes.NewBuffer(data), yaml.CustomUnmarshaler(customUnmarshaler))
decoder := yaml.NewDecoder(bytes.NewBuffer(data), yaml.CustomUnmarshaler(aliaeUnmarshaler))
err := decoder.Decode(&aliae)
if err != nil {
return nil, fmt.Errorf("Failed to parse config file: %s", err)
Expand Down
4 changes: 2 additions & 2 deletions src/config/unmarshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ type StringFunc struct {
Name []byte
}

func customUnmarshaler(a *Aliae, b []byte) error {
func aliaeUnmarshaler(a *Aliae, b []byte) error {
data, err := includeUnmarshaler(b)
if err != nil {
return err
}

decoder := yaml.NewDecoder(bytes.NewBuffer(data))
decoder := yaml.NewDecoder(bytes.NewBuffer(data), yaml.CustomUnmarshaler(templateUmarshaler))
if err = decoder.Decode(a); err != nil {
return err
}
Expand Down
46 changes: 46 additions & 0 deletions src/config/unmarshal_strings.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package config

import (
"bytes"
"strings"

"github.com/goccy/go-yaml"
"github.com/jandedobbeleer/aliae/src/shell"
)

func templateUmarshaler(t *shell.Template, b []byte) error {
return stringUmarshaler((*string)(t), b)
}

func stringUmarshaler(s *string, b []byte) error {
if value, OK := unmarshalFoldedBlockScalar(string(b)); OK {
*s = value
return nil
}

decoder := yaml.NewDecoder(bytes.NewBuffer(b))
return decoder.Decode(s)
}

func unmarshalFoldedBlockScalar(value string) (string, bool) {
if !strings.HasPrefix(value, ">") {
return value, false
}

lineBreak := "\n"
if strings.Contains(value, "\r\n") {
lineBreak = "\r\n"
}

value = strings.ReplaceAll(value, ">", "")
value = strings.TrimSpace(value)
splitted := strings.Split(value, lineBreak)

for i, line := range splitted {
splitted[i] = strings.TrimSpace(line)
}

value = strings.Join(splitted, " ")

return value, true
}
39 changes: 39 additions & 0 deletions src/config/unmarshal_strings_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package config

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestUnmarshalStrings(t *testing.T) {
tests := []struct {
name string
input string
expected string
}{
{name: "standard string", input: "test", expected: "test"},
{
name: "folded block scalar",
expected: "one two three",
input: `>
one
two
three`,
},
{
name: "literal block scalar",
expected: `one
two`,
input: `|
one
two`,
},
}

for _, tc := range tests {
var result string
_ = stringUmarshaler(&result, []byte(tc.input))
assert.Equal(t, tc.expected, result)
}
}
Loading