From 53e877183abe0dbbb623c39380101bc369e9f265 Mon Sep 17 00:00:00 2001 From: Rob Mensching Date: Sat, 2 Dec 2017 00:44:45 -0800 Subject: Support tuples from extensions and make SourcePath a path instead of string --- src/WixToolset.Data/IntermediateFieldExtensions.cs | 72 +++++++++++++++++----- 1 file changed, 58 insertions(+), 14 deletions(-) (limited to 'src/WixToolset.Data/IntermediateFieldExtensions.cs') diff --git a/src/WixToolset.Data/IntermediateFieldExtensions.cs b/src/WixToolset.Data/IntermediateFieldExtensions.cs index c551b455..be225452 100644 --- a/src/WixToolset.Data/IntermediateFieldExtensions.cs +++ b/src/WixToolset.Data/IntermediateFieldExtensions.cs @@ -11,6 +11,8 @@ namespace WixToolset.Data public static IntermediateField Set(this IntermediateField field, object value) { + var data = value; + if (field == null) { throw new ArgumentNullException(nameof(field)); @@ -21,30 +23,72 @@ namespace WixToolset.Data } else if (field.Type == IntermediateFieldType.Bool && !(value is bool)) { - throw new ArgumentException(nameof(value)); + if (value is int) + { + data = ((int)value) != 0; + } + else if (value is string str) + { + if (str.Equals("yes", StringComparison.OrdinalIgnoreCase) || str.Equals("true", StringComparison.OrdinalIgnoreCase)) + { + data = true; + } + else if (str.Equals("no", StringComparison.OrdinalIgnoreCase) || str.Equals("false", StringComparison.OrdinalIgnoreCase)) + { + data = false; + } + else + { + throw new ArgumentException(nameof(value)); + } + } + else + { + throw new ArgumentException(nameof(value)); + } } else if (field.Type == IntermediateFieldType.Number && !(value is int)) { - throw new ArgumentException(nameof(value)); + if (value is string str && Int32.TryParse(str, out var number)) + { + data = number; + } + else + { + throw new ArgumentException(nameof(value)); + } } else if (field.Type == IntermediateFieldType.String && !(value is string)) { - throw new ArgumentException(nameof(value)); - } - else if (field.Type == IntermediateFieldType.Path && !(value is IntermediateFieldPathValue || value is string)) - { - throw new ArgumentException(nameof(value)); - } - - if (field.Type == IntermediateFieldType.Path && value != null && value is string) - { - value = new IntermediateFieldPathValue { Path = (string)value }; + if (value is int) + { + data = value.ToString(); + } + else if (value is bool b) + { + data = b ? "true" : "false"; + } + else + { + throw new ArgumentException(nameof(value)); + } + } + else if (field.Type == IntermediateFieldType.Path && !(value is IntermediateFieldPathValue)) + { + if (value is string str) + { + data = new IntermediateFieldPathValue { Path = str }; + } + else + { + throw new ArgumentException(nameof(value)); + } } field.Value = new IntermediateFieldValue { Context = valueContext, - Data = value, + Data = data, PreviousValue = field.Value }; @@ -103,7 +147,7 @@ namespace WixToolset.Data return field.Value.AsNumber() != 0; case IntermediateFieldType.String: - return !System.String.IsNullOrEmpty(field.Value.AsString()); + return !String.IsNullOrEmpty(field.Value.AsString()); default: throw new InvalidCastException($"Cannot convert field {field.Name} with type {field.Type} to boolean"); -- cgit v1.2.3-55-g6feb