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

Use enum instead of module for HTTP::WebSocket::CloseCodes #8981

Merged

Conversation

Sija
Copy link
Contributor

@Sija Sija commented Apr 1, 2020

Followup to #8975

@bcardiff
Copy link
Member

bcardiff commented Apr 1, 2020

I think the problem with close codes is that there are lots of values. Is not the same situation as http status codes which are fixed by the rfc.

I'm fine with using enums, if the option for using integers are still available. And if having those overloads does not prevent the autocasting feature to work on enums.

Ref: https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent#Status_codes

@Sija
Copy link
Contributor Author

Sija commented Apr 1, 2020

I'm fine with using enums, if the option for using integers are still available. And if having those overloads does not prevent the autocasting feature to work on enums.

@bcardiff It is available, and it has to be for http status codes to work as well, since there are many unassigned ones that could be used by the developers anyway.

autocasting will obviously work only for the assigned ones.

@straight-shoota
Copy link
Member

Is not the same situation as http status codes which are fixed by the rfc.

HTTP status codes are in fact extensible and not limited to registered ones (see https://tools.ietf.org/html/rfc2616#section-6.1.1). So it's one and the same thing.

autocasting will obviously work only for the assigned ones.

I think a problem could be when we need to know all possible enum values at compile time. That's at least a possible scenario we should be aware of.

But for now it is IMO fine to apply this change because the close codes are conceptually identical to HTTP status codes (registered set, but extensible). If the implementation of enum changes at some point to only allow pre-known values, we would need to figure out something else anyway. And probably also take these codes into consideration.

@Blacksmoke16
Copy link
Member

If there is a valid range like there is with HTTP::Status, we could do something like https://github.com/crystal-lang/crystal/blob/master/src/http/status.cr#L71-L76 to validate the value is within that range.

@Sija Sija force-pushed the use-enum-for-http-websocket-close-codes branch from 6b9933e to d8dd32d Compare April 1, 2020 14:45
@Sija
Copy link
Contributor Author

Sija commented Apr 1, 2020

@Blacksmoke16 Good idea! I've force-pushed changes with the check.

@asterite
Copy link
Member

asterite commented Apr 1, 2020

My idea is to force enums to have valid values, always. Right now Enum.new and also when returning enums from C bindings don't have this check, but we should have them.

Then, we could introduce an @[OpenEnum] or similar annotation to say "This enum can have more values than those listed", and then the compiler would always require an else when doing case over such enum.

@Sija
Copy link
Contributor Author

Sija commented Apr 1, 2020

@asterite Sounds good, but please open an RFC issue about that, to not to stall this PR.

@asterite
Copy link
Member

asterite commented Apr 1, 2020

Oh, of course. It's an idea, it's not necessary for this PR.

Copy link
Contributor

@RX14 RX14 left a comment

Choose a reason for hiding this comment

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

I'm not sure this has any actual benefits...

spec/std/http/web_socket_spec.cr Outdated Show resolved Hide resolved
src/http/web_socket/close_code.cr Outdated Show resolved Hide resolved
@Sija Sija force-pushed the use-enum-for-http-websocket-close-codes branch 2 times, most recently from 19e3196 to fecec34 Compare April 1, 2020 17:39
@Sija
Copy link
Contributor Author

Sija commented Apr 2, 2020

Anything left to do in here?

@Sija Sija force-pushed the use-enum-for-http-websocket-close-codes branch from fecec34 to 30b7ea3 Compare April 2, 2020 12:02
@Sija
Copy link
Contributor Author

Sija commented Apr 2, 2020

All comments addressed.

@bcardiff bcardiff merged commit ab8b044 into crystal-lang:master Apr 2, 2020
@bcardiff bcardiff added this to the 0.34.0 milestone Apr 2, 2020
carlhoerberg pushed a commit to carlhoerberg/crystal that referenced this pull request Apr 29, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants