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

fallback to empty string message "" when get nil value #326

Merged
merged 1 commit into from
Mar 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 i18n/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ func stringSubmap(k string, v interface{}, strdata map[string]string) error {
func isMessage(v interface{}) bool {
reservedKeys := []string{"id", "description", "hash", "leftdelim", "rightdelim", "zero", "one", "two", "few", "many", "other"}
switch data := v.(type) {
case string:
case nil, string:
return true
case map[string]interface{}:
for _, key := range reservedKeys {
Expand Down
7 changes: 7 additions & 0 deletions i18n/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,13 @@
messages = append(messages, childMessages...)
}

case nil:
if isInitialCall {
return nil, errInvalidTranslationFile
}

Check warning on line 118 in i18n/parse.go

View check run for this annotation

Codecov / codecov/patch

i18n/parse.go#L117-L118

Added lines #L117 - L118 were not covered by tests
m, err := NewMessage("")
return []*Message{m}, err

default:
return nil, fmt.Errorf("unsupported file format %T", raw)
}
Expand Down
31 changes: 31 additions & 0 deletions i18n/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,37 @@ outer:
}},
},
},
{
name: "YAML empty key test",
file: `
some-keys:
non-empty-key: not empty
empty-key-but-type-specified: ""
empty-key:
null-key: null`,
path: "en.yaml",
unmarshalFuncs: map[string]UnmarshalFunc{"yaml": yaml.Unmarshal},
messageFile: &MessageFile{
Path: "en.yaml",
Tag: language.English,
Format: "yaml",
Messages: []*Message{
{
ID: "some-keys.non-empty-key",
Other: "not empty",
},
{
ID: "some-keys.empty-key-but-type-specified",
},
{
ID: "some-keys.empty-key",
},
{
ID: "some-keys.null-key",
},
},
},
},
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
Expand Down
Loading