Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

feat: Addcodersdk.NullTime, change workspace build deadline#3552

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
mafredri merged 5 commits intomainfrommafredri/workspace-build-pointer
Aug 25, 2022

Conversation

mafredri
Copy link
Member

I took a slightly different approach in this PR than what originally proposed in#2015, it's closer to my proposal in a later comment.

Currently we embeddsql.NullTime forone reason, it gives us database scanner/valuer and means we can pass nullable times to the DB by doingt.NullTime.

We can simplify this though, if it's not needed so that we have plaincodersdk.NullTime{Time time.Time; Valid boold}.

Fixes#2015

@mafredrimafredri self-assigned thisAug 18, 2022
@mafredri
Copy link
MemberAuthor

// ping@johnstcn, what do you think about this approach?

@mafredrimafredriforce-pushed themafredri/workspace-build-pointer branch froma7c0920 to43fbbeaCompareAugust 18, 2022 15:20
@johnstcn
Copy link
Member

// ping@johnstcn, what do you think about this approach?

:chefs-kiss:

@mafredrimafredri marked this pull request as ready for reviewAugust 18, 2022 15:39
@mafredrimafredri requested a review froma team as acode ownerAugust 18, 2022 15:39
@@ -50,7 +50,7 @@ type WorkspaceBuild struct {
InitiatorID uuid.UUID `json:"initiator_id"`
InitiatorUsername string `json:"initiator_name"`
Job ProvisionerJob `json:"job"`
Deadlinetime.Time `json:"deadline"`
DeadlineNullTime`json:"deadline,omitempty"`
Copy link
Member

Choose a reason for hiding this comment

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

Based on the comment in the attached issue, it's unclear what the use-case for knowing the time is null is that a pointer wouldn't solve.

Is this related to the TypeScript generation?

Copy link
MemberAuthor

@mafredrimafredriAug 18, 2022
edited
Loading

Choose a reason for hiding this comment

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

I think this is a cleaner approach from a usability perspective in Go code. That is to say, the benefit is not in the frontend talking to the backend, it's using these values in the backend or cli.

IMO it'd be nice for us to consistently useNullTime over*time.Time in the API. Things like not being able to take a pointer of a function call&time.Now() are not an issue when you can writecodersdk.NewNullTime(time.Now(), true). Likewise when using the type, there's no need for theif t != nil check.

There are work-arounds for making pointer use a bit more convenient, like usingcoderd/util/ptr, but it's an extra step to using them vs accessing the concrete type directly.

One last point in favor ofNullTime, it's safer to use. There's no risk of doing a nil pointer dereference.

Copy link
Member

Choose a reason for hiding this comment

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

FWIW working with pointers makes me sad; I much prefer theNullXXX approach.
Unless we start going down the lines of having a genericOptional[T] type?

Copy link
Member

Choose a reason for hiding this comment

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

I'm hesitant to stray from such strong language primitives. Do we imagine having aNullXXX for every type that could be nullable? This seems odd to me because embedded structs would likely be pointers withomitempty rather thanNull<StructName>. Are we promoting thisonly for built-in types?

Copy link
MemberAuthor

@mafredrimafredriAug 22, 2022
edited
Loading

Choose a reason for hiding this comment

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

I understand why you may be hesitant, but I don't think it will be a problem. If we strive to make the zero value useful in most cases, the need for nullable values will be pretty low. We can also look atcodersdk (today), there we only have a couple of primitives (int, string) and time.

      7 *int64      5 *time.Time      3 *string      2 *uuid.UUID

Theuuid package already hasuuid.NullUUID, so it would make sense to use that one instead. In fact, the occurrence ofuuid.NullUUID is much larger in our codebase than that of*uuid.UUID (14 vs 3), yet we have both.

I also foresee that the need for a nullable struct will be very rare (we have none incodersdk, currently), and in those cases it would likely be a custom type for which we can simply implement a custom json marshaler/unmarshaler. It's unfortunate Go never got around to addingomitzero/omitempty to thejson package, because many times that would solve the use-case for using pointer-structs (i.e. often the reason is not to detect omission, it's to create a specific type of output).

