Class: Natsuzora::Contract::TypeRefResolver

Inherits:
Object
  • Object
show all
Defined in:
lib/natsuzora/contract/type_ref_resolver.rb

Overview

Walks an AST tree and replaces AST::Ref nodes with concrete contracts looked up from a TypeDef registry. Missing or unavailable refs are delegated to caller-supplied callbacks.

Instance Method Summary collapse

Constructor Details

#initialize(types, target:, on_missing:, on_unavailable:, on_cyclic:) ⇒ TypeRefResolver

Returns a new instance of TypeRefResolver.



13
14
15
16
17
18
19
20
# File 'lib/natsuzora/contract/type_ref_resolver.rb', line 13

def initialize(types, target:, on_missing:, on_unavailable:, on_cyclic:)
  @types = types
  @target = target
  @on_missing = on_missing
  @on_unavailable = on_unavailable
  @on_cyclic = on_cyclic
  @visiting = Set.new
end

Instance Method Details

#resolve(contract) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/natsuzora/contract/type_ref_resolver.rb', line 22

def resolve(contract)
  case contract
  when AST::Ref
    resolve_ref(contract.name)
  when AST::Record
    AST::Record.new(
      contract.properties.transform_values { |c| resolve(c) },
      contract.required
    )
  when AST::List
    AST::List.new(resolve(contract.items))
  else
    contract
  end
end