Skip to content

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

Vici37
Copy link
Contributor

@Vici37 Vici37 commented May 28, 2025

This is the output of compiling cr-source-typer and running it with the below incantation:

CRYSTAL_PATH="./src" CRYSTAL_HAS_WRAPPER=1 ./typify spec/std_spec.cr
--error-trace --exclude src/crystal/ 
--stats --progress 
--union-size-threshold 2
--ignore-private-defs 
--ignore-protected-defs  
src/json

This is related to #15682 .

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?
Copy link
Contributor Author

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.

Copy link
Member

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.

Suggested change
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.

@@ -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
Copy link
Member

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.

Suggested change
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?
Copy link
Member

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.

Suggested change
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.

@@ -291,12 +291,12 @@ class JSON::Builder
end

# Flushes the underlying `IO`.
def flush
def flush : IO?
Copy link
Member

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.

@@ -116,11 +116,11 @@ module Iterator(T)
end
end

def Nil.new(pull : JSON::PullParser)
def Nil.new(pull : JSON::PullParser) : Nil
Copy link
Member

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.

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.

2 participants