Skip to content

Commit

Permalink
Revert "Fix issue with blank strings and type extensions (#381)" (#554)
Browse files Browse the repository at this point in the history
This reverts commit 2788dff.
  • Loading branch information
matthewmcgarvey authored Dec 4, 2020
1 parent ad871ba commit a76a1d7
Show file tree
Hide file tree
Showing 16 changed files with 38 additions and 136 deletions.
24 changes: 12 additions & 12 deletions spec/type_extensions/array_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,32 @@ require "../spec_helper"

describe "Array" do
it "parses Array(Bool) from Array(String)" do
result = Bool::Lucky.parse!(["true"])
result.should eq([true])
result = Bool::Lucky.parse(["true"])
result.value.should eq([true])
end

it "parses Array(String)" do
result = String::Lucky.parse!(["test"])
result.should eq(["test"])
result = String::Lucky.parse(["test"])
result.value.should eq(["test"])
end

it "parses Array(Int32) from Array(String)" do
result = Int32::Lucky.parse!(["10"])
result.should eq([10])
result = Int32::Lucky.parse(["10"])
result.value.should eq([10])
end

it "parses Array(Int16) from Array(String)" do
result = Int16::Lucky.parse!(["1"])
result.should eq([1_i16])
result = Int16::Lucky.parse(["1"])
result.value.should eq([1_i16])
end

it "parses Array(Int64) from Array(String)" do
result = Int64::Lucky.parse!(["1000"])
result.should eq([1000_i64])
result = Int64::Lucky.parse(["1000"])
result.value.should eq([1000_i64])
end

it "parses Array(Float64) from Array(String)" do
result = Float64::Lucky.parse!(["3.1415"])
result.should eq([3.1415])
result = Float64::Lucky.parse(["3.1415"])
result.value.should eq([3.1415])
end
end
17 changes: 0 additions & 17 deletions spec/type_extensions/bool_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,4 @@ describe Bool do
true.blank?.should be_false
end
end

describe "parsing" do
it "parses empty string as nil" do
parse("").should be_a(Avram::Type::SuccessfulCast(Nil))
end

it "parses strings" do
parse("true").value.should eq(true)
parse("1").value.should eq(true)
parse("false").value.should eq(false)
parse("0").value.should eq(false)
end
end
end

private def parse(value)
Bool::Lucky.parse(value)
end
14 changes: 3 additions & 11 deletions spec/type_extensions/enum_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,14 @@ require "../spec_helper"
include LazyLoadHelpers

describe "Enum" do
it "parses String enum" do
Issue::Status::Lucky.parse("1").value.should eq(Issue::Status.new(Issue::AvramStatus::Closed))
end

it "parses empty String enum" do
Issue::Status::Lucky.parse("").should be_a(Avram::Type::SuccessfulCast(Nil))
end

it "checks enum" do
it "check enum" do
issue = IssueBox.create

issue.status.enum.should eq(Issue::AvramStatus::Opened)
issue.role.enum.should eq(Issue::AvramRole::Issue)
end

it "updates enum" do
it "update enum" do
issue = IssueBox.create

updated_issue = Issue::SaveOperation.update!(issue, status: Issue::Status.new(:closed))
Expand All @@ -27,7 +19,7 @@ describe "Enum" do
updated_issue.role.enum.should eq(Issue::AvramRole::Issue)
end

it "accesses enum methods" do
it "access enum methods" do
issue = IssueBox.create

issue.status.opened?.should eq(true)
Expand Down
19 changes: 19 additions & 0 deletions spec/type_extensions/int32_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require "../spec_helper"

describe "Int32" do
it "parses Int32 from String" do
result = Int32::Lucky.parse("10")
result.value.should eq(10)
end

it "parses Int32 from Int64" do
result = Int32::Lucky.parse(400_i64)
result.value.should eq(400)
end

it "returns FailedCast when overflow from Int64 to Int32" do
result = Int32::Lucky.parse(2147483648)
result.value.should eq(nil)
result.should be_a(Avram::Type::FailedCast)
end
end
67 changes: 0 additions & 67 deletions spec/type_extensions/int_spec.cr

This file was deleted.

6 changes: 0 additions & 6 deletions spec/type_extensions/time_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,6 @@ require "../spec_helper"

describe "Time column type" do
describe ".parse" do
it "returns nil if parsing an empty String" do
result = Time::Lucky.parse("")

result.should be_a(Avram::Type::SuccessfulCast(Nil))
end

it "casts various formats successfully" do
time = Time.local
times = {
Expand Down
4 changes: 0 additions & 4 deletions spec/type_extensions/uuid_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@ require "../spec_helper"

describe "UUID column type" do
describe ".parse" do
it "returns successful cast if string is nil" do
UUID::Lucky.parse("").should be_a(Avram::Type::SuccessfulCast(Nil))
end

it "casts a UUID successfully" do
uuid = UUID.new("a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11")
UUID::Lucky.parse(uuid).value.should eq uuid
Expand Down
2 changes: 0 additions & 2 deletions src/avram/charms/bool_extensions.cr
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ struct Bool
include Avram::Type

def parse(value : String)
return parse(nil) if value.blank?

if %w(true 1).includes? value
SuccessfulCast(Bool).new true
elsif %w(false 0).includes? value
Expand Down
2 changes: 0 additions & 2 deletions src/avram/charms/enum_extensions.cr
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ macro avram_enum(enum_name, &block)
Lucky
end

def_equals @enum
getter :enum

# You may need to prefix with {{ @type }}
Expand Down Expand Up @@ -42,7 +41,6 @@ macro avram_enum(enum_name, &block)
end

def parse(value : String)
return parse(nil) if value.blank?
SuccessfulCast({{ enum_name }}).new({{ enum_name }}.new(value.to_i))
end

Expand Down
1 change: 0 additions & 1 deletion src/avram/charms/int16_extensions.cr
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ struct Int16
end

def parse(value : String)
return parse(nil) if value.blank?
SuccessfulCast(Int16).new value.to_i16
rescue ArgumentError
FailedCast.new
Expand Down
1 change: 0 additions & 1 deletion src/avram/charms/int32_extensions.cr
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ struct Int32
end

def parse(value : String)
return parse(nil) if value.blank?
SuccessfulCast(Int32).new value.to_i
rescue ArgumentError
FailedCast.new
Expand Down
1 change: 0 additions & 1 deletion src/avram/charms/int64_extensions.cr
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ struct Int64
end

def parse(value : String)
return parse(nil) if value.blank?
SuccessfulCast(Int64).new value.to_i64
rescue ArgumentError
FailedCast.new
Expand Down
4 changes: 1 addition & 3 deletions src/avram/charms/time_extensions.cr
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ struct Time
value
end

def parse(value : String) : SuccessfulCast(Time) | SuccessfulCast(Nil) | FailedCast
return parse(nil) if value.blank?

def parse(value : String) : SuccessfulCast(Time) | FailedCast
# Prefer user defined string formats
try_parsing_with_string_formats(value) ||
# Then try default formats
Expand Down
1 change: 0 additions & 1 deletion src/avram/charms/uuid_extensions.cr
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ struct UUID
end

def parse(value : String)
return parse(nil) if value.blank?
SuccessfulCast(UUID).new(UUID.new(value))
rescue
FailedCast.new
Expand Down
9 changes: 2 additions & 7 deletions src/avram/type.cr
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,13 @@ module Avram::Type
def parse(values : Array(String))
casts = values.map { |value| parse(value) }
if casts.all?(&.is_a?(SuccessfulCast))
casts.map(&.as(SuccessfulCast))
values = casts.map { |c| c.as(SuccessfulCast).value }
parse(values)
else
FailedCast.new
end
end

def parse!(values : Array(T)) forall T
values.map do |v|
parse!(v)
end
end

def parse!(value)
parse(value).as(SuccessfulCast).value
end
Expand Down
2 changes: 1 addition & 1 deletion src/avram/where.cr
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ module Avram::Where
end

abstract class ValueHoldingSqlClause < SqlClause
getter value : String | Array(String) | Array(Int32) | Nil
getter value : String | Array(String) | Array(Int32)

def initialize(@column, @value)
end
Expand Down

0 comments on commit a76a1d7

Please sign in to comment.