# Best way to store key-value pairs in plugin settings?

**URL:** <https://meta.discourse.org/t/best-way-to-store-key-value-pairs-in-plugin-settings/68059>\
**Category:** Development\
**Created:** [August 15, 2017, 6:47am UTC](https://meta.discourse.org/t/best-way-to-store-key-value-pairs-in-plugin-settings/68059 "2017-08-15T06:47:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![john-lynch](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/john-lynch/32/81514_2.png) [@john-lynch](https://meta.discourse.org/u/john-lynch)\
**Post date:** [August 15, 2017, 6:47am UTC](https://meta.discourse.org/t/best-way-to-store-key-value-pairs-in-plugin-settings/68059/1 "2017-08-15T06:47:58Z")

</div>

I’m working on a very simple plugin to perform simple textual replacements in a Discourse post. Specifically, when a user types `/keyword` in a post, I want to replace that sequence with an arbitrary string (in this case, a kaomoji, so that I can map `/shrug` to `¯\_(ツ)_/¯`). Yes, it’s very silly.

In any case, the plugin is mostly complete: I can detect the pattern and perform the replacement in my dev environment. However, the mapping from keyword to kaomoji is currently specified as a Javascript literal in the plugin’s discourse-markdown Javascript file. That’s not great.

I’d like to be able to define the mapping of keywords to kaomoji (or whatever) in the plugin’s settings so that I don’t have to modify a server-side Javascript file every time I want to change what mappings are allowed. I cannot find a good way to do this. The closest I’ve come is using a “list” setting (with values like `shrug:¯\_(ツ)_/¯`, but that gives me back a string of `|` seperated strings, which is problematic when working with values like kaomoji that are highly likely to contain `|` in them.

I could hack around this by disallowing `|` in the mapping and splitting the list myself, but before I do, I thought I’d ask if there were a better way than “list” to do what I’m talking about. Is there any reasonable way to create and edit set of key-value pairs through a plugin’s settings? What’s the best way to approach this for both ease of editing and maximum permissiveness in the allowed characters?

---

<div class="post-metadata">

**Author:** ![john-lynch](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/john-lynch/32/81514_2.png) [@john-lynch](https://meta.discourse.org/u/john-lynch)\
**Post date:** [August 15, 2017, 2:07pm UTC](https://meta.discourse.org/t/best-way-to-store-key-value-pairs-in-plugin-settings/68059/2 "2017-08-15T14:07:26Z")

</div>

Overnight it occurred to me that I could also simply use (for example) a JSON string that represented the mapping, then parse that string in the plugin. A JSON string would have the advantage of having escape logic for special characters built in. To really make this work though, you’d want a multiline text box to edit the setting, and [the one post I found](https://meta.discourse.org/t/multi-line-settings-is-it-possible/21433) while searching for this functionality simply recommended a “list” (which was indeed appropriate for the use case in question).

Anyway, some form of first class key-value pair support would be ideal, but this seems like the second-best option. Is a multiline text box possible?

---

<div class="post-metadata">

**Author:** ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)\
**Post date:** [August 15, 2017, 2:21pm UTC](https://meta.discourse.org/t/best-way-to-store-key-value-pairs-in-plugin-settings/68059/3 "2017-08-15T14:21:51Z")

</div>

There is first class key-value see: PluginStore, example here: [https://meta.discourse.org/t/brand-new-plugin-interface/8793/29](https://meta.discourse.org/t/brand-new-plugin-interface/8793/29)

But, if you went this path you would also need to build admin UI involving a controller and ember view to handle data entry into it, also you would need to figure out how to ship the information to the client. Totally doable but a pretty large amount of work.

---

<div class="post-metadata">

**Author:** ![john-lynch](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/john-lynch/32/81514_2.png) [@john-lynch](https://meta.discourse.org/u/john-lynch)\
**Post date:** [August 17, 2017, 6:39am UTC](https://meta.discourse.org/t/best-way-to-store-key-value-pairs-in-plugin-settings/68059/4 "2017-08-17T06:39:24Z")

</div>

I wanted to get an initial version out the door, so I have deferred going with this heavy-weight route, and opted for having the user enter a string of JSON to configure the supported kaomoji replacements. It’s clunky, but it works.

In any case, I have the plugin live on my server now. You can find it [here](https://github.com/john-lynch/discourse-kaomoji). I welcome any feedback. I’m a web, Rails, and Discourse newb, so I’m eager to learn. I did my best to ape the practices of other popular plugins. Thanks for your help.