Copy link
Member

Choose a reason for hiding this comment

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

I appreciate the explanation! I agree thatNullUUID is classically used, but that's typically been used for database operations, not the API.

I'll defer here though. You've spent a lot more time thinking about this than I, so I approve!

johnstcn and mafredri reacted with heart emoji
Copy link
MemberAuthor

@mafredrimafredriAug 25, 2022
edited
Loading

Choose a reason for hiding this comment

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

Leaving this here for future reference, and perhaps an alternative avenue for achieving the same in a generic way. (Did some prototyping on this with@johnstcn some days ago)

typeOptional[Tany]struct {ValueTValidbool}// Of returns a new Optional of the given value.funcOf[Tany](oT)Optional[T] {returnOptional[T]{Value:o,Valid:true,}}// MarshalJSON implements json.Marshaler.func (oOptional[T])MarshalJSON() ([]byte,error) {// omitted for brevity}// UnmarshalJSON implements json.Unmarshaler.func (o*Optional[T])UnmarshalJSON(data []byte)error {// omitted for brevity}typeAPIRequeststruct {TimeOptional[time.Time]`json:"time"`}// other packagefuncsample() {r:= codersdk.APIRequest{}r.Time=codersdk.Of(time.Now())ifr.Time.Valid {// if we care about null._=r.Time.Value}// zero value is ok without null check too_=r.Time.Value.IsZero()}

The namingsOptional,Of, etc are all up in the air, they could beNull andNewFrom instead.

Copy link
Contributor

@jsjoeiojsjoeio left a comment

Choose a reason for hiding this comment

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

✅ FE

I only see an update to thedeadline property being made optional now. I imagine this shouldn't have any consequences assuming we have tests for code using that interface.

If there is something else you want reviewed by FE, ping me!

@mafredrimafredriforce-pushed themafredri/workspace-build-pointer branch 2 times, most recently froma77ae97 to0c66c39CompareAugust 25, 2022 12:18
@mafredri
Copy link
MemberAuthor

mafredri commentedAug 25, 2022
edited
Loading

✅ FE

I only see an update to thedeadline property being made optional now. I imagine this shouldn't have any consequences assuming we have tests for code using that interface.

If there is something else you want reviewed by FE, ping me!

@jsjoeio Your comment made me take a look at the site code, and some updates were indeed required. Please take a look ate390cef (#3552).

I believe none of this was caught due to the function signature ofdayjs which accepts basically any type, includingnull andundefined. Tests also use mock API objects so those would've conformed to the old format.

jsjoeio reacted with thumbs up emoji

@mafredrimafredriforce-pushed themafredri/workspace-build-pointer branch from3a3cb79 toe390cefCompareAugust 25, 2022 12:26
@mafredrimafredriforce-pushed themafredri/workspace-build-pointer branch from89576ed tofdb657eCompareAugust 25, 2022 14:05
Copy link
Contributor

@jsjoeiojsjoeio left a comment

Choose a reason for hiding this comment

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

Thanks for going the extra mile to double-check any side effects on the FE! 👏🏼

…utton.tsxCo-authored-by: Joe Previte <jjprevite@gmail.com>
@mafredrimafredri merged commit78a2494 intomainAug 25, 2022
@mafredrimafredri deleted the mafredri/workspace-build-pointer branchAugust 25, 2022 16:10
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@jsjoeiojsjoeiojsjoeio approved these changes

@johnstcnjohnstcnjohnstcn approved these changes

@kylecarbskylecarbskylecarbs approved these changes

@EmyrkEmyrkAwaiting requested review from Emyrk

Assignees

@mafredrimafredri

Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

Codersdk: make workspace_build.deadline a pointer
4 participants
@mafredri@johnstcn@jsjoeio@kylecarbs

[8]ページ先頭

©2009-2025 Movatter.jp