Skip to content

Commit

Permalink
Serialize and deserialize metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
Txus committed Oct 10, 2022
1 parent 1398acb commit 0b75894
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 29 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
fql (0.2.1)
fql (0.2.2)
activerecord (~> 6.0)
i18n (~> 1.8)
sorbet-rails (~> 0.7.3)
Expand Down
4 changes: 2 additions & 2 deletions lib/fql/query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ def not
self.class.new(DSL::Not.new(expr: expr))
end

private

sig { returns(DSL::BoolExpr) }
attr_reader :expr

private

sig { returns(Library) }
attr_reader :library
end
Expand Down
46 changes: 23 additions & 23 deletions lib/fql/serde/json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ def serialize_expression(expr)
when Array
expr.map { |e| serialize_expression(e) }
when Query::DSL::And
{ op: "and", lhs: serialize_expression(expr.lhs), rhs: serialize_expression(expr.rhs) }
{ op: "and", lhs: serialize_expression(expr.lhs), rhs: serialize_expression(expr.rhs), metadata: expr.metadata }
when Query::DSL::Or
{ op: "or", lhs: serialize_expression(expr.lhs), rhs: serialize_expression(expr.rhs) }
{ op: "or", lhs: serialize_expression(expr.lhs), rhs: serialize_expression(expr.rhs), metadata: expr.metadata }
when Query::DSL::Not
{ op: "not", expr: serialize_expression(expr.expr) }
{ op: "not", expr: serialize_expression(expr.expr), metadata: expr.metadata }
when Query::DSL::Eq, Query::DSL::Gt, Query::DSL::Gte, Query::DSL::Lt, Query::DSL::Lte
operator = case expr
when Query::DSL::Eq then "eq"
Expand All @@ -55,21 +55,21 @@ def serialize_expression(expr)
when Query::DSL::Lt then "lt"
when Query::DSL::Lte then "lte"
end
{ op: operator, lhs: serialize_expression(expr.lhs), rhs: serialize_expression(expr.rhs) }
{ op: operator, lhs: serialize_expression(expr.lhs), rhs: serialize_expression(expr.rhs), metadata: expr.metadata }
when Query::DSL::OneOf
{ op: "one_of", member: serialize_expression(expr.member), set: serialize_expression(expr.set) }
{ op: "one_of", member: serialize_expression(expr.member), set: serialize_expression(expr.set), metadata: expr.metadata }
when Query::DSL::Contains
{ op: "contains", lhs: serialize_expression(expr.lhs), rhs: serialize_expression(expr.rhs) }
{ op: "contains", lhs: serialize_expression(expr.lhs), rhs: serialize_expression(expr.rhs), metadata: expr.metadata }
when Query::DSL::Rel
{ op: "rel", name: expr.name.map(&:to_s) }
{ op: "rel", name: expr.name.map(&:to_s), metadata: expr.metadata }
when Query::DSL::Attr
{ op: "attr", target: serialize_expression(expr.target), name: expr.name.to_s }
{ op: "attr", target: serialize_expression(expr.target), name: expr.name.to_s, metadata: expr.metadata }
when Query::DSL::Var
{ op: "var", name: expr.name.to_s }
{ op: "var", name: expr.name.to_s, metadata: expr.metadata }
when Query::DSL::Call
{ op: "call", name: expr.name.to_s, arguments: expr.arguments.map { |arg| serialize_expression(arg) } }
{ op: "call", name: expr.name.to_s, arguments: expr.arguments.map { |arg| serialize_expression(arg) }, metadata: expr.metadata }
when Query::DSL::MatchesRegex
{ op: "matches_regex", lhs: serialize_expression(expr.lhs), rhs: expr.rhs.to_s }
{ op: "matches_regex", lhs: serialize_expression(expr.lhs), rhs: expr.rhs.to_s, metadata: expr.metadata }
else
T.absurd(expr)
end
Expand All @@ -90,29 +90,29 @@ def parse_expression(expr)
parse_expression(expr[:rhs]).map do |rhs|
lhs = T.cast(lhs, Query::DSL::BoolExpr)
rhs = T.cast(rhs, Query::DSL::BoolExpr)
Query::DSL::And.new(lhs: lhs, rhs: rhs)
Query::DSL::And.new(lhs: lhs, rhs: rhs, metadata: expr[:metadata])
end
end
when "eq"
parse_expression(expr[:lhs]).bind do |lhs|
parse_expression(expr[:rhs]).map do |rhs|
lhs = T.cast(lhs, Query::DSL::ValueExpr)
rhs = T.cast(rhs, T.nilable(Query::DSL::ValueExpr))
Query::DSL::Eq.new(lhs: lhs, rhs: rhs)
Query::DSL::Eq.new(lhs: lhs, rhs: rhs, metadata: expr[:metadata])
end
end
when "or"
parse_expression(expr[:lhs]).bind do |lhs|
parse_expression(expr[:rhs]).map do |rhs|
lhs = T.cast(lhs, Query::DSL::BoolExpr)
rhs = T.cast(rhs, Query::DSL::BoolExpr)
Query::DSL::Or.new(lhs: lhs, rhs: rhs)
Query::DSL::Or.new(lhs: lhs, rhs: rhs, metadata: expr[:metadata])
end
end
when "not"
parse_expression(expr[:expr]).map do |expression|
expression = T.cast(expression, Query::DSL::BoolExpr)
Query::DSL::Not.new(expr: expression)
Query::DSL::Not.new(expr: expression, metadata: expr[:metadata])
end
when "gt", "gte", "lt", "lte"
parse_expression(expr[:lhs]).bind do |lhs|
Expand All @@ -128,18 +128,18 @@ def parse_expression(expr)
Query::DSL::Lt
when "lte"
Query::DSL::Lte
end).new(lhs: lhs, rhs: rhs)
end).new(lhs: lhs, rhs: rhs, metadata: expr[:metadata])
end
end
when "rel"
Outcome.ok(Query::DSL::Rel.new(name: expr[:name].map(&:to_sym)))
Outcome.ok(Query::DSL::Rel.new(name: expr[:name].map(&:to_sym), metadata: expr[:metadata]))
when "attr"
parse_expression(expr[:target]).map do |target|
target = T.cast(target, Query::DSL::Rel)
Query::DSL::Attr.new(target: target, name: expr[:name].to_sym)
Query::DSL::Attr.new(target: target, name: expr[:name].to_sym, metadata: expr[:metadata])
end
when "var"
Outcome.ok(Query::DSL::Var.new(name: expr[:name].to_sym))
Outcome.ok(Query::DSL::Var.new(name: expr[:name].to_sym, metadata: expr[:metadata]))
when "call"
args = expr[:arguments].map { |arg| parse_expression(arg) }
errors = args.select(&:error?).map do |errored|
Expand All @@ -149,22 +149,22 @@ def parse_expression(expr)
Outcome.error(errors.join(", "))
else
parsed = args.map(&:value)
Outcome.ok(Query::DSL::Call.new(name: expr[:name].to_sym, arguments: parsed))
Outcome.ok(Query::DSL::Call.new(name: expr[:name].to_sym, arguments: parsed, metadata: expr[:metadata]))
end
when "one_of"
parse_expression(expr[:member]).map do |member|
member = T.cast(member, Query::DSL::ValueExpr)
Query::DSL::OneOf.new(member: member, set: expr[:set])
Query::DSL::OneOf.new(member: member, set: expr[:set], metadata: expr[:metadata])
end
when "contains"
parse_expression(expr[:lhs]).map do |lhs|
lhs = T.cast(lhs, Query::DSL::ValueExpr)
Query::DSL::Contains.new(lhs: lhs, rhs: expr[:rhs])
Query::DSL::Contains.new(lhs: lhs, rhs: expr[:rhs], metadata: expr[:metadata])
end
when "matches_regex"
parse_expression(expr[:lhs]).map do |lhs|
lhs = T.cast(lhs, Query::DSL::ValueExpr)
Query::DSL::MatchesRegex.new(lhs: lhs, rhs: expr[:rhs])
Query::DSL::MatchesRegex.new(lhs: lhs, rhs: expr[:rhs], metadata: expr[:metadata])
end
else
Outcome.error("unrecognized op '#{expr[:op]}'")
Expand Down
2 changes: 1 addition & 1 deletion lib/fql/version.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# typed: false
module FQL
VERSION = "0.2.1".freeze
VERSION = "0.2.2".freeze
end
2 changes: 1 addition & 1 deletion rbi/fql.rbi
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# typed: strong
module FQL
VERSION = "0.2.1".freeze
VERSION = "0.2.2".freeze

class Library
extend T::Sig
Expand Down
2 changes: 1 addition & 1 deletion spec/fql/serde/json_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

matcher :roundtrip do
match do |expression|
serialized = subject.serialize(expression)
serialized = subject.serialize(F.with_meta({foo: :bar}, expression))
subject.serialize(
subject.deserialize(
serialized
Expand Down
13 changes: 13 additions & 0 deletions spec/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,16 @@ class TestUserLibrary < FQL::Library
F.eq(a, b)
end
end
class TestUserLibrary < FQL::Library
function(:country) do
F.attr(F.rel(:address), :country)
end

function(:echo) do |what|
what
end

function(:my_eq) do |a, b|
F.eq(a, b)
end
end

0 comments on commit 0b75894

Please sign in to comment.