Skip to content

Implement System::User on Windows#14933

Merged
straight-shoota merged 5 commits intocrystal-lang:masterfrom
HertzDevil:feature/windows-system-user
Aug 25, 2024
Merged

Implement System::User on Windows#14933
straight-shoota merged 5 commits intocrystal-lang:masterfrom
HertzDevil:feature/windows-system-user

Conversation

@HertzDevil
Copy link
Contributor

Depends on #14929.

This is for the most part a straight port of Go's implementation, including their interpretation of primary groups on Windows (as opposed to whatever Cygwin does).

@HertzDevil HertzDevil added kind:feature platform:windows Windows support based on the MSVC toolchain / Win32 API topic:stdlib:system labels Aug 22, 2024
Copy link
Contributor

@Sija Sija left a comment

Choose a reason for hiding this comment

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

Style related suggestions.

end

private def self.from_sid(sid : LibC::SID*) : ::System::User?
canonical = sid_to_name(sid) || return
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
canonical = sid_to_name(sid) || return
return unless canonical = sid_to_name(sid)

Copy link
Contributor

@Sija Sija Aug 23, 2024

Choose a reason for hiding this comment

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

@straight-shoota Why thumbs down? It's an established pattern, used as well one line below...

Copy link
Member

@straight-shoota straight-shoota Aug 23, 2024

Choose a reason for hiding this comment

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

No, this is not an established pattern. And it never will be (if it's up to me).
In a trailing conditional, the condition should always be a predicate without side effects (like canonical.type.sid_type_user?).
Assignments should never be in a trailing condition.

The current style is concise and expressive. It focuses on the main action, an assignment to a local variable. The value that we want to assign comes from a method call and if it is unavailable, we return. That's very readable. Your suggestion is not. It hides the fact that this is an assignment behind the less significant control flow action for error handling.

Copy link
Collaborator

@ysbaddaden ysbaddaden Aug 24, 2024

Choose a reason for hiding this comment

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

TBH I write return unless x = y in all my crystal code. I like that the return statement is explicit, and not buried at the end of an expression (especially below).

I also prefer a single style over a mix of them.

Copy link
Contributor

Choose a reason for hiding this comment

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

end

private def self.lookup_primary_group_id(name : String, domain : String) : String?
domain_sid = name_to_sid(domain) || return
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
domain_sid = name_to_sid(domain) || return
return unless domain_sid = name_to_sid(domain)

@straight-shoota
Copy link
Member

I noticed the Go implementation has quite some helpful code comments that explain how the code works and why.
Could we have some of that in the Crystal implementation as well?

@straight-shoota straight-shoota added this to the 1.14.0 milestone Aug 23, 2024
@straight-shoota straight-shoota merged commit 8878c8b into crystal-lang:master Aug 25, 2024
@HertzDevil HertzDevil deleted the feature/windows-system-user branch August 25, 2024 20:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind:feature platform:windows Windows support based on the MSVC toolchain / Win32 API topic:stdlib:system

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

4 participants