Skip to content

Commit

Permalink
fallback to empty string message when get nil value
Browse files Browse the repository at this point in the history
  • Loading branch information
LemonNekoGH committed Mar 31, 2024
1 parent 521f196 commit 1b3ae62
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
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 @@ func recGetMessages(raw interface{}, isMapMessage, isInitialCall bool) ([]*Messa
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

0 comments on commit 1b3ae62

Please sign in to comment.