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

[WIP] Refactor recursive types #15

Open
wants to merge 2 commits into
base: master
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
9 changes: 9 additions & 0 deletions lib/finitio/generation/proxy_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module Finitio
class ProxyType

def generate_data(*args, &bl)
@target&.generate_data(*args, &bl)
end

end # class ProxyType
end # module Finitio
2 changes: 1 addition & 1 deletion lib/finitio/json_schema/hash_based_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def to_json_schema(*args, &bl)
}
unless heading.empty?
base[:properties] = heading.inject({}){|ps,a|
ps.merge(a.name => a.type.to_json_schema(*args, &bl))
ps.merge(a.name => a.type.to_json_schema(*args, &bl))
}
end
unless (reqs = heading.select{|a| a.required? }).empty?
Expand Down
1 change: 1 addition & 0 deletions lib/finitio/support.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ def compare_attrs(h1, h2, &bl)
require_relative 'support/dress_helper'
require_relative 'support/type_factory'
require_relative 'support/fetch_scope'
require_relative 'support/proxy_resolution_scope'
require_relative 'support/compilation'
31 changes: 31 additions & 0 deletions lib/finitio/support/proxy_resolution_scope.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
module Finitio
class ProxyResolutionScope
def initialize(system)
@system = system
@built = {}
@building = {}
end
attr_reader :built

def fetch(name)
@built[name] || build_it(name) || import_it(name, &bl)
end

def build_it(name)
if under_build = @building[name]
under_build
else
return nil unless type = @system.fetch(name, false)

@building[name] = type
@built[name] = type.resolve_proxies(self)
@building[name] = nil
@built[name]
end
end

def import_it(name, &bl)
@system.fetch_on_imports(name, &bl)
end
end # class ProxyResolutionScope
end # module Finitio
19 changes: 8 additions & 11 deletions lib/finitio/system.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,24 +50,23 @@ def main
def fetch(name, with_imports = true, &bl)
if with_imports
@types.fetch(name) do
_fetch(name, @imports, &bl)
fetch_on_imports(name, &bl)
end
else
@types.fetch(name, &bl)
end
end

def _fetch(name, imports, &bl)
def fetch_on_imports(name, imports = @imports, &bl)
if imports.empty?
raise KeyError, %Q{key not found: "#{name}"} unless bl
bl.call(name)
else
imports.first.fetch(name, false) do
_fetch(name, imports[1..-1], &bl)
fetch_on_imports(name, imports[1..-1], &bl)
end
end
end
private :_fetch

def factory
@factory ||= TypeFactory.new
Expand All @@ -89,14 +88,12 @@ def parse(source)
Syntax.compile(source, self.dup)
end

def resolve_proxies(recurse = true)
rebuilt = {}
scope = FetchScope.new(self, rebuilt)
types.each_with_object(rebuilt) do |(name,type),memo|
rebuilt[name] = type.resolve_proxies(scope)
def resolve_proxies
scope = ProxyResolutionScope.new(self)
types.each_key do |name|
scope.fetch(name)
end
resolved = System.new(rebuilt, imports)
recurse ? resolved.resolve_proxies(false) : resolved
System.new(scope.built, imports)
end

def inspect
Expand Down
2 changes: 1 addition & 1 deletion lib/finitio/type/proxy_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def default_name
end

def resolve_proxies(system)
system.fetch(target_name){
system.fetch(target_name) {
raise Error, "No such type `#{target_name}` in #{system}"
}
end
Expand Down
2 changes: 1 addition & 1 deletion spec/json_schema/test_recursive_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module JsonSchema
system['Tree']
}

xit 'works as expected' do
it 'works as expected' do
expect(type.to_json_schema).to eql({
type: "object",
properties: {
Expand Down
20 changes: 19 additions & 1 deletion spec/syntax/test_compile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,24 @@ module Finitio
end

context 'with AD types' do
let(:source){
<<-EOF.strip
Int = .Integer
Byte = Int(i | i>0)
Color = .Color <rgb> { r: Byte, g: Byte, b: Byte }
Colors = [Color]
EOF
}

it{ should be_a(System) }

it 'should work fine' do
got = subject['Colors'].dress([{ r: 10, g: 15, b: 20 }])
expect(got.first).to be_a(Color)
end
end

context 'with AD types that make forward references' do
let(:source){
<<-EOF.strip
Colors = [Color]
Expand All @@ -88,7 +106,7 @@ module Finitio

it{ should be_a(System) }

it 'should work ine' do
it 'should work fine' do
got = subject['Colors'].dress([{ r: 10, g: 15, b: 20 }])
expect(got.first).to be_a(Color)
end
Expand Down