-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Conversation
@ryangribble, waht do you mean with
|
What I meant by that comment was to have a string field containing the actual API response as well as the enum field containing the (attempted) deserialized enum value. Eg say the field is |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We also need to make sure to only do this for enums in response fields, we don't want to add this to enums only used on requests
object parsed = new object(); | ||
try | ||
{ | ||
parsed = Enum.Parse(type, stringValue, ignoreCase: true); |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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, ????);
There was a problem hiding this comment.
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
So, that means that we search all response types which have an enum, add a |
Yep that's what I was suggesting, although am interested in feedback from yourself and others, if this "feels" good... |
Feels good to me |
I think it's a good solution. My only concerns was that we miss if any new values are added by the api. So like @M-Zuber says, we should implement a mechanism which inform us if there is a new added member. |
That idea was only to allow us to then properly add the values into the enum, but I don't see a way to have As long as we can safely parse any value by having a |
Ok, then lets go this way. |
Sorry @maddin2016 I may not have been clear enough here. The changes you've made wont achieve the desired result if you consider this example using your method public class SampleResponse
{
public enum MyEnum { Value1, Value2, Unknown };
public MyEnum? MyProperty { get; protected set; }
public string MyPropertyText { get { return MyProperty.ToString(); }
} And say the API response contains json With your current changes we will get There is also the case that when the field is So what we actually need to do is something similar to the way we sometimes handle unix timestamps where we map the actual api response field ( something like this: public enum MyEnum
{
Value1,
Value2,
Unknown
}
public class SampleResponse
{
[Parameter(Key = "ignoreThisField")]
public MyEnum? MyProperty { get { return MyPropertyText.ParseEnumWithDefault<MyEnum>(MyEnum.Unknown); } }
[Parameter(Key = "my_property")]
public string MyPropertyText { get; protected set; }
} With the helper function StringExtensions.cs public static TEnum? ParseEnumWithDefault<TEnum>(this string input, TEnum defaultValue) where TEnum : struct
{
if (input == null) return null;
TEnum value;
return Enum.TryParse(input, out value) ? value : defaultValue;
} Thoughts? |
Ok. Now it's clear to me. I already had this in mind, but i don't know how to implement. Many thanks!! |
Cool... A couple more thoughts:
Im not sure if these changes are getting to kludgy though, need some feedback from @shiftkey if we are heading too far down a rabbit hole. I guess the alternative was throwing exceptions whenever an API response changed which wasnt great either :) |
Ok, then i wait for feedback from @shiftkey before going on. |
And what we should discuss is the naiming of the new member |
There is always the trouble of a partner reusing a synthetic value (ie: 'unknown'). unknown is probably sufficient, but more descriptive naming may lessen the risk. An additional positive of more descriptive naming is that an external developer seeing the event type will know that this is an artifact of the octokit library as opposed to something odd with the REST api. octokit_unsupported_type or simply octokit_unsupported might be an alternative. That said, thank you, @maddin2016. This will be a useful feature for our team. |
Hey, folks. Any more thought on this PR? We are seeing more issues of the kind noted above in the original issue. |
Bump @shiftkey if you want to comment at all? Im happy to proceed with this change... My most recent points for consideration still stand. And a couple of further points:
|
|
@ryangribble @andrejo-msft @M-Zuber this all seems reasonable 👍 |
Is this change still needed after #1595? |
Nope, this one can be closed in favour of #1595 👍 |
Fix #1502