Module: Psych::Merge

Defined in:
lib/psych/merge.rb,
lib/psych/merge/emitter.rb,
lib/psych/merge/version.rb,
lib/psych/merge/provider.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

Psych-backed YAML structural merge provider integration.

Defined Under Namespace

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

Constant Summary collapse

PACKAGE_NAME =
'psych-merge'
DESTINATION_WINS_ARRAY_POLICY =
{
  surface: 'array',
  name: 'destination_wins_array'
}.freeze
BACKEND_REFERENCE =

Returns:

  • (Object)
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



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

def available_yaml_backends
  [BACKEND_REFERENCE]
end

.match_yaml_owners(template, destination) ⇒ Object



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

def match_yaml_owners(template, destination)
  Ast::Merge::YamlDocument.match_owners(template, destination)
end

.merge_providerProvider

Returns:



890
891
892
# File 'lib/psych/merge/provider.rb', line 890

def merge_provider
  @merge_provider ||= Provider.new
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



169
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
# File 'lib/psych/merge.rb', line 169

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



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

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)
  Ast::Merge::YamlDocument.analyze(parsed, dialect)
rescue TreeHaver::Error, StandardError => e
  parse_error_result(e.message)
end

.register_backend!Object



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

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

.register_provider!(replace: false) ⇒ Object

Parameters:

  • replace: (Boolean) (defaults to: false)

Returns:

  • (Object)


894
895
896
# File 'lib/psych/merge/provider.rb', line 894

def register_provider!(replace: false)
  Ast::Merge.register_provider(merge_provider, replace: replace)
end

.yaml_backend_feature_profile(backend: nil) ⇒ Object



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

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



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

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

.yaml_plan_context(backend: nil) ⇒ Object



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

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



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

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