Skip to content

Commit 88177bc

Browse files
authored
feat: dictionaries and enumerable key-values generate map[key]value CRD properties (#350)
This fixes #348.
1 parent 5d86c77 commit 88177bc

File tree

3 files changed

+32
-6
lines changed

3 files changed

+32
-6
lines changed

src/KubeOps/Operator/Entities/Extensions/EntityToCrdExtensions.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ internal static class EntityToCrdExtensions
2929
private const string Float = "float";
3030
private const string Double = "double";
3131
private const string DateTime = "date-time";
32+
private const string Map = "map[{0}]{1}";
3233

3334
private static readonly string[] IgnoredToplevelProperties = { "metadata", "apiversion", "kind" };
3435

@@ -238,9 +239,26 @@ private static V1JSONSchemaProps MapType(
238239
additionalColumns,
239240
jsonPath);
240241
}
242+
else if (!isSimpleType
243+
&& type.IsGenericType && type.GetGenericTypeDefinition() == typeof(IDictionary<,>))
244+
{
245+
var genericTypes = type.GenericTypeArguments;
246+
var keyType = MapType(genericTypes[0], additionalColumns, jsonPath).Type;
247+
var valueType = MapType(genericTypes[1], additionalColumns, jsonPath).Type;
248+
props.Type = string.Format(Map, keyType, valueType);
249+
}
250+
else if (!isSimpleType
251+
&& type.IsGenericType && type.GetGenericTypeDefinition() == typeof(IEnumerable<>)
252+
&& type.GenericTypeArguments.Length == 1 && type.GenericTypeArguments.Single().IsGenericType
253+
&& type.GenericTypeArguments.Single().GetGenericTypeDefinition() == typeof(KeyValuePair<,>))
254+
{
255+
var genericTypes = type.GenericTypeArguments.Single().GenericTypeArguments;
256+
var keyType = MapType(genericTypes[0], additionalColumns, jsonPath).Type;
257+
var valueType = MapType(genericTypes[1], additionalColumns, jsonPath).Type;
258+
props.Type = string.Format(Map, keyType, valueType);
259+
}
241260
else if (!isSimpleType &&
242261
(typeof(IDictionary).IsAssignableFrom(type) ||
243-
(type.IsGenericType && type.GetGenericTypeDefinition() == typeof(IDictionary<,>)) ||
244262
(type.IsGenericType &&
245263
type.GetGenericArguments().FirstOrDefault()?.IsGenericType == true &&
246264
type.GetGenericArguments().FirstOrDefault()?.GetGenericTypeDefinition() ==

tests/KubeOps.Test/Operator/Entities/CrdGeneration.Test.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ public void Should_Not_Add_Status_SubResource_If_Absent()
5757
[InlineData("Bool", "boolean", null)]
5858
[InlineData("DateTime", "string", "date-time")]
5959
[InlineData("Enum", "string", null)]
60+
[InlineData(nameof(TestSpecEntitySpec.GenericDictionary), "map[string]string", null)]
61+
[InlineData(nameof(TestSpecEntitySpec.KeyValueEnumerable), "map[string]string", null)]
6062
public void Should_Set_The_Correct_Type_And_Format_For_Types(string fieldName, string typeName, string? format)
6163
{
6264
var crd = _testSpecEntity.CreateCrd();
@@ -272,21 +274,21 @@ public void Should_Set_Preserve_Unknown_Fields_On_Dictionaries()
272274
}
273275

274276
[Fact]
275-
public void Should_Set_Preserve_Unknown_Fields_On_Generic_Dictionaries()
277+
public void Should_Not_Set_Preserve_Unknown_Fields_On_Generic_Dictionaries()
276278
{
277279
var crd = _testSpecEntity.CreateCrd();
278-
280+
279281
var specProperties = crd.Spec.Versions.First().Schema.OpenAPIV3Schema.Properties["spec"];
280-
specProperties.Properties["genericDictionary"].XKubernetesPreserveUnknownFields.Should().BeTrue();
282+
specProperties.Properties["genericDictionary"].XKubernetesPreserveUnknownFields.Should().BeNull();
281283
}
282284

283285
[Fact]
284-
public void Should_Set_Preserve_Unknown_Fields_On_KeyValuePair_Enumerable()
286+
public void Should_Not_Set_Preserve_Unknown_Fields_On_KeyValuePair_Enumerable()
285287
{
286288
var crd = _testSpecEntity.CreateCrd();
287289

288290
var specProperties = crd.Spec.Versions.First().Schema.OpenAPIV3Schema.Properties["spec"];
289-
specProperties.Properties["keyValueEnumerable"].XKubernetesPreserveUnknownFields.Should().BeTrue();
291+
specProperties.Properties["keyValueEnumerable"].XKubernetesPreserveUnknownFields.Should().BeNull();
290292
}
291293

292294
[Fact]

tests/KubeOps.Test/TestEntities/TestSpecEntity.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,15 @@ public class TestSpecEntitySpec
114114
public IDictionary Dictionary { get; set; } = new Dictionary<string, string>();
115115

116116
public IDictionary<string, string> GenericDictionary { get; set; } = new Dictionary<string, string>();
117+
public IDictionary<string, string> NormalGenericDictionary { get; set; } = new Dictionary<string, string>();
118+
public IDictionary<string, string>? NullableGenericDictionary { get; set; } = new Dictionary<string, string>();
117119

118120
public IEnumerable<KeyValuePair<string, string>> KeyValueEnumerable { get; set; } =
119121
new Dictionary<string, string>();
122+
public IEnumerable<KeyValuePair<string, string>> NormalKeyValueEnumerable { get; set; } =
123+
new Dictionary<string, string>();
124+
public IEnumerable<KeyValuePair<string, string>>? NullableKeyValueEnumerable { get; set; } =
125+
new Dictionary<string, string>();
120126

121127
[PreserveUnknownFields]
122128
public object PreserveUnknownFields { get; set; } = new object();

0 commit comments

Comments
 (0)