Skip to content
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

[WIP] Add unknown member to enums #1504

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion Octokit.Tests.Conventions/TypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,18 @@ private static bool HasGenericTypeDefinition(this Type type, Type genericTypeDef
}
}

public enum TypeCategory { Other, Task, GenericTask, ReadOnlyList, ClientInterface }
public enum TypeCategory
{
Other,
Task,
GenericTask,
ReadOnlyList,
ClientInterface,
/// <summary>
/// Used as a placeholder for unknown fields
/// </summary>
UnknownType
}

public struct TypeInfo
{
Expand Down
6 changes: 5 additions & 1 deletion Octokit.Tests/Models/RequestParametersTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,11 @@ public enum Enomnomnom
Yuck,

[Parameter(Value = "noms")]
NomNomNom
NomNomNom,
/// <summary>
/// Used as a placeholder for unknown fields
/// </summary>
UnknownType
}
}
}
Expand Down
8 changes: 6 additions & 2 deletions Octokit.Tests/SimpleJsonSerializerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ public void DeserializesEnum()

Assert.Equal(SomeEnum.Unicode, sample.SomeEnum);
}

[Fact]
public void RemovesDashFromEnums()
{
Expand Down Expand Up @@ -364,7 +364,11 @@ public enum SomeEnum
Utf8,
[Parameter(Value = "something else")]
SomethingElse,
Unicode
Unicode,
/// <summary>
/// Used as a placeholder for unknown fields
/// </summary>
UnknownType
}

}
6 changes: 5 additions & 1 deletion Octokit/Authentication/AuthenticationType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ public enum AuthenticationType
/// <summary>
/// Delegated access to a third party
/// </summary>
Oauth
Oauth,
/// <summary>
/// Used as a placeholder for unknown fields
/// </summary>
UnknownType
}
}
7 changes: 6 additions & 1 deletion Octokit/Clients/Enterprise/EnterpriseProbe.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@ public enum EnterpriseProbeResult
/// Request timed out or DNS failed. So it's probably the case it's not an enterprise server but
/// we can't know for sure.
/// </summary>
Failed
Failed,

/// <summary>
/// Used as a placeholder for unknown fields
/// </summary>
UnknownType
}
}
7 changes: 6 additions & 1 deletion Octokit/Clients/IRepositoryContentsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,11 @@ public enum ArchiveFormat
/// </summary>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Zipball")]
[Parameter(Value = "zipball")]
Zipball
Zipball,

/// <summary>
/// Used as a placeholder for unknown fields
/// </summary>
UnknownType
}
}
13 changes: 11 additions & 2 deletions Octokit/Clients/OrganizationMembersClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ public enum OrganizationMembersFilter
/// Members without two-factor authentication enabled
/// </summary>
[Parameter(Value = "2fa_disabled")]
TwoFactorAuthenticationDisabled
TwoFactorAuthenticationDisabled,
/// <summary>
/// Used as a placeholder for unknown fields
/// </summary>
UnknownType
}

public enum OrganizationMembersRole
Expand All @@ -34,7 +38,12 @@ public enum OrganizationMembersRole
Admin,

[Parameter(Value = "member")]
Member
Member,

/// <summary>
/// Used as a placeholder for unknown fields
/// </summary>
UnknownType
}

/// <summary>
Expand Down
7 changes: 6 additions & 1 deletion Octokit/Exceptions/TwoFactorAuthorizationException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@ public enum TwoFactorType
/// <summary>
/// Receive via application
/// </summary>
AuthenticatorApp
AuthenticatorApp,

/// <summary>
/// Used as a placeholder for unknown fields
/// </summary>
UnknownType
}
}
15 changes: 13 additions & 2 deletions Octokit/Http/SimpleJsonSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,19 @@ public override object DeserializeObject(object value, Type type)
//dictionary does not contain enum value and has no custom attribute. So add it for future loops and return value
// remove '-' from values coming in to be able to enum utf-8
stringValue = RemoveHyphenAndUnderscore(stringValue);
var parsed = Enum.Parse(type, stringValue, ignoreCase: true);
_cachedEnums[type].Add(value, parsed);
object parsed = new object();
try
{
parsed = Enum.Parse(type, stringValue, ignoreCase: true);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should use Enum.TryParse() method here, rather than deliberately throwing/catching an exception

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To use TryParse i need an instance TEnum. How can i achieve this from Type type

Enum.TryParse(stringValue, true, ????);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah yeah sorry i didnt look at the diff context and see now that it's inside a non generic method etc, so the way you've done it is ok

_cachedEnums[type].Add(value, parsed);
}
catch(ArgumentException)
{
//We have a value that is actually not a member of the enum. So add it on the fly and maybe create an issue so that we get informed
parsed = Enum.Parse(type, "unknowntype", ignoreCase: true);
_cachedEnums[type].Add(value, parsed);
}

return parsed;
}
}
Expand Down
35 changes: 30 additions & 5 deletions Octokit/Models/Request/IssueRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,12 @@ public enum IssueFilter
/// <summary>
/// All issues the authenticated user can see, regardless of participation or creation.
/// </summary>
All
All,

/// <summary>
/// Used as a placeholder for unknown fields
/// </summary>
UnknownType
}

