Module: Psych::Merge

Defined in:
lib/psych/merge.rb,
lib/psych/merge/emitter.rb,
lib/psych/merge/version.rb,
lib/psych/merge/diff_mapper.rb,
lib/psych/merge/freeze_node.rb,
lib/psych/merge/debug_logger.rb,
lib/psych/merge/merge_result.rb,
lib/psych/merge/node_wrapper.rb,
lib/psych/merge/smart_merger.rb,
lib/psych/merge/file_analysis.rb,
lib/psych/merge/comment_tracker.rb,
lib/psych/merge/conflict_resolver.rb,
lib/psych/merge/node_type_normalizer.rb,
lib/psych/merge/mapping_match_refiner.rb,
lib/psych/merge/partial_template_merger.rb,
sig/psych/merge.rbs

Overview

Smart merge system for YAML files using Psych AST analysis. Provides intelligent merging by understanding YAML structure rather than treating files as plain text.

Defined Under Namespace

Modules: DebugLogger, NodeTypeNormalizer, Version Classes: CommentTracker, ConflictResolver, CorruptionDetectedError, DestinationParseError, DiffMapper, Emitter, Error, FileAnalysis, FreezeNode, MappingEntry, MappingMatchRefiner, MergeResult, NodeWrapper, ParseError, PartialTemplateMerger, SmartMerger, TemplateParseError

Constant Summary collapse

PACKAGE_NAME =
'psych-merge'
DESTINATION_WINS_ARRAY_POLICY =
{
  surface: 'array',
  name: 'destination_wins_array'
}.freeze
BACKEND_REFERENCE =
TreeHaver::BackendReference.new(id: 'psych', family: 'native').freeze
BACKEND_REGISTRY =
Struct.new(:registered, :mutex).new(false, Mutex.new)
VERSION =

Current gem version exposed at the traditional constant location.

Returns:

  • (String)
Version::VERSION
NodeTypingNormalizer =

Alias for the shared normalizer module from ast-merge

Ast::Merge::NodeTyping::Normalizer

Class Method Summary collapse

Class Method Details

.available_yaml_backendsObject



123
124
125
# File 'lib/psych/merge.rb', line 123

def available_yaml_backends
  [BACKEND_REFERENCE]
end

.match_yaml_owners(template, destination) ⇒ Object



166
167
168
# File 'lib/psych/merge.rb', line 166

def match_yaml_owners(template, destination)
  Yaml::Merge.match_yaml_owners(template, destination)
end

.merge_yaml(template_source, destination_source, dialect, backend: nil, comment_merge_policy: :preserve_destination, preference: :destination, add_template_only_nodes: true, add_template_only_sequence_items: false, **_options) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/psych/merge.rb', line 170

def merge_yaml(
  template_source,
  destination_source,
  dialect,
  backend: nil,
  comment_merge_policy: :preserve_destination,
  preference: :destination,
  add_template_only_nodes: true,
  add_template_only_sequence_items: false,
  **_options
)
  requested = backend.to_s.empty? ? BACKEND_REFERENCE.id : backend.to_s
  unless requested == BACKEND_REFERENCE.id
    return unsupported_feature_merge_result("Unsupported YAML backend #{requested}.")
  end
  return unsupported_feature_merge_result("Unsupported YAML dialect #{dialect}.") unless dialect == 'yaml'

  output = SmartMerger.new(
    template_source,
    destination_source,
    preference: preference,
    add_template_only_nodes: add_template_only_nodes,
    add_template_only_sequence_items: add_template_only_sequence_items,
    recursive: true,
    comment_merge_policy: comment_merge_policy
  ).merge

  {
    ok: true,
    diagnostics: [],
    output: output,
    policies: [DESTINATION_WINS_ARRAY_POLICY]
  }
rescue TemplateParseError => e
  { ok: false, diagnostics: [diagnostic('error', 'template_parse_error', e.message)], policies: [] }
rescue DestinationParseError => e
  { ok: false, diagnostics: [diagnostic('error', 'destination_parse_error', e.message)], policies: [] }
rescue StandardError => e
  { ok: false, diagnostics: [diagnostic('error', 'merge_error', e.message)], policies: [] }
end

.parse_yaml(source, dialect, backend: nil) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/psych/merge.rb', line 153

def parse_yaml(source, dialect, backend: nil)
  requested = backend.to_s.empty? ? BACKEND_REFERENCE.id : backend.to_s
  unless requested == BACKEND_REFERENCE.id
    return unsupported_feature_parse_result("Unsupported YAML backend #{requested}.")
  end
  return unsupported_feature_parse_result("Unsupported YAML dialect #{dialect}.") unless dialect == 'yaml'

  parsed = yaml_value_for_source(source)
  Yaml::Merge.analyze_yaml_document(parsed, dialect)
rescue TreeHaver::Error, StandardError => e
  parse_error_result(e.message)
end

.register_backend!Object



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/psych/merge.rb', line 211

def register_backend!
  BACKEND_REGISTRY.mutex.synchronize do
    return if BACKEND_REGISTRY.registered

    TreeHaver::BackendRegistry.register(BACKEND_REFERENCE)

    TreeHaver.register_language(
      :yaml,
      backend_module: TreeHaver::Backends::Psych,
      backend_type: :psych,
      gem_name: 'psych'
    )

    BACKEND_REGISTRY.registered = true
  end
end

.yaml_backend_feature_profile(backend: nil) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
# File 'lib/psych/merge.rb', line 127

def yaml_backend_feature_profile(backend: nil)
  requested = backend.to_s.empty? ? BACKEND_REFERENCE.id : backend.to_s
  unless requested == BACKEND_REFERENCE.id
    return unsupported_feature_result("Unsupported YAML backend #{requested}.")
  end

  yaml_feature_profile.merge(
    backend: BACKEND_REFERENCE.id,
    backend_ref: BACKEND_REFERENCE.to_h
  )
end

.yaml_feature_profileObject



115
116
117
118
119
120
121
# File 'lib/psych/merge.rb', line 115

def yaml_feature_profile
  {
    family: 'yaml',
    supported_dialects: ['yaml'],
    supported_policies: [DESTINATION_WINS_ARRAY_POLICY]
  }
end

.yaml_plan_context(backend: nil) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/psych/merge.rb', line 139

def yaml_plan_context(backend: nil)
  profile = yaml_backend_feature_profile(backend: backend)
  return profile if profile[:ok] == false

  {
    family_profile: yaml_feature_profile,
    feature_profile: {
      backend: profile[:backend],
      supports_dialects: true,
      supported_policies: profile[:supported_policies]
    }
  }
end

.yaml_value_for_source(source) ⇒ Object



228
229
230
231
232
233
# File 'lib/psych/merge.rb', line 228

def yaml_value_for_source(source)
  tree = TreeHaver.with_backend(BACKEND_REFERENCE.id) do
    TreeHaver.parser_for(:yaml, backend_type: :psych).parse(source)
  end
  yaml_document_value_from_tree(tree)
end