Module: Json::Merge
- Defined in:
- lib/json/merge.rb,
lib/json/merge/version.rb
Defined Under Namespace
Modules: Version
Constant Summary collapse
- PACKAGE_NAME =
"json-merge"- DESTINATION_WINS_ARRAY_POLICY =
{ surface: "array", name: "destination_wins_array" }.freeze
- TRAILING_COMMA_FALLBACK_POLICY =
{ surface: "fallback", name: "trailing_comma_destination_fallback" }.freeze
- VERSION =
Version::VERSION
Class Method Summary collapse
- .json_feature_profile ⇒ Object
- .json_parse_request(source, dialect) ⇒ Object
- .match_json_owners(template, destination) ⇒ Object
- .merge_json(template_source, destination_source, dialect) ⇒ Object
- .parse_json(source, dialect) ⇒ Object
- .parse_json_with_language_pack(source, dialect) ⇒ Object
Class Method Details
.json_feature_profile ⇒ Object
20 21 22 23 24 25 26 |
# File 'lib/json/merge.rb', line 20 def json_feature_profile { family: "json", supported_dialects: %w[json jsonc], supported_policies: [DESTINATION_WINS_ARRAY_POLICY, TRAILING_COMMA_FALLBACK_POLICY] } end |
.json_parse_request(source, dialect) ⇒ Object
28 29 30 |
# File 'lib/json/merge.rb', line 28 def json_parse_request(source, dialect) TreeHaver::ParserRequest.new(source: source, language: "json", dialect: dialect) end |
.match_json_owners(template, destination) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/json/merge.rb', line 65 def match_json_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_map do |owner| next unless destination_paths[owner[:path]] { template_path: owner[:path], destination_path: owner[:path] } end, 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_json(template_source, destination_source, dialect) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/json/merge.rb', line 80 def merge_json(template_source, destination_source, dialect) template_result = parse_json(template_source, dialect) return { ok: false, diagnostics: template_result[:diagnostics] } unless template_result[:ok] destination_result = parse_json(destination_source, dialect) if destination_result[:ok] output = JSON.generate( merge_json_values( JSON.parse(template_result.dig(:analysis, :normalized_source)), JSON.parse(destination_result.dig(:analysis, :normalized_source)) ) ) return { ok: true, diagnostics: [], output: output, policies: [DESTINATION_WINS_ARRAY_POLICY] } end fallback_source = try_destination_trailing_comma_fallback(destination_source) if fallback_source retried = parse_json(fallback_source, dialect) if retried[:ok] output = JSON.generate( merge_json_values( JSON.parse(template_result.dig(:analysis, :normalized_source)), JSON.parse(retried.dig(:analysis, :normalized_source)) ) ) return { ok: true, diagnostics: [ fallback_applied("stripped trailing commas from destination before retrying json merge.") ], output: output, policies: [DESTINATION_WINS_ARRAY_POLICY, TRAILING_COMMA_FALLBACK_POLICY] } end end { ok: false, diagnostics: destination_result[:diagnostics].map do |diagnostic| diagnostic[:category] == "parse_error" ? diagnostic.merge(category: "destination_parse_error") : diagnostic end } end |
.parse_json(source, dialect) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/json/merge.rb', line 41 def parse_json(source, dialect) normalized_source = dialect == "jsonc" ? strip_json_comments(source) : source allows_comments = dialect == "jsonc" return parse_failure("Trailing commas are not supported for #{dialect}.") if detect_trailing_comma(normalized_source) parsed = JSON.parse(normalized_source) canonical = JSON.generate(parsed) analysis = { kind: "json", dialect: dialect, allows_comments: allows_comments, normalized_source: canonical, root_kind: json_root_kind(parsed), owners: collect_json_owners(parsed) } { ok: true, diagnostics: [], analysis: analysis } rescue JSON::ParserError => e parse_failure(e.) end |
.parse_json_with_language_pack(source, dialect) ⇒ Object
32 33 34 35 36 37 38 39 |
# File 'lib/json/merge.rb', line 32 def parse_json_with_language_pack(source, dialect) return unsupported_jsonc_language_pack_result if dialect != "json" backend_result = TreeHaver.parse_with_language_pack(json_parse_request(source, dialect)) return { ok: false, diagnostics: backend_result[:diagnostics] } unless backend_result[:ok] parse_json(source, dialect) end |