Skip to content

Commit

Permalink
Refactor TypeValidator in prep for #691
Browse files Browse the repository at this point in the history
  • Loading branch information
anthony-c-martin committed Apr 23, 2021
1 parent 3ebfc28 commit 271ab22
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 103 deletions.
2 changes: 1 addition & 1 deletion src/Bicep.Core/Emit/TemplateWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ private void EmitParameter(JsonTextWriter jsonWriter, ParameterSymbol parameterS

case ObjectSyntax modifierSyntax:
// this would throw on duplicate properties in the object node - we are relying on emitter checking for errors at the beginning
var properties = modifierSyntax.ToKnownPropertyValueDictionary();
var properties = modifierSyntax.ToNamedPropertyValueDictionary();

emitter.EmitProperty("type", GetTemplateTypeName(primitiveType, IsSecure(properties.TryGetValue("secure"))));

Expand Down
29 changes: 17 additions & 12 deletions src/Bicep.Core/Syntax/ObjectSyntaxExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using Bicep.Core.Extensions;
Expand All @@ -10,25 +11,29 @@ namespace Bicep.Core.Syntax
public static class ObjectSyntaxExtensions
{
/// <summary>
/// Converts a syntactically valid object syntax node to a hashset of property name strings. Will throw if you provide a node with duplicate properties.
/// Converts a syntactically valid object syntax node to a dictionary mapping property name strings to property syntax nodes. Returns the first property in the case of duplicate names.
/// </summary>
/// <param name="syntax">The object syntax node</param>
public static ImmutableHashSet<string> ToKnownPropertyNames(this ObjectSyntax syntax) =>
syntax.Properties.Select(p => p.TryGetKeyText()).ToImmutableHashSetExcludingNull(LanguageConstants.IdentifierComparer);
public static ImmutableDictionary<string, ObjectPropertySyntax> ToNamedPropertyDictionary(this ObjectSyntax syntax)
{
var dictionary = new Dictionary<string, ObjectPropertySyntax>(LanguageConstants.IdentifierComparer);
foreach (var property in syntax.Properties)
{
if (property.TryGetKeyText() is {} key && !dictionary.ContainsKey(key))
{
dictionary[key] = property;
}
}

/// <summary>
/// Converts a syntactically valid object syntax node to a dictionary mapping property name strings to property value expressions. Will throw if you provide a node with duplicate properties.
/// </summary>
/// <param name="syntax">The object syntax node</param>
public static ImmutableDictionary<string, SyntaxBase> ToKnownPropertyValueDictionary(this ObjectSyntax syntax) =>
syntax.Properties.ToImmutableDictionaryExcludingNull(p => p.TryGetKeyText(), p => p.Value, LanguageConstants.IdentifierComparer);
return dictionary.ToImmutableDictionary(LanguageConstants.IdentifierComparer);
}

/// <summary>
/// Converts a syntactically valid object syntax node to a dictionary mapping property name strings to property syntax nodes. Will throw if you provide a node with duplicate properties.
/// Converts a syntactically valid object syntax node to a dictionary mapping property name strings to property syntax node values. Returns the first property value in the case of duplicate names.
/// </summary>
/// <param name="syntax">The object syntax node</param>
public static ImmutableDictionary<string, ObjectPropertySyntax> ToNamedPropertyDictionary(this ObjectSyntax syntax) =>
syntax.Properties.ToImmutableDictionaryExcludingNull(p => p.TryGetKeyText(), LanguageConstants.IdentifierComparer);
public static ImmutableDictionary<string, SyntaxBase> ToNamedPropertyValueDictionary(this ObjectSyntax syntax)
=> ToNamedPropertyDictionary(syntax).ToImmutableDictionary(x => x.Key, x => x.Value.Value, LanguageConstants.IdentifierComparer);

/// <summary>
/// Returns the specified property by name on any valid or invalid object syntax node if there is exactly one property by that name.
Expand Down
Loading

0 comments on commit 271ab22

Please sign in to comment.