-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(config): implement custom string unmarshaler
resolves #210
- Loading branch information
1 parent
5db47dd
commit a5ae11a
Showing
4 changed files
with
88 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |