-
Notifications
You must be signed in to change notification settings - Fork 841
Revamp data classification model. #4593
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
144 changes: 144 additions & 0 deletions
144
...ries/Microsoft.Extensions.Compliance.Abstractions/Classification/DataClassificationSet.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using Microsoft.Shared.Diagnostics; | ||
| using Microsoft.Shared.Pools; | ||
|
|
||
| namespace Microsoft.Extensions.Compliance.Classification; | ||
|
|
||
| /// <summary> | ||
| /// Represents a set of data classes. | ||
geeknoid marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| /// </summary> | ||
| public sealed class DataClassificationSet : IEquatable<DataClassificationSet> | ||
| { | ||
| private readonly HashSet<DataClassification> _classifications = []; | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="DataClassificationSet"/> class. | ||
| /// </summary> | ||
| /// <param name="classification">The class to include in the set.</param> | ||
| public DataClassificationSet(DataClassification classification) | ||
| { | ||
| _ = _classifications.Add(classification); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="DataClassificationSet"/> class. | ||
| /// </summary> | ||
| /// <param name="classifications">The classes to include in the set.</param> | ||
| public DataClassificationSet(IEnumerable<DataClassification> classifications) | ||
| { | ||
| _ = Throw.IfNull(classifications); | ||
| _classifications.UnionWith(classifications); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="DataClassificationSet"/> class. | ||
| /// </summary> | ||
| /// <param name="classifications">The classes to include in the set.</param> | ||
| public DataClassificationSet(params DataClassification[] classifications) | ||
| { | ||
| _ = Throw.IfNull(classifications); | ||
| _classifications.UnionWith(classifications); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Converts a data classification to a data classification set. | ||
| /// </summary> | ||
| /// <param name="classification">The classification to include in the set.</param> | ||
| public static implicit operator DataClassificationSet(DataClassification classification) | ||
| { | ||
| return FromDataClassification(classification); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Converts a data classification to a data classification set. | ||
| /// </summary> | ||
| /// <param name="classification">The classification to include in the set.</param> | ||
| /// <returns>The resulting data classification set.</returns> | ||
| public static DataClassificationSet FromDataClassification(DataClassification classification) => new(classification); | ||
|
|
||
| /// <summary> | ||
| /// Creates a new data classification that combines the current classifications with another set. | ||
| /// </summary> | ||
| /// <param name="other">The other set.</param> | ||
| /// <returns>The resulting set which combines the current instance's classifications and the other ones.</returns> | ||
geeknoid marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| public DataClassificationSet Union(DataClassificationSet other) | ||
| { | ||
| _ = Throw.IfNull(other); | ||
|
|
||
| var result = new DataClassificationSet(other._classifications); | ||
| result._classifications.UnionWith(_classifications); | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets a hash code for the current object instance. | ||
| /// </summary> | ||
| /// <returns>The hash code value.</returns> | ||
| public override int GetHashCode() => _classifications.GetHashCode(); | ||
|
|
||
| /// <summary> | ||
| /// Compares an object with the current instance to see if they contain the same data classes. | ||
| /// </summary> | ||
| /// <param name="obj">The other data classification to compare to.</param> | ||
| /// <returns><see langword="true"/> if the two sets contain the same classifications.</returns> | ||
| public override bool Equals(object? obj) => Equals(obj as DataClassificationSet); | ||
|
|
||
| /// <summary> | ||
| /// Compares an object with the current instance to see if they contain the same data classes. | ||
| /// </summary> | ||
| /// <param name="other">The other data classification to compare to.</param> | ||
| /// <returns><see langword="true"/> if the two sets contain the same classifications.</returns> | ||
| public bool Equals(DataClassificationSet? other) | ||
| { | ||
| if (other == null) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| return _classifications.SetEquals(other._classifications); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Returns a string representation of this object. | ||
| /// </summary> | ||
| /// <returns>The string representation of this object.</returns> | ||
| public override string ToString() | ||
| { | ||
| var sb = PoolFactory.SharedStringBuilderPool.Get(); | ||
|
|
||
| var a = _classifications.ToArray(); | ||
| Array.Sort(a, (x, y) => | ||
xakep139 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| var result = string.Compare(x.TaxonomyName, y.TaxonomyName, StringComparison.Ordinal); | ||
| if (result == 0) | ||
| { | ||
| result = string.Compare(x.Value, y.Value, StringComparison.Ordinal); | ||
| } | ||
|
|
||
| return result; | ||
| }); | ||
|
|
||
| _ = sb.Append('['); | ||
| foreach (var c in a) | ||
| { | ||
| if (sb.Length > 1) | ||
| { | ||
| _ = sb.Append(','); | ||
| } | ||
|
|
||
| _ = sb.Append(c); | ||
| } | ||
|
|
||
| _ = sb.Append(']'); | ||
| var result = sb.ToString(); | ||
| PoolFactory.SharedStringBuilderPool.Return(sb); | ||
|
|
||
| return result; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.