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

Change nonsense return types to Nil: JSON and YAML #10622

Merged
merged 4 commits into from
Aug 2, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/big/json.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ require "big"

class JSON::Builder
# Writes a big decimal.
def number(number : BigDecimal) : IO
def number(number : BigDecimal) : Nil
scalar do
@io << number
end
Expand All @@ -26,7 +26,7 @@ struct BigInt
to_s
end

def to_json(json : JSON::Builder) : IO
def to_json(json : JSON::Builder) : Nil
json.number(self)
end
end
Expand Down Expand Up @@ -56,7 +56,7 @@ struct BigFloat
to_s
end

def to_json(json : JSON::Builder) : IO
def to_json(json : JSON::Builder) : Nil
json.number(self)
end
end
Expand Down Expand Up @@ -86,7 +86,7 @@ struct BigDecimal
to_s
end

def to_json(json : JSON::Builder) : IO
def to_json(json : JSON::Builder) : Nil
json.number(self)
end
end
26 changes: 13 additions & 13 deletions src/json/builder.cr
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class JSON::Builder
end

# Starts a document.
def start_document : JSON::Builder::DocumentStartState
def start_document : Nil
case state = @state.last
when StartState
@state[-1] = DocumentStartState.new
Expand Down Expand Up @@ -62,28 +62,28 @@ class JSON::Builder
end

# Writes a `null` value.
def null : IO
def null : Nil
scalar do
@io << "null"
end
end

# Writes a boolean value.
def bool(value : Bool) : IO
def bool(value : Bool) : Nil
scalar do
@io << value
end
end

# Writes an integer.
def number(number : Int) : IO
def number(number : Int) : Nil
scalar do
@io << number
end
end

# Writes a float.
def number(number : Float) : IO
def number(number : Float) : Nil
scalar do
case number
when .nan?
Expand All @@ -100,7 +100,7 @@ class JSON::Builder
# by invoking `to_s` on it.
#
# This method can also be used to write the name of an object field.
def string(value) : IO
def string(value) : Nil
string = value.to_s

scalar(string: true) do
Expand Down Expand Up @@ -157,22 +157,22 @@ class JSON::Builder
# the IO without processing. This is the only method that
# might lead to invalid JSON being generated, so you must
# be sure that *string* contains a valid JSON string.
def raw(string : String) : IO
def raw(string : String) : Nil
scalar do
@io << string
end
end

# Writes the start of an array.
def start_array : IO
def start_array : Nil
start_scalar
increase_indent
@state.push ArrayState.new(empty: true)
@io << '['
end

# Writes the end of an array.
def end_array : JSON::Builder::ArrayState | JSON::Builder::DocumentEndState | JSON::Builder::ObjectState
def end_array : Nil
case state = @state.last
when ArrayState
@state.pop
Expand All @@ -193,15 +193,15 @@ class JSON::Builder
end

# Writes the start of an object.
def start_object : IO
def start_object : Nil
start_scalar
increase_indent
@state.push ObjectState.new(empty: true, name: true)
@io << '{'
end

# Writes the end of an object.
def end_object : JSON::Builder::ArrayState | JSON::Builder::DocumentEndState | JSON::Builder::ObjectState
def end_object : Nil
case state = @state.last
when ObjectState
unless state.name
Expand Down Expand Up @@ -235,12 +235,12 @@ class JSON::Builder
end

# :ditto:
def scalar(value : Int | Float) : IO
def scalar(value : Int | Float) : Nil
number(value)
end

# :ditto:
def scalar(value : String) : IO
def scalar(value : String) : Nil
string(value)
end

