Skip to content

Commit

Permalink
fix(config): implement custom string unmarshaler
Browse files Browse the repository at this point in the history
resolves #210
  • Loading branch information
JanDeDobbeleer committed Dec 31, 2024
1 parent 5db47dd commit a5ae11a
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 3 deletions.
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)
}
}

0 comments on commit a5ae11a

Please sign in to comment.