fix: make the data-model data#457
Conversation
|
Remove the use of `Map` from data-model, to enable using pre-parsed messages.
34822b7 to
0b72b88
Compare
|
Could you explain a bit more how the current approach is making things difficult for you, preferably with some code examples? It seems like this might be best considered first via an issue, rather than jumping directly into an implementation. |
|
I wasn't sure how to best approach this, it's a learning experience for me. I will make a mental note, and next time you'll see an issue before implementation. I parse and validate my messages as part of my build pipeline and collect and output the result as data in JSON files. Example message Gets turned into {
"type": "message",
"declarations": [
{
"type": "input",
"name": "name",
"value": {
"type": "expression",
"arg": { "type": "variable", "name": "name" },
"functionRef": { "type": "function", "name": "string" }
}
},
{
"type": "input",
"name": "when",
"value": {
"type": "expression",
"arg": { "type": "variable", "name": "when" },
"functionRef": {
"type": "function",
"name": "datetime",
"options": {
"dateStyle": { "type": "literal", "value": "short" },
"timeStyle": { "type": "literal", "value": "short" }
}
}
}
}
],
"pattern": [
"Hi ",
{ "type": "expression", "arg": { "type": "variable", "name": "name" } },
"! - ",
{ "type": "expression", "arg": { "type": "variable", "name": "when" } }
]
}Then I use this in my application (pseudo-code): import hi from './hi.json';
const msg = new MessageFormat('en', hi);
const greeting = msg.format({ name: 'Client', when: '1930-05-31T17:35:00' });The problem I run into is as part of Hopefully my explanation added the missing context / meaning. Cheers, |
|
Splitting the parsing like that isn't something that's been considered very deeply here, as the original design of the library is meant to act as a polyfill for a native-code Using Objects rather than Maps would also match the spec's The question then becomes, is there a reason to support both Maps and Objects, or only Objects? At the very least, we should make sure that prototype pollution is not an issue. |
|
If prototype pollution is handled, I don't see a reason to also support If I wanted to add a short helper function in order to safely get a own-property from an object, you have any suggestion where to put it, or you think it's something that can be handled inline for each case? Something like: const getOwn = <T extends object>(object: T | undefined, name: keyof T) =>
object && Object.hasOwn(object, name) ? object[name] : undefined;Any and all input greatly appreciated, and thanks for such quick responses. |
eemeli
left a comment
There was a problem hiding this comment.
After a bit more consideration, I think this is indeed a good change. I've added some comments inline; most significant is making sure that when replacing a .get() we don't look into the prototype chain unless we're sure to have created the object ourselves, or if anything being present there would be an error in any case.
Co-authored-by: Eemeli Aro <eemeli@gmail.com>
|
@fatbrain Looks like a Prettier formatter run is needed? |
Remove the use of
Mapfrom data-model, to enable using pre-parsed messages. Removing the need for parsing while running my application.