https://github.com/machibuse/porticle.grpc.guidmapper
User nullable String and Guid properties with GRPC in c#
https://github.com/machibuse/porticle.grpc.guidmapper
csharp grpc guid nullable-reference-types protobuf
Last synced: 7 months ago
JSON representation
User nullable String and Guid properties with GRPC in c#
- Host: GitHub
- URL: https://github.com/machibuse/porticle.grpc.guidmapper
- Owner: Machibuse
- License: mit
- Created: 2025-07-06T12:14:12.000Z (7 months ago)
- Default Branch: main
- Last Pushed: 2025-07-15T22:07:41.000Z (7 months ago)
- Last Synced: 2025-07-16T21:05:29.155Z (7 months ago)
- Topics: csharp, grpc, guid, nullable-reference-types, protobuf
- Language: C#
- Homepage:
- Size: 25.4 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Porticle.Grpc.GuidMapper
A Roslyn-based post-processor for protoc-generated files that adds native Guid, Guid? and string? types in gRPC services.
## Build State
[](https://github.com/Machibuse/Porticle.CLDR/actions/workflows/release.yaml)
## Nuget
[](https://www.nuget.org/packages/Porticle.Grpc.GuidMapper/) [](https://www.nuget.org/packages/Porticle.CLDR.Units/)
## Overview
This library allows you to automatically convert PROTO string or StringValue fields to C# Guid, Guid? or string? types in protoc-generated files, enabling seamless integration of
GUIDs in your gRPC services without manual conversion
code.
## Installation
### Install the package via NuGet:
```powershell
dotnet add package Porticle.Grpc.GuidMapper
```
After installing the Package, this Post build step ist dynamically added to your build.
```msbuild
<_FilesToPostProcess Include="$(MSBuildProjectDirectory)\%(Protobuf_Compile.OutputDir)\%(Protobuf_Compile.Filename)" />
```
Dont wonder ist you cant se it in your csproj file. It is dynamically added when your build is processed.
## Usage
There are three things you can do in your .proto files:
- Add `// [GrpcGuid]` as comment to a string field - Converts the corresponding c# string property to Guid
- Add `// [GrpcGuid]` as comment to a StringValue field - Converts the corresponding c# string property to Guid?
- Add `// [NullableString]` as comment to a StringValue field - Converts the corresponding c# string property to string?
First an Example of a default .proto file
### Without GuidMapper
```protobuf
syntax = "proto3";
import "google/protobuf/wrappers.proto";
message User {
// Guid of the user object
string id = 1;
// Optional parent UserId
google.protobuf.StringValue optional_parent_user_id = 2;
// Optional description
google.protobuf.StringValue description = 3;
// List of roles
repeated string role_ids = 4;
}
```
Will result in generated code like this, everything is a string
```csharp
/// Guid of the user object
public string Id {
get { return id_; }
set { id_ = pb::ProtoPreconditions.CheckNotNull(value, "value"); }
}
/// Optional Guid of the parent UserId
public string OptionalParentUserId {
get { return optionalParentUserId_; }
set { optionalParentUserId_ = value; }
}
/// Optional description string
public string Description {
get { return description_; }
set { description_ = value; }
}
/// List of roles
public pbc::RepeatedField RoleIds {
get { return roleIds_; }
}
```
### With GuidMapper
```protobuf
syntax = "proto3";
import "google/protobuf/wrappers.proto";
message User {
// [GrpcGuid] Guid of the user object
string id = 1;
// [GrpcGuid] Optional Guid of the parent UserId
google.protobuf.StringValue optional_parent_user_id = 2;
// [NullableString] Optional description string
google.protobuf.StringValue description = 3;
// [GrpcGuid] List of roles
repeated string role_ids = 4;
}
```
Will result in generated code like this, using string? Guid and Guid?
```csharp
/// [GrpcGuid] Guid of the user object
public global::System.Guid Id {
get { return global::System.Guid.Parse(id_); }
set { id_ = (value).ToString("D"); }
}
/// [GrpcGuid] Optional Guid of the parent UserId
public global::System.Guid? OptionalParentUserId {
get { if(optionalParentUserId_==null) return default; return global::System.Guid.Parse(optionalParentUserId_); }
set { optionalParentUserId_ = (value)?.ToString("D"); }
}
#nullable enable
/// [NullableString] Optional description string
public string? Description {
get { return description_; }
set { description_ = value; }
}
#nullable disable
/// [GrpcGuid] List of roles
public IList RoleIds {
get {return new RepeatedFieldGuidWrapper(roleIds_); }
}
```
## What currently is not Possible?
- Mapping `repeated google.protobuf.StringValue` to `List` or `List` because grpc internally uses `RepeatedField` instead of `RepeatedField`.
This may be a bug in protoc compiler, because it is also not possible to add `null` to `repeated google.protobuf.StringValue` because ther is a not null check in the Add function in `RepeatedField`
- This Tool actually don't works when protoc / Grpc.Tools is compiled with GOOGLE_PROTOBUF_REFSTRUCT_COMPATIBILITY_MODE