Last updated Jan 2, 2025

Add a deserializer

Why add a deserializer?

  1. You don't need your own custom context plugin
  2. You're using the built-in context plugins (http, file, etc)
  3. The data being consumed by the plugin is in a format that the built-in deserializers don't support

Create a subclass

Sovereign provides a ConfigDeserializer class that can be subclassed.

As an example let's make a msgpack deserializer

1
2
import msgpack
from typing import Any
from sovereign.dynamic_config.deser import ConfigDeserializer


class Msgpack(ConfigDeserializer):
    def deserialize(self, input: Any) -> Any:
        return msgpack.unpackb(input)

Add the entry point to your package

The following script adds your Python module to the list of available deserializers, which Sovereign checks at runtime:

1
2
from setuptools import setup, find_packages

setup(
    name='my_deser_plugin',
    packages=find_packages(),
    entry_points={
        "sovereign.deserializers": [
            "msgpack = my_deser_plugin.deser:Msgpack",
        ]
    }
)

This will install the above Python module into an entry point named sovereign.deser,
with a name of msgpack

Install the module alongside Sovereign

You'll need to install the package on the same machine or container where you've installed sovereign, otherwise it won't be able to access the entry point when it starts.

Run python setup.py install and you should see output similar to the following:

1
2
$ python setup.py install

TODO: show expected output

Adding the plugin to your template context

Using the name specified in the entry points, you can refer to your new plugin in template context:

1
2
template_context:
  context:
    my_api_backend:
      protocol: 'https'
      serialization: 'msgpack'
      path: 'mydata.server.internal'

Rate this page: