Skip to content

Commit a4397c3

Browse files
author
machibuse
committed
Update README to document [Decimal] support for string/StringValue fields
1 parent 705c187 commit a4397c3

1 file changed

Lines changed: 42 additions & 11 deletions

File tree

README.md

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Porticle.Grpc.TypeMapper
22

3-
A Roslyn-based post-processor for protoc-generated files that adds automatic mappings for `Guid`, `Guid?`, `string?` or nullable enums. By simply adding this package and adding
3+
A Roslyn-based post-processor for protoc-generated files that adds automatic mappings for `Guid`, `Guid?`, `decimal`, `decimal?`, `string?` or nullable enums. By simply adding this
4+
package and adding
45
comments to
56
your `.proto` file.
67

@@ -18,19 +19,21 @@ This library adds automatic conversion for:
1819

1920
- Protobuf string to C# Guid
2021
- Protobuf google.Protobuf.StringValue to C# Guid?
22+
- Protobuf string to C# decimal
23+
- Protobuf google.Protobuf.StringValue to C# decimal?
2124
- Protobuf google.Protobuf.StringValue to C# string?
2225
- Protobuf optional enum to C# nullable enum
2326

2427
This Library adds a Roslyn Postprocessing zu the c# files generated by the protoc compiler.
25-
Enabling seamless integration of Guid, Guid?, string?, nullable Enums and `NRT Support` in your gRPC services without manual conversion.
28+
Enabling seamless integration of Guid, Guid?, decimal, decimal?, string?, nullable Enums and `NRT Support` in your gRPC services without manual conversion.
2629
Code.
2730

2831
## TL/DR
2932

3033
- Add `// [GrpcGuid]` as comment to a string or StringValue proto field to get Guid/Guid? in generated c# code
34+
- Add `// [Decimal]` as comment to a string or StringValue proto field to get decimal/decimal? in generated c# code
3135
- Add `// [NullableString]` as comment to a string or StringValue proto field to get string? in generated c# code
3236
- Add `// [NullableEnum]` as comment to an optional enum proto field to get MyEnum? in generated c# code
33-
- Add `// [NullableEnum]` as comment to an optional enum proto field to get MyEnum? in generated c# code
3437
- Add PorticleGrpcTypeMapper_WrapAllNonNullableStrings as Property to your Project to wrap all not nullable proto stings in #nullable enable/disable
3538
- Add PorticleGrpcTypeMapper_WrapAllNullableStrings as Property to your Project to wrap all proto StringValue fields in #nullable enable/disable and change `string` to `string?`
3639

@@ -66,6 +69,8 @@ There are three things you can do in your .proto files:
6669

6770
- Add `// [GrpcGuid]` as comment to a string field - Converts the corresponding c# string property to Guid
6871
- Add `// [GrpcGuid]` as comment to a StringValue field - Converts the corresponding c# string property to Guid?
72+
- Add `// [Decimal]` as comment to a string field - Converts the corresponding c# string property to decimal
73+
- Add `// [Decimal]` as comment to a StringValue field - Converts the corresponding c# string property to decimal?
6974
- Add `// [NullableString]` as comment to a StringValue field - Converts the corresponding c# string property to string?
7075
- Add `// [NullableEnum]` as comment to a optional enum field - Converts the corresponding optional proto enum to a C# nullable Enum
7176

@@ -96,7 +101,10 @@ message User {
96101
// List of roles
97102
repeated string role_ids = 4;
98103
99-
// Simple Enum
104+
// Price of the item
105+
string price = 6;
106+
107+
// Simple Enum
100108
optional TestEnum foo_bar = 5;
101109
}
102110
```
@@ -127,12 +135,18 @@ public pbc::RepeatedField<string> RoleIds {
127135
get { return roleIds_; }
128136
}
129137

138+
/// <summary>Price of the item</summary>
139+
public string Price {
140+
get { return price_; }
141+
set { price_ = pb::ProtoPreconditions.CheckNotNull(value, "value"); }
142+
}
143+
130144
public global::Porticle.Grpc.UnitTests.TestEnum FooBar {
131-
get {
132-
if ((_hasBits0 & 1) != 0)
133-
return fooBar_;
134-
else
135-
return FooBarDefaultValue;
145+
get {
146+
if ((_hasBits0 & 1) != 0)
147+
return fooBar_;
148+
else
149+
return FooBarDefaultValue;
136150
}
137151
set {
138152
_hasBits0 |= 1;
@@ -166,12 +180,15 @@ message User {
166180
// [GrpcGuid] List of roles
167181
repeated string role_ids = 4;
168182
169-
// [NullableEnum] Simple Enum
183+
// [Decimal] Price of the item
184+
string price = 6;
185+
186+
// [NullableEnum] Simple Enum
170187
optional TestEnum foo_bar = 5;
171188
}
172189
```
173190

174-
Will result in generated code like this, using string? Guid and Guid? and a nullable Enum
191+
Will result in generated code like this, using string?, Guid, Guid?, decimal and a nullable Enum
175192

176193
```csharp
177194
/// <summary>[GrpcGuid] Guid of the user object</summary>
@@ -222,6 +239,20 @@ public IList<Guid> RoleIds {
222239
}
223240
}
224241

242+
/// <summary>[Decimal] Price of the item</summary>
243+
public decimal Price {
244+
get
245+
{
246+
// Parse the internal string as decimal using InvariantCulture
247+
return decimal.Parse(price_, System.Globalization.CultureInfo.InvariantCulture);
248+
}
249+
set
250+
{
251+
// Set the internal string from the given decimal
252+
price_ = (value).ToString(System.Globalization.CultureInfo.InvariantCulture);
253+
}
254+
}
255+
225256
public global::Porticle.Grpc.UnitTests.TestEnum? FooBar {
226257
get {
227258
if ((_hasBits0 & 1) != 0)

0 commit comments

Comments
 (0)