From 7462108b714d07161126dcedda5312daef54ae13 Mon Sep 17 00:00:00 2001 From: Rob Mensching Date: Fri, 24 May 2019 08:47:36 -0700 Subject: Add tuple.IsNull(), remove fields when set to default and other small fixes --- src/WixToolset.Data/IntermediateField.cs | 41 ++++-------- src/WixToolset.Data/IntermediateFieldExtensions.cs | 2 + src/WixToolset.Data/IntermediateFieldValue.cs | 30 ++------- src/WixToolset.Data/IntermediateTuple.cs | 4 +- src/WixToolset.Data/IntermediateTupleExtensions.cs | 74 ++++++++++++++++------ src/WixToolset.Data/Tuples/RemoveRegistryTuple.cs | 2 +- src/WixToolset.Data/Tuples/WixBundleTuple.cs | 6 +- 7 files changed, 78 insertions(+), 81 deletions(-) diff --git a/src/WixToolset.Data/IntermediateField.cs b/src/WixToolset.Data/IntermediateField.cs index 9aed3d81..5b511d02 100644 --- a/src/WixToolset.Data/IntermediateField.cs +++ b/src/WixToolset.Data/IntermediateField.cs @@ -1,4 +1,4 @@ -// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information. +// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information. namespace WixToolset.Data { @@ -9,10 +9,7 @@ namespace WixToolset.Data [DebuggerDisplay("Name={Name,nq} Type={Type} Value={Value?.AsString()}")] public class IntermediateField { - public IntermediateField(IntermediateFieldDefinition definition) - { - this.Definition = definition; - } + public IntermediateField(IntermediateFieldDefinition definition) => this.Definition = definition; public IntermediateFieldDefinition Definition { get; } @@ -26,46 +23,30 @@ namespace WixToolset.Data internal IntermediateFieldValue Value { get; set; } - public static explicit operator bool(IntermediateField field) - { - return field.AsBool(); - } + public static explicit operator bool(IntermediateField field) => field.AsBool(); - public static explicit operator bool? (IntermediateField field) - { - return field.AsNullableBool(); - } + public static explicit operator bool? (IntermediateField field) => field.AsNullableBool(); - public static explicit operator int(IntermediateField field) - { - return field.AsNumber(); - } + public static explicit operator int(IntermediateField field) => field.AsNumber(); - public static explicit operator int? (IntermediateField field) - { - return field.AsNullableNumber(); - } + public static explicit operator int? (IntermediateField field) => field.AsNullableNumber(); - public static explicit operator string(IntermediateField field) - { - return field.AsString(); - } + public static explicit operator string(IntermediateField field) => field.AsString(); internal static IntermediateField Deserialize(IntermediateFieldDefinition definition, Uri baseUri, JsonObject jsonObject) { - var field = new IntermediateField(definition); + IntermediateField field = null; if (jsonObject != null) { + field = new IntermediateField(definition); + field.Value = IntermediateFieldValue.Deserialize(jsonObject, baseUri, definition.Type); } return field; } - internal JsonObject Serialize() - { - return this.Value?.Serialize(); - } + internal JsonObject Serialize() => this.Value?.Serialize(); } } diff --git a/src/WixToolset.Data/IntermediateFieldExtensions.cs b/src/WixToolset.Data/IntermediateFieldExtensions.cs index 2f8aec1d..a73a6552 100644 --- a/src/WixToolset.Data/IntermediateFieldExtensions.cs +++ b/src/WixToolset.Data/IntermediateFieldExtensions.cs @@ -9,6 +9,8 @@ namespace WixToolset.Data [ThreadStatic] internal static string valueContext; + public static bool IsNull(this IntermediateField field) => field?.Value?.Data == null; + public static bool AsBool(this IntermediateField field) { if (field == null || field.Value == null || field.Value.Data == null) diff --git a/src/WixToolset.Data/IntermediateFieldValue.cs b/src/WixToolset.Data/IntermediateFieldValue.cs index b09903dc..ca109e7f 100644 --- a/src/WixToolset.Data/IntermediateFieldValue.cs +++ b/src/WixToolset.Data/IntermediateFieldValue.cs @@ -15,35 +15,17 @@ namespace WixToolset.Data public IntermediateFieldValue PreviousValue { get; internal set; } - public static explicit operator bool(IntermediateFieldValue value) - { - return value.AsBool(); - } + public static explicit operator bool(IntermediateFieldValue value) => value.AsBool(); - public static explicit operator bool? (IntermediateFieldValue value) - { - return value.AsNullableBool(); - } + public static explicit operator bool? (IntermediateFieldValue value) => value.AsNullableBool(); - public static explicit operator int(IntermediateFieldValue value) - { - return value.AsNumber(); - } + public static explicit operator int(IntermediateFieldValue value) => value.AsNumber(); - public static explicit operator int? (IntermediateFieldValue value) - { - return value.AsNullableNumber(); - } + public static explicit operator int? (IntermediateFieldValue value) => value.AsNullableNumber(); - public static explicit operator IntermediateFieldPathValue(IntermediateFieldValue value) - { - return value.AsPath(); - } + public static explicit operator IntermediateFieldPathValue(IntermediateFieldValue value) => value.AsPath(); - public static explicit operator string(IntermediateFieldValue value) - { - return value.AsString(); - } + public static explicit operator string(IntermediateFieldValue value) => value.AsString(); internal static IntermediateFieldValue Deserialize(JsonObject jsonObject, Uri baseUri, IntermediateFieldType type) { diff --git a/src/WixToolset.Data/IntermediateTuple.cs b/src/WixToolset.Data/IntermediateTuple.cs index 09ff7692..bc46e3b4 100644 --- a/src/WixToolset.Data/IntermediateTuple.cs +++ b/src/WixToolset.Data/IntermediateTuple.cs @@ -46,9 +46,9 @@ namespace WixToolset.Data creator.TryGetTupleDefinitionByName(definitionName, out var definition); // TODO: this isn't sufficient. var tuple = definition.CreateTuple(sourceLineNumbers, id); - for (var i = 0; i < fieldsJson.Count; ++i) + for (var i = 0; i < fieldsJson.Count && i < tuple.Fields.Length; ++i) { - if (tuple.Fields.Length > i && fieldsJson[i] is JsonObject fieldJson) + if (fieldsJson[i] is JsonObject fieldJson) { tuple.Fields[i] = IntermediateField.Deserialize(tuple.Definition.FieldDefinitions[i], baseUri, fieldJson); } diff --git a/src/WixToolset.Data/IntermediateTupleExtensions.cs b/src/WixToolset.Data/IntermediateTupleExtensions.cs index f5b30d47..dd488dc5 100644 --- a/src/WixToolset.Data/IntermediateTupleExtensions.cs +++ b/src/WixToolset.Data/IntermediateTupleExtensions.cs @@ -1,36 +1,26 @@ -// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information. +// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information. namespace WixToolset.Data { public static class IntermediateTupleExtensions { - public static bool AsBool(this IntermediateTuple tuple, int index) - { - return tuple?.Fields[index].AsBool() ?? false; - } + public static bool AsBool(this IntermediateTuple tuple, int index) => tuple?.Fields[index].AsBool() ?? false; - public static bool? AsNullableBool(this IntermediateTuple tuple, int index) - { - return tuple?.Fields[index].AsNullableBool(); - } + public static bool? AsNullableBool(this IntermediateTuple tuple, int index) => tuple?.Fields[index].AsNullableBool(); - public static int AsNumber(this IntermediateTuple tuple, int index) - { - return tuple?.Fields[index].AsNumber() ?? 0; - } + public static int AsNumber(this IntermediateTuple tuple, int index) => tuple?.Fields[index].AsNumber() ?? 0; - public static int? AsNullableNumber(this IntermediateTuple tuple, int index) - { - return tuple?.Fields[index].AsNullableNumber(); - } + public static int? AsNullableNumber(this IntermediateTuple tuple, int index) => tuple?.Fields[index].AsNullableNumber(); - public static string AsString(this IntermediateTuple tuple, int index) - { - return tuple?.Fields[index].AsString(); - } + public static string AsString(this IntermediateTuple tuple, int index) => tuple?.Fields[index].AsString(); public static IntermediateField Set(this IntermediateTuple tuple, int index, bool value) { + if (value == default && NoFieldMetadata(tuple, index)) + { + return tuple.Fields[index] = null; + } + var definition = tuple.Definition.FieldDefinitions[index]; var field = tuple.Fields[index].Set(definition, value); @@ -40,6 +30,11 @@ namespace WixToolset.Data public static IntermediateField Set(this IntermediateTuple tuple, int index, bool? value) { + if (value == default && NoFieldMetadata(tuple, index)) + { + return tuple.Fields[index] = null; + } + var definition = tuple.Definition.FieldDefinitions[index]; var field = tuple.Fields[index].Set(definition, value); @@ -49,6 +44,11 @@ namespace WixToolset.Data public static IntermediateField Set(this IntermediateTuple tuple, int index, long value) { + if (value == default && NoFieldMetadata(tuple, index)) + { + return tuple.Fields[index] = null; + } + var definition = tuple.Definition.FieldDefinitions[index]; var field = tuple.Fields[index].Set(definition, value); @@ -58,6 +58,11 @@ namespace WixToolset.Data public static IntermediateField Set(this IntermediateTuple tuple, int index, long? value) { + if (value == default && NoFieldMetadata(tuple, index)) + { + return tuple.Fields[index] = null; + } + var definition = tuple.Definition.FieldDefinitions[index]; var field = tuple.Fields[index].Set(definition, value); @@ -67,6 +72,11 @@ namespace WixToolset.Data public static IntermediateField Set(this IntermediateTuple tuple, int index, int value) { + if (value == default && NoFieldMetadata(tuple, index)) + { + return tuple.Fields[index] = null; + } + var definition = tuple.Definition.FieldDefinitions[index]; var field = tuple.Fields[index].Set(definition, value); @@ -76,6 +86,11 @@ namespace WixToolset.Data public static IntermediateField Set(this IntermediateTuple tuple, int index, int? value) { + if (value == default && NoFieldMetadata(tuple, index)) + { + return tuple.Fields[index] = null; + } + var definition = tuple.Definition.FieldDefinitions[index]; var field = tuple.Fields[index].Set(definition, value); @@ -85,6 +100,11 @@ namespace WixToolset.Data public static IntermediateField Set(this IntermediateTuple tuple, int index, IntermediateFieldPathValue value) { + if (value == default && NoFieldMetadata(tuple, index)) + { + return tuple.Fields[index] = null; + } + var definition = tuple.Definition.FieldDefinitions[index]; var field = tuple.Fields[index].Set(definition, value); @@ -94,11 +114,23 @@ namespace WixToolset.Data public static IntermediateField Set(this IntermediateTuple tuple, int index, string value) { + if (value == default && NoFieldMetadata(tuple, index)) + { + return tuple.Fields[index] = null; + } + var definition = tuple.Definition.FieldDefinitions[index]; var field = tuple.Fields[index].Set(definition, value); return tuple.Fields[index] = field; } + + private static bool NoFieldMetadata(IntermediateTuple tuple, int index) + { + var field = tuple?.Fields[index]; + + return field?.Context == null && field?.PreviousValue == null; + } } } diff --git a/src/WixToolset.Data/Tuples/RemoveRegistryTuple.cs b/src/WixToolset.Data/Tuples/RemoveRegistryTuple.cs index 42f15660..f4ec17bf 100644 --- a/src/WixToolset.Data/Tuples/RemoveRegistryTuple.cs +++ b/src/WixToolset.Data/Tuples/RemoveRegistryTuple.cs @@ -51,7 +51,7 @@ namespace WixToolset.Data.Tuples public RegistryRootType Root { - get => (RegistryRootType)this.Fields[(int)RemoveRegistryTupleFields.Root]?.AsNumber(); + get => (RegistryRootType)this.Fields[(int)RemoveRegistryTupleFields.Root].AsNumber(); set => this.Set((int)RemoveRegistryTupleFields.Root, (int)value); } diff --git a/src/WixToolset.Data/Tuples/WixBundleTuple.cs b/src/WixToolset.Data/Tuples/WixBundleTuple.cs index f7cd4f23..8f6f760a 100644 --- a/src/WixToolset.Data/Tuples/WixBundleTuple.cs +++ b/src/WixToolset.Data/Tuples/WixBundleTuple.cs @@ -32,7 +32,7 @@ namespace WixToolset.Data new IntermediateFieldDefinition(nameof(WixBundleTupleFields.UpgradeCode), IntermediateFieldType.String), new IntermediateFieldDefinition(nameof(WixBundleTupleFields.BundleId), IntermediateFieldType.String), new IntermediateFieldDefinition(nameof(WixBundleTupleFields.ProviderKey), IntermediateFieldType.String), - new IntermediateFieldDefinition(nameof(WixBundleTupleFields.PerMachine), IntermediateFieldType.Number), + new IntermediateFieldDefinition(nameof(WixBundleTupleFields.PerMachine), IntermediateFieldType.Bool), }, typeof(WixBundleTuple)); } @@ -211,9 +211,9 @@ namespace WixToolset.Data.Tuples set => this.Set((int)WixBundleTupleFields.ProviderKey, value); } - public int PerMachine + public bool PerMachine { - get => (int)this.Fields[(int)WixBundleTupleFields.PerMachine]; + get => (bool)this.Fields[(int)WixBundleTupleFields.PerMachine]; set => this.Set((int)WixBundleTupleFields.PerMachine, value); } } -- cgit v1.2.3-55-g6feb