Skip to content
This repository has been archived by the owner on Nov 1, 2023. It is now read-only.

Commit

Permalink
Container converter (#1821)
Browse files Browse the repository at this point in the history
* Add db translation layer for container

* Remove debugging artifact

* fmt

* Remove unnecessary file

* PR comments

* Format
  • Loading branch information
tevoinea authored Apr 22, 2022
1 parent 8654536 commit 20bd3c3
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
16 changes: 16 additions & 0 deletions src/ApiService/ApiService/OneFuzzTypes/Model.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Endpoint = System.String;
using GroupId = System.Guid;
using PrincipalId = System.Guid;
using System.Text.Json;

namespace Microsoft.OneFuzz.Service;

Expand Down Expand Up @@ -393,11 +394,26 @@ Dictionary<string, string> Tags

) : EntityBase();

[JsonConverter(typeof(ContainerConverter))]
public record Container(string ContainerName)
{
public string ContainerName { get; } = ContainerName.All(c => char.IsLetterOrDigit(c) || c == '-') ? ContainerName : throw new ArgumentException("Container name must have only numbers, letters or dashes");
}

public class ContainerConverter : JsonConverter<Container>
{
public override Container? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var containerName = reader.GetString();
return containerName == null ? null : new Container(containerName);
}

public override void Write(Utf8JsonWriter writer, Container value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.ContainerName);
}
}

public record Notification(
DateTime? Timestamp,
Container Container,
Expand Down
38 changes: 37 additions & 1 deletion src/ApiService/Tests/OrmTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -276,5 +276,41 @@ public void TestEventSerialization2()
var actualEvent = converter.ToRecord<EventMessage>(te);
Assert.Equal(expectedEvent, actualEvent);
}

record Entity3(
[PartitionKey] int Id,
[RowKey] string TheName,
Container Container
) : EntityBase();

[Fact]
public void TestContainerSerialization()
{
var container = new Container("abc-123");
var expected = new Entity3(123, "abc", container);
var converter = new EntityConverter();

var tableEntity = converter.ToTableEntity(expected);
var actual = converter.ToRecord<Entity3>(tableEntity);

Assert.Equal(expected.Container.ContainerName, actual.Container.ContainerName);
Assert.Equal(expected.Container.ContainerName, tableEntity.GetString("container"));
}

[Fact]
public void TestContainerSerialization2()
{
var entityJson =
@"{
""Id"": 123,
""TheName"": ""abc"",
""Container"": ""abc-123""
}";
var entity = JsonSerializer.Deserialize<Entity3>(entityJson);

Assert.Equal(123, entity?.Id);
Assert.Equal("abc", entity?.TheName);
Assert.Equal("abc-123", entity?.Container.ContainerName);
}
}
}
}

0 comments on commit 20bd3c3

Please sign in to comment.