mirror of
https://github.com/microsoft/onefuzz.git
synced 2025-06-22 14:19:03 +00:00
Use the json serializer to convert enum to the DB (#1910)
* Use the json serializer to convert enum to the DB * format
This commit is contained in:
@ -202,6 +202,7 @@ public static class PoolStateHelper {
|
||||
public static IReadOnlySet<PoolState> Available => _available;
|
||||
}
|
||||
|
||||
[SkipRename]
|
||||
public enum Architecture {
|
||||
x86_64
|
||||
}
|
||||
|
@ -37,6 +37,8 @@ public sealed class CustomEnumConverter<T> : JsonConverter<T> where T : Enum {
|
||||
|
||||
private const string ValueSeparator = ",";
|
||||
|
||||
private readonly bool _skipFormat;
|
||||
|
||||
public CustomEnumConverter(JsonNamingPolicy namingPolicy, JsonSerializerOptions options, object[]? knownValues) {
|
||||
_namingPolicy = namingPolicy;
|
||||
|
||||
@ -49,14 +51,14 @@ public sealed class CustomEnumConverter<T> : JsonConverter<T> where T : Enum {
|
||||
}
|
||||
|
||||
var type = typeof(T);
|
||||
var skipFormat = type.GetCustomAttribute<SkipRename>() != null;
|
||||
_skipFormat = type.GetCustomAttribute<SkipRename>() != null;
|
||||
if (continueProcessing) {
|
||||
Array values = Enum.GetValues(type);
|
||||
|
||||
for (int i = 0; i < values.Length; i++) {
|
||||
T value = (T)values.GetValue(i)!;
|
||||
|
||||
if (!TryProcessValue(value, skipFormat)) {
|
||||
if (!TryProcessValue(value, _skipFormat)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -68,7 +70,7 @@ public sealed class CustomEnumConverter<T> : JsonConverter<T> where T : Enum {
|
||||
return false;
|
||||
}
|
||||
|
||||
FormatAndAddToCaches(value, options.Encoder, skipFormat);
|
||||
FormatAndAddToCaches(value, options.Encoder, _skipFormat);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@ -103,7 +105,7 @@ public sealed class CustomEnumConverter<T> : JsonConverter<T> where T : Enum {
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
|
||||
formatted = FormatAndAddToCaches(value, options.Encoder);
|
||||
formatted = FormatAndAddToCaches(value, options.Encoder, _skipFormat);
|
||||
}
|
||||
|
||||
writer.WriteStringValue(formatted);
|
||||
|
@ -186,13 +186,6 @@ public class EntityConverter {
|
||||
) {
|
||||
return (prop.columnName, value);
|
||||
}
|
||||
if (prop.type.IsEnum) {
|
||||
var values =
|
||||
(value?.ToString()?.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)
|
||||
.Select(CaseConverter.PascalToSnake)).EnsureNotNull($"Unable to read enum data {value}");
|
||||
|
||||
return (prop.columnName, string.Join(",", values));
|
||||
}
|
||||
|
||||
var serialized = JsonSerializer.Serialize(value, _options);
|
||||
return (prop.columnName, serialized.Trim('"'));
|
||||
@ -246,12 +239,6 @@ public class EntityConverter {
|
||||
return entity.GetInt32(fieldName);
|
||||
} else if (ef.type == typeof(long) || ef.type == typeof(long?)) {
|
||||
return entity.GetInt64(fieldName);
|
||||
} else if (ef.type.IsEnum) {
|
||||
var stringValues =
|
||||
entity.GetString(fieldName).Split(",", StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)
|
||||
.Select(CaseConverter.SnakeToPascal);
|
||||
|
||||
return Enum.Parse(ef.type, string.Join(",", stringValues));
|
||||
} else {
|
||||
var outputType = ef.type;
|
||||
if (ef.discriminator != null) {
|
||||
|
@ -346,6 +346,40 @@ namespace Tests {
|
||||
}
|
||||
|
||||
|
||||
[SkipRename]
|
||||
enum DoNotRename {
|
||||
test1,
|
||||
Test_2,
|
||||
TEST3
|
||||
}
|
||||
|
||||
|
||||
[Flags]
|
||||
[SkipRename]
|
||||
enum DoNotRenameFlag {
|
||||
test1 = 1 << 0,
|
||||
Test_2 = 1 << 1,
|
||||
TEST3 = 1 << 2,
|
||||
}
|
||||
record TestEntity3(DoNotRename Enum, DoNotRenameFlag flag) : EntityBase();
|
||||
|
||||
|
||||
[Fact]
|
||||
public void TestSkipRename() {
|
||||
|
||||
var entityConverter = new EntityConverter();
|
||||
|
||||
var expected = new TestEntity3(DoNotRename.TEST3, DoNotRenameFlag.Test_2 | DoNotRenameFlag.test1);
|
||||
var tableEntity = entityConverter.ToTableEntity(expected);
|
||||
Assert.Equal("TEST3", tableEntity.GetString("enum"));
|
||||
Assert.Equal("test1,Test_2", tableEntity.GetString("flag"));
|
||||
|
||||
var actual = entityConverter.ToRecord<TestEntity3>(tableEntity);
|
||||
|
||||
Assert.Equal(expected, actual);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user