-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Add type restrictions to json #15840
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
base: master
Are you sure you want to change the base?
Conversation
src/json/any.cr
Outdated
@@ -130,7 +130,7 @@ struct JSON::Any | |||
|
|||
# Traverses the depth of a structure and returns the value. | |||
# Returns `nil` if not found. | |||
def dig?(index_or_key : String | Int, *subkeys) : JSON::Any? | |||
def dig?(index_or_key : String | Int, *subkeys : Int | String) : JSON::Any? |
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 couldn't think of a reason for subkeys
to be anything other than ints and strings in a JSON object, so I left the resolved type restriction. Let me know if this is too restrictive for some reason.
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.
Sounds good 👍
But for polish, we should use the same ordering of the union in both places.
def dig?(index_or_key : String | Int, *subkeys : Int | String) : JSON::Any? | |
def dig?(index_or_key : Int | String, *subkeys : Int | String) : JSON::Any? |
Ditto below.
src/json/builder.cr
Outdated
@@ -277,7 +277,7 @@ class JSON::Builder | |||
# Writes an object's field and value. | |||
# The field's name is first converted to a `String` by invoking | |||
# `to_s` on it. | |||
def field(name, value) | |||
def field(name : String | Int32, value : _) : Nil |
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.
suggestion: For retrieval, the type restriction to Int | String
makes sense. But here it shouldn't be stricter than that of #string
.
def field(name : String | Int32, value : _) : Nil | |
def field(name : _, value : _) : Nil |
src/json/any.cr
Outdated
@@ -130,7 +130,7 @@ struct JSON::Any | |||
|
|||
# Traverses the depth of a structure and returns the value. | |||
# Returns `nil` if not found. | |||
def dig?(index_or_key : String | Int, *subkeys) : JSON::Any? | |||
def dig?(index_or_key : String | Int, *subkeys : Int | String) : JSON::Any? |
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.
Sounds good 👍
But for polish, we should use the same ordering of the union in both places.
def dig?(index_or_key : String | Int, *subkeys : Int | String) : JSON::Any? | |
def dig?(index_or_key : Int | String, *subkeys : Int | String) : JSON::Any? |
Ditto below.
src/json/builder.cr
Outdated
@@ -291,12 +291,12 @@ class JSON::Builder | |||
end | |||
|
|||
# Flushes the underlying `IO`. | |||
def flush | |||
def flush : IO? |
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.
suggestion: This should probably be Nil
like in IO
implementations.
src/json/from_json.cr
Outdated
@@ -116,11 +116,11 @@ module Iterator(T) | |||
end | |||
end | |||
|
|||
def Nil.new(pull : JSON::PullParser) | |||
def Nil.new(pull : JSON::PullParser) : Nil |
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.
issue: This is a constructor, so the return type should be self
.
Ditto all below.
This is the output of compiling cr-source-typer and running it with the below incantation:
This is related to #15682 .