From e7a6cad087c4142c75683a32b5876c34c5e23647 Mon Sep 17 00:00:00 2001 From: Oleh Prypin Date: Sun, 11 Apr 2021 20:16:23 +0200 Subject: [PATCH 1/3] Auto-deduced type annotations: JSON and YAML --- src/big/json.cr | 8 ++++---- src/json/builder.cr | 26 +++++++++++------------ src/json/pull_parser.cr | 4 ++-- src/json/to_json.cr | 34 +++++++++++++++---------------- src/oauth2/access_token/bearer.cr | 2 +- src/oauth2/access_token/mac.cr | 2 +- src/uuid/json.cr | 2 +- src/yaml/any.cr | 2 +- src/yaml/builder.cr | 12 +++++------ src/yaml/nodes/parser.cr | 8 ++++---- src/yaml/parser.cr | 2 +- src/yaml/pull_parser.cr | 2 +- src/yaml/schema/core/parser.cr | 6 +++--- src/yaml/schema/fail_safe.cr | 8 ++++---- 14 files changed, 59 insertions(+), 59 deletions(-) diff --git a/src/big/json.cr b/src/big/json.cr index c59746bece0f..e07ddcbc3739 100644 --- a/src/big/json.cr +++ b/src/big/json.cr @@ -3,7 +3,7 @@ require "big" class JSON::Builder # Writes a big decimal. - def number(number : BigDecimal) + def number(number : BigDecimal) : IO scalar do @io << number end @@ -26,7 +26,7 @@ struct BigInt to_s end - def to_json(json : JSON::Builder) + def to_json(json : JSON::Builder) : IO json.number(self) end end @@ -56,7 +56,7 @@ struct BigFloat to_s end - def to_json(json : JSON::Builder) + def to_json(json : JSON::Builder) : IO json.number(self) end end @@ -86,7 +86,7 @@ struct BigDecimal to_s end - def to_json(json : JSON::Builder) + def to_json(json : JSON::Builder) : IO json.number(self) end end diff --git a/src/json/builder.cr b/src/json/builder.cr index d848d954bc4e..edb91fd89ec8 100644 --- a/src/json/builder.cr +++ b/src/json/builder.cr @@ -28,7 +28,7 @@ class JSON::Builder end # Starts a document. - def start_document + def start_document : JSON::Builder::DocumentStartState case state = @state.last when StartState @state[-1] = DocumentStartState.new @@ -62,28 +62,28 @@ class JSON::Builder end # Writes a `null` value. - def null + def null : IO scalar do @io << "null" end end # Writes a boolean value. - def bool(value : Bool) + def bool(value : Bool) : IO scalar do @io << value end end # Writes an integer. - def number(number : Int) + def number(number : Int) : IO scalar do @io << number end end # Writes a float. - def number(number : Float) + def number(number : Float) : IO scalar do case number when .nan? @@ -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) + def string(value) : IO string = value.to_s scalar(string: true) do @@ -157,14 +157,14 @@ 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) + def raw(string : String) : IO scalar do @io << string end end # Writes the start of an array. - def start_array + def start_array : IO start_scalar increase_indent @state.push ArrayState.new(empty: true) @@ -172,7 +172,7 @@ class JSON::Builder end # Writes the end of an array. - def end_array + def end_array : JSON::Builder::ArrayState | JSON::Builder::DocumentEndState | JSON::Builder::ObjectState case state = @state.last when ArrayState @state.pop @@ -193,7 +193,7 @@ class JSON::Builder end # Writes the start of an object. - def start_object + def start_object : IO start_scalar increase_indent @state.push ObjectState.new(empty: true, name: true) @@ -201,7 +201,7 @@ class JSON::Builder end # Writes the end of an object. - def end_object + def end_object : JSON::Builder::ArrayState | JSON::Builder::DocumentEndState | JSON::Builder::ObjectState case state = @state.last when ObjectState unless state.name @@ -235,12 +235,12 @@ class JSON::Builder end # :ditto: - def scalar(value : Int | Float) + def scalar(value : Int | Float) : IO number(value) end # :ditto: - def scalar(value : String) + def scalar(value : String) : IO string(value) end diff --git a/src/json/pull_parser.cr b/src/json/pull_parser.cr index a4591f98e602..bf4337d7db04 100644 --- a/src/json/pull_parser.cr +++ b/src/json/pull_parser.cr @@ -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) + def read_raw(json) : IO | JSON::PullParser::Kind case @kind when .null? read_next @@ -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 + def skip : Bool @lexer.skip = true skip_internal @lexer.skip = false diff --git a/src/json/to_json.cr b/src/json/to_json.cr index 819a9b02a3f6..0697fd451277 100644 --- a/src/json/to_json.cr +++ b/src/json/to_json.cr @@ -25,7 +25,7 @@ class Object end struct Nil - def to_json(json : JSON::Builder) + def to_json(json : JSON::Builder) : IO json.null end @@ -35,13 +35,13 @@ struct Nil end struct Bool - def to_json(json : JSON::Builder) + def to_json(json : JSON::Builder) : IO json.bool(self) end end struct Int - def to_json(json : JSON::Builder) + def to_json(json : JSON::Builder) : IO json.number(self) end @@ -51,7 +51,7 @@ struct Int end struct Float - def to_json(json : JSON::Builder) + def to_json(json : JSON::Builder) : IO json.number(self) end @@ -61,7 +61,7 @@ struct Float end class String - def to_json(json : JSON::Builder) + def to_json(json : JSON::Builder) : IO json.string(self) end @@ -71,7 +71,7 @@ class String end struct Path - def to_json(json : JSON::Builder) + def to_json(json : JSON::Builder) : IO @name.to_json(json) end @@ -81,7 +81,7 @@ struct Path end struct Symbol - def to_json(json : JSON::Builder) + def to_json(json : JSON::Builder) : IO json.string(to_s) end @@ -91,7 +91,7 @@ struct Symbol end class Array - def to_json(json : JSON::Builder) + def to_json(json : JSON::Builder) : Nil json.array do each &.to_json(json) end @@ -99,7 +99,7 @@ class Array end class Deque - def to_json(json : JSON::Builder) + def to_json(json : JSON::Builder) : Nil json.array do each &.to_json(json) end @@ -107,7 +107,7 @@ class Deque end struct Set - def to_json(json : JSON::Builder) + def to_json(json : JSON::Builder) : Nil json.array do each &.to_json(json) end @@ -120,7 +120,7 @@ class Hash # Keys are serialized by invoking `to_json_object_key` on them. # Values are serialized with the usual `to_json(json : JSON::Builder)` # method. - def to_json(json : JSON::Builder) + def to_json(json : JSON::Builder) : Nil json.object do each do |key, value| json.field key.to_json_object_key do @@ -132,7 +132,7 @@ class Hash end struct Tuple - def to_json(json : JSON::Builder) + def to_json(json : JSON::Builder) : IO json.array do {% for i in 0...T.size %} self[{{i}}].to_json(json) @@ -154,7 +154,7 @@ struct NamedTuple end struct Time::Format - def to_json(value : Time, json : JSON::Builder) + def to_json(value : Time, json : JSON::Builder) : IO format(value).to_json(json) end end @@ -263,7 +263,7 @@ struct Time # a time value. # # See `#from_json` for reference. - def to_json(json : JSON::Builder) + def to_json(json : JSON::Builder) : IO json.string(Time::Format::RFC_3339.format(self, fraction_digits: 0)) end end @@ -343,7 +343,7 @@ end # person.to_json # => %({"birth_date":1459859781}) # ``` module Time::EpochConverter - def self.to_json(value : Time, json : JSON::Builder) + def self.to_json(value : Time, json : JSON::Builder) : IO json.number(value.to_unix) end end @@ -367,7 +367,7 @@ end # timestamp.to_json # => %({"value":1459860483856}) # ``` module Time::EpochMillisConverter - def self.to_json(value : Time, json : JSON::Builder) + def self.to_json(value : Time, json : JSON::Builder) : IO json.number(value.to_unix_ms) end end @@ -394,7 +394,7 @@ end # raw.to_json # => %({"value":123456789876543212345678987654321}) # ``` module String::RawConverter - def self.to_json(value : String, json : JSON::Builder) + def self.to_json(value : String, json : JSON::Builder) : IO json.raw(value) end end diff --git a/src/oauth2/access_token/bearer.cr b/src/oauth2/access_token/bearer.cr index d219ce4c4101..efe65cdd0304 100644 --- a/src/oauth2/access_token/bearer.cr +++ b/src/oauth2/access_token/bearer.cr @@ -13,7 +13,7 @@ class OAuth2::AccessToken::Bearer < OAuth2::AccessToken request.headers["Authorization"] = "Bearer #{access_token}" end - def to_json(json : JSON::Builder) + def to_json(json : JSON::Builder) : IO? json.object do json.field "token_type", "bearer" json.field "access_token", access_token diff --git a/src/oauth2/access_token/mac.cr b/src/oauth2/access_token/mac.cr index d5152ee873a9..68420b31942f 100644 --- a/src/oauth2/access_token/mac.cr +++ b/src/oauth2/access_token/mac.cr @@ -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) + def to_json(json : JSON::Builder) : IO json.object do json.field "token_type", "mac" json.field "access_token", access_token diff --git a/src/uuid/json.cr b/src/uuid/json.cr index d9b56f7258ca..637c3d6c4f71 100644 --- a/src/uuid/json.cr +++ b/src/uuid/json.cr @@ -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) + def to_json(json : JSON::Builder) : IO json.string(to_s) end diff --git a/src/yaml/any.cr b/src/yaml/any.cr index e037afa2ceb7..385ad43637ea 100644 --- a/src/yaml/any.cr +++ b/src/yaml/any.cr @@ -311,7 +311,7 @@ struct YAML::Any raw.to_yaml(io) end - def to_json(builder : JSON::Builder) + def to_json(builder : JSON::Builder) : IO? if (raw = self.raw).is_a?(Slice) raise "Can't serialize #{raw.class} to JSON" else diff --git a/src/yaml/builder.cr b/src/yaml/builder.cr index 77924f184809..ab179190884f 100644 --- a/src/yaml/builder.cr +++ b/src/yaml/builder.cr @@ -62,7 +62,7 @@ class YAML::Builder end # Ends a YAML stream. - def end_stream + def end_stream : IO | Int32 | Nil emit stream_end @io.flush end @@ -97,14 +97,14 @@ class YAML::Builder end # Starts a sequence. - def start_sequence(anchor : String? = nil, tag : String? = nil, style : YAML::SequenceStyle = YAML::SequenceStyle::ANY) + def start_sequence(anchor : String? = nil, tag : String? = nil, style : YAML::SequenceStyle = YAML::SequenceStyle::ANY) : Nil implicit = tag ? 0 : 1 emit sequence_start, get_anchor(anchor), string_to_unsafe(tag), implicit, style increase_nesting end # Ends a sequence. - def end_sequence + def end_sequence : Int32 emit sequence_end decrease_nesting end @@ -116,14 +116,14 @@ class YAML::Builder end # Starts a mapping. - def start_mapping(anchor : String? = nil, tag : String? = nil, style : YAML::MappingStyle = YAML::MappingStyle::ANY) + def start_mapping(anchor : String? = nil, tag : String? = nil, style : YAML::MappingStyle = YAML::MappingStyle::ANY) : Nil implicit = tag ? 0 : 1 emit mapping_start, get_anchor(anchor), string_to_unsafe(tag), implicit, style increase_nesting end # Ends a mapping. - def end_mapping + def end_mapping : Int32 emit mapping_end decrease_nesting end @@ -186,7 +186,7 @@ class YAML::Builder end # Closes the builder, freeing up resources. - def close + def close : Bool finalize @closed = true end diff --git a/src/yaml/nodes/parser.cr b/src/yaml/nodes/parser.cr index 31366cc8bb57..758fc1bb377c 100644 --- a/src/yaml/nodes/parser.cr +++ b/src/yaml/nodes/parser.cr @@ -46,7 +46,7 @@ class YAML::Nodes::Parser < YAML::Parser node.start_column = @pull_parser.start_column.to_i end - def end_value(node) + def end_value(node) : Int32 node.end_line = @pull_parser.end_line.to_i node.end_column = @pull_parser.end_column.to_i end @@ -72,15 +72,15 @@ class YAML::Nodes::Parser < YAML::Parser documents << document end - def add_to_document(document, node) + def add_to_document(document, node) : Array(YAML::Nodes::Node) document << node end - def add_to_sequence(sequence, node) + def add_to_sequence(sequence, node) : Array(YAML::Nodes::Node) sequence << node end - def add_to_mapping(mapping, key, value) + def add_to_mapping(mapping, key, value) : Array(YAML::Nodes::Node) mapping[key] = value end end diff --git a/src/yaml/parser.cr b/src/yaml/parser.cr index 308b3fb0af34..1d1ab385514c 100644 --- a/src/yaml/parser.cr +++ b/src/yaml/parser.cr @@ -157,7 +157,7 @@ abstract class YAML::Parser end # Closes this parser, freeing up resources. - def close + def close : Bool @pull_parser.close end diff --git a/src/yaml/pull_parser.cr b/src/yaml/pull_parser.cr index d572a7e8f6b1..4115eebe44c9 100644 --- a/src/yaml/pull_parser.cr +++ b/src/yaml/pull_parser.cr @@ -321,7 +321,7 @@ class YAML::PullParser LibYAML.yaml_event_delete(pointerof(@event)) end - def close + def close : Bool finalize @closed = true end diff --git a/src/yaml/schema/core/parser.cr b/src/yaml/schema/core/parser.cr index ba8629bac067..b5cdb4ccc5a5 100644 --- a/src/yaml/schema/core/parser.cr +++ b/src/yaml/schema/core/parser.cr @@ -36,15 +36,15 @@ class YAML::Schema::Core::Parser < YAML::Parser Any.new(Core.parse_scalar(@pull_parser)) end - def add_to_documents(documents, document) + def add_to_documents(documents, document) : Array(YAML::Any) documents << document end - def add_to_document(document, node) + def add_to_document(document, node) : Array(YAML::Any) document.as_a << node end - def add_to_sequence(sequence, node) + def add_to_sequence(sequence, node) : Array(YAML::Any) sequence.as_a << node end diff --git a/src/yaml/schema/fail_safe.cr b/src/yaml/schema/fail_safe.cr index ec480b2b7108..8edac563fbe3 100644 --- a/src/yaml/schema/fail_safe.cr +++ b/src/yaml/schema/fail_safe.cr @@ -50,19 +50,19 @@ module YAML::Schema::FailSafe Any.new(@pull_parser.value) end - def add_to_documents(documents, document) + def add_to_documents(documents, document) : Array(YAML::Any) documents << document end - def add_to_document(document, node) + def add_to_document(document, node) : Array(YAML::Any) document.as_a << node end - def add_to_sequence(sequence, node) + def add_to_sequence(sequence, node) : Array(YAML::Any) sequence.as_a << node end - def add_to_mapping(mapping, key, value) + def add_to_mapping(mapping, key, value) : YAML::Any mapping.as_h[key] = value end end From a241dcbec3994029e5c1d57696604c5807cc4a75 Mon Sep 17 00:00:00 2001 From: Oleh Prypin Date: Mon, 12 Apr 2021 01:02:17 +0200 Subject: [PATCH 2/3] Change nonsense return types to Nil: JSON and YAML --- src/big/json.cr | 8 ++++---- src/json/builder.cr | 26 +++++++++++++------------- src/json/pull_parser.cr | 4 ++-- src/json/to_json.cr | 26 +++++++++++++------------- src/oauth2/access_token/bearer.cr | 2 +- src/oauth2/access_token/mac.cr | 2 +- src/uuid/json.cr | 2 +- src/yaml/any.cr | 2 +- src/yaml/builder.cr | 8 ++++---- src/yaml/nodes/parser.cr | 8 ++++---- src/yaml/parser.cr | 2 +- src/yaml/pull_parser.cr | 2 +- src/yaml/schema/core/parser.cr | 6 +++--- src/yaml/schema/fail_safe.cr | 8 ++++---- 14 files changed, 53 insertions(+), 53 deletions(-) diff --git a/src/big/json.cr b/src/big/json.cr index e07ddcbc3739..a1f71bb68ee5 100644 --- a/src/big/json.cr +++ b/src/big/json.cr @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/src/json/builder.cr b/src/json/builder.cr index edb91fd89ec8..04644ef28a7a 100644 --- a/src/json/builder.cr +++ b/src/json/builder.cr @@ -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 @@ -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? @@ -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 @@ -157,14 +157,14 @@ 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) @@ -172,7 +172,7 @@ class JSON::Builder 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 @@ -193,7 +193,7 @@ 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) @@ -201,7 +201,7 @@ class JSON::Builder 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 @@ -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 diff --git a/src/json/pull_parser.cr b/src/json/pull_parser.cr index bf4337d7db04..82edd541ca89 100644 --- a/src/json/pull_parser.cr +++ b/src/json/pull_parser.cr @@ -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 @@ -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 diff --git a/src/json/to_json.cr b/src/json/to_json.cr index 0697fd451277..b77dc7bb63d3 100644 --- a/src/json/to_json.cr +++ b/src/json/to_json.cr @@ -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 @@ -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 @@ -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 @@ -61,7 +61,7 @@ struct Float end class String - def to_json(json : JSON::Builder) : IO + def to_json(json : JSON::Builder) : Nil json.string(self) end @@ -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 @@ -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 @@ -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) @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/src/oauth2/access_token/bearer.cr b/src/oauth2/access_token/bearer.cr index efe65cdd0304..9bd6d0572045 100644 --- a/src/oauth2/access_token/bearer.cr +++ b/src/oauth2/access_token/bearer.cr @@ -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 diff --git a/src/oauth2/access_token/mac.cr b/src/oauth2/access_token/mac.cr index 68420b31942f..9ba4dfe3f52b 100644 --- a/src/oauth2/access_token/mac.cr +++ b/src/oauth2/access_token/mac.cr @@ -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 diff --git a/src/uuid/json.cr b/src/uuid/json.cr index 637c3d6c4f71..040668f4a6b8 100644 --- a/src/uuid/json.cr +++ b/src/uuid/json.cr @@ -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 diff --git a/src/yaml/any.cr b/src/yaml/any.cr index 385ad43637ea..bc8d95bf385a 100644 --- a/src/yaml/any.cr +++ b/src/yaml/any.cr @@ -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 diff --git a/src/yaml/builder.cr b/src/yaml/builder.cr index ab179190884f..ddc2c2244353 100644 --- a/src/yaml/builder.cr +++ b/src/yaml/builder.cr @@ -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 @@ -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 @@ -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 @@ -186,7 +186,7 @@ class YAML::Builder end # Closes the builder, freeing up resources. - def close : Bool + def close : Nil finalize @closed = true end diff --git a/src/yaml/nodes/parser.cr b/src/yaml/nodes/parser.cr index 758fc1bb377c..8f44c292a507 100644 --- a/src/yaml/nodes/parser.cr +++ b/src/yaml/nodes/parser.cr @@ -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 @@ -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 diff --git a/src/yaml/parser.cr b/src/yaml/parser.cr index 1d1ab385514c..1a81b5e7ad34 100644 --- a/src/yaml/parser.cr +++ b/src/yaml/parser.cr @@ -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 diff --git a/src/yaml/pull_parser.cr b/src/yaml/pull_parser.cr index 4115eebe44c9..ba23cbb5239d 100644 --- a/src/yaml/pull_parser.cr +++ b/src/yaml/pull_parser.cr @@ -321,7 +321,7 @@ class YAML::PullParser LibYAML.yaml_event_delete(pointerof(@event)) end - def close : Bool + def close : Nil finalize @closed = true end diff --git a/src/yaml/schema/core/parser.cr b/src/yaml/schema/core/parser.cr index b5cdb4ccc5a5..8ff23d0157b3 100644 --- a/src/yaml/schema/core/parser.cr +++ b/src/yaml/schema/core/parser.cr @@ -36,15 +36,15 @@ class YAML::Schema::Core::Parser < YAML::Parser Any.new(Core.parse_scalar(@pull_parser)) end - def add_to_documents(documents, document) : Array(YAML::Any) + def add_to_documents(documents, document) : Nil documents << document end - def add_to_document(document, node) : Array(YAML::Any) + def add_to_document(document, node) : Nil document.as_a << node end - def add_to_sequence(sequence, node) : Array(YAML::Any) + def add_to_sequence(sequence, node) : Nil sequence.as_a << node end diff --git a/src/yaml/schema/fail_safe.cr b/src/yaml/schema/fail_safe.cr index 8edac563fbe3..3a88e56b78fd 100644 --- a/src/yaml/schema/fail_safe.cr +++ b/src/yaml/schema/fail_safe.cr @@ -50,19 +50,19 @@ module YAML::Schema::FailSafe Any.new(@pull_parser.value) end - def add_to_documents(documents, document) : Array(YAML::Any) + def add_to_documents(documents, document) : Nil documents << document end - def add_to_document(document, node) : Array(YAML::Any) + def add_to_document(document, node) : Nil document.as_a << node end - def add_to_sequence(sequence, node) : Array(YAML::Any) + def add_to_sequence(sequence, node) : Nil sequence.as_a << node end - def add_to_mapping(mapping, key, value) : YAML::Any + def add_to_mapping(mapping, key, value) : Nil mapping.as_h[key] = value end end From 913ce5823c1c837a21cb78a9ea9552d82e55cca1 Mon Sep 17 00:00:00 2001 From: Oleh Prypin Date: Mon, 12 Apr 2021 21:25:20 +0200 Subject: [PATCH 3/3] Fix a bug relying on truthiness of JSON::Builder#string --- src/compiler/crystal/tools/doc/type.cr | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/compiler/crystal/tools/doc/type.cr b/src/compiler/crystal/tools/doc/type.cr index 2db6db348050..a212ac1b8d07 100644 --- a/src/compiler/crystal/tools/doc/type.cr +++ b/src/compiler/crystal/tools/doc/type.cr @@ -760,7 +760,7 @@ class Crystal::Doc::Type builder.field "full_name", full_name builder.field "name", name builder.field "abstract", abstract? - builder.field "superclass" { superclass.try(&.to_json_simple(builder)) || builder.scalar(nil) } + builder.field "superclass" { (s = superclass) ? s.to_json_simple(builder) : builder.null } builder.field "ancestors" do builder.array do ancestors.each &.to_json_simple(builder) @@ -795,7 +795,7 @@ class Crystal::Doc::Type including_types.each &.to_json_simple(builder) end end - builder.field "namespace" { namespace.try(&.to_json_simple(builder)) || builder.scalar(nil) } + builder.field "namespace" { (n = namespace) ? n.to_json_simple(builder) : builder.null } builder.field "doc", doc builder.field "summary", formatted_summary builder.field "class_methods", class_methods