Skip to content

Commit

Permalink
Fixed resource tags ToHashtable method microsoft#798
Browse files Browse the repository at this point in the history
  • Loading branch information
BernieWhite committed Sep 6, 2021
1 parent d9fdce1 commit 0d9d157
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 2 deletions.
5 changes: 5 additions & 0 deletions docs/CHANGELOG-v1.md
Expand Up @@ -10,6 +10,11 @@ See [upgrade notes][upgrade-notes] for helpful information when upgrading from p

## Unreleased

What's changed since v1.7.0:

- Bug fixes:
- Fixed ResourceTags does not contain a method named ToHashtable. [#798](https://github.com/microsoft/PSRule/issues/798)

## v1.7.0

What's changed since v1.6.0:
Expand Down
14 changes: 13 additions & 1 deletion src/PSRule/Definitions/Resource.cs
Expand Up @@ -139,6 +139,9 @@ public sealed class ResourceTags : Dictionary<string, string>
{
public ResourceTags() : base(StringComparer.OrdinalIgnoreCase) { }

/// <summary>
/// Convert from a hashtable to resource tags.
/// </summary>
internal static ResourceTags FromHashtable(Hashtable hashtable)
{
if (hashtable == null || hashtable.Count == 0)
Expand All @@ -151,6 +154,9 @@ internal static ResourceTags FromHashtable(Hashtable hashtable)
return tags;
}

/// <summary>
/// Convert from a dictionary of string pairs to resource tags.
/// </summary>
internal static ResourceTags FromDictionary(Dictionary<string, string> dictionary)
{
if (dictionary == null)
Expand All @@ -163,11 +169,17 @@ internal static ResourceTags FromDictionary(Dictionary<string, string> dictionar
return tags;
}

internal Hashtable ToHashtable()
/// <summary>
/// Convert resource tags to a hashtable.
/// </summary>
public Hashtable ToHashtable()
{
return new Hashtable(this, StringComparer.OrdinalIgnoreCase);
}

/// <summary>
/// Check if a specific resource tag exists.
/// </summary>
internal bool Contains(object key, object value)
{
if (key == null || value == null || !(key is string k) || !ContainsKey(k))
Expand Down
1 change: 0 additions & 1 deletion src/PSRule/Definitions/Rules/Rule.cs
Expand Up @@ -38,7 +38,6 @@ public RuleV1(string apiVersion, SourceFile source, ResourceMetadata metadata, R
string ILanguageBlock.Id => Name;
}


internal sealed class RuleV1Spec : Spec, IRuleSpec
{
public LanguageIf Condition { get; set; }
Expand Down
3 changes: 3 additions & 0 deletions tests/PSRule.Tests/RulesTests.cs
Expand Up @@ -26,6 +26,9 @@ public void ReadYamlRule()
var rule = HostHelper.GetRuleYaml(GetSource(), context).ToArray();
Assert.NotNull(rule);
Assert.Equal("BasicRule", rule[0].RuleName);

var hashtable = rule[0].Tag.ToHashtable();
Assert.Equal("tag", hashtable["feature"]);
}

[Fact]
Expand Down

0 comments on commit 0d9d157

Please sign in to comment.