diff --git a/spec/std/json/serialization_spec.cr b/spec/std/json/serialization_spec.cr index 249a727b77c6..c61cee52c3ca 100644 --- a/spec/std/json/serialization_spec.cr +++ b/spec/std/json/serialization_spec.cr @@ -120,11 +120,11 @@ describe "JSON serialization" do Union(Bool, Array(Int32)).from_json(%(true)).should be_true end - {% for type in %w(Int8 Int16 Int32 Int64 UInt8 UInt16 UInt32 UInt64).map(&.id) %} - it "deserializes union with {{type}} (fast path)" do - Union({{type}}, Array(Int32)).from_json(%(#{ {{type}}::MAX })).should eq({{type}}::MAX) - end - {% end %} + {% for type in Int::Primitive.union_types %} + it "deserializes union with {{type}} (fast path)" do + Union({{type}}, Array(Int32)).from_json(%(#{ {{type}}::MAX })).should eq({{type}}::MAX) + end + {% end %} it "deserializes union with Float32 (fast path)" do Union(Float32, Array(Int32)).from_json(%(1)).should eq(1) diff --git a/src/json/from_json.cr b/src/json/from_json.cr index 56b396a5e80b..b7ed5ef5abc2 100644 --- a/src/json/from_json.cr +++ b/src/json/from_json.cr @@ -66,7 +66,7 @@ def Bool.new(pull : JSON::PullParser) pull.read_bool end -{% for type in %w(Int8 Int16 Int32 Int64 UInt8 UInt16 UInt32 UInt64) %} +{% for type in Int::Primitive.union_types %} def {{type.id}}.new(pull : JSON::PullParser) {{type.id}}.new(pull.read_int) end diff --git a/src/random/system.cr b/src/random/system.cr index 54fdbebfd440..a9ca0dae272f 100644 --- a/src/random/system.cr +++ b/src/random/system.cr @@ -18,7 +18,7 @@ module Random::System Crystal::System::Random.next_u end - {% for type in [UInt8, UInt16, UInt32, UInt64] %} + {% for type in Int::Unsigned.union_types %} # Generates a random integer of a given type. The number of bytes to # generate can be limited; by default it will generate as many bytes as # needed to fill the integer size. @@ -46,7 +46,7 @@ module Random::System end {% end %} - {% for type in [Int8, Int16, Int32, Int64] %} + {% for type in Int::Signed.union_types %} private def rand_type(type : {{type}}.class, needed_bytes = sizeof({{type}})) : {{type}} result = rand_type({{"U#{type}".id}}, needed_bytes) {{type}}.new(result) diff --git a/src/yaml/from_yaml.cr b/src/yaml/from_yaml.cr index 62d293164805..5554756c7611 100644 --- a/src/yaml/from_yaml.cr +++ b/src/yaml/from_yaml.cr @@ -34,7 +34,7 @@ def Bool.new(pull : YAML::PullParser) pull.read_scalar == "true" end -{% for type in %w(Int8 Int16 Int32 Int64 UInt8 UInt16 UInt32 UInt64) %} +{% for type in Int::Primitive.union_types %} def {{type.id}}.new(pull : YAML::PullParser) location = pull.location begin