Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

align with go json unmarshal #1397

Merged
merged 1 commit into from
Sep 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/KubernetesClient.Models/ResourceQuantity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public override string ToString()

protected bool Equals(ResourceQuantity other)
{
return Format == other?.Format && _unitlessValue.Equals(other._unitlessValue);
return _unitlessValue.Equals(other?._unitlessValue);
}

public override bool Equals(object obj)
Expand Down
16 changes: 15 additions & 1 deletion src/KubernetesClient.Models/ResourceQuantityJsonConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,23 @@ namespace k8s.Models
{
internal sealed class ResourceQuantityJsonConverter : JsonConverter<ResourceQuantity>
{
// https://github.com/kubernetes/apimachinery/blob/4b14f804a0babdcc58e695d72f77ad29f536511e/pkg/api/resource/quantity.go#L683
public override ResourceQuantity Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return new ResourceQuantity(reader.GetString());
switch (reader.TokenType)
{
case JsonTokenType.Null:
return new ResourceQuantity(null);
case JsonTokenType.Number:
if (reader.TryGetDouble(out var val))
{
return new ResourceQuantity(Convert.ToString(val));
}

return reader.GetDecimal();
default:
return new ResourceQuantity(reader.GetString());
}
}

public override void Write(Utf8JsonWriter writer, ResourceQuantity value, JsonSerializerOptions options)
Expand Down
46 changes: 42 additions & 4 deletions tests/KubernetesClient.Tests/QuantityValueTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using k8s.Models;
using System;
using System.Collections.Generic;
using Xunit;
using static k8s.Models.ResourceQuantity.SuffixFormat;

Expand Down Expand Up @@ -200,7 +201,7 @@
}

[Fact]
public void Serialize()
public void SerializeJson()
{
{
ResourceQuantity quantity = 12000;
Expand All @@ -209,10 +210,25 @@
}

[Fact]
public void DeserializeYaml()
public void DeserializeJson()
{
var value = KubernetesYaml.Deserialize<ResourceQuantity>("\"1\"");
Assert.Equal(new ResourceQuantity(1, 0, DecimalSI), value);
// str
{
var value = KubernetesJson.Deserialize<Dictionary<string, ResourceQuantity>>(@"{""cpu"": ""1.1""}");
Assert.Equal(new ResourceQuantity(11, -1, DecimalSI), value["cpu"]);
}

Check warning on line 220 in tests/KubernetesClient.Tests/QuantityValueTests.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

Code should not contain trailing whitespace

Check warning on line 220 in tests/KubernetesClient.Tests/QuantityValueTests.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

Code should not contain trailing whitespace

Check warning on line 220 in tests/KubernetesClient.Tests/QuantityValueTests.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

Code should not contain trailing whitespace

Check warning on line 220 in tests/KubernetesClient.Tests/QuantityValueTests.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

Code should not contain trailing whitespace
// int
{
var value = KubernetesJson.Deserialize<Dictionary<string, ResourceQuantity>>(@"{""cpu"": 1}");
Assert.Equal(new ResourceQuantity(1, 0, DecimalSI), value["cpu"]);
}

// double
{
var value = KubernetesJson.Deserialize<Dictionary<string, ResourceQuantity>>(@"{""cpu"": 1.1}");
Assert.Equal(new ResourceQuantity(11, -1, DecimalSI), value["cpu"]);
}
}

[Fact]
Expand All @@ -221,5 +237,27 @@
var value = KubernetesYaml.Serialize(new ResourceQuantity(1, -1, DecimalSI));
Assert.Equal("100m", value);
}

[Fact]
public void DeserializeYaml()
{
// str
{
var value = KubernetesYaml.Deserialize<ResourceQuantity>("\"1\"");
Assert.Equal(new ResourceQuantity(1, 0, DecimalSI), value);
}

// int
{
var value = KubernetesYaml.Deserialize<ResourceQuantity>("1");
Assert.Equal(new ResourceQuantity(1, 0, DecimalSI), value);
}

// double
{
var value = KubernetesYaml.Deserialize<ResourceQuantity>("1.1");
Assert.Equal(new ResourceQuantity(11, -1, DecimalSI), value);
}
}
}
}