andywilsonuk / StringTokenFormatter

Provides string extension methods for replacing tokens within strings (using the format '{name}') with their specified lookup value.
Apache License 2.0
35 stars 1 forks source link

Feature request: Add a function that will return tokens in string #4

Closed DanAvni closed 6 years ago

DanAvni commented 6 years ago

I am going to build a large template engine that will have a lot of possible objects the template can reference. Since I do not know the template at compile time, I want to optimize the dictionary of tokens I build to be filled with the required tokens so I do not waste time building tokens that will not be used. Please add a method in the TokenReplacer class that will return a list of detected tokens. That way I can scan the list and build the token dictionary as needed

andywilsonuk commented 6 years ago

Hi Dan,

I like the idea of this however I'd like to clarify something first and that is the reason you can't use the technique in Usage 4 (the passing in a function-based approach) is because you want to pass the list of used tokens to a networked datastore (database etc), is that assumption correct?

If that assumption is correct you could work around the problem by writing a function that builds a list.

Andy

DanAvni commented 6 years ago

I'mnot sure I understood what is Usage 4. I am going to have a few functions each can fill my token dictionary with properties of specific objects. All I am looking for is saving CPU cycles building up the dictionary for properties of items no one references. for example when my user enters the template text I could scan it and remember that this template contains reference to the task and to a task entry, then when filling up the token dictionary I could fill it up with just these tokens and skip unused tokens (e.g. Customer tokens)

andywilsonuk commented 6 years ago

OK, with Usage 4 I think you should be able to do this to immediately solve your problem:

var usedTokens = new List(); string original = "start {middle} end"; Func<string, object> func = (token) => { usedTokens.Add(token); return string.Empty; }; string result = original.FormatToken(func);

Andy

DanAvni commented 6 years ago

@andywilsonuk , I tried your sample code (nice idea BTW) but it does not work. The list returned in your sample is empty. I could write a class that will implement IDictionary and that would probably work but it seems like an overkill for such a simple task.

Usage 4 only works when calling with a specific token name and does not work when I omit the token name

andywilsonuk commented 6 years ago

Ah yes, I see why it doesn't work but I'm not really happy that it doesn't. I want to implement the ability to get a list of tokens found however the code needs an upgrade to do that. I've started to plan out what that might look like and I'll put some details into issue #5 which goes into more details about other use-cases that can be supported as part of the improvements.

I'll leave this issue open until I have implemented the Get a list of used tokens functional.

andywilsonuk commented 6 years ago

I've added a method to 2.0.6 that allows you to get the list of matched tokens; you can use it like this:

DefaultTokenMatcher matcher = new DefaultTokenMatcher(); string input = "{a}, {b}"; var matched = matcher.MatchedTokens(input);

I think that provides the functionality you need.

DanAvni commented 6 years ago

Thanks! will check it next week

DanAvni commented 6 years ago

Works great! thank you