Expand Down
4 changes: 2 additions & 2 deletions src/json/pull_parser.cr
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ class JSON::PullParser
# Reads the new value and fill the a JSON builder with it.
#
# Use this method with a `JSON::Builder` to read a JSON while building another one.
def read_raw(json) : IO | JSON::PullParser::Kind
def read_raw(json) : Nil
case @kind
when .null?
read_next
Expand Down Expand Up @@ -556,7 +556,7 @@ class JSON::PullParser
#
# It skips the whole value, not only the next lexer's token.
# For example if the next value is an array, the whole array will be skipped.
def skip : Bool
def skip : Nil
@lexer.skip = true
skip_internal
@lexer.skip = false
Expand Down
26 changes: 13 additions & 13 deletions src/json/to_json.cr
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class Object
end

struct Nil
def to_json(json : JSON::Builder) : IO
def to_json(json : JSON::Builder) : Nil
json.null
end

Expand All @@ -35,13 +35,13 @@ struct Nil
end

struct Bool
def to_json(json : JSON::Builder) : IO
def to_json(json : JSON::Builder) : Nil
json.bool(self)
end
end

struct Int
def to_json(json : JSON::Builder) : IO
def to_json(json : JSON::Builder) : Nil
json.number(self)
end

Expand All @@ -51,7 +51,7 @@ struct Int
end

struct Float
def to_json(json : JSON::Builder) : IO
def to_json(json : JSON::Builder) : Nil
json.number(self)
end

Expand All @@ -61,7 +61,7 @@ struct Float
end

class String
def to_json(json : JSON::Builder) : IO
def to_json(json : JSON::Builder) : Nil
oprypin marked this conversation as resolved.
Show resolved Hide resolved
json.string(self)
end

Expand All @@ -71,7 +71,7 @@ class String
end

struct Path
def to_json(json : JSON::Builder) : IO
def to_json(json : JSON::Builder) : Nil
@name.to_json(json)
end

Expand All @@ -81,7 +81,7 @@ struct Path
end

struct Symbol
def to_json(json : JSON::Builder) : IO
def to_json(json : JSON::Builder) : Nil
json.string(to_s)
end

Expand Down Expand Up @@ -132,7 +132,7 @@ class Hash
end

struct Tuple
def to_json(json : JSON::Builder) : IO
def to_json(json : JSON::Builder) : Nil
json.array do
{% for i in 0...T.size %}
self[{{i}}].to_json(json)
Expand All @@ -154,7 +154,7 @@ struct NamedTuple
end

struct Time::Format
def to_json(value : Time, json : JSON::Builder) : IO
def to_json(value : Time, json : JSON::Builder) : Nil
format(value).to_json(json)
end
end
Expand Down Expand Up @@ -263,7 +263,7 @@ struct Time
# a time value.
#
# See `#from_json` for reference.
def to_json(json : JSON::Builder) : IO
def to_json(json : JSON::Builder) : Nil
json.string(Time::Format::RFC_3339.format(self, fraction_digits: 0))
end
end
Expand Down Expand Up @@ -343,7 +343,7 @@ end
# person.to_json # => %({"birth_date":1459859781})
# ```
module Time::EpochConverter
def self.to_json(value : Time, json : JSON::Builder) : IO
def self.to_json(value : Time, json : JSON::Builder) : Nil
json.number(value.to_unix)
end
end
Expand All @@ -367,7 +367,7 @@ end
# timestamp.to_json # => %({"value":1459860483856})
# ```
module Time::EpochMillisConverter
def self.to_json(value : Time, json : JSON::Builder) : IO
def self.to_json(value : Time, json : JSON::Builder) : Nil
json.number(value.to_unix_ms)
end
end
Expand All @@ -394,7 +394,7 @@ end
# raw.to_json # => %({"value":123456789876543212345678987654321})
# ```
module String::RawConverter
def self.to_json(value : String, json : JSON::Builder) : IO
def self.to_json(value : String, json : JSON::Builder) : Nil
json.raw(value)
end
end
2 changes: 1 addition & 1 deletion src/oauth2/access_token/bearer.cr
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class OAuth2::AccessToken::Bearer < OAuth2::AccessToken
request.headers["Authorization"] = "Bearer #{access_token}"
end

