Module: Yaml::Merge

Defined in:
lib/yaml/merge.rb,
lib/yaml/merge/version.rb

Defined Under Namespace

Modules: Version

Constant Summary collapse

PACKAGE_NAME =
"yaml-merge"
DESTINATION_WINS_ARRAY_POLICY =
{
  surface: "array",
  name: "destination_wins_array"
}.freeze
BACKEND_REFERENCE =
TreeHaver::KREUZBERG_LANGUAGE_PACK_BACKEND
VERSION =
Version::VERSION

Class Method Summary collapse

Class Method Details

.analyze_yaml_document(parsed, dialect) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/yaml/merge.rb', line 71

def analyze_yaml_document(parsed, dialect)
  return unsupported_feature_parse_result("Unsupported YAML dialect #{dialect}.") unless dialect == "yaml"
  return parse_error_result("YAML documents must parse to a mapping root.") unless parsed.is_a?(Hash)

  validated = validate_yaml_node(parsed, "")
  return { ok: false, diagnostics: [validated[:diagnostic]], policies: [] } unless validated[:ok]

  {
    ok: true,
    diagnostics: [],
    analysis: {
      kind: "yaml",
      dialect: "yaml",
      normalized_source: canonical_yaml(validated[:value]),
      root_kind: "mapping",
      owners: collect_yaml_owners(validated[:value])
    },
    policies: []
  }
end

.available_yaml_backendsObject



26
27
28
# File 'lib/yaml/merge.rb', line 26

def available_yaml_backends
  [BACKEND_REFERENCE]
end

.match_yaml_owners(template, destination) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/yaml/merge.rb', line 92

def match_yaml_owners(template, destination)
  destination_paths = destination[:owners].to_h { |owner| [owner[:path], true] }
  template_paths = template[:owners].to_h { |owner| [owner[:path], true] }

  {
    matched: template[:owners]
      .filter { |owner| destination_paths[owner[:path]] }
      .map { |owner| { template_path: owner[:path], destination_path: owner[:path] } },
    unmatched_template: template[:owners].map { |owner| owner[:path] }.reject { |path| destination_paths[path] },
    unmatched_destination: destination[:owners].map { |owner| owner[:path] }.reject { |path| template_paths[path] }
  }
end

.merge_yaml(template_source, destination_source, dialect, backend: nil) ⇒ Object



105
106
107
108
109
110
111
112
# File 'lib/yaml/merge.rb', line 105

def merge_yaml(template_source, destination_source, dialect, backend: nil)
  resolved_backend = resolve_backend(backend)
  return unsupported_feature_merge_result("Unsupported YAML backend #{resolved_backend}.") unless resolved_backend == BACKEND_REFERENCE.id

  merge_yaml_with_parser(template_source, destination_source, dialect) do |source, parse_dialect|
    parse_yaml(source, parse_dialect, backend: resolved_backend)
  end
end

.merge_yaml_with_parser(template_source, destination_source, dialect) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/yaml/merge.rb', line 114

def merge_yaml_with_parser(template_source, destination_source, dialect)
  template = yield(template_source, dialect)
  return { ok: false, diagnostics: template[:diagnostics], policies: [] } unless template[:ok]

  destination = yield(destination_source, dialect)
  unless destination[:ok]
    return {
      ok: false,
      diagnostics: destination[:diagnostics].map do |diagnostic|
        diagnostic[:category] == "parse_error" ? diagnostic.merge(category: "destination_parse_error") : diagnostic
      end,
      policies: []
    }
  end

  template_document = YAML.safe_load(template.dig(:analysis, :normalized_source), permitted_classes: [], aliases: false)
  destination_document = YAML.safe_load(destination.dig(:analysis, :normalized_source), permitted_classes: [], aliases: false)
  unless template_document.is_a?(Hash) && destination_document.is_a?(Hash)
    return parse_error_merge_result("YAML documents must parse to a mapping root.")
  end

  {
    ok: true,
    diagnostics: [],
    output: canonical_yaml(merge_yaml_mappings(template_document, destination_document)),
    policies: [DESTINATION_WINS_ARRAY_POLICY]
  }
rescue StandardError => e
  {
    ok: false,
    diagnostics: [{ severity: "error", category: "destination_parse_error", message: e.message }],
    policies: []
  }
end

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



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/yaml/merge.rb', line 54

def parse_yaml(source, dialect, backend: nil)
  return unsupported_feature_parse_result("Unsupported YAML dialect #{dialect}.") unless dialect == "yaml"

  resolved_backend = resolve_backend(backend)
  return unsupported_feature_parse_result("Unsupported YAML backend #{resolved_backend}.") unless resolved_backend == BACKEND_REFERENCE.id

  syntax_result = TreeHaver.parse_with_language_pack(
    TreeHaver::ParserRequest.new(source: source, language: "yaml")
  )
  return { ok: false, diagnostics: syntax_result[:diagnostics], policies: [] } unless syntax_result[:ok]

  parsed = YAML.safe_load(source, permitted_classes: [], aliases: false)
  analyze_yaml_document(parsed, dialect)
rescue StandardError => e
  parse_error_result(e.message)
end

.yaml_backend_feature_profile(backend: nil) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/yaml/merge.rb', line 30

def yaml_backend_feature_profile(backend: nil)
  resolved_backend = resolve_backend(backend)
  return unsupported_feature_result("Unsupported YAML backend #{resolved_backend}.") unless resolved_backend == BACKEND_REFERENCE.id

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

.yaml_feature_profileObject



18
19
20
21
22
23
24
# File 'lib/yaml/merge.rb', line 18

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

.yaml_plan_context(backend: nil) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/yaml/merge.rb', line 40

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: false,
      supported_policies: profile[:supported_policies]
    }
  }
end