Skip to content

Commit a5ae11a

Browse files
fix(config): implement custom string unmarshaler
resolves #210
1 parent 5db47dd commit a5ae11a

File tree

4 files changed

+88
-3
lines changed

4 files changed

+88
-3
lines changed

src/config/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ func getRemoteConfig(url string) (*Aliae, error) {
102102
func parseConfig(data []byte) (*Aliae, error) {
103103
var aliae Aliae
104104

105-
decoder := yaml.NewDecoder(bytes.NewBuffer(data), yaml.CustomUnmarshaler(customUnmarshaler))
105+
decoder := yaml.NewDecoder(bytes.NewBuffer(data), yaml.CustomUnmarshaler(aliaeUnmarshaler))
106106
err := decoder.Decode(&aliae)
107107
if err != nil {
108108
return nil, fmt.Errorf("Failed to parse config file: %s", err)

src/config/unmarshal.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ type StringFunc struct {
2626
Name []byte
2727
}
2828

29-
func customUnmarshaler(a *Aliae, b []byte) error {
29+
func aliaeUnmarshaler(a *Aliae, b []byte) error {
3030
data, err := includeUnmarshaler(b)
3131
if err != nil {
3232
return err
3333
}
3434

35-
decoder := yaml.NewDecoder(bytes.NewBuffer(data))
35+
decoder := yaml.NewDecoder(bytes.NewBuffer(data), yaml.CustomUnmarshaler(templateUmarshaler))
3636
if err = decoder.Decode(a); err != nil {
3737
return err
3838
}

src/config/unmarshal_strings.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package config
2+
3+
import (
4+
"bytes"
5+
"strings"
6+
7+
"github.com/goccy/go-yaml"
8+
"github.com/jandedobbeleer/aliae/src/shell"
9+
)
10+
11+
func templateUmarshaler(t *shell.Template, b []byte) error {
12+
return stringUmarshaler((*string)(t), b)
13+
}
14+
15+
func stringUmarshaler(s *string, b []byte) error {
16+
if value, OK := unmarshalFoldedBlockScalar(string(b)); OK {
17+
*s = value
18+
return nil
19+
}
20+
21+
decoder := yaml.NewDecoder(bytes.NewBuffer(b))
22+
return decoder.Decode(s)
23+
}
24+
25+
func unmarshalFoldedBlockScalar(value string) (string, bool) {
26+
if !strings.HasPrefix(value, ">") {
27+
return value, false
28+
}
29+
30+
lineBreak := "\n"
31+
if strings.Contains(value, "\r\n") {
32+
lineBreak = "\r\n"
33+
}
34+
35+
value = strings.ReplaceAll(value, ">", "")
36+
value = strings.TrimSpace(value)
37+
splitted := strings.Split(value, lineBreak)
38+
39+
for i, line := range splitted {
40+
splitted[i] = strings.TrimSpace(line)
41+
}
42+
43+
value = strings.Join(splitted, " ")
44+
45+
return value, true
46+
}

src/config/unmarshal_strings_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package config
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestUnmarshalStrings(t *testing.T) {
10+
tests := []struct {
11+
name string
12+
input string
13+
expected string
14+
}{
15+
{name: "standard string", input: "test", expected: "test"},
16+
{
17+
name: "folded block scalar",
18+
expected: "one two three",
19+
input: `>
20+
one
21+
two
22+
three`,
23+
},
24+
{
25+
name: "literal block scalar",
26+
expected: `one
27+
two`,
28+
input: `|
29+
one
30+
two`,
31+
},
32+
}
33+
34+
for _, tc := range tests {
35+
var result string
36+
_ = stringUmarshaler(&result, []byte(tc.input))
37+
assert.Equal(t, tc.expected, result)
38+
}
39+
}

0 commit comments

Comments
 (0)