def to_json(json : JSON::Builder) : IO?
def to_json(json : JSON::Builder) : Nil
json.object do
json.field "token_type", "bearer"
json.field "access_token", access_token
Expand Down
2 changes: 1 addition & 1 deletion src/oauth2/access_token/mac.cr
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class OAuth2::AccessToken::Mac < OAuth2::AccessToken
Base64.strict_encode OpenSSL::HMAC.digest(digest, mac_key, normalized_request_string)
end

def to_json(json : JSON::Builder) : IO
def to_json(json : JSON::Builder) : Nil
json.object do
json.field "token_type", "mac"
json.field "access_token", access_token
Expand Down
2 changes: 1 addition & 1 deletion src/uuid/json.cr
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ struct UUID
# uuid = UUID.new("87b3042b-9b9a-41b7-8b15-a93d3f17025e")
# uuid.to_json # => "\"87b3042b-9b9a-41b7-8b15-a93d3f17025e\""
# ```
def to_json(json : JSON::Builder) : IO
def to_json(json : JSON::Builder) : Nil
json.string(to_s)
end

Expand Down
2 changes: 1 addition & 1 deletion src/yaml/any.cr
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ struct YAML::Any
raw.to_yaml(io)
end

def to_json(builder : JSON::Builder) : IO?
def to_json(builder : JSON::Builder) : Nil
if (raw = self.raw).is_a?(Slice)
raise "Can't serialize #{raw.class} to JSON"
else
Expand Down
8 changes: 4 additions & 4 deletions src/yaml/builder.cr
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class YAML::Builder
end

# Ends a YAML stream.
def end_stream : IO | Int32 | Nil
def end_stream : Nil
emit stream_end
@io.flush
end
Expand Down Expand Up @@ -104,7 +104,7 @@ class YAML::Builder
end

# Ends a sequence.
def end_sequence : Int32
def end_sequence : Nil
emit sequence_end
decrease_nesting
end
Expand All @@ -123,7 +123,7 @@ class YAML::Builder
end

# Ends a mapping.
def end_mapping : Int32
def end_mapping : Nil
emit mapping_end
decrease_nesting
end
Expand Down Expand Up @@ -186,7 +186,7 @@ class YAML::Builder
end

# Closes the builder, freeing up resources.
def close : Bool
def close : Nil
finalize
@closed = true
end
Expand Down
8 changes: 4 additions & 4 deletions src/yaml/nodes/parser.cr
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class YAML::Nodes::Parser < YAML::Parser
node.start_column = @pull_parser.start_column.to_i
end

def end_value(node) : Int32
def end_value(node) : Nil
node.end_line = @pull_parser.end_line.to_i
node.end_column = @pull_parser.end_column.to_i
end
Expand All @@ -72,15 +72,15 @@ class YAML::Nodes::Parser < YAML::Parser
documents << document
end

def add_to_document(document, node) : Array(YAML::Nodes::Node)
def add_to_document(document, node) : Nil
document << node
end

def add_to_sequence(sequence, node) : Array(YAML::Nodes::Node)
def add_to_sequence(sequence, node) : Nil
sequence << node
end

def add_to_mapping(mapping, key, value) : Array(YAML::Nodes::Node)
def add_to_mapping(mapping, key, value) : Nil
mapping[key] = value
end
end
2 changes: 1 addition & 1 deletion src/yaml/parser.cr
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ abstract class YAML::Parser
end

# Closes this parser, freeing up resources.
def close : Bool
def close : Nil
@pull_parser.close
end

Expand Down
2 changes: 1 addition & 1 deletion src/yaml/pull_parser.cr
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ class YAML::PullParser
LibYAML.yaml_event_delete(pointerof(@event))
end

def close : Bool
def close : Nil
finalize
@closed = true
end
Expand Down
Loading