Added Unescape method to Entry

This commit is contained in:
Sasha Koshka 2024-04-28 12:46:22 -04:00
parent 73ea9d304b
commit bdbb90fd32
1 changed files with 20 additions and 0 deletions

View File

@ -250,6 +250,24 @@ func (entry Entry) Localize (locale locale.Locale) string {
return entry.Value
}
// Unescape returns a new copy of this entry with its main value parsed and
// unescaped as a string, and its localized values parsed and unescaped as
// localestrings.
func (entry Entry) Unescape () (Entry, error) {
value, err := ParseString(entry.Value)
if err != nil { return Entry { }, err }
localizedValue := Entry {
Value: value,
Localized: make(map[locale.Locale] string),
}
for name, localized := range entry.Localized {
unescaped, err := ParseLocaleString(localized)
if err != nil { return Entry { }, err }
localizedValue.Localized[name] = unescaped
}
return localizedValue, nil
}
// TODO have functions to parse/validate all data types
// ParseString parses a value of type string.
@ -269,6 +287,8 @@ func ParseString (value string) (string, error) {
// The escape sequences \s, \n, \t, \r, and \\ are supported, meaning ASCII
// space, newline, tab, carriage return, and backslash, respectively.
func ParseLocaleString (value string) (string, error) {
value, err := escapeString(value)
if err != nil { return "", err }
return value, nil
}