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

Compiler#visit_implication, Compiler#visit_intersection and a couple of changes in specs #452

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
10 changes: 10 additions & 0 deletions lib/dry/types/compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,16 @@ def visit_any(meta)
registry["any"].meta(meta)
end

def visit_intersection(node)
*types, meta = node
types.map { |type| visit(type) }.reduce(:&).meta(meta)
end

def visit_implication(node)
*types, meta = node
types.map { |type| visit(type) }.reduce(:>).meta(meta)
end

def compile_fn(fn)
type, *node = fn

Expand Down
36 changes: 36 additions & 0 deletions spec/dry/types/compiler_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -377,4 +377,40 @@
expect(type).to eql(source)
end
end

context "composites" do
let(:strict_nil_ast) do
[:constrained,
[[:nominal, [NilClass, {}]],
[:predicate, [:type?, [[:type, NilClass], [:input, Undefined]]]]]]
end

let(:strict_integer_ast) do
[:constrained,
[[:nominal, [Integer, {}]],
[:predicate, [:type?, [[:type, Integer], [:input, Undefined]]]]]]
end

let(:any_numeric_ast) do
[:constrained, [[:any, {}], [:predicate, [:type?, [[:type, Numeric], [:input, Undefined]]]]]]
end

it 'builds a sum' do
ast = [:sum, [strict_nil_ast, strict_integer_ast, {}]]
type = compiler.(ast)
expect(type).to eql(Dry::Types['integer'].optional)
end

it 'builds an implication' do
ast = [:implication, [any_numeric_ast, strict_integer_ast, {}]]
type = compiler.(ast)
expect(type).to eql(Dry::Types['any'].constrained(type: Numeric) > Dry::Types['integer'])
end

it 'builds an intersection' do
ast = [:intersection, [any_numeric_ast, strict_integer_ast, {}]]
type = compiler.(ast)
expect(type).to eql(Dry::Types['any'].constrained(type: Numeric) & Dry::Types['integer'])
end
end
end
13 changes: 6 additions & 7 deletions spec/dry/types/implication_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -216,15 +216,14 @@
end

describe "#meta" do
context "optional types" do
let(:meta) { {foo: :bar} }
let(:meta) { {foo: :bar} }

subject(:type) { t::Nominal::String.optional }
subject(:type) { t::Nominal::Hash > t.Hash(foo: t::Nominal::Integer) }

it "uses meta from the right branch" do
expect(type.meta(meta).meta).to eql(meta)
expect(type.meta(meta).right.meta).to eql(meta)
end
it "has no special meta handling" do
expect(type.meta(meta).meta).to eql(meta)
expect(type.meta(meta).left.meta).to eql({})
expect(type.meta(meta).right.meta).to eql({})
end
end
end
13 changes: 6 additions & 7 deletions spec/dry/types/intersection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -231,15 +231,14 @@
end

describe "#meta" do
context "optional types" do
let(:meta) { {foo: :bar} }
let(:meta) { {foo: :bar} }

subject(:type) { Dry::Types["string"].optional }
subject(:type) { t::Nominal::Hash & t.Hash(foo: t::Nominal::Integer) }

it "uses meta from the right branch" do
expect(type.meta(meta).meta).to eql(meta)
expect(type.meta(meta).right.meta).to eql(meta)
end
it "has no special meta handling" do
expect(type.meta(meta).meta).to eql(meta)
expect(type.meta(meta).left.meta).to eql({})
expect(type.meta(meta).right.meta).to eql({})
end
end
end