Skip to content

Commit

Permalink
Crystal v1.0.0 enum compat
Browse files Browse the repository at this point in the history
Enums serialise to strings by default as of 1.0.0
This is a backwards compatible PR.
  • Loading branch information
caspiano committed Mar 23, 2021
1 parent 267a4d0 commit e00df1a
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

This is a [RethinkDB](http://rethinkdb.com/) Driver for the [Crystal Language](http://crystal-lang.org/).

[![Build Status](https://travis-ci.org/kingsleyh/crystal-rethinkdb.svg?branch=master)](https://travis-ci.org/kingsleyh/crystal-rethinkdb) [![Crystal Version](https://img.shields.io/badge/crystal%20-0.35.1-brightgreen.svg)](https://crystal-lang.org/api/0.35.1/)
[![Build Status](https://travis-ci.org/kingsleyh/crystal-rethinkdb.svg?branch=master)](https://travis-ci.org/kingsleyh/crystal-rethinkdb) [![Crystal Version](https://img.shields.io/badge/crystal%20-1.0.0-brightgreen.svg)](https://crystal-lang.org/api/1.0.0/)

## Installation

Expand Down
4 changes: 2 additions & 2 deletions shard.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: rethinkdb
version: 0.2.1
crystal: ~> 0.34
version: 0.2.2
crystal: ">= 0.34"
license: MIT

dependencies:
Expand Down
2 changes: 1 addition & 1 deletion spec/reql_spec_generator.cr
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ data = YAML.parse(yaml_fixes File.read(ARGV[0]))
puts "describe #{data["desc"].inspect} do"
if tables = data["table_variable_name"]?
puts
tables.as_s.split(", ").map(&.split(" ")).flatten.each_with_index do |tablevar, i|
tables.as_s.split(", ").flat_map(&.split(' ')).each_with_index do |tablevar, i|
random_name = "test_#{Time.utc.to_unix}_#{rand(10000)}_#{i + 1}"
puts " r.db(\"test\").table_create(#{random_name.inspect}).run(Fixtures::TestDB.conn)"
puts " #{tablevar} = r.db(\"test\").table(#{random_name.inspect})"
Expand Down
17 changes: 13 additions & 4 deletions src/rethinkdb/connection.cr
Original file line number Diff line number Diff line change
Expand Up @@ -255,11 +255,20 @@ module RethinkDB
end

struct Response < Message
{% if compare_versions(Crystal::VERSION, "0.36.1") == 1 %}
@[JSON::Field(converter: Enum::ValueConverter(RethinkDB::ResponseType))]
{% end %}
getter t : RethinkDB::ResponseType
getter r : Array(QueryResult)
{% if compare_versions(Crystal::VERSION, "0.36.1") == 1 %}
@[JSON::Field(converter: Enum::ValueConverter(RethinkDB::ErrorType))]
{% end %}
getter e : ErrorType?
getter b : Array(JSON::Any)?
getter p : JSON::Any?
{% if compare_versions(Crystal::VERSION, "0.36.1") == 1 %}
@[JSON::Field(converter: ArrayConverter(Enum::ValueConverter(RethinkDB::ResponseNote)))]
{% end %}
getter n : Array(RethinkDB::ResponseNote) = [] of RethinkDB::ResponseNote

private FEED_NOTES = [
Expand Down Expand Up @@ -327,21 +336,21 @@ module RethinkDB
end

def query_term(term)
send_query [QueryType::START, term.to_reql, runopts].to_json
send_query QueryType::START, term.to_reql, runopts
read_response
end

def query_continue
send_query [QueryType::CONTINUE].to_json
send_query QueryType::CONTINUE
read_response
end

private def send_query(query)
private def send_query(type : QueryType, *rest)
if id == 0
raise ReqlDriverError.new("Bug: Using already finished stream.")
end

query_slice = query.to_slice
query_slice = ({type.value} + rest).to_json.to_slice
conn.try_write do
conn.sock.write_bytes(id, IO::ByteFormat::LittleEndian)
conn.sock.write_bytes(query_slice.size, IO::ByteFormat::LittleEndian)
Expand Down
4 changes: 2 additions & 2 deletions src/rethinkdb/serialization.cr
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Array(T)
def to_reql
JSON.parse([
RethinkDB::TermType::MAKE_ARRAY.to_i64,
map { |x| x.to_reql },
map &.to_reql,
].to_json)
end
end
Expand All @@ -17,7 +17,7 @@ struct Tuple
def to_reql
JSON.parse([
RethinkDB::TermType::MAKE_ARRAY.to_i64,
to_a.map { |x| x.to_reql },
to_a.map &.to_reql,
].to_json)
end
end
Expand Down

0 comments on commit e00df1a

Please sign in to comment.