@@ -1056,9 +1056,9 @@ internal static void ModuleInit()
1056
1056
/// <param name=""obj"">Object to covert.</param>
1057
1057
/// <returns>The <see cref=""ReferenceField""/> of provided <paramref name=""obj""/> or <c>default</c> if <paramref name=""obj""/> is <c>null</c>.</returns>
1058
1058
[return: global::System.Diagnostics.CodeAnalysis.NotNullIfNotNull(""obj"")]
1059
- public static implicit operator string?(global::Thinktecture.Tests.TestValueObject obj)
1059
+ public static implicit operator string?(global::Thinktecture.Tests.TestValueObject? obj)
1060
1060
{
1061
- return obj.ReferenceField;
1061
+ return obj? .ReferenceField;
1062
1062
}
1063
1063
1064
1064
/// <summary>
@@ -1151,6 +1151,220 @@ public int CompareTo(global::Thinktecture.Tests.TestValueObject obj)
1151
1151
" ) ;
1152
1152
}
1153
1153
1154
+ [ Fact ]
1155
+ public void Should_generate_struct_with_int_key_member ( )
1156
+ {
1157
+ var source = @"
1158
+ using System;
1159
+ using Thinktecture;
1160
+
1161
+ namespace Thinktecture.Tests
1162
+ {
1163
+ [ValueObject]
1164
+ public readonly partial struct TestValueObject
1165
+ {
1166
+ public readonly int StructField;
1167
+ }
1168
+ }
1169
+ " ;
1170
+ var output = GetGeneratedOutput < ValueObjectSourceGenerator > ( source , typeof ( ValueObjectAttribute ) . Assembly ) ;
1171
+ AssertOutput ( output , _GENERATED_HEADER + @"
1172
+ namespace Thinktecture.Tests
1173
+ {
1174
+ public class TestValueObject_ValueObjectTypeConverter : global::Thinktecture.ValueObjectTypeConverter<global::Thinktecture.Tests.TestValueObject, int>
1175
+ {
1176
+ /// <inheritdoc />
1177
+ protected override global::Thinktecture.Tests.TestValueObject ConvertFrom(int structField)
1178
+ {
1179
+ return global::Thinktecture.Tests.TestValueObject.Create(structField);
1180
+ }
1181
+
1182
+ /// <inheritdoc />
1183
+ protected override int GetKeyValue(global::Thinktecture.Tests.TestValueObject obj)
1184
+ {
1185
+ return (int) obj;
1186
+ }
1187
+ }
1188
+
1189
+ [global::Thinktecture.Internal.ValueObjectConstructor(nameof(StructField))]
1190
+ [global::Thinktecture.Internal.KeyedValueObject]
1191
+ [global::System.ComponentModel.TypeConverter(typeof(global::Thinktecture.Tests.TestValueObject_ValueObjectTypeConverter))]
1192
+ partial struct TestValueObject : global::System.IEquatable<global::Thinktecture.Tests.TestValueObject>, global::System.IFormattable, global::System.IComparable, global::System.IComparable<global::Thinktecture.Tests.TestValueObject>
1193
+ {
1194
+ [global::System.Runtime.CompilerServices.ModuleInitializer]
1195
+ internal static void ModuleInit()
1196
+ {
1197
+ var convertFromKey = new global::System.Func<int, global::Thinktecture.Tests.TestValueObject>(global::Thinktecture.Tests.TestValueObject.Create);
1198
+ global::System.Linq.Expressions.Expression<global::System.Func<int, global::Thinktecture.Tests.TestValueObject>> convertFromKeyExpression = static structField => global::Thinktecture.Tests.TestValueObject.Create(structField);
1199
+ global::System.Linq.Expressions.Expression<global::System.Func<int, global::Thinktecture.Tests.TestValueObject>> convertFromKeyExpressionViaCtor = static structField => new global::Thinktecture.Tests.TestValueObject(structField);
1200
+
1201
+ var convertToKey = new global::System.Func<global::Thinktecture.Tests.TestValueObject, int>(static item => item.StructField);
1202
+ global::System.Linq.Expressions.Expression<global::System.Func<global::Thinktecture.Tests.TestValueObject, int>> convertToKeyExpression = static obj => obj.StructField;
1203
+
1204
+ var tryCreate = new global::Thinktecture.Internal.Validate<global::Thinktecture.Tests.TestValueObject, int>(global::Thinktecture.Tests.TestValueObject.TryCreate);
1205
+
1206
+ var type = typeof(global::Thinktecture.Tests.TestValueObject);
1207
+ var metadata = new global::Thinktecture.Internal.ValueObjectMetadata(type, typeof(int), false, false, convertFromKey, convertFromKeyExpression, convertFromKeyExpressionViaCtor, convertToKey, convertToKeyExpression, tryCreate);
1208
+
1209
+ global::Thinktecture.Internal.ValueObjectMetadataLookup.AddMetadata(type, metadata);
1210
+ }
1211
+
1212
+ private static readonly global::System.Type _type = typeof(global::Thinktecture.Tests.TestValueObject);
1213
+
1214
+ public static readonly global::Thinktecture.Tests.TestValueObject Empty = default;
1215
+
1216
+ public static global::Thinktecture.Tests.TestValueObject Create(int structField)
1217
+ {
1218
+ var validationResult = global::System.ComponentModel.DataAnnotations.ValidationResult.Success;
1219
+ ValidateFactoryArguments(ref validationResult, ref structField);
1220
+
1221
+ if(validationResult != global::System.ComponentModel.DataAnnotations.ValidationResult.Success)
1222
+ throw new global::System.ComponentModel.DataAnnotations.ValidationException(validationResult!.ErrorMessage ?? ""Validation failed."");
1223
+
1224
+ var obj = new global::Thinktecture.Tests.TestValueObject(structField);
1225
+ obj.FactoryPostInit();
1226
+
1227
+ return obj;
1228
+ }
1229
+
1230
+ public static global::System.ComponentModel.DataAnnotations.ValidationResult? TryCreate(
1231
+ int structField,
1232
+ [global::System.Diagnostics.CodeAnalysis.MaybeNull] out global::Thinktecture.Tests.TestValueObject obj)
1233
+ {
1234
+ var validationResult = global::System.ComponentModel.DataAnnotations.ValidationResult.Success;
1235
+ ValidateFactoryArguments(ref validationResult, ref structField);
1236
+
1237
+ if (validationResult == global::System.ComponentModel.DataAnnotations.ValidationResult.Success)
1238
+ {
1239
+ obj = new global::Thinktecture.Tests.TestValueObject(structField);
1240
+ obj.FactoryPostInit();
1241
+ }
1242
+ else
1243
+ {
1244
+ obj = default;
1245
+ }
1246
+
1247
+ return validationResult;
1248
+ }
1249
+
1250
+ static partial void ValidateFactoryArguments(ref global::System.ComponentModel.DataAnnotations.ValidationResult? validationResult, ref int structField);
1251
+
1252
+ partial void FactoryPostInit();
1253
+
1254
+ /// <summary>
1255
+ /// Implicit conversion to the type <see cref=""int""/>.
1256
+ /// </summary>
1257
+ /// <param name=""obj"">Object to covert.</param>
1258
+ /// <returns>The <see cref=""StructField""/> of provided <paramref name=""obj""/> or <c>default</c> if <paramref name=""obj""/> is <c>null</c>.</returns>
1259
+ [return: global::System.Diagnostics.CodeAnalysis.NotNullIfNotNull(""obj"")]
1260
+ public static implicit operator int?(global::Thinktecture.Tests.TestValueObject? obj)
1261
+ {
1262
+ return obj?.StructField;
1263
+ }
1264
+
1265
+ /// <summary>
1266
+ /// Implicit conversion to the type <see cref=""int""/>.
1267
+ /// </summary>
1268
+ /// <param name=""obj"">Object to covert.</param>
1269
+ /// <returns>The <see cref=""StructField""/> of provided <paramref name=""obj""/>.</returns>
1270
+ public static implicit operator int(global::Thinktecture.Tests.TestValueObject obj)
1271
+ {
1272
+ return obj.StructField;
1273
+ }
1274
+
1275
+ /// <summary>
1276
+ /// Explicit conversion from the type <see cref=""int""/>.
1277
+ /// </summary>
1278
+ /// <param name=""structField"">Value to covert.</param>
1279
+ /// <returns>An instance of <see cref=""TestValueObject""/>.</returns>
1280
+ public static explicit operator global::Thinktecture.Tests.TestValueObject(int structField)
1281
+ {
1282
+ return global::Thinktecture.Tests.TestValueObject.Create(structField);
1283
+ }
1284
+
1285
+ private TestValueObject(int structField)
1286
+ {
1287
+ ValidateConstructorArguments(ref structField);
1288
+
1289
+ this.StructField = structField;
1290
+ }
1291
+
1292
+ static partial void ValidateConstructorArguments(ref int structField);
1293
+
1294
+ /// <summary>
1295
+ /// Compares to instances of <see cref=""TestValueObject""/>.
1296
+ /// </summary>
1297
+ /// <param name=""obj"">Instance to compare.</param>
1298
+ /// <param name=""other"">Another instance to compare.</param>
1299
+ /// <returns><c>true</c> if objects are equal; otherwise <c>false</c>.</returns>
1300
+ public static bool operator ==(global::Thinktecture.Tests.TestValueObject obj, global::Thinktecture.Tests.TestValueObject other)
1301
+ {
1302
+ return obj.Equals(other);
1303
+ }
1304
+
1305
+ /// <summary>
1306
+ /// Compares to instances of <see cref=""TestValueObject""/>.
1307
+ /// </summary>
1308
+ /// <param name=""obj"">Instance to compare.</param>
1309
+ /// <param name=""other"">Another instance to compare.</param>
1310
+ /// <returns><c>false</c> if objects are equal; otherwise <c>true</c>.</returns>
1311
+ public static bool operator !=(global::Thinktecture.Tests.TestValueObject obj, global::Thinktecture.Tests.TestValueObject other)
1312
+ {
1313
+ return !(obj == other);
1314
+ }
1315
+
1316
+ /// <inheritdoc />
1317
+ public override bool Equals(object? other)
1318
+ {
1319
+ return other is global::Thinktecture.Tests.TestValueObject obj && Equals(obj);
1320
+ }
1321
+
1322
+ /// <inheritdoc />
1323
+ public bool Equals(global::Thinktecture.Tests.TestValueObject other)
1324
+ {
1325
+ return this.StructField.Equals(other.StructField);
1326
+ }
1327
+
1328
+ /// <inheritdoc />
1329
+ public override int GetHashCode()
1330
+ {
1331
+ return global::System.HashCode.Combine(this.StructField);
1332
+ }
1333
+
1334
+ /// <inheritdoc />
1335
+ public override string? ToString()
1336
+ {
1337
+ return this.StructField.ToString();
1338
+ }
1339
+
1340
+ /// <inheritdoc />
1341
+ public string ToString(string? format, global::System.IFormatProvider? formatProvider = null)
1342
+ {
1343
+ return this.StructField.ToString(format, formatProvider);
1344
+ }
1345
+
1346
+ /// <inheritdoc />
1347
+ public int CompareTo(object? obj)
1348
+ {
1349
+ if(obj is null)
1350
+ return 1;
1351
+
1352
+ if(obj is not global::Thinktecture.Tests.TestValueObject valueObject)
1353
+ throw new global::System.ArgumentException(""Argument must be of type \""TestValueObject\""."", nameof(obj));
1354
+
1355
+ return this.CompareTo(valueObject);
1356
+ }
1357
+
1358
+ /// <inheritdoc />
1359
+ public int CompareTo(global::Thinktecture.Tests.TestValueObject obj)
1360
+ {
1361
+ return this.StructField.CompareTo(obj.StructField);
1362
+ }
1363
+ }
1364
+ }
1365
+ " ) ;
1366
+ }
1367
+
1154
1368
[ Fact ]
1155
1369
public void Should_generate_struct_with_string_key_member_and_NullInFactoryMethodsYieldsNull_should_be_ignored ( )
1156
1370
{
@@ -1261,9 +1475,9 @@ internal static void ModuleInit()
1261
1475
/// <param name=""obj"">Object to covert.</param>
1262
1476
/// <returns>The <see cref=""ReferenceField""/> of provided <paramref name=""obj""/> or <c>default</c> if <paramref name=""obj""/> is <c>null</c>.</returns>
1263
1477
[return: global::System.Diagnostics.CodeAnalysis.NotNullIfNotNull(""obj"")]
1264
- public static implicit operator string?(global::Thinktecture.Tests.TestValueObject obj)
1478
+ public static implicit operator string?(global::Thinktecture.Tests.TestValueObject? obj)
1265
1479
{
1266
- return obj.ReferenceField;
1480
+ return obj? .ReferenceField;
1267
1481
}
1268
1482
1269
1483
/// <summary>
@@ -1466,7 +1680,7 @@ internal static void ModuleInit()
1466
1680
[return: global::System.Diagnostics.CodeAnalysis.NotNullIfNotNull(""obj"")]
1467
1681
public static implicit operator string?(global::Thinktecture.Tests.TestValueObject? obj)
1468
1682
{
1469
- return obj is null ? null : obj .ReferenceField;
1683
+ return obj? .ReferenceField;
1470
1684
}
1471
1685
1472
1686
/// <summary>
@@ -1684,7 +1898,7 @@ internal static void ModuleInit()
1684
1898
[return: global::System.Diagnostics.CodeAnalysis.NotNullIfNotNull(""obj"")]
1685
1899
public static implicit operator int?(global::Thinktecture.Tests.TestValueObject? obj)
1686
1900
{
1687
- return obj is null ? null : obj .StructField;
1901
+ return obj? .StructField;
1688
1902
}
1689
1903
1690
1904
/// <summary>
@@ -1929,7 +2143,7 @@ internal static void ModuleInit()
1929
2143
[return: global::System.Diagnostics.CodeAnalysis.NotNullIfNotNull(""obj"")]
1930
2144
public static implicit operator string?(global::Thinktecture.Tests.TestValueObject? obj)
1931
2145
{
1932
- return obj is null ? null : obj .ReferenceField;
2146
+ return obj? .ReferenceField;
1933
2147
}
1934
2148
1935
2149
/// <summary>
@@ -2147,7 +2361,7 @@ internal static void ModuleInit()
2147
2361
[return: global::System.Diagnostics.CodeAnalysis.NotNullIfNotNull(""obj"")]
2148
2362
public static implicit operator int?(global::Thinktecture.Tests.TestValueObject? obj)
2149
2363
{
2150
- return obj is null ? null : obj .StructField;
2364
+ return obj? .StructField;
2151
2365
}
2152
2366
2153
2367
/// <summary>
@@ -2385,7 +2599,7 @@ internal static void ModuleInit()
2385
2599
[return: global::System.Diagnostics.CodeAnalysis.NotNullIfNotNull(""obj"")]
2386
2600
public static implicit operator string?(global::Thinktecture.Tests.TestValueObject? obj)
2387
2601
{
2388
- return obj is null ? null : obj .ReferenceField;
2602
+ return obj? .ReferenceField;
2389
2603
}
2390
2604
2391
2605
/// <summary>
@@ -2604,7 +2818,7 @@ internal static void ModuleInit()
2604
2818
[return: global::System.Diagnostics.CodeAnalysis.NotNullIfNotNull(""obj"")]
2605
2819
public static implicit operator int?(global::Thinktecture.Tests.TestValueObject? obj)
2606
2820
{
2607
- return obj is null ? null : obj .ReferenceField;
2821
+ return obj? .ReferenceField;
2608
2822
}
2609
2823
2610
2824
/// <summary>
@@ -2846,7 +3060,7 @@ internal static void ModuleInit()
2846
3060
[return: global::System.Diagnostics.CodeAnalysis.NotNullIfNotNull(""obj"")]
2847
3061
public static implicit operator global::Thinktecture.Tests.Foo?(global::Thinktecture.Tests.TestValueObject? obj)
2848
3062
{
2849
- return obj is null ? null : obj .ReferenceField;
3063
+ return obj? .ReferenceField;
2850
3064
}
2851
3065
2852
3066
/// <summary>
0 commit comments