-
Notifications
You must be signed in to change notification settings - Fork 16
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
Add duration property to the Item model to support the new duration feature. #35
Conversation
…pendent if statement. To follow the clean code rule: csharpsquid:S3358 (Ternary operators should not be nested)
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.
Could you please write at least one integration tests with duration?
src/Todoist.Net/Models/Duration.cs
Outdated
{ | ||
return TimeSpan.FromDays(Amount); | ||
} | ||
throw new NotImplementedException(); |
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.
I'd suggest to change ifs to switch pattern in this way static analyzers will find an issue if we add a new DurationUnit in the future.
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.
You have a good point, but the problem is that we are dealing with static values that won't work with switch patterns.
I've looked it up and found a way to make the code more readable, following this SO answer, I did the following:
switch (Unit)
{
case var _ when Unit == DurationUnit.Minute:
return TimeSpan.FromMinutes(Amount);
case var _ when Unit == DurationUnit.Day:
return TimeSpan.FromDays(Amount);
default:
throw new NotImplementedException();
}
Unfortunately, this still doesn't guide static analyzers to help us add support for new values.
I thought of adding an enum
that can be mapped to each of the DurationUnit
values, but again, analyzers will be useful only if new values are added both to the class and the enum.
Maybe there's something that I'm missing, what do you think?
Thank you for the contribution! 👍🏻 |
This fixes the issue with update requests that removes durations.
Kudos, SonarCloud Quality Gate passed!
|
You were right about this, the test I wrote was failing at first, but then I found that the [JsonProperty("duration", NullValueHandling = NullValueHandling.Include)]
public Duration Duration { get; set; } In addition to the integration test, I wrote a couple of unit tests for the public int Amount
{
get => _amount;
set => _amount = value > 0
? value
: throw new ArgumentOutOfRangeException(nameof(Amount), "Duration amount must be greater than zero.");
} All tests are passing now, but on my machine, the integration test I added was failing due to inconsistency in conversion between In my case (time zone is GMT +2), the value I think I might've faced a similar issue while I was working on my project and it's clear now why it's happening. I will wait until we get done with this pull request and then open an issue ticket for it. |
Fixes #34
Changes include:
Duration
model to be mapped against the task duration JSON object.DurationUnit
model that extendsStringEnum
and supports two values: minute & day.Duration
property of typeDuration
in theItem
model.