Skip to content

Commit 5de1c25

Browse files
tg123Copilot
andauthored
migrate to record (#1665)
* migrate to record * chore: update project files and clean up unused references * refactor: convert classes to records and simplify constructors for IntOrString, ResourceQuantity, and V1Patch * fix: define IsExternalInit to resolve CS0518 error in IntOrString * refactor: change IntOrString and ResourceQuantity from records to structs, update implicit conversions, and simplify null checks * refactor: add JsonPropertyName attribute to Value property in IntOrString struct * refactor: simplify V1Patch constructor and improve argument validation * refactor: remove unnecessary CultureInfo parameter in ToInt method * Update src/KubernetesClient/Models/ResourceQuantity.cs Co-authored-by: Copilot <[email protected]> * Update src/KubernetesClient/Models/IntOrString.cs Co-authored-by: Copilot <[email protected]> * Revert "Update src/KubernetesClient/Models/ResourceQuantity.cs" This reverts commit 62b20a6. * refactor: remove commented-out formatting check and simplify build command * refactor: remove IValidate.cs from project references in Aot and Classic --------- Co-authored-by: Copilot <[email protected]>
1 parent 9695506 commit 5de1c25

34 files changed

+213
-452
lines changed

.github/workflows/buildtest.yaml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,8 @@ jobs:
1717
dotnet-version: |
1818
8.0.x
1919
9.0.x
20-
# - name: Check Format
21-
# # don't check formatting on Windows b/c of CRLF issues.
22-
# if: matrix.os == 'ubuntu-latest'
23-
# run: dotnet format --severity error --verify-no-changes --exclude ./src/KubernetesClient/generated/
2420
- name: Build
25-
run: dotnet build --configuration Release -v detailed
21+
run: dotnet build --configuration Release
2622
- name: Test
2723
run: dotnet test --configuration Release --collect:"Code Coverage;Format=Cobertura" --logger trx --results-directory TestResults --settings CodeCoverage.runsettings --no-build
2824
- name: Upload coverage to Codecov

examples/csrApproval/Program.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,15 @@ string GenerateCertificate(string name)
6767

6868
var replace = new List<V1CertificateSigningRequestCondition>
6969
{
70-
new ("True", "Approved", DateTime.UtcNow, DateTime.UtcNow, "This certificate was approved by k8s client", "Approve"),
70+
new V1CertificateSigningRequestCondition
71+
{
72+
Type = "Approved",
73+
Status = "True",
74+
Reason = "Approve",
75+
Message = "This certificate was approved by k8s client",
76+
LastUpdateTime = DateTime.UtcNow,
77+
LastTransitionTime = DateTime.UtcNow,
78+
},
7179
};
7280
readCert.Status.Conditions = replace;
7381

examples/customResource/cResource.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ public override string ToString()
1919
}
2020
}
2121

22-
public class CResourceSpec
22+
public record CResourceSpec
2323
{
2424
[JsonPropertyName("cityName")]
2525
public string CityName { get; set; }
2626
}
2727

28-
public class CResourceStatus : V1Status
28+
public record CResourceStatus : V1Status
2929
{
3030
[JsonPropertyName("temperature")]
3131
public string Temperature { get; set; }

examples/resize/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
{
2424
Requests = new Dictionary<string, ResourceQuantity>()
2525
{
26-
["cpu"] = new ResourceQuantity("100m"),
26+
["cpu"] = "100m",
2727
},
2828
},
2929
},

src/KubernetesClient.Aot/KubernetesClient.Aot.csproj

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,13 @@
2525
<Compile Include="..\KubernetesClient\Models\IMetadata.cs" />
2626
<Compile Include="..\KubernetesClient\Models\IntOrStringJsonConverter.cs" />
2727
<Compile Include="..\KubernetesClient\Models\IntOrStringYamlConverter.cs" />
28-
<Compile Include="..\KubernetesClient\Models\IntstrIntOrString.cs" />
28+
<Compile Include="..\KubernetesClient\Models\IntOrString.cs" />
2929
<Compile Include="..\KubernetesClient\Models\ISpec.cs" />
3030
<Compile Include="..\KubernetesClient\Models\IStatus.cs" />
31-
<Compile Include="..\KubernetesClient\IValidate.cs" />
3231
<Compile Include="..\KubernetesClient\Models\KubernetesEntityAttribute.cs" />
3332
<Compile Include="..\KubernetesClient\Models\KubernetesList.cs" />
3433
<Compile Include="..\KubernetesClient\KubernetesObject.cs" />
3534
<Compile Include="..\KubernetesClient\Models\ModelExtensions.cs" />
36-
<Compile Include="..\KubernetesClient\Models\ModelVersionConverter.cs" />
3735
<Compile Include="..\KubernetesClient\Models\NodeMetrics.cs" />
3836
<Compile Include="..\KubernetesClient\Models\NodeMetricsList.cs" />
3937
<Compile Include="..\KubernetesClient\Models\PodMetrics.cs" />
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// IntOrString.cs(7,36): error CS0518: Predefined type 'System.Runtime.CompilerServices.IsExternalInit' is not defined or imported
2+
namespace System.Runtime.CompilerServices
3+
{
4+
internal static class IsExternalInit { }
5+
}

src/KubernetesClient.Classic/KubernetesClient.Classic.csproj

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,15 @@
2727
<Compile Include="..\KubernetesClient\Models\IMetadata.cs" />
2828
<Compile Include="..\KubernetesClient\Models\IntOrStringJsonConverter.cs" />
2929
<Compile Include="..\KubernetesClient\Models\IntOrStringYamlConverter.cs" />
30-
<Compile Include="..\KubernetesClient\Models\IntstrIntOrString.cs" />
30+
<Compile Include="..\KubernetesClient\Models\IntOrString.cs" />
3131
<Compile Include="..\KubernetesClient\Models\ISpec.cs" />
3232
<Compile Include="..\KubernetesClient\Models\IStatus.cs" />
33-
<Compile Include="..\KubernetesClient\IValidate.cs" />
3433
<Compile Include="..\KubernetesClient\Models\KubernetesEntityAttribute.cs" />
3534
<Compile Include="..\KubernetesClient\KubernetesJson.cs" />
3635
<Compile Include="..\KubernetesClient\Models\KubernetesList.cs" />
3736
<Compile Include="..\KubernetesClient\KubernetesObject.cs" />
3837
<Compile Include="..\KubernetesClient\KubernetesYaml.cs" />
3938
<Compile Include="..\KubernetesClient\Models\ModelExtensions.cs" />
40-
<Compile Include="..\KubernetesClient\Models\ModelVersionConverter.cs" />
4139
<Compile Include="..\KubernetesClient\Models\NodeMetrics.cs" />
4240
<Compile Include="..\KubernetesClient\Models\NodeMetricsList.cs" />
4341
<Compile Include="..\KubernetesClient\Models\PodMetrics.cs" />

src/KubernetesClient/IValidate.cs

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
namespace k8s.Models
2+
{
3+
[JsonConverter(typeof(IntOrStringJsonConverter))]
4+
public struct IntOrString
5+
{
6+
public string? Value { get; private init; }
7+
8+
public static implicit operator IntOrString(int v)
9+
{
10+
return Convert.ToString(v);
11+
}
12+
13+
public static implicit operator IntOrString(long v)
14+
{
15+
return Convert.ToString(v);
16+
}
17+
18+
public static implicit operator string(IntOrString v)
19+
{
20+
return v.Value;
21+
}
22+
23+
public static implicit operator IntOrString(string v)
24+
{
25+
return new IntOrString { Value = v };
26+
}
27+
28+
public override string ToString()
29+
{
30+
return Value;
31+
}
32+
33+
public int ToInt()
34+
{
35+
return int.Parse(Value);
36+
}
37+
}
38+
}

src/KubernetesClient/Models/IntOrStringJsonConverter.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
namespace k8s.Models
22
{
3-
internal sealed class IntOrStringJsonConverter : JsonConverter<IntstrIntOrString>
3+
internal sealed class IntOrStringJsonConverter : JsonConverter<IntOrString>
44
{
5-
public override IntstrIntOrString Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
5+
public override IntOrString Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
66
{
77
switch (reader.TokenType)
88
{
@@ -17,14 +17,14 @@ public override IntstrIntOrString Read(ref Utf8JsonReader reader, Type typeToCon
1717
throw new NotSupportedException();
1818
}
1919

20-
public override void Write(Utf8JsonWriter writer, IntstrIntOrString value, JsonSerializerOptions options)
20+
public override void Write(Utf8JsonWriter writer, IntOrString value, JsonSerializerOptions options)
2121
{
2222
if (writer == null)
2323
{
2424
throw new ArgumentNullException(nameof(writer));
2525
}
2626

27-
var s = value?.Value;
27+
var s = value.Value;
2828

2929
if (long.TryParse(s, out var intv))
3030
{

0 commit comments

Comments
 (0)