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

Fix ambiguous call with untyped int literal in {JSON,YAML}::Any.new #13618

Merged
merged 3 commits into from
Jul 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
13 changes: 13 additions & 0 deletions spec/std/json/any_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,19 @@ require "json"
require "yaml"

describe JSON::Any do
it ".new" do
JSON::Any.new(nil).raw.should be_nil
JSON::Any.new(true).raw.should eq true
JSON::Any.new(1_i64).raw.should eq 1_i64
JSON::Any.new(1).raw.should eq 1
JSON::Any.new(1_u8).raw.should eq 1
JSON::Any.new(0.0).raw.should eq 0.0
JSON::Any.new(0.0_f32).raw.should eq 0.0
JSON::Any.new("foo").raw.should eq "foo"
JSON::Any.new([] of JSON::Any).raw.should eq [] of JSON::Any
JSON::Any.new({} of String => JSON::Any).raw.should eq({} of String => JSON::Any)
end

describe "casts" do
it "gets nil" do
JSON.parse("null").as_nil.should be_nil
Expand Down
16 changes: 16 additions & 0 deletions spec/std/yaml/any_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,22 @@ private def it_fetches_from_hash?(key, *equivalent_keys)
end

describe YAML::Any do
it ".new" do
YAML::Any.new(nil).raw.should be_nil
YAML::Any.new(true).raw.should eq true
YAML::Any.new(1_i64).raw.should eq 1_i64
YAML::Any.new(1).raw.should eq 1
YAML::Any.new(1_u8).raw.should eq 1
YAML::Any.new(0.0).raw.should eq 0.0
YAML::Any.new(0.0_f32).raw.should eq 0.0
YAML::Any.new("foo").raw.should eq "foo"
YAML::Any.new(Time.utc(2023, 7, 2)).raw.should eq Time.utc(2023, 7, 2)
YAML::Any.new(Bytes[1, 2, 3]).raw.should eq Bytes[1, 2, 3]
YAML::Any.new([] of YAML::Any).raw.should eq [] of YAML::Any
YAML::Any.new({} of YAML::Any => YAML::Any).raw.should eq({} of YAML::Any => YAML::Any)
YAML::Any.new(Set(YAML::Any).new).raw.should eq Set(YAML::Any).new
end

describe "casts" do
it "gets nil" do
YAML.parse("").as_nil.should be_nil
Expand Down
12 changes: 12 additions & 0 deletions src/json/any.cr
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,18 @@ struct JSON::Any
def initialize(@raw : Type)
end

# :ditto:
def self.new(raw : Int)
# FIXME: Workaround for https://github.com/crystal-lang/crystal/issues/11645
new(raw.to_i64)
end

# :ditto:
def self.new(raw : Float)
# FIXME: Workaround for https://github.com/crystal-lang/crystal/issues/11645
new(raw.to_f64)
end

# Assumes the underlying value is an `Array` or `Hash` and returns its size.
# Raises if the underlying value is not an `Array` or `Hash`.
def size : Int
Expand Down
12 changes: 12 additions & 0 deletions src/yaml/any.cr
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,18 @@ struct YAML::Any
def initialize(@raw : Type)
end

# :ditto:
def self.new(raw : Int)
# FIXME: Workaround for https://github.com/crystal-lang/crystal/issues/11645
new(raw.to_i64)
end

# :ditto:
def self.new(raw : Float)
# FIXME: Workaround for https://github.com/crystal-lang/crystal/issues/11645
new(raw.to_f64)
end

# Assumes the underlying value is an `Array` or `Hash` and returns its size.
#
# Raises if the underlying value is not an `Array` or `Hash`.
Expand Down