Protobuf module for panamap
Project description
panamap-proto
Panamap-proto adds protobuf support to panamap object mapper.
Installation
Use the package manager pip to install panamap-proto.
pip install panamap
Usage
Initialize mapper
To enabe protobuf support you must pass ProtoMappingDescriptor
in list to custom_descriptors
parameter to Mapper
constructor:
from panamap import Mapper
from panamap_proto import ProtoMappingDescriptor
mapper = Mapper(custom_descriptors=[ProtoMappingDescriptor])
After that you can set up mapping for protobuf generated classes.
Map classes
To map message Simple
:
// messages.proto
syntax = "proto3";
message Simple {
string value = 1;
}
You should use folowing configuration:
from panamap import Mapper
from panamap_proto import ProtoMappingDescriptor
from messages_pb2 import Simple
mapper = Mapper(custom_descriptors=[ProtoMappingDescriptor])
mapper.mapping(Simple, SimpleData).map_matching().register()
s = mapper.map(SimpleData("abc"), Simple)
print(s.value)
# 'abc'
Map enums
There is many ways to map python enums to protobuf generated enums, but the easiest way is to use values_map
utility method:
// messages.proto
enum Lang {
PYTHON = 0;
CPP = 1;
JAVA = 2;
}
from enum import Enum
from panamap import Mapper, values_map
from panamap_proto import ProtoMappingDescriptor
from messages_pb2 import Lang
class PyLang(Enum):
PYTHON = 1
CPP = 2
JAVA = 3
mapper = Mapper(custom_descriptors=[ProtoMappingDescriptor])
pairs = [
(PyLang.PYTHON, Lang.Value("PYTHON")),
(PyLang.JAVA, Lang.Value("JAVA")),
(PyLang.CPP, Lang.Value("CPP")),
]
mapper.mapping(PyLang, Lang) \
.l_to_r_converter(values_map({py: proto for py, proto in pairs})) \
.r_to_l_converter(values_map({proto: py for py, proto in pairs})) \
.register()
Contributing
Contributing described in separate document.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
panamap-proto-1.1.1.tar.gz
(3.5 kB
view details)
File details
Details for the file panamap-proto-1.1.1.tar.gz
.
File metadata
- Download URL: panamap-proto-1.1.1.tar.gz
- Upload date:
- Size: 3.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.7.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7558391741b1eaeba09249c34adf2be3a9c7c5b4377847cf5ddf769f9f27e972 |
|
MD5 | 6c956e4d7ab7ba1384882b0b40e642ce |
|
BLAKE2b-256 | 4e727301123364307473d38b49101d75767aea0617b26371d0790c976150da48 |