public static bool TryConvertFromRegistry(object?registryValue, Setting.ValueKind resultValueKind, out object?resultValue) { if (registryValue == null) { resultValue = null; return(false); } if (registryValue is Int32 intValue) { switch (resultValueKind) { case Setting.ValueKind.Boolean: resultValue = intValue != 0; return(true); case Setting.ValueKind.Integer: resultValue = (long)intValue; return(true); } resultValue = null; return(false); } if (registryValue is Int64 longValue) { switch (resultValueKind) { case Setting.ValueKind.Boolean: resultValue = longValue != 0; return(true); case Setting.ValueKind.Integer: resultValue = (long)longValue; return(true); } resultValue = null; return(false); } if (registryValue is string stringValue) { switch (resultValueKind) { case Setting.ValueKind.String: resultValue = stringValue; return(true); } resultValue = null; return(false); } resultValue = null; return(false); }
public async Task TestCapturePass(Setting.ValueKind kind, SystemValueKind valueKind, object systemValue, object value) { var services = new ServiceCollection(); services.AddLogging(); services.AddSingleton <IServiceProvider>(provider => provider); services.AddSingleton <ISystemSettingFactory, MockSystemSettingFactory>(); var mockFactory = new MockSystemSettingFactory(); var serviceProvider = services.BuildServiceProvider(); var loggerFactory = new LoggerFactory(); var logger = loggerFactory.CreateLogger <SystemSettingHandler>(); createCount = 0; getCount = 0; setCount = 0; var handlerDescription = new SystemSettingHandlerDescription("thesetting", valueKind); handlerDescription.IntegerMap = new string[3] { "zero", "one", "two" }; handlerDescription.ReverseIntegerMap = new Dictionary <string, long> { { "zero", 0L }, { "one", 1L }, { "two", 2L } }; var setting = new Setting() { Name = "test", Kind = kind, HandlerDescription = handlerDescription }; var handler = new SystemSettingHandler(setting, mockFactory, serviceProvider, logger); MockSystemSetting.nextGetResponder = () => { return(systemValue); }; var result = await handler.Capture(); Assert.Equal(1, createCount); Assert.Equal(1, getCount); Assert.Equal(0, setCount); Assert.True(result.Success); Assert.NotNull(result.Value); Assert.Equal(value, result.Value); }
public void TestJsonDeserialize(Setting.ValueKind kind, object defval, Dictionary <string, object> handler, Dictionary <string, object> finalizer, SettingHandlerDescription.HandlerKind handlerKind, SettingFinalizerDescription.HandlerKind finalizerKind) { var options = new JsonSerializerOptions(); options.Converters.Add(new JsonElementInferredTypeConverter()); options.Converters.Add(new SettingHandlerDescription.JsonConverter()); options.Converters.Add(new SettingFinalizerDescription.JsonConverter()); //minimum number of fields var json = JsonSerializer.Serialize(new Dictionary <string, object>() { { "name", "settingname" }, { "type", kind } }); var setting = JsonSerializer.Deserialize <Setting>(json, options); Assert.NotNull(setting); Assert.Equal("settingname", setting.Name); Assert.Equal(kind, setting.Kind); Assert.Null(setting.HandlerDescription); Assert.Null(setting.FinalizerDescription); Assert.Null(setting.Default); json = JsonSerializer.Serialize(new Dictionary <string, object>() { { "name", "settingname" }, { "type", kind }, { "default", defval }, { "handler", handler }, { "finalizer", finalizer } }); setting = JsonSerializer.Deserialize <Setting>(json, options); Assert.NotNull(setting); Assert.Equal("settingname", setting.Name); Assert.Equal(kind, setting.Kind); Assert.Equal(defval, setting.Default); Assert.NotNull(setting.HandlerDescription); Assert.Equal(handlerKind, setting.HandlerDescription.Kind); Assert.NotNull(setting.FinalizerDescription); Assert.Equal(finalizerKind, setting.FinalizerDescription.Kind); }
public bool TryConvertFromIni(string?iniValue, Setting.ValueKind valueKind, out object?resultValue) { if (iniValue == null) { resultValue = null; return(false); } switch (valueKind) { case Setting.ValueKind.String: resultValue = iniValue; return(true); case Setting.ValueKind.Boolean: resultValue = iniValue == "1"; return(true); case Setting.ValueKind.Integer: if (Int64.TryParse(iniValue, out var longValue)) { resultValue = longValue; return(true); } resultValue = null; return(false); case Setting.ValueKind.Double: if (Double.TryParse(iniValue, out var doubleValue)) { resultValue = doubleValue; return(true); } resultValue = null; return(false); } resultValue = null; return(false); }
public async Task TestCaptureFail(Setting.ValueKind kind, SystemValueKind valueKind, object value) { var services = new ServiceCollection(); services.AddLogging(); services.AddSingleton <IServiceProvider>(provider => provider); services.AddSingleton <ISystemSettingFactory, MockSystemSettingFactory>(); var mockFactory = new MockSystemSettingFactory(); var serviceProvider = services.BuildServiceProvider(); var loggerFactory = new LoggerFactory(); var logger = loggerFactory.CreateLogger <SystemSettingHandler>(); createCount = 0; getCount = 0; setCount = 0; var setting = new Setting() { Name = "test", Kind = kind, HandlerDescription = new SystemSettingHandlerDescription("thesetting", valueKind) }; var handler = new SystemSettingHandler(setting, mockFactory, serviceProvider, logger); MockSystemSetting.nextGetResponder = () => { return(value); }; var result = await handler.Capture(); Assert.Equal(1, createCount); Assert.Equal(1, getCount); Assert.Equal(0, setCount); Assert.False(result.Success); Assert.Null(result.Value); }
public bool TryConvertFromSystem(object?systemValue, Setting.ValueKind valueKind, out object?resultValue) { if (systemValue is string stringValue) { switch (valueKind) { case Setting.ValueKind.String: resultValue = stringValue; return(true); case Setting.ValueKind.Integer: if (Description.ValueKind == SystemValueKind.IdPrefixedEnum) { if (stringValue.StartsWith(Description.SettingId)) { if (long.TryParse(stringValue.Substring(Description.SettingId.Length), out var longValue)) { resultValue = longValue; return(true); } } } else if (Description.ReverseIntegerMap is Dictionary <string, long> map) { if (map.TryGetValue(stringValue, out long longValue)) { resultValue = longValue; return(true); } } resultValue = null; return(false); } resultValue = null; return(false); } if (systemValue is bool boolValue) { switch (valueKind) { case Setting.ValueKind.Boolean: resultValue = boolValue; return(true); } resultValue = null; return(false); } if (systemValue is UInt32 intValue) { switch (valueKind) { case Setting.ValueKind.Integer: resultValue = (long)intValue; return(true); } resultValue = null; return(false); } if (systemValue != null) { logger.LogDebug("Got type {0} from system for {1}", systemValue.GetType().Name, Description.SettingId); } else { logger.LogDebug("Got null from system for {1}", Description.SettingId); } resultValue = null; return(false); }