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

ArgumentException when trying to parse po file with double entries #26

Open
FabianFrischmann opened this issue Jan 17, 2025 · 6 comments

Comments

@FabianFrischmann
Copy link

Hi,

I noticed the following behaviour when trying to parse a po file containing the same key multiple times.

If I understood it correctly errors like this should be returned in POParserResult#Diagnostics or?

The current result hoever is the following:

The Lib tries to parse a file containing a key 2 times and throws.

System.ArgumentException: An item with the same key has already been added.
at System.ThrowHelper.ThrowArgumentException(System.ExceptionResource)
at System.Collections.ObjectModel.KeyedCollection<TKey, TItem>.AddKey(TKey, TItem)
at System.Collections.ObjectModel.KeyedCollection<TKey, TItem>.InsertItem(int, TItem)
at Karambolo.PO.POCatalog.InsertItem(int, Karambolo.PO.IPOEntry)
at System.Collections.ObjectModel.Collection.Add(T)
at Karambolo.PO.POParser.Parse(System.IO.TextReader)
at Karambolo.PO.POParserExtensions.Parse(Karambolo.PO.POParser, string)
at GuptaUtils.Extensions.LocalizationExtensions.VocabularyProvider.ParseFile(...) //reading and parsing the file

Is this behaviour intentional or an internal error?


Using version Karambolo.PO.Compact 1.11.1

@FabianFrischmann FabianFrischmann changed the title ArgumentException when trying to parse po file with double entry ArgumentException when trying to parse po file with double entries Jan 17, 2025
@adams85
Copy link
Owner

adams85 commented Jan 17, 2025

Hello @FabianFrischmann,

could you provide an example of input that produces the error when fed to the parser?

@FabianFrischmann
Copy link
Author

Of course,

// a part of my en.po (shortened for a smaller size)
msgid ""
msgstr ""
"Language: en\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=n != 1;\n"
"X-Generator: Weblate 5.7.2\n"

msgid "common.all"
msgstr "All"

msgid "common.all"
msgstr "All"

@adams85
Copy link
Owner

adams85 commented Jan 17, 2025

Thank you. I can confirm that this is unwanted behavior, it should be reported as a diagnostic warning or error. I'm just not sure which one would be the better approach.

That is, when a duplicate key is encountered,

  1. record a warning but continue parsing, or
  2. record an error and interrupt parsing?

I'm leaning towards option 1 because the rest of the file can still contain valid entries, so this allows the consumer to decide whether or not to ignore entries with duplicate keys.

What's your opinion?

@FabianFrischmann
Copy link
Author

Personally I am for Option 1.

A duplicate Entry is not directly an error, the rest of the file might still be valid.

Maybe count it as an error, if the file contains the key multiple times with differend values?

@adams85
Copy link
Owner

adams85 commented Jan 17, 2025

Personally I am for Option 1.

A duplicate Entry is not directly an error, the rest of the file might still be valid.

👍

Maybe count it as an error, if the file contains the key multiple times with differend values?

While this behavior may be somewhat justified, duplicate keys are definitely an indication of incorrect input that the user should deal with. So, maybe a warning is enough in both cases, I wouldn't complicate the parser by tracking values of duplicate entries.

@FabianFrischmann
Copy link
Author

While this behavior may be somewhat justified, duplicate keys are definitely an indication of incorrect input that the user should deal with. So, maybe a warning is enough in both cases, I wouldn't complicate the parser by tracking values of duplicate entries.

you're right

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants