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

avoid empty case statement #124

Merged
merged 3 commits into from
Jan 27, 2018
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
6 changes: 6 additions & 0 deletions spec/granite_orm/fields/casting_spec.cr
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{% for adapter in GraniteExample::ADAPTERS %}
{% model_constant = "Review#{adapter.camelcase.id}".id %}
{% empty_model_constant = "Empty#{adapter.camelcase.id}".id %}

describe "{{ adapter.id }} #casting_to_fields" do
it "casts string to int" do
Expand All @@ -11,5 +12,10 @@
model = {{ model_constant }}.new({ "downvotes" => "" })
model.errors.size.should eq 1
end

it "compiles with empty fields" do
model = {{ empty_model_constant }}.new
model.should_not eq nil
end
end
{% end %}
18 changes: 18 additions & 0 deletions spec/spec_models.cr
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ end
school_table = "school_#{adapter_literal}s".id
nation_county_table = "nation_county_#{adapter_literal}s".id
review_table = "review_#{adapter_literal}s".id
empty_table = "empty_#{adapter_literal}s".id

if adapter == "pg"
primary_key_sql = "BIGSERIAL PRIMARY KEY".id
Expand Down Expand Up @@ -210,6 +211,21 @@ end
end
end

class Empty{{ adapter_const_suffix }} < Granite::ORM::Base
adapter {{ adapter_literal }}
table_name "{{ empty_table }}"
primary id : Int64

def self.drop_and_create
exec "DROP TABLE IF EXISTS {{ empty_table }}"
exec <<-SQL
CREATE TABLE {{ empty_table }} (
id {{ primary_key_sql }}
)
SQL
end
end

module GraniteExample
@@model_classes << Parent{{ adapter_const_suffix }}
@@model_classes << Teacher{{ adapter_const_suffix }}
Expand All @@ -219,6 +235,7 @@ end
@@model_classes << School{{ adapter_const_suffix }}
@@model_classes << Nation::County{{ adapter_const_suffix }}
@@model_classes << Review{{ adapter_const_suffix }}
@@model_classes << Empty{{ adapter_const_suffix }}
end

Spec.before_each do
Expand All @@ -230,6 +247,7 @@ end
School{{ adapter_const_suffix }}.clear
Nation::County{{ adapter_const_suffix }}.clear
Review{{ adapter_const_suffix }}.clear
Empty{{ adapter_const_suffix }}.clear
end

{% end %}
50 changes: 26 additions & 24 deletions src/granite_orm/fields.cr
Original file line number Diff line number Diff line change
Expand Up @@ -115,31 +115,33 @@ module Granite::ORM::Fields

# Casts params and sets fields
private def cast_to_field(name, value : Type)
case name.to_s
{% for _name, type in FIELDS %}
when "{{_name.id}}"
return @{{_name.id}} = nil if value.nil?
{% if type.id == Int32.id %}
@{{_name.id}} = value.is_a?(String) ? value.to_i32(strict: false) : value.is_a?(Int64) ? value.to_i32 : value.as(Int32)
{% elsif type.id == Int64.id %}
@{{_name.id}} = value.is_a?(String) ? value.to_i64(strict: false) : value.as(Int64)
{% elsif type.id == Float32.id %}
@{{_name.id}} = value.is_a?(String) ? value.to_f32(strict: false) : value.is_a?(Float64) ? value.to_f32 : value.as(Float32)
{% elsif type.id == Float64.id %}
@{{_name.id}} = value.is_a?(String) ? value.to_f64(strict: false) : value.as(Float64)
{% elsif type.id == Bool.id %}
@{{_name.id}} = ["1", "yes", "true", true].includes?(value)
{% elsif type.id == Time.id %}
if value.is_a?(Time)
@{{_name.id}} = value
elsif value.to_s =~ TIME_FORMAT_REGEX
@{{_name.id}} = Time.parse(value.to_s, "%F %X")
end
{% else %}
@{{_name.id}} = value.to_s
{% unless FIELDS.empty? %}
case name.to_s
{% for _name, type in FIELDS %}
when "{{_name.id}}"
return @{{_name.id}} = nil if value.nil?
{% if type.id == Int32.id %}
@{{_name.id}} = value.is_a?(String) ? value.to_i32(strict: false) : value.is_a?(Int64) ? value.to_i32 : value.as(Int32)
{% elsif type.id == Int64.id %}
@{{_name.id}} = value.is_a?(String) ? value.to_i64(strict: false) : value.as(Int64)
{% elsif type.id == Float32.id %}
@{{_name.id}} = value.is_a?(String) ? value.to_f32(strict: false) : value.is_a?(Float64) ? value.to_f32 : value.as(Float32)
{% elsif type.id == Float64.id %}
@{{_name.id}} = value.is_a?(String) ? value.to_f64(strict: false) : value.as(Float64)
{% elsif type.id == Bool.id %}
@{{_name.id}} = ["1", "yes", "true", true].includes?(value)
{% elsif type.id == Time.id %}
if value.is_a?(Time)
@{{_name.id}} = value
elsif value.to_s =~ TIME_FORMAT_REGEX
@{{_name.id}} = Time.parse(value.to_s, "%F %X")
end
{% else %}
@{{_name.id}} = value.to_s
{% end %}
{% end %}
{% end %}
end
end
{% end %}
rescue ex
errors << Granite::ORM::Error.new(name, ex.message)
end
Expand Down