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

Extend the characters allowed for selectors #454

Open
Mirchev98 opened this issue Dec 6, 2024 · 6 comments
Open

Extend the characters allowed for selectors #454

Mirchev98 opened this issue Dec 6, 2024 · 6 comments
Labels

Comments

@Mirchev98
Copy link

Mirchev98 commented Dec 6, 2024

Hello! Using the latest version of SmartFormat, I encountered an issue, where my template cannot be processed when it contains Cyrillic letters.

The code:

using SmartFormat;
namespace TestSmartFormat
{
	public class AttributeData
	{
		public string DisplayText { get; set; }
	}

	public class Document
	{
		public Dictionary<string, AttributeData> ArchiveAttributes { get; set; }
	}

	public class UnifiedDocumentData
	{
		public Document Document { get; set; }
	}

	class Program
	{
		static void Main(string[] args)
		{
			// Initialize sample data with Cyrillic keys
			var data = new UnifiedDocumentData
			{
				Document = new Document
				{
					ArchiveAttributes = new Dictionary<string, AttributeData>
					{
						{ "София", new AttributeData { DisplayText = "София" } },
						{ "Пловдив", new AttributeData { DisplayText = "Пловдив" } }
					}
				}
			};

			// Define templates
			string template1 = "{ArchiveAttributes[София].DisplayText}";
			string template2 = "{ArchiveAttributes[Пловдив].DisplayText}";

			var formatter = Smart.CreateDefaultSmartFormat();

			try
			{
				// Perform formatting
				string result1 = formatter.Format(template1, data.Document);
				string result2 = formatter.Format(template2, data.Document);

				// Display results
				Console.WriteLine($"Template 1 Result: {result1}"); // Expected: София
				Console.WriteLine($"Template 2 Result: {result2}"); // Expected: Пловдив
			}
			catch (Exception ex)
			{
				Console.WriteLine($"Unexpected Error: {ex.Message}");
			}
		}
	}
}

The error:
Unexpected Error: The format string has 5 issues:
'0x421': Invalid character in the selector, '0x43E': Invalid character in the selector, '0x444': Invalid character in the selector, '0x438': Invalid character in the selector, '0x44F': Invalid character in the selector
In: "{ArchiveAttributes[София].DisplayText}"
At: -------------------^^^^^

However, when I change the following part to contain only English, it works perfectly well:

var data = new UnifiedDocumentData
			{
				Document = new Document
				{
					ArchiveAttributes = new Dictionary<string, AttributeData>
					{
						{ "Sofia", new AttributeData { DisplayText = "София" } },
						{ "Plovdiv", new AttributeData { DisplayText = "Пловдив" } }
					}
				}
			};

			// Define templates
			string template1 = "{ArchiveAttributes[Sofia].DisplayText}";
			string template2 = "{ArchiveAttributes[Plovdiv].DisplayText}";

I am unsure if I am missing something, or if it is genuinely a bug.

Thanks in advance for your help!

@axunonb
Copy link
Member

axunonb commented Dec 6, 2024

The default character that are allowed for selectors are "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-". This is by design.
You may, however, add more characters, if needed:

// Create a list of all Cyrillic characters
var cyrillicChars = new List<char>();
for (var c = '\u0400'; c <= '\u04FF'; c++)
{
    cyrillicChars.Add(c);
}
for (var c = '\u0500'; c <= '\u052F'; c++)
{
    cyrillicChars.Add(c);
}

var settings = new SmartSettings();
settings.Parser.AddCustomSelectorChars(cyrillicChars);
var formatter = Smart.CreateDefaultSmartFormat(settings);

With this extension, your sample will work.

Might be an enhancement to remove the valid character set, and just exclude specific reserved characters.

@axunonb axunonb changed the title Error encountering Cyrillic letters Extend the characters allowed for selectors Dec 6, 2024
@axunonb
Copy link
Member

axunonb commented Dec 6, 2024

@karljj1 What do you think about extending the valid characters for selectors in general?

@karljj1
Copy link
Collaborator

karljj1 commented Dec 6, 2024

@karljj1 What do you think about extending the valid characters for selectors in general?

That's a good idea, especially when dealing with localization. Id expect any character to work for a selector except those that have other functions.

@Mirchev98
Copy link
Author

@axunonb That works perfectly well, it solved the issue. Thank you!

@vdachev-david
Copy link

@karljj1 , @axunonb , how about allowing to use literals (e.g. {ArchiveAttributes["Sofia"]} in order to access the dictionary item with key "Sofia")? This way one can access dictionary items with different key types without extending the valid characters.

@axunonb
Copy link
Member

axunonb commented Jan 6, 2025

@vdachev-david Using a numeric index is already implemented, but you mean a string index here{ArchiveAttributes["Sofia"]}?

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

No branches or pull requests

4 participants