Files
octokit.net/Octokit/Models/Request/SearchUsersRequest.cs
Kristian Hellang 5ee4d64046 Add StringEnum to handle unknown enum values returned from API (#1595)
* Added StringEnum<TEnum>

* Added tests

* Make sure the serializer can work with StringEnum

* Use StringEnum for EventInfo.Event

* Add convention test to assert that all Response models use StringEnum<> to wrap enum properties

* Add Stringnum<> to all response types failing convention test

* Handle StringEnum to Enum conversion when Issue response model populates IssueUpdate request model

* Fix unit test

* Refactor SimpleJsonSerializer to expose the DeserializeEnum strategy so it can be used in StringEnum class

* Need to expose/use SerializeEnum functionality too, so we use the correct string representation of enum values that have custom properties (eg ReactionType Plus1 to "+1")

* fix unit tests, since the string is now the "correct" upstream api value

* Add a couple of tests for the Enum serialize/deserialize when underscores, hyphens and custom property attributes are present

* Compare parsed values for equality

* add convention test to ensure enum members all have Parameter property set

* update test to cover implicit conversions too

* this test should work but fails at the moment due to magic hyphen removal in deserializer causing a one way trip from utf-8 to EncodingType.Utf8 with no way to get back

* (unsuccesfully) expand event info test to try to catch more cases of unknown event types

* fix broken integration test while im here

* Fixed build errors after .NET Core merge

* Value -> StringValue, ParsedValue -> Value

* Don't allow StringValue to be null

* Ignore enums not used in request/response models

* Added ParameterAttribute to almost all enum values

* Ignore Language enum

* Fix failing tests

* Fix milestone sort parameter and tests

* whitespace

* fix milestone unit tests

* Fix StringEnum.Equals ... This could've been embarrassing!

* Change SimpleJsonSerializer Enum handling to only use `[Parameter()]` attributes (no more magic removal of hyphen/underscores from strings)

* Tidy up this integration test while im here

* Only test request/response enums in convention test

* Keep skipping Language

* Remove unused method

* Remove excluded enum types

* Removed unnecessary ParameterAttributes

* Remove unused enum

* Add StringEnum test for string-comparison of two invalid values

* Bring back IssueCommentSort and use it in IssueCommentRequest

This reverts commit 38a4a291d1476ef8c992fe0f76956974b6f32a49.

* Use assembly instead of namespace for Octokit check

* Add failing test to reproduce the issue where only the first enum paramter/value was added to the cache

* Fix deserializer enum cache to include all enum members rather than only the first member encountered

* Use a static SimpleJsonSerializer in StringEnum

* Remove serializer instance in StringEnum

* Add some documentation on StringEnum<TEnum>

* Fix parameter value to resolve failing integration test
2017-06-25 19:29:57 +10:00

204 lines
6.4 KiB
C#

using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Linq;
using Octokit.Internal;
namespace Octokit
{
/// <summary>
/// Searching Users
/// </summary>
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public class SearchUsersRequest : BaseSearchRequest
{
/// <summary>
/// Initializes a new instance of the <see cref="SearchUsersRequest"/> class.
/// </summary>
/// <param name="term">The search term.</param>
public SearchUsersRequest(string term) : base(term)
{
}
/// <summary>
/// Optional Sort field. One of followers, repositories, or joined. If not provided (null), results are sorted by best match.
/// <remarks>https://help.github.com/articles/searching-users#sorting</remarks>
/// </summary>
public UsersSearchSort? SortField { get; set; }
/// <summary>
/// The sort field as a string.
/// </summary>
public override string Sort
{
get { return SortField.ToParameter(); }
}
/// <summary>
/// Filter users based on the number of followers they have.
/// <remarks>https://help.github.com/articles/searching-users#followers</remarks>
/// </summary>
public Range Followers { get; set; }
/// <summary>
/// Filter users based on when they joined.
/// <remarks>https://help.github.com/articles/searching-users#created</remarks>
/// </summary>
public DateRange Created { get; set; }
/// <summary>
/// Filter users by the location indicated in their profile.
/// <remarks>https://help.github.com/articles/searching-users#location</remarks>
/// </summary>
public string Location { get; set; }
/// <summary>
/// Filters users based on the number of repositories they have.
/// <remarks>https://help.github.com/articles/searching-users#repository-count</remarks>
/// </summary>
public Range Repositories { get; set; }
/// <summary>
/// Search for users that have repositories that match a certain language.
/// <remarks>https://help.github.com/articles/searching-users#language</remarks>
/// </summary>
public Language? Language { get; set; }
/// <summary>
/// With this qualifier you can restrict the search to just personal accounts or just organization accounts.
/// <remarks>https://help.github.com/articles/searching-users#type</remarks>
/// </summary>
public AccountSearchType? AccountType { get; set; }
private IEnumerable<UserInQualifier> _inQualifier;
/// <summary>
/// Qualifies which fields are searched. With this qualifier you can restrict the search to just the username, public email, full name, or any combination of these.
/// <remarks>https://help.github.com/articles/searching-users#search-in</remarks>
/// </summary>
public IEnumerable<UserInQualifier> In
{
get
{
return _inQualifier;
}
set
{
if (value != null && value.Any())
_inQualifier = value.Distinct().ToList();
}
}
public override IReadOnlyList<string> MergedQualifiers()
{
var parameters = new List<string>();
if (AccountType != null)
{
parameters.Add(string.Format(CultureInfo.InvariantCulture, "type:{0}", AccountType));
}
if (In != null)
{
parameters.Add(string.Format(CultureInfo.InvariantCulture, "in:{0}", string.Join(",", In)));
}
if (Repositories != null)
{
parameters.Add(string.Format(CultureInfo.InvariantCulture, "repos:{0}", Repositories));
}
if (Location.IsNotBlank())
{
parameters.Add(string.Format(CultureInfo.InvariantCulture, "location:{0}", Location));
}
if (Language != null)
{
parameters.Add(string.Format(CultureInfo.InvariantCulture, "language:{0}", Language));
}
if (Created != null)
{
parameters.Add(string.Format(CultureInfo.InvariantCulture, "created:{0}", Created));
}
if (Followers != null)
{
parameters.Add(string.Format(CultureInfo.InvariantCulture, "followers:{0}", Followers));
}
return new ReadOnlyCollection<string>(parameters);
}
internal string DebuggerDisplay
{
get
{
return string.Format(CultureInfo.InvariantCulture, "Term: {0} Sort: {1}", Term, Sort);
}
}
}
/// <summary>
/// Account Type used to filter search result
/// </summary>
public enum AccountSearchType
{
/// <summary>
/// User account
/// </summary>
[Parameter(Value = "user")]
User,
/// <summary>
/// Organization account
/// </summary>
[Parameter(Value = "org")]
Org
}
/// <summary>
/// User type to filter search results
/// </summary>
public enum UserInQualifier
{
/// <summary>
/// Search by the username
/// </summary>
[SuppressMessage("Microsoft.Naming", "CA1702:CompoundWordsShouldBeCasedCorrectly", MessageId = "Username")]
[Parameter(Value = "login")]
Username,
/// <summary>
/// Search by the user's email address
/// </summary>
[Parameter(Value = "email")]
Email,
/// <summary>
/// Search by the user's full name
/// </summary>
[SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Fullname")]
[Parameter(Value = "fullname")]
Fullname
}
/// <summary>
///
/// </summary>
public enum UsersSearchSort
{
[Parameter(Value = "followers")]
Followers,
[Parameter(Value = "repositories")]
Repositories,
[Parameter(Value = "joined")]
Joined
}
}