/// <summary>
Expand All @@ -138,7 +143,12 @@ public enum ItemStateFilter
/// <summary>
/// All the items.
/// </summary>
All
All,

/// <summary>
/// Used as a placeholder for unknown fields
/// </summary>
UnknownType
}

/// <summary>
Expand All @@ -154,7 +164,12 @@ public enum ItemState
/// <summary>
/// Items that are closed
/// </summary>
Closed
Closed,

/// <summary>
/// Used as a placeholder for unknown fields
/// </summary>
UnknownType
}

/// <summary>
Expand All @@ -175,7 +190,12 @@ public enum IssueSort
/// <summary>
/// Sort by the number of comments
/// </summary>
Comments
Comments,

/// <summary>
/// Used as a placeholder for unknown fields
/// </summary>
UnknownType
}

/// <summary>
Expand All @@ -193,6 +213,11 @@ public enum SortDirection
/// Sort descending
/// </summary>
[Parameter(Value = "desc")]
Descending
Descending,

/// <summary>
/// Used as a placeholder for unknown fields
/// </summary>
UnknownType
}
}
7 changes: 6 additions & 1 deletion Octokit/Models/Request/MergePullRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ public enum PullRequestMergeMethod
/// <summary>
/// Rebase and merge
/// </summary>
Rebase
Rebase,

/// <summary>
/// Used as a placeholder for unknown fields
/// </summary>
UnknownType
}
}
6 changes: 5 additions & 1 deletion Octokit/Models/Request/MilestoneRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ public enum MilestoneSort
{
[Parameter(Value = "due_date")]
DueDate,
Completeness
Completeness,
/// <summary>
/// Used as a placeholder for unknown fields
/// </summary>
UnknownType
}
}
7 changes: 6 additions & 1 deletion Octokit/Models/Request/NewDeployment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ public enum DeployTask
/// Deploy migrations only.
/// </summary>
[Parameter(Value = "deploy:migrations")]
DeployMigrations
DeployMigrations,

/// <summary>
/// Used as a placeholder for unknown fields
/// </summary>
UnknownType
}
}
6 changes: 5 additions & 1 deletion Octokit/Models/Request/NewRepositoryWebHook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ Dictionary<string, string> GetWebHookConfig()
public enum WebHookContentType
{
Form,
Json
Json,
/// <summary>
/// Used as a placeholder for unknown fields
/// </summary>
UnknownType
}
}
7 changes: 6 additions & 1 deletion Octokit/Models/Request/Permission.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ public enum Permission
/// <summary>
/// team members can pull, but not push to or administer these repositories
/// </summary>
Pull
Pull,

/// <summary>
/// Used as a placeholder for unknown fields
/// </summary>
UnknownType
}
}
6 changes: 5 additions & 1 deletion Octokit/Models/Request/PullRequestRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ public enum PullRequestSort
/// Sort by age (filtering by pulls updated in the last month)
/// </summary>
[Parameter(Value = "long-running")]
LongRunning
LongRunning,
/// <summary>
/// Used as a placeholder for unknown fields
/// </summary>
UnknownType
}
}
7 changes: 6 additions & 1 deletion Octokit/Models/Request/RepositoryForksListRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ public enum Sort
/// <summary>
/// Sort by the number of stargazers.
/// </summary>
Stargazers
Stargazers,

/// <summary>
/// Used as a placeholder for unknown fields
/// </summary>
UnknownType
}
}
28 changes: 24 additions & 4 deletions Octokit/Models/Request/RepositoryRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,12 @@ public enum RepositoryType
/// <summary>
/// Return repositories for which the current authenticated user is a member of the org or team.
/// </summary>
Member
Member,

/// <summary>
/// Used as a placeholder for unknown fields
/// </summary>
UnknownType
}

/// <summary>
Expand All @@ -129,7 +134,12 @@ public enum RepositorySort
/// Sort by the repository name.
/// </summary>
[Parameter(Value = "full_name")]
FullName
FullName,

/// <summary>
/// Used as a placeholder for unknown fields
/// </summary>
UnknownType
}

/// <summary>
Expand All @@ -150,7 +160,12 @@ public enum RepositoryVisibility
/// <summary>
/// Return both public and private repositories
/// </summary>
All
All,

/// <summary>
/// Used as a placeholder for unknown fields
/// </summary>
UnknownType
}

/// <summary>
Expand Down Expand Up @@ -197,6 +212,11 @@ public enum RepositoryAffiliation
/// Returns all repositories where user is owner,collaborator or organization member.
/// </summary>
[Parameter(Value = "owner, collaborator, organization_member")]
All
All,

/// <summary>
/// Used as a placeholder for unknown fields
/// </summary>
UnknownType
}
}
12 changes: 10 additions & 2 deletions Octokit/Models/Request/SearchCodeRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -223,14 +223,22 @@ internal string DebuggerDisplay
public enum CodeSearchSort
{
[Parameter(Value = "indexed")]
Indexed
Indexed,
/// <summary>
/// Used as a placeholder for unknown fields
/// </summary>
UnknownType
}

public enum CodeInQualifier
{
[Parameter(Value = "file")]
File,
[Parameter(Value = "path")]
Path
Path,
/// <summary>
/// Used as a placeholder for unknown fields
/// </summary>
UnknownType
}
}
Loading