Skip to content

Commit

Permalink
Replace hard-coded class name arrays with TypeNode#union_types call…
Browse files Browse the repository at this point in the history
…s to appropriate constants
  • Loading branch information
Sija committed Sep 22, 2017
1 parent a214c8d commit ffb1314
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 12 deletions.
10 changes: 5 additions & 5 deletions spec/std/json/serialization_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/json/from_json.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/primitives.cr
Original file line number Diff line number Diff line change
Expand Up @@ -267,9 +267,9 @@ end
# unions.

{% begin %}
{% ints = %w(Int8 Int16 Int32 Int64 UInt8 UInt16 UInt32 UInt64) %}
{% floats = %w(Float32 Float64) %}
{% nums = %w(Int8 Int16 Int32 Int64 UInt8 UInt16 UInt32 UInt64 Float32 Float64) %}
{% ints = Int::Primitive.union_types %}
{% floats = Float::Primitive.union_types %}
{% nums = Number::Primitive.union_types %}
{% binaries = {"+" => "adding", "-" => "subtracting", "*" => "multiplying", "/" => "dividing"} %}

{% for num in nums %}
Expand Down
4 changes: 2 additions & 2 deletions src/random/system.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/yaml/from_yaml.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ffb1314

Please sign in to comment.