Skip to content

Commit

Permalink
Closes #85.
Browse files Browse the repository at this point in the history
  • Loading branch information
scottdorman committed Nov 18, 2020
1 parent 82d9881 commit cdbbab1
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 23 deletions.
40 changes: 38 additions & 2 deletions src/Cadru.Scim/Cadru.Scim.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion src/Cadru.Scim/FilterExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,13 @@ public class FilterExpression : IFilterExpression
/// <inheritdoc/>
public string ToFilterExpression(bool prependQuerySeprator = true)
{
return $@"{(prependQuerySeprator ? "?" : "")}filter={ this }";
return this.ToFilterExpression(new FilterExpressionFormatOptions { IncludeQuerySeparator = prependQuerySeprator });
}

/// <inheritdoc/>
public string ToFilterExpression(FilterExpressionFormatOptions options)
{
return $@"{(options.IncludeQuerySeparator ? "?" : "")}{(options.IncludeFilterParameterName ? "filter=" : "")}{ this }";
}

/// <summary>
Expand Down
42 changes: 42 additions & 0 deletions src/Cadru.Scim/FilterExpressionFormatOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//------------------------------------------------------------------------------
// <copyright file="IFilter.cs"
// company="Scott Dorman"
// library="Cadru">
// Copyright (C) 2001-2020 Scott Dorman.
// </copyright>
//
// <license>
// Licensed under the Microsoft Public License (Ms-PL) (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://opensource.org/licenses/Ms-PL.html
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </license>
//------------------------------------------------------------------------------

namespace Cadru.Scim.Filters
{
/// <summary>
/// Provides options to be used when formatting an <see cref="IFilter"/>.
/// </summary>
public sealed class FilterExpressionFormatOptions
{
/// <summary>
/// To prepend the "?" query string separator,
/// <see langword="true"/>; otherwise, <see langword="false"/>
/// </summary>
public bool IncludeQuerySeparator { get; set; } = true;

/// <summary>
/// To prepend the "filter=" query string parameter key,
/// <see langword="true"/>; otherwise, <see langword="false"/>
/// </summary>
public bool IncludeFilterParameterName { get; set; } = true;
}
}
29 changes: 12 additions & 17 deletions src/Cadru.Scim/FilterGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,13 @@ public class FilterGroup : IFilterGroup
/// <inheritdoc/>
public string ToFilterExpression(bool prependQuerySeprator = true)
{
return $"?filter={ this }";
return this.ToFilterExpression(new FilterExpressionFormatOptions { IncludeQuerySeparator = prependQuerySeprator });
}

/// <inheritdoc/>
public string ToFilterExpression(FilterExpressionFormatOptions options)
{
return $@"{(options.IncludeQuerySeparator ? "?" : "")}{(options.IncludeFilterParameterName ? "filter=" : "")}{ this }";
}

/// <summary>
Expand All @@ -52,23 +58,12 @@ public string ToFilterExpression(bool prependQuerySeprator = true)
/// <returns>A string that represents the current <see cref="FilterGroup"></see>.</returns>
public override string ToString()
{
var brackets = Array.Empty<string>();
switch (this.GroupingCharacter)
var brackets = this.GroupingCharacter switch
{
case GroupingCharacter.Parentheses:
brackets = new string[]
{
"(", ")"
};
break;

case GroupingCharacter.SquareBracket:
brackets = new string[]
{
"[", "]"
};
break;
}
GroupingCharacter.Parentheses => new string[] { "(", ")" },
GroupingCharacter.SquareBracket => new string[] { "[", "]" },
_ => new string[] { String.Empty, String.Empty }
};

return $"{ brackets[0] }{ String.Join($" { this.LogicalOperator } ", this.Filters) }{ brackets[1] }";
}
Expand Down
14 changes: 12 additions & 2 deletions src/Cadru.Scim/IFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,23 @@ public interface IFilter
{
/// <summary>
/// Returns a string that represents the current
/// <see cref="FilterExpression"></see> as a valid query
/// <see cref="IFilter"></see> as a valid query
/// </summary>
/// <param name="prependQuerySeprator">
/// To prepend the "?" query string separator,
/// <see langword="true"></see>; otherwise, <see langword="false"></see>.
/// </param>
/// <returns>A string that represents the current <see cref="FilterExpression"></see>.</returns>
/// <returns>A string that represents the current <see cref="IFilter"/>.</returns>
string ToFilterExpression(bool prependQuerySeprator = true);

/// <summary>
/// Returns a string that represents the current
/// <see cref="IFilter"></see> as a valid query
/// </summary>
/// <param name="options">
/// The options to use when formatting the <see cref="IFilter"/>.
/// </param>
/// <returns>A string that represents the current <see cref="IFilter"/>.</returns>
string ToFilterExpression(FilterExpressionFormatOptions options);
}
}
8 changes: 7 additions & 1 deletion src/Cadru.Scim/NotFilterGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,13 @@ public class NotFilterGroup : IFilterGroup
/// <inheritdoc/>
public string ToFilterExpression(bool prependQuerySeprator = true)
{
return $"?filter={ this }";
return this.ToFilterExpression(new FilterExpressionFormatOptions { IncludeQuerySeparator = prependQuerySeprator });
}

/// <inheritdoc/>
public string ToFilterExpression(FilterExpressionFormatOptions options)
{
return $@"{(options.IncludeQuerySeparator ? "?" : "")}{(options.IncludeFilterParameterName ? "filter=" : "")}{ this }";
}

/// <summary>
Expand Down

0 comments on commit cdbbab1

Please sign in to comment.