Skip to content

Commit 04d7445

Browse files
committed
refactor: convert classes to records and simplify constructors for IntOrString, ResourceQuantity, and V1Patch
1 parent 638f614 commit 04d7445

File tree

9 files changed

+45
-104
lines changed

9 files changed

+45
-104
lines changed

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
},
Lines changed: 9 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
namespace k8s.Models
22
{
33
[JsonConverter(typeof(IntOrStringJsonConverter))]
4-
public partial class IntOrString
4+
public record IntOrString
55
{
6+
[JsonPropertyName("value")]
7+
public string Value { get; init; }
8+
69
public static implicit operator IntOrString(int v)
710
{
8-
return new IntOrString { Value = Convert.ToString(v) };
11+
return new IntOrString(Convert.ToString(v));
912
}
1013

1114
public static implicit operator IntOrString(long v)
1215
{
13-
return new IntOrString { Value = Convert.ToString(v) };
16+
return new IntOrString(Convert.ToString(v));
1417
}
1518

1619
public static implicit operator string(IntOrString v)
@@ -20,37 +23,12 @@ public static implicit operator string(IntOrString v)
2023

2124
public static implicit operator IntOrString(string v)
2225
{
23-
return new IntOrString { Value = v };
24-
}
25-
26-
protected bool Equals(IntOrString other)
27-
{
28-
return string.Equals(Value, other?.Value);
29-
}
30-
31-
public override bool Equals(object obj)
32-
{
33-
if (ReferenceEquals(null, obj))
34-
{
35-
return false;
36-
}
37-
38-
if (ReferenceEquals(this, obj))
39-
{
40-
return true;
41-
}
42-
43-
if (obj.GetType() != GetType())
44-
{
45-
return false;
46-
}
47-
48-
return Equals((IntOrString)obj);
26+
return new IntOrString(v);
4927
}
5028

51-
public override int GetHashCode()
29+
public override string ToString()
5230
{
53-
return Value != null ? Value.GetHashCode() : 0;
31+
return Value;
5432
}
5533
}
5634
}

src/KubernetesClient/Models/IntOrStringYamlConverter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public object ReadYaml(IParser parser, Type type, ObjectDeserializer rootDeseria
2121
return null;
2222
}
2323

24-
return new IntOrString { Value = scalar?.Value };
24+
return scalar?.Value;
2525
}
2626
finally
2727
{

src/KubernetesClient/Models/ResourceQuantity.cs

Lines changed: 9 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ namespace k8s.Models
5454
/// cause implementors to also use a fixed point implementation.
5555
/// </summary>
5656
[JsonConverter(typeof(ResourceQuantityJsonConverter))]
57-
public partial class ResourceQuantity
57+
public record ResourceQuantity
5858
{
5959
public enum SuffixFormat
6060
{
@@ -85,12 +85,6 @@ public ResourceQuantity(decimal n, int exp, SuffixFormat format)
8585
Format = format;
8686
}
8787

88-
public ResourceQuantity(string s)
89-
{
90-
Value = s;
91-
CustomInit();
92-
}
93-
9488
public SuffixFormat Format { get; private set; }
9589

9690
public string CanonicalizeString()
@@ -103,39 +97,6 @@ public override string ToString()
10397
return CanonicalizeString();
10498
}
10599

106-
protected bool Equals(ResourceQuantity other)
107-
{
108-
return _unitlessValue.Equals(other?._unitlessValue);
109-
}
110-
111-
public override bool Equals(object obj)
112-
{
113-
if (ReferenceEquals(null, obj))
114-
{
115-
return false;
116-
}
117-
118-
if (ReferenceEquals(this, obj))
119-
{
120-
return true;
121-
}
122-
123-
if (obj.GetType() != GetType())
124-
{
125-
return false;
126-
}
127-
128-
return Equals((ResourceQuantity)obj);
129-
}
130-
131-
public override int GetHashCode()
132-
{
133-
unchecked
134-
{
135-
return ((int)Format * 397) ^ _unitlessValue.GetHashCode();
136-
}
137-
}
138-
139100
//
140101
// CanonicalizeString = go version CanonicalizeBytes
141102
// CanonicalizeBytes returns the canonical form of q and its suffix (see comment on Quantity).
@@ -163,18 +124,17 @@ public string CanonicalizeString(SuffixFormat suffixFormat)
163124
return Suffixer.AppendMaxSuffix(_unitlessValue, suffixFormat);
164125
}
165126

166-
// ctor
167-
partial void CustomInit()
127+
public ResourceQuantity(string v)
168128
{
169-
if (Value == null)
129+
if (v == null)
170130
{
171131
// No value has been defined, initialize to 0.
172132
_unitlessValue = new Fraction(0);
173133
Format = SuffixFormat.BinarySI;
174134
return;
175135
}
176136

177-
var value = Value.Trim();
137+
var value = v.Trim();
178138

179139
var si = value.IndexOfAny(SuffixChars);
180140
if (si == -1)
@@ -194,6 +154,11 @@ partial void CustomInit()
194154
}
195155
}
196156

157+
public static implicit operator ResourceQuantity(string v)
158+
{
159+
return new ResourceQuantity(v);
160+
}
161+
197162
private static bool HasMantissa(Fraction value)
198163
{
199164
if (value.IsZero)

src/KubernetesClient/Models/ResourceQuantityJsonConverter.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@ public override ResourceQuantity Read(ref Utf8JsonReader reader, Type typeToConv
88
switch (reader.TokenType)
99
{
1010
case JsonTokenType.Null:
11-
return new ResourceQuantity { Value = null };
11+
return null;
1212
case JsonTokenType.Number:
1313
if (reader.TryGetDouble(out var val))
1414
{
15-
return new ResourceQuantity { Value = Convert.ToString(val) };
15+
return Convert.ToString(val);
1616
}
1717

1818
return reader.GetDecimal();
1919
default:
20-
return new ResourceQuantity { Value = reader.GetString() };
20+
return reader.GetString();
2121
}
2222
}
2323

src/KubernetesClient/Models/ResourceQuantityYamlConverter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public object ReadYaml(IParser parser, Type type, ObjectDeserializer rootDeseria
2121
return null;
2222
}
2323

24-
return new ResourceQuantity { Value = scalar?.Value };
24+
return scalar?.Value;
2525
}
2626
finally
2727
{

src/KubernetesClient/Models/V1Patch.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
namespace k8s.Models
22
{
33
[JsonConverter(typeof(V1PatchJsonConverter))]
4-
public partial record class V1Patch
4+
public record V1Patch
55
{
66
public enum PatchType
77
{
@@ -31,17 +31,17 @@ public enum PatchType
3131
ApplyPatch,
3232
}
3333

34+
[JsonPropertyName("content")]
35+
[JsonInclude]
36+
public object Content { get; private set; }
37+
3438
public PatchType Type { get; private set; }
3539

3640
public V1Patch(object body, PatchType type)
3741
{
3842
Content = body;
3943
Type = type;
40-
CustomInit();
41-
}
4244

43-
partial void CustomInit()
44-
{
4545
if (Content == null)
4646
{
4747
throw new ArgumentNullException(nameof(Content), "object must be set");

src/LibKubernetesGenerator/ModelGenerator.cs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,36 @@ public void Generate(OpenApiDocument swagger, IncrementalGeneratorPostInitializa
1919
{
2020
var sc = scriptObjectFactory.CreateScriptObject();
2121

22+
var genSkippedTypes = new HashSet<string>
23+
{
24+
"IntOrString",
25+
"ResourceQuantity",
26+
"V1Patch",
27+
};
28+
2229
var extSkippedTypes = new HashSet<string>
2330
{
2431
"V1WatchEvent",
2532
};
2633

2734
var typeOverrides = new Dictionary<string, string>
2835
{
29-
{ "IntOrString", "class" },
30-
{ "ResourceQuantity", "class" },
36+
// not used at the moment
3137
};
3238

3339
foreach (var kv in swagger.Definitions)
3440
{
3541
var def = kv.Value;
3642
var clz = classNameHelper.GetClassNameForSchemaDefinition(def);
43+
44+
if (genSkippedTypes.Contains(clz))
45+
{
46+
continue;
47+
}
48+
3749
var hasExt = def.ExtensionData != null
3850
&& def.ExtensionData.ContainsKey("x-kubernetes-group-version-kind")
39-
&& !extSkippedTypes.Contains(classNameHelper.GetClassName(def));
51+
&& !extSkippedTypes.Contains(clz);
4052

4153

4254
var typ = "record";
@@ -51,7 +63,6 @@ public void Generate(OpenApiDocument swagger, IncrementalGeneratorPostInitializa
5163
sc.SetValue("typ", typ, true);
5264
sc.SetValue("hasExt", hasExt, true);
5365

54-
5566
context.RenderToContext("Model.cs.template", sc, $"Models_{clz}.g.cs");
5667
}
5768
}

src/LibKubernetesGenerator/templates/Model.cs.template

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,12 @@ public partial {{typ}} {{clz}} {{ if hasExt }} : {{ GetInterfaceName def }} {{ e
2121
public const string KubePluralName = "{{ GetPlural def }}";
2222
{{ end }}
2323

24-
/// <summary>
25-
/// Initializes a new instance of the {{GetClassName def}} class.
26-
/// </summary>
27-
public {{clz}}()
28-
{
29-
CustomInit();
30-
}
31-
32-
/// <summary>
33-
/// An initialization method that performs custom operations like setting defaults
34-
/// </summary>
35-
partial void CustomInit();
36-
3724
{{ for property in properties }}
3825
/// <summary>
3926
/// {{ToXmlDoc property.description}}
4027
/// </summary>
4128
[JsonPropertyName("{{property.name}}")]
42-
public {{ if property.IsRequired }} required {{ end }} {{GetDotNetType property}} {{GetDotNetName property.name "field"}} { get; set; }
29+
public {{ if property.IsRequired }} required {{ end }} {{GetDotNetType property}} {{GetDotNetName property.name "field"}} { get; set; }
4330
{{ end }}
4431
}
4532

0 commit comments

Comments
 (0)