ProtobufDataMapper 0.0.4-pre

This is a prerelease version of ProtobufDataMapper.
dotnet add package ProtobufDataMapper --version 0.0.4-pre                
NuGet\Install-Package ProtobufDataMapper -Version 0.0.4-pre                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="ProtobufDataMapper" Version="0.0.4-pre" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ProtobufDataMapper --version 0.0.4-pre                
#r "nuget: ProtobufDataMapper, 0.0.4-pre"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install ProtobufDataMapper as a Cake Addin
#addin nuget:?package=ProtobufDataMapper&version=0.0.4-pre&prerelease

// Install ProtobufDataMapper as a Cake Tool
#tool nuget:?package=ProtobufDataMapper&version=0.0.4-pre&prerelease                

PDM: Protobuf Data Mapper

PDM is a library for mapping and transforming one Protobuf message into another.

It was initially hallucinated by ChatGPT as a Python library, but this implementation is in C#.

Installation

You can install PDM via NuGet:

Install-Package ProtobufDataMapper

Or via .NET CLI:

dotnet add package ProtobufDataMapper

Usage

Here's a basic example of how to use PDM:

Sample Code

public byte[] Transform(byte[] sourceMessage)
{
	// Transforms from the supplied sourceMessage which 
	// is created from the ThreeFields protobuf and arrives in 
	// ProtoBuf wire format into a TwoFields type message which
	// is returned in ProtoBuf wire format.

	var targetMapping = new List<PDM.Entities.Transformation>()
		{
			{ new PDM.Entities.Transformation() // Remove field 10 from the output
				{
					TransformationType = PDM.Enums.TransformationType.ReplaceField,
					SubType = "blacklist",
					Value = "10"
				}
			}
		};

	var mapper = new PDM.ProtobufMapper(targetMapping);
	return await mapper.MapAsync(sourceMessage);
}

This code is based on the following ProtoBuf file definitions:

Source Message

message ThreeFields {
    string StringValue = 5;
    float FloatValue = 10;
    int32 IntegerValue = 15;
}

Target Message

message TwoFields {
    string StringValue = 5;
    int32 IntegerValue = 15;
}

If no transformations are supplied, all fields are copied directly to the output. Thus, if no changes are needed and the source message is to be simply copied to the target, no Transformations are required. In this example the target message only requires 2 of the 3 fields, so we use the ReplaceField.blacklist transform to remove the unwanted field from the output while still copying the other 2 fields directly to the target.

Target mappings can be supplied by configuration allowing the same codebase to be reconfigured and used for various transformations as needed.

Json Configuration

{
	"Transformations": [
		{
			"TransformationType": "InsertField",
			"SubType": "include",
			"Value": "0"
		},

		{
			"TransformationType": "ReplaceField",
			"SubType": "renames",
			"Value": "3000:5"
		},

		{
			"TransformationType": "ReplaceField",
			"SubType": "renames",
			"Value": "4200:10"
		},

		{
			"TransformationType": "InsertField",
			"SubType": "static",
			"Value": "15:VarInt:173559425"
		},
	]
}

In your application code, you can dynamically transform protobuf messages by injecting an IConfiguration instance, and then building a ProtobufMapper with the specified configurations.

public class MyTransformer
{
	private readonly ProtobufMapper mapper;

	public MyTransformer(IConfiguration configuration)
	{
		var transformations = configuration?.BuildTransformations("Transformations") ?? throw new ArgumentNullException(configuration);
		this.mapper = new ProtobufMapper(transformations);
	}

	public async Task<byte[]> TransformMessage(byte[] sourceMessageBytes)
	{
		var resultMessageBytes = await this.mapper.MapAsync(sourceMessageBytes);
		return resultMessageBytes;
	}
}

Contributing

Contributions are welcome! If you have an idea for a new feature or find a bug, please open an issue. If you'd like to contribute code, please fork the repository and submit a pull request.

Before submitting a pull request, please ensure that your code passes the existing unit tests and that any new code is adequately tested. To run the tests locally, run:

License

PDM is licensed under the GNU Affero General Public License v3.0. See LICENSE for more information.

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.0.4-pre 104 3/18/2023
0.0.4-dev 89 3/18/2023
0.0.3-dev 86 3/10/2023
0.0.2-dev 84 3/6/2023
0.0.1-dev 81 3/6/2023