Class: Odin::Resolver::ImportResolver

Inherits:
Object
  • Object
show all
Defined in:
lib/odin/resolver/import_resolver.rb

Constant Summary collapse

MAX_IMPORT_DEPTH =
32
MAX_TOTAL_IMPORTS =
1000

Instance Method Summary collapse

Constructor Details

#initialize(loader: nil) ⇒ ImportResolver

Returns a new instance of ImportResolver.



11
12
13
14
15
16
# File 'lib/odin/resolver/import_resolver.rb', line 11

def initialize(loader: nil)
  @loader = loader || method(:default_loader)
  @active_chain = Set.new   # paths currently being resolved (true-cycle detection)
  @schema_cache = {}        # parsed schemas keyed by absolute path (diamond reuse)
  @total_loaded = 0
end

Instance Method Details

#resolve(schema, base_path: ".") ⇒ Object

Resolve all imports in a schema, returning a flattened schema



19
20
21
22
23
24
25
26
27
28
# File 'lib/odin/resolver/import_resolver.rb', line 19

def resolve(schema, base_path: ".")
  return schema if schema.imports.empty?

  imported_schemas = []
  schema.imports.each do |imp|
    resolve_import(imp, base_path, imported_schemas, depth: 0)
  end

  flatten(schema, imported_schemas)
end

#resolve_with_registry(schema, base_path: ".") ⇒ Object

Resolve imports and return [flattened_schema, type_registry]. base_path is the schema file path; imports resolve relative to its directory. The registry namespaces imported types by alias for @alias.typename lookup.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/odin/resolver/import_resolver.rb', line 33

def resolve_with_registry(schema, base_path: ".")
  import_dir = File.directory?(base_path) ? base_path : File.dirname(base_path)
  imported_schemas = []
  schema.imports.each do |imp|
    resolve_import(imp, import_dir, imported_schemas, depth: 0)
  end

  registry = TypeRegistry.new
  imported_schemas.each do |entry|
    registry.register_all(entry[:schema].types, entry[:alias_name])
  end
  registry.register_all(schema.types, nil)

  flattened = schema.imports.empty? ? schema : flatten(schema, imported_schemas)
  [flattened, registry]
end