Skip to content

Commit

Permalink
Fix ambiguous call with untyped int literal in {JSON,YAML}::Any.new (
Browse files Browse the repository at this point in the history
  • Loading branch information
straight-shoota authored Jul 7, 2023
1 parent ea92174 commit 3268c81
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 0 deletions.
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

0 comments on commit 3268c81

Please sign in to comment.