Skip to content

Commit

Permalink
bump to latest PG and DB shards to ensure everything works. Fixing co…
Browse files Browse the repository at this point in the history
…mpilation errors
  • Loading branch information
jwoertink committed Dec 29, 2023
1 parent 0346e61 commit e2528b9
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 9 deletions.
2 changes: 1 addition & 1 deletion shard.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ dependencies:
version: ~> 0.3.0
pg:
github: will/crystal-pg
version: ~> 0.27.0
version: ~> 0.28.0
habitat:
github: luckyframework/habitat
version: ~> 0.4.7
Expand Down
15 changes: 7 additions & 8 deletions src/avram/model.cr
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,6 @@ abstract class Avram::Model
include Avram::Polymorphic
include Avram::SchemaEnforcer

module JSONConverter(T)
def self.from_rs(rs : DB::ResultSet)
value = rs.read(JSON::PullParser?)
T.new(value) if value
end
end

macro inherited
COLUMNS = [] of Nil # types are not checked in macros
ASSOCIATIONS = [] of Nil # types are not checked in macros
Expand Down Expand Up @@ -237,9 +230,15 @@ abstract class Avram::Model
key: {{ type_declaration.var }},
{% if is_serialized %}
converter: JSONConverter({{ data_type }}),
{% elsif data_type.id == Array(Float64).id %}
converter: PG::NumericArrayFloatConverter,
{% end %}
)]
@{{ type_declaration }}
{% if data_type.is_a?(Generic) || is_serialized %}
@{{ type_declaration.var }} : {{ data_type }}{% if nilable %}?{% end %}
{% else %}
@{{ type_declaration.var }} : {{ data_type }}::Lucky::ColumnType{% if nilable %}?{% end %}
{% end %}

{% COLUMNS << {name: type_declaration.var, type: data_type, nilable: nilable.id, autogenerated: autogenerated, value: value, serialized: is_serialized, allow_blank: allow_blank} %}
end
Expand Down
8 changes: 8 additions & 0 deletions src/ext/pg/json_converter.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# This is used for serialized JSON objects.
# e.g. `column theme : Theme, serialize: true`
module JSONConverter(T)
def self.from_rs(rs : DB::ResultSet)
value = rs.read(JSON::PullParser?)
T.new(value) if value
end
end
12 changes: 12 additions & 0 deletions src/ext/pg/numeric_array_float_converter.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Extends the PG shard and adds a converter for
# converting `Array(PG::Numeric)` columns to `Array(Float64)`. This
# can be used with raw SQL queries.
# ```
# @[DB::Field(converter: PG::NumericArrayFloatConverter)]
# property average_amount : Array(Float64)
# ```
module PG::NumericArrayFloatConverter
def self.from_rs(rs : DB::ResultSet)
rs.read(Array(PG::Numeric)).map(&.to_f)
end
end

0 comments on commit e2528b9

Please sign in to comment.