Skip to content

Commit

Permalink
Added missing support for storing enumerables.
Browse files Browse the repository at this point in the history
  • Loading branch information
svenrog committed May 7, 2024
1 parent 82c5b18 commit b4ecb13
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright (c) Geta Digital. All rights reserved.
// Licensed under Apache-2.0. See the LICENSE file in the project root for more information

using System;
using System.Text.Json;

namespace Geta.Optimizely.GenericLinks.Converters.Values
{
public class EnumerableValueWriter : ILinkDataValueWriter
{
public bool CanWrite(Type type)
{
if (typeof(Enum).IsAssignableFrom(type))
return true;

return false;
}

public void Write(Utf8JsonWriter writer, object value)
{
writer.WriteNumberValue((int)value);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -125,5 +125,6 @@ private static void TryAddJsonValueWriters(IServiceCollection services)
services.TryAddEnumerable(ServiceDescriptor.Singleton<ILinkDataValueWriter, DoubleValueWriter>());
services.TryAddEnumerable(ServiceDescriptor.Singleton<ILinkDataValueWriter, DecimalValueWriter>());
services.TryAddEnumerable(ServiceDescriptor.Singleton<ILinkDataValueWriter, DateTimeValueWriter>());
services.TryAddEnumerable(ServiceDescriptor.Singleton<ILinkDataValueWriter, EnumerableValueWriter>());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void AttributeConverters_can_Convert()
subject = new JsonAttributeConverter();

Assert.True(subject.CanConvert(typeof(DialogContentOptions)));
Assert.Contains("test", subject.Convert(new DialogContentOptions { BaseClass = "test" }));
Assert.Contains("test", subject.Convert(new DialogContentOptions { BaseClass = "test" }));
}

[Fact]
Expand Down Expand Up @@ -277,7 +277,7 @@ public void SystemTextLinkDataConverter_can_Write()
jsonWriter.Flush();
memoryStream.Flush();
memoryStream.Seek(0, SeekOrigin.Begin);

using var reader = new StreamReader(memoryStream, leaveOpen: true);
var json = reader.ReadToEnd();

Expand Down Expand Up @@ -371,7 +371,7 @@ private static SystemTextLinkDataConverter<TLinkData> CreateSystemTextLinkDataCo
where TLinkData : LinkData, new()
{
var serviceCollection = new ServiceCollection();

serviceCollection.AddSingleton<IPropertyReflector, DefaultPropertyReflector>();
serviceCollection.AddSingleton<ILinkModelConverter>(CreateLinkModelConverter());
serviceCollection.AddSingleton<ILinkDataValueWriter, StringValueWriter>();
Expand All @@ -380,6 +380,7 @@ private static SystemTextLinkDataConverter<TLinkData> CreateSystemTextLinkDataCo
serviceCollection.AddSingleton<ILinkDataValueWriter, DoubleValueWriter>();
serviceCollection.AddSingleton<ILinkDataValueWriter, DecimalValueWriter>();
serviceCollection.AddSingleton<ILinkDataValueWriter, DateTimeValueWriter>();
serviceCollection.AddSingleton<ILinkDataValueWriter, EnumerableValueWriter>();

var serviceProvider = serviceCollection.BuildServiceProvider();
var factory = new SystemTextLinkDataJsonConverterFactory(serviceProvider);
Expand Down Expand Up @@ -430,5 +431,5 @@ private static IEnumerable<ILinkDataAttibuteConverter> CreateAttributeConverters
yield return new JsonAttributeConverter();
}


}

0 comments on commit b4ecb13

Please sign in to comment.