You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add support for [DateTime] and [DateTimeOffset] transformations in PropertyVisitor, update proto definitions, extend unit tests, and update documentation for Timestamp handling
Copy file name to clipboardExpand all lines: CLAUDE.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,7 +4,7 @@
4
4
5
5
Porticle.Grpc.TypeMapper is a Roslyn-based post-processor shipped as a NuGet development dependency. It hooks into the build pipeline after `Protobuf_Compile` and rewrites protoc-generated C# classes to add support for Guids, decimals, nullable strings, and nullable enums — features not natively supported by Protocol Buffers.
6
6
7
-
Proto field comments (`[GrpcGuid]`, `[Decimal]`, `[NullableString]`, `[NullableEnum]`) or global project properties control which transformations are applied.
7
+
Proto field comments (`[GrpcGuid]`, `[Decimal]`, `[DateTime]`, `[DateTimeOffset]`, `[NullableString]`, `[NullableEnum]`) or global project properties control which transformations are applied.
Copy file name to clipboardExpand all lines: README.md
+61-1Lines changed: 61 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,7 @@
1
1
# Porticle.Grpc.TypeMapper
2
2
3
-
A Roslyn-based post-processor for protoc-generated files that adds automatic mappings for `Guid`, `Guid?`, `decimal`, `decimal?`, `string?`, nullable enums and nullable reference
3
+
A Roslyn-based post-processor for protoc-generated files that adds automatic mappings for `Guid`, `Guid?`, `decimal`, `decimal?`, `DateTime?`, `DateTimeOffset?`, `string?`,
4
+
nullable enums and nullable reference
4
5
types. By simply adding this
5
6
package and adding
6
7
comments to
@@ -22,6 +23,8 @@ This library adds automatic conversion for:
22
23
- Protobuf google.Protobuf.StringValue to C# Guid?
23
24
- Protobuf string to C# decimal
24
25
- Protobuf google.Protobuf.StringValue to C# decimal?
26
+
- Protobuf google.protobuf.Timestamp to C# DateTime?
27
+
- Protobuf google.protobuf.Timestamp to C# DateTimeOffset?
25
28
- Protobuf google.Protobuf.StringValue to C# string?
26
29
- Protobuf optional enum to C# nullable enum
27
30
- Full nullable reference type support per message via `[NullableReferenceTypes]`
@@ -34,6 +37,8 @@ Code.
34
37
35
38
- Add `// [GrpcGuid]` as comment to a string or StringValue proto field to get Guid/Guid? in generated c# code
36
39
- Add `// [Decimal]` as comment to a string or StringValue proto field to get decimal/decimal? in generated c# code
40
+
- Add `// [DateTime]` as comment to a google.protobuf.Timestamp proto field to get DateTime? in generated c# code (UTC only - assigning a non-UTC DateTime throws)
41
+
- Add `// [DateTimeOffset]` as comment to a google.protobuf.Timestamp proto field to get DateTimeOffset? in generated c# code
37
42
- Add `// [NullableString]` as comment to a string or StringValue proto field to get string? in generated c# code
38
43
- Add `// [NullableEnum]` as comment to an optional enum proto field to get MyEnum? in generated c# code
39
44
- Add `// [NullableReferenceTypes]` as comment above a message to wrap all reference type properties with `#nullable enable/disable` and make nullable ones `string?` / `TypeName?`
@@ -74,6 +79,8 @@ There are several things you can do in your .proto files:
74
79
- Add `// [GrpcGuid]` as comment to a StringValue field - Converts the corresponding c# string property to Guid?
75
80
- Add `// [Decimal]` as comment to a string field - Converts the corresponding c# string property to decimal
76
81
- Add `// [Decimal]` as comment to a StringValue field - Converts the corresponding c# string property to decimal?
82
+
- Add `// [DateTime]` as comment to a google.protobuf.Timestamp field - Converts the corresponding c# Timestamp property to DateTime?
83
+
- Add `// [DateTimeOffset]` as comment to a google.protobuf.Timestamp field - Converts the corresponding c# Timestamp property to DateTimeOffset?
77
84
- Add `// [NullableString]` as comment to a StringValue field - Converts the corresponding c# string property to string?
78
85
- Add `// [NullableEnum]` as comment to a optional enum field - Converts the corresponding optional proto enum to a C# nullable Enum
79
86
- Add `// [NullableReferenceTypes]` as comment above a message definition - Enables full nullable reference type support for the entire message (see below)
@@ -284,6 +291,59 @@ public global::Porticle.Grpc.UnitTests.TestEnum? FooBar {
Add `// [DateTime]` or `// [DateTimeOffset]` as comment to a `google.protobuf.Timestamp` field to get a `DateTime?` or `DateTimeOffset?` property in the generated C# code.
297
+
Since message fields are always optional in proto3, the resulting property is always nullable.
298
+
299
+
The conversion uses the original functions provided by Google.Protobuf: `Timestamp.FromDateTime`/`ToDateTime` and `Timestamp.FromDateTimeOffset`/`ToDateTimeOffset`.
300
+
301
+
**Note:**`Timestamp.FromDateTime` only accepts `DateTime` values with `DateTimeKind.Utc`. Assigning a `DateTime` with kind `Local` or `Unspecified` throws an `ArgumentException`.
302
+
`DateTimeOffset` values can have any offset - they are converted to UTC internally, so reading the property back always returns offset zero.
303
+
304
+
```protobuf
305
+
syntax = "proto3";
306
+
307
+
import "google/protobuf/timestamp.proto";
308
+
309
+
message Order {
310
+
// [DateTime] Creation time (UTC)
311
+
google.protobuf.Timestamp created_at = 1;
312
+
313
+
// [DateTimeOffset] Delivery time
314
+
google.protobuf.Timestamp delivered_at = 2;
315
+
}
316
+
```
317
+
318
+
Will result in generated code like this:
319
+
320
+
```csharp
321
+
/// <summary>[DateTime] Creation time (UTC)</summary>
322
+
publicglobal::System.DateTime?CreatedAt {
323
+
get {
324
+
// return null when the underlying Timestamp is null
325
+
if (createdAt_==null) returndefault;
326
+
// convert using the original Google.Protobuf function
327
+
returncreatedAt_.ToDateTime();
328
+
}
329
+
set {
330
+
// Timestamp.FromDateTime throws an ArgumentException when value.Kind is not Utc
Add `// [NullableReferenceTypes]` as a comment above a `message` definition to automatically apply nullable reference type annotations to **all** properties in that message:
0 commit comments