Module: Json::Merge
- Defined in:
- lib/json/merge.rb,
lib/json/merge/emitter.rb,
lib/json/merge/version.rb,
lib/json/merge/freeze_node.rb,
lib/json/merge/debug_logger.rb,
lib/json/merge/merge_result.rb,
lib/json/merge/node_wrapper.rb,
lib/json/merge/smart_merger.rb,
lib/json/merge/file_analysis.rb,
lib/json/merge/comment_tracker.rb,
lib/json/merge/conflict_resolver.rb,
lib/json/merge/object_match_refiner.rb,
sig/json/merge.rbs
Defined Under Namespace
Modules: DebugLogger, Version
Classes: CommentTracker, ConflictResolver, CorruptionDetectedError, DestinationParseError, Emitter, Error, FileAnalysis, FreezeNode, JsoncDialectError, MergeResult, NodeWrapper, ObjectMatchRefiner, ParseError, SmartMerger, TemplateParseError
Constant Summary
collapse
- PACKAGE_NAME =
'json-merge'
- DESTINATION_WINS_ARRAY_POLICY =
{
surface: 'array',
name: 'destination_wins_array'
}.freeze
- TREE_SITTER_BACKEND =
TreeHaver::KREUZBERG_LANGUAGE_PACK_BACKEND
- BACKEND_REGISTRY =
Struct.new(:registered, :mutex).new(false, Mutex.new)
- VERSION =
Current gem version exposed at the traditional constant location.
Version::VERSION
Class Method Summary
collapse
-
.available_json_backends ⇒ Object
-
.decode_string_literal(text, dialect: :json) ⇒ Object
-
.json_backend_feature_profile(backend: nil) ⇒ Object
-
.json_feature_profile ⇒ Object
-
.json_plan_context(backend: nil) ⇒ Object
-
.json_value_for_node(node, dialect: :json) ⇒ Object
-
.json_value_for_source(source, dialect: 'json', backend: nil) ⇒ Object
-
.match_json_owners(template, destination) ⇒ Object
-
.merge_json(template_source, destination_source, dialect, backend: nil) ⇒ Object
-
.object_key_for_literal(text, dialect: :json) ⇒ Object
-
.parse_json(source, dialect, backend: nil) ⇒ Object
-
.parse_number_literal(text, dialect: :json) ⇒ Object
-
.register_backend! ⇒ Object
Class Method Details
.available_json_backends ⇒ Object
.decode_string_literal(text, dialect: :json) ⇒ Object
243
244
245
246
247
|
# File 'lib/json/merge.rb', line 243
def decode_string_literal(text, dialect: :json)
return decode_json_string_literal(text) unless dialect == :json5
decode_json5_string_literal(text)
end
|
.json_backend_feature_profile(backend: nil) ⇒ Object
74
75
76
77
78
79
80
81
82
83
84
|
# File 'lib/json/merge.rb', line 74
def json_backend_feature_profile(backend: nil)
requested = requested_json_backend_id(backend)
unless available_json_backends.any? { |backend_ref| backend_ref.id == requested }
return unsupported_feature_result("Unsupported JSON backend #{requested}.")
end
json_feature_profile.merge(
backend: requested,
backend_ref: TREE_SITTER_BACKEND.to_h
)
end
|
.json_feature_profile ⇒ Object
62
63
64
65
66
67
68
|
# File 'lib/json/merge.rb', line 62
def json_feature_profile
{
family: 'json',
supported_dialects: %w[json jsonc json5],
supported_policies: [DESTINATION_WINS_ARRAY_POLICY]
}
end
|
.json_plan_context(backend: nil) ⇒ Object
86
87
88
89
90
91
92
93
94
95
96
97
98
|
# File 'lib/json/merge.rb', line 86
def json_plan_context(backend: nil)
profile = json_backend_feature_profile(backend: backend)
return profile if profile[:ok] == false
{
family_profile: json_feature_profile,
feature_profile: {
backend: profile[:backend],
supports_dialects: true,
supported_policies: profile[:supported_policies]
}
}
end
|
.json_value_for_node(node, dialect: :json) ⇒ Object
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
|
# File 'lib/json/merge.rb', line 206
def json_value_for_node(node, dialect: :json)
case node.type.to_s
when 'document'
child = node.semantic_children.first
if child
json_value_for_node(NodeWrapper.new(child, lines: node.lines, source: node.source, dialect: dialect),
dialect: dialect)
end
when 'object'
node.pairs.each_with_object({}) do |pair, object|
key = pair.key_name
next unless key
object[key] = json_value_for_node(pair.value_node, dialect: dialect)
end
when 'array'
node.elements.map { |element| json_value_for_node(element, dialect: dialect) }
when 'string'
decode_string_literal(node.text, dialect: dialect)
when 'number'
parse_number_literal(node.text, dialect: dialect)
when 'true'
true
when 'false'
false
when 'null'
nil
end
end
|
.json_value_for_source(source, dialect: 'json', backend: nil) ⇒ Object
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
|
# File 'lib/json/merge.rb', line 183
def json_value_for_source(source, dialect: 'json', backend: nil)
dialect = normalize_json_dialect(dialect)
requested = requested_json_backend_id(backend)
unless json_backend_available_for_analysis?(requested, dialect: dialect)
raise ParseError, "Unsupported JSON backend #{requested}."
end
if dialect == :json && detect_trailing_comma(source)
raise ParseError,
"Trailing commas are not supported for #{dialect}."
end
raise ParseError, "Comments are not supported for #{dialect}." if dialect == :json && (source)
register_backend!
analysis = TreeHaver.with_backend(requested) { FileAnalysis.new(source, dialect: dialect) }
unless analysis.valid?
message = analysis.errors.map { |error| error.respond_to?(:message) ? error.message : error.inspect }.join(', ')
raise ParseError, message
end
json_value_for_node(analysis.root_node, dialect: dialect)
end
|
.match_json_owners(template, destination) ⇒ Object
137
138
139
|
# File 'lib/json/merge.rb', line 137
def match_json_owners(template, destination)
Ast::Merge::OwnerSelection.match_by_path(template, destination)
end
|
.merge_json(template_source, destination_source, dialect, backend: nil) ⇒ Object
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
|
# File 'lib/json/merge.rb', line 141
def merge_json(template_source, destination_source, dialect, backend: nil)
dialect = normalize_json_dialect(dialect)
requested = requested_json_backend_id(backend)
unless json_backend_available_for_analysis?(requested, dialect: dialect)
return unsupported_feature_result("Unsupported JSON backend #{requested}.")
end
template_result = parse_json(template_source, dialect, backend: requested)
return { ok: false, diagnostics: template_result[:diagnostics] } unless template_result[:ok]
destination_result = parse_json(destination_source, dialect, backend: requested)
if destination_result[:ok]
return {
ok: true,
diagnostics: [],
output: TreeHaver.with_backend(requested) do
merge_json_sources(template_source, destination_source, dialect: dialect)
end,
policies: [DESTINATION_WINS_ARRAY_POLICY]
}
end
{
ok: false,
diagnostics: destination_result[:diagnostics].map do |diagnostic|
diagnostic[:category] == 'parse_error' ? diagnostic.merge(category: 'destination_parse_error') : diagnostic
end
}
end
|
.object_key_for_literal(text, dialect: :json) ⇒ Object
236
237
238
239
240
241
|
# File 'lib/json/merge.rb', line 236
def object_key_for_literal(text, dialect: :json)
literal = text.to_s
return literal if dialect == :json5 && !%w[' "].include?(literal[0])
decode_string_literal(literal, dialect: dialect)
end
|
.parse_json(source, dialect, backend: nil) ⇒ Object
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
128
129
130
131
132
133
134
135
|
# File 'lib/json/merge.rb', line 100
def parse_json(source, dialect, backend: nil)
dialect = normalize_json_dialect(dialect)
requested = requested_json_backend_id(backend)
unless json_backend_available_for_analysis?(requested, dialect: dialect)
return unsupported_feature_result("Unsupported JSON backend #{requested}.")
end
if dialect == :json && detect_trailing_comma(source)
return parse_failure("Trailing commas are not supported for #{dialect}.")
end
if dialect == :json && (source)
return parse_failure("Comments are not supported for #{dialect}.")
end
register_backend!
analysis = TreeHaver.with_backend(requested) { FileAnalysis.new(source, dialect: dialect) }
unless analysis.valid?
return parse_failure(analysis.errors.map do |error|
error.respond_to?(:message) ? error.message : error.inspect
end.join(', '))
end
{
ok: true,
diagnostics: [],
analysis: {
dialect: dialect.to_s,
allows_comments: %i[jsonc json5].include?(dialect),
root_kind: json_analysis_root_kind(analysis),
owners: collect_file_analysis_owners(analysis)
},
policies: []
}
rescue TreeHaver::Error, StandardError => e
parse_failure(e.message)
end
|
.parse_number_literal(text, dialect: :json) ⇒ Object
317
318
319
320
321
322
323
324
325
326
327
328
|
# File 'lib/json/merge.rb', line 317
def parse_number_literal(text, dialect: :json)
return parse_json_number(text) unless dialect == :json5
literal = text.to_s.delete('_')
return Float::INFINITY if ['Infinity', '+Infinity'].include?(literal)
return -Float::INFINITY if literal == '-Infinity'
return Float::NAN if %w[NaN +NaN -NaN].include?(literal)
Integer(literal, 0)
rescue ArgumentError
Float(literal)
end
|
.register_backend! ⇒ Object
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
# File 'lib/json/merge.rb', line 43
def register_backend!
BACKEND_REGISTRY.mutex.synchronize do
return if BACKEND_REGISTRY.registered
TreeHaver::BackendRegistry.register(TREE_SITTER_BACKEND)
grammar_finder = TreeHaver::GrammarFinder.new(:json)
grammar_finder.register! if grammar_finder.available?
json5_grammar_finder = TreeHaver::GrammarFinder.new(:json5)
json5_grammar_finder.register! if json5_grammar_finder.available?
BACKEND_REGISTRY.registered = true
end
end
|