Class: Ecoportal::API::GraphQL::Diff::IdResolver

Inherits:
Object
  • Object
show all
Defined in:
lib/ecoportal/api/graphql/diff/id_resolver.rb

Overview

Resolves the human keys a VersionDiff records (stage NAME, section HEADING) into the target-doc ids that move commands need (moveField -> section id, add/removeStageSection -> stage id). A structural diff only knows those human keys; this fills the gap WITHOUT guessing — it looks them up in a real target doc (the deploy destination), so CommandSynthesizer can emit faithful move commands.

Answers resolve(kind, key) => id | nil. A nil (ambiguous / not found) keeps the move UNSUPPORTED rather than picking a wrong id.

resolver = IdResolver.from_doc(prod_template_doc) CommandSynthesizer.new(changes, resolver: resolver)

AMBIGUITY — if two stages share a name (or two sections a heading), the key is ambiguous and resolves to nil (the caller must disambiguate). Uniqueness is the safe default.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(index = {}) ⇒ IdResolver

Build from an explicit map: { stage: { 'Report' => 'stg1' }, section: { 'Location' => 'sec1' } }



21
22
23
# File 'lib/ecoportal/api/graphql/diff/id_resolver.rb', line 21

def initialize(index = {})
  @index = index
end

Class Method Details

.from_doc(doc) ⇒ Object

Build a resolver by indexing a page/template doc's stages (by name) and sections (by heading). Duplicate keys are dropped (resolve -> nil) so we never pick arbitrarily.



27
28
29
30
31
32
33
34
35
36
# File 'lib/ecoportal/api/graphql/diff/id_resolver.rb', line 27

def self.from_doc(doc)
  doc ||= {}
  stages   = {}
  sections = {}
  Array(doc['stages']).each do |st|
    mark(stages, st['name'], st['id'])
    Array(st['sections']).each { |sec| mark(sections, sec['heading'], sec['id']) }
  end
  new(stage: strip_ambiguous(stages), section: strip_ambiguous(sections))
end

Instance Method Details

#resolve(kind, key) ⇒ String?

Returns the target id for (kind, key), or nil if unknown/ambiguous.

Returns:

  • (String, nil)

    the target id for (kind, key), or nil if unknown/ambiguous.



39
40
41
# File 'lib/ecoportal/api/graphql/diff/id_resolver.rb', line 39

def resolve(kind, key)
  @index.dig(kind.to_sym, key)
end