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/native_projection.rb,
lib/psych/merge/provider_extension.rb,
lib/psych/merge/node_type_normalizer.rb,
lib/psych/merge/mapping_match_refiner.rb,
lib/psych/merge/partial_template_merger.rb,
lib/psych/merge/rspec/provider_snapshot_extension.rb,
sig/psych/merge.rbs

Overview

Psych-backed YAML provider integration.

Defined Under Namespace

Modules: DebugLogger, NativeProjection, NodeTypeNormalizer, ProviderExtension, RSpec, 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_backends ⇒ Object



125
126
127
# File 'lib/psych/merge.rb', line 125

def available_yaml_backends
  [BACKEND_REFERENCE]
end

.match_yaml_owners(template, destination) ⇒ Object



168
169
170
# File 'lib/psych/merge.rb', line 168

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

.merge_provider ⇒ Provider

Returns:



173
174
175
# File 'lib/psych/merge/provider.rb', line 173

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



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

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



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

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



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

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)


177
178
179
# File 'lib/psych/merge/provider.rb', line 177

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

.yaml_backend_feature_profile(backend: nil) ⇒ Object



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

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_profile ⇒ Object



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

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

.yaml_plan_context(backend: nil) ⇒ Object



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

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



230
231
232
233
234
235
# File 'lib/psych/merge.rb', line 230

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