lutaml-lml 0.3.5 → 0.3.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (114) hide show
  1. checksums.yaml +4 -4
  2. data/lib/lutaml-lml.rb +5 -0
  3. data/lib/lutaml_lml/version.rb +6 -0
  4. metadata +12 -164
  5. data/exe/lutaml-lml +0 -6
  6. data/exe/lutaml-lml-lsp +0 -11
  7. data/lib/lutaml/lml/association_label_resolver.rb +0 -52
  8. data/lib/lutaml/lml/cli.rb +0 -489
  9. data/lib/lutaml/lml/data_processor/attribute_processing.rb +0 -133
  10. data/lib/lutaml/lml/data_processor/collection_processing.rb +0 -37
  11. data/lib/lutaml/lml/data_processor/instance_processing.rb +0 -77
  12. data/lib/lutaml/lml/data_processor/value_declaration_processing.rb +0 -70
  13. data/lib/lutaml/lml/data_processor/value_processing.rb +0 -103
  14. data/lib/lutaml/lml/data_processor/view_processing.rb +0 -26
  15. data/lib/lutaml/lml/data_processor.rb +0 -59
  16. data/lib/lutaml/lml/diagnostic.rb +0 -149
  17. data/lib/lutaml/lml/document_builder.rb +0 -481
  18. data/lib/lutaml/lml/entity_types.rb +0 -30
  19. data/lib/lutaml/lml/executor/adapter_helpers.rb +0 -45
  20. data/lib/lutaml/lml/executor/condition_evaluator.rb +0 -169
  21. data/lib/lutaml/lml/executor/csv_adapter.rb +0 -88
  22. data/lib/lutaml/lml/executor/format_adapter.rb +0 -54
  23. data/lib/lutaml/lml/executor/xml_adapter.rb +0 -102
  24. data/lib/lutaml/lml/executor.rb +0 -96
  25. data/lib/lutaml/lml/format/adapter/document.rb +0 -11
  26. data/lib/lutaml/lml/format/adapter/mapping.rb +0 -31
  27. data/lib/lutaml/lml/format/adapter/standard_adapter.rb +0 -162
  28. data/lib/lutaml/lml/format/adapter/transform.rb +0 -11
  29. data/lib/lutaml/lml/format.rb +0 -29
  30. data/lib/lutaml/lml/formatter/base.rb +0 -79
  31. data/lib/lutaml/lml/formatter/canonical.rb +0 -736
  32. data/lib/lutaml/lml/formatter/elk.rb +0 -45
  33. data/lib/lutaml/lml/formatter/graphviz/document_formatter.rb +0 -89
  34. data/lib/lutaml/lml/formatter/graphviz/html_builder.rb +0 -72
  35. data/lib/lutaml/lml/formatter/graphviz/node_formatter.rb +0 -89
  36. data/lib/lutaml/lml/formatter/graphviz/relationship_formatter.rb +0 -130
  37. data/lib/lutaml/lml/formatter/graphviz.rb +0 -90
  38. data/lib/lutaml/lml/formatter/svg_pdf/path_parser.rb +0 -134
  39. data/lib/lutaml/lml/formatter/svg_pdf.rb +0 -285
  40. data/lib/lutaml/lml/formatter.rb +0 -12
  41. data/lib/lutaml/lml/grammar/lml.artifact.json +0 -14993
  42. data/lib/lutaml/lml/grammar/lml.parg +0 -389
  43. data/lib/lutaml/lml/grammar.rb +0 -60
  44. data/lib/lutaml/lml/has_attributes.rb +0 -14
  45. data/lib/lutaml/lml/import_resolver.rb +0 -96
  46. data/lib/lutaml/lml/layout/elk_engine.rb +0 -15
  47. data/lib/lutaml/lml/layout/elk_graph_builder.rb +0 -129
  48. data/lib/lutaml/lml/layout/elk_svg_markers.rb +0 -45
  49. data/lib/lutaml/lml/layout/elk_svg_renderer.rb +0 -119
  50. data/lib/lutaml/lml/layout/engine.rb +0 -17
  51. data/lib/lutaml/lml/layout/graph_viz_engine.rb +0 -19
  52. data/lib/lutaml/lml/layout.rb +0 -12
  53. data/lib/lutaml/lml/literal_value.rb +0 -32
  54. data/lib/lutaml/lml/lsp/conversions.rb +0 -78
  55. data/lib/lutaml/lml/lsp/keywords.rb +0 -43
  56. data/lib/lutaml/lml/lsp/server.rb +0 -378
  57. data/lib/lutaml/lml/lsp/store.rb +0 -68
  58. data/lib/lutaml/lml/lsp/transport.rb +0 -53
  59. data/lib/lutaml/lml/lsp.rb +0 -16
  60. data/lib/lutaml/lml/model_compiler/enums.rb +0 -172
  61. data/lib/lutaml/lml/model_compiler/hydration.rb +0 -123
  62. data/lib/lutaml/lml/model_compiler/serialization_mappings.rb +0 -188
  63. data/lib/lutaml/lml/model_compiler/string_formats.rb +0 -42
  64. data/lib/lutaml/lml/model_compiler/validation.rb +0 -65
  65. data/lib/lutaml/lml/model_compiler.rb +0 -323
  66. data/lib/lutaml/lml/models/action.rb +0 -10
  67. data/lib/lutaml/lml/models/association.rb +0 -29
  68. data/lib/lutaml/lml/models/cardinality.rb +0 -10
  69. data/lib/lutaml/lml/models/collection.rb +0 -11
  70. data/lib/lutaml/lml/models/constraint.rb +0 -23
  71. data/lib/lutaml/lml/models/data_type.rb +0 -41
  72. data/lib/lutaml/lml/models/derived_field.rb +0 -14
  73. data/lib/lutaml/lml/models/diagram.rb +0 -20
  74. data/lib/lutaml/lml/models/document.rb +0 -84
  75. data/lib/lutaml/lml/models/enum.rb +0 -38
  76. data/lib/lutaml/lml/models/fidelity.rb +0 -10
  77. data/lib/lutaml/lml/models/group.rb +0 -11
  78. data/lib/lutaml/lml/models/instance.rb +0 -26
  79. data/lib/lutaml/lml/models/instance_collection.rb +0 -16
  80. data/lib/lutaml/lml/models/instances_export.rb +0 -10
  81. data/lib/lutaml/lml/models/instances_import.rb +0 -11
  82. data/lib/lutaml/lml/models/mapping.rb +0 -15
  83. data/lib/lutaml/lml/models/mapping_rule.rb +0 -16
  84. data/lib/lutaml/lml/models/member_field.rb +0 -14
  85. data/lib/lutaml/lml/models/namespace.rb +0 -16
  86. data/lib/lutaml/lml/models/operation.rb +0 -26
  87. data/lib/lutaml/lml/models/operation_parameter.rb +0 -12
  88. data/lib/lutaml/lml/models/package.rb +0 -26
  89. data/lib/lutaml/lml/models/primitive_type.rb +0 -33
  90. data/lib/lutaml/lml/models/reference.rb +0 -23
  91. data/lib/lutaml/lml/models/serialization_mapping.rb +0 -19
  92. data/lib/lutaml/lml/models/string_format.rb +0 -16
  93. data/lib/lutaml/lml/models/top_element_attribute.rb +0 -116
  94. data/lib/lutaml/lml/models/uml_class.rb +0 -85
  95. data/lib/lutaml/lml/models/value.rb +0 -15
  96. data/lib/lutaml/lml/models/view_filter.rb +0 -11
  97. data/lib/lutaml/lml/models/view_import.rb +0 -11
  98. data/lib/lutaml/lml/parser.rb +0 -19
  99. data/lib/lutaml/lml/pipeline.rb +0 -73
  100. data/lib/lutaml/lml/positioned.rb +0 -13
  101. data/lib/lutaml/lml/preprocessor.rb +0 -130
  102. data/lib/lutaml/lml/quoting.rb +0 -60
  103. data/lib/lutaml/lml/source.rb +0 -49
  104. data/lib/lutaml/lml/source_position.rb +0 -20
  105. data/lib/lutaml/lml/symbol_index.rb +0 -200
  106. data/lib/lutaml/lml/transform.rb +0 -119
  107. data/lib/lutaml/lml/types/text_type.rb +0 -28
  108. data/lib/lutaml/lml/types.rb +0 -10
  109. data/lib/lutaml/lml/validator.rb +0 -458
  110. data/lib/lutaml/lml/version.rb +0 -7
  111. data/lib/lutaml/lml/view_resolution.rb +0 -32
  112. data/lib/lutaml/lml/view_resolver.rb +0 -48
  113. data/lib/lutaml/lml/yaml_parser.rb +0 -17
  114. data/lib/lutaml/lml.rb +0 -130
@@ -1,489 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'thor'
4
- require 'pathname'
5
- require 'json'
6
- require 'lutaml/lml'
7
-
8
- module Lutaml
9
- module Cli
10
- # Thor CLI commands for LutaML DSL generation and validation.
11
- class LmlCommands < Thor
12
- include ::Lutaml::Lml::HasAttributes
13
-
14
- SUPPORTED_FORMATS = %w[yaml lutaml exp].freeze
15
- DEFAULT_INPUT_FORMAT = 'lutaml'
16
- DEFAULT_OUTPUT_FORMAT = 'svg'
17
-
18
- EXTENSION_FORMAT_MAP = {
19
- '.svg' => 'svg', '.svgz' => 'svgz',
20
- '.png' => 'png', '.gif' => 'gif', '.jpg' => 'jpg', '.jpeg' => 'jpeg',
21
- '.pdf' => 'pdf', '.ps' => 'ps', '.eps' => 'eps',
22
- '.dot' => 'dot', '.xdot' => 'xdot',
23
- '.fig' => 'fig', '.json' => 'json',
24
- '.imap' => 'imap', '.cmapx' => 'cmapx'
25
- }.freeze
26
-
27
- PARSE_HANDLERS = {
28
- 'lutaml' => ->(path) { File.open(path) { |f| Lutaml::Lml.parse(f) } },
29
- 'yaml' => ->(path) { Lutaml::Lml::YamlParser.parse(path.to_s) },
30
- 'yml' => ->(path) { Lutaml::Lml::YamlParser.parse(path.to_s) },
31
- 'exp' => lambda { |path|
32
- require 'lutaml/express'
33
- Lutaml::Express::Parsers::Exp.parse(File.new(path))
34
- },
35
- }.freeze
36
-
37
- FORMATTER_ATTR_TARGETS = %i[graph edge node].freeze
38
-
39
- def initialize(*args)
40
- super
41
- @formatter = case options[:layout]
42
- when 'elk'
43
- ::Lutaml::Formatter::Elk.new
44
- else
45
- ::Lutaml::Formatter::Graphviz.new
46
- end
47
- @out_object = $stdout
48
- end
49
-
50
- desc 'generate [PATHS]', 'Generate diagram output from LutaML DSL files'
51
- long_desc <<-DESC
52
- Generate diagram output from one or more LutaML DSL files.
53
-
54
- Supports multiple input formats (lutaml, yaml, exp) and can output
55
- to various formats (svg, png, dot, pdf, etc.).
56
-
57
- Examples:
58
- lutaml lml generate model.lutaml -o diagram.svg
59
-
60
- lutaml lml generate model.lutaml -o diagram.dot -t dot
61
-
62
- lutaml lml generate model.lutaml project.lutaml -o output/
63
- DESC
64
- method_option :output, type: :string, aliases: '-o',
65
- desc: 'Output path (file or directory)'
66
- method_option :formatter, type: :string, aliases: '-f',
67
- desc: 'Output formatter (default: graphviz)'
68
- method_option :type, type: :string, aliases: '-t',
69
- desc: 'Output format type (svg, png, dot, etc.)'
70
- method_option :input_format, type: :string, aliases: '-i',
71
- desc: 'Input format (lutaml, yaml, exp)'
72
- method_option :graph, type: :string, aliases: '-g',
73
- desc: 'Graph attributes (key=value,key2=value2)'
74
- method_option :edge, type: :string, aliases: '-e',
75
- desc: 'Edge attributes (key=value,key2=value2)'
76
- method_option :node, type: :string, aliases: '-n',
77
- desc: 'Node attributes (key=value,key2=value2)'
78
- method_option :all, type: :string, aliases: '-a',
79
- desc: 'Set attributes for graph, edge, and node'
80
- method_option :layout, type: :string, enum: %w[graphviz elk], default: 'graphviz',
81
- desc: 'Layout engine (elk requires the elkrb gem)'
82
- def generate(*paths)
83
- assert_input_paths(paths)
84
-
85
- setup_options
86
- @paths = paths.map { |path| Pathname.new(path) }
87
-
88
- assert_output_path
89
-
90
- @paths.each { |input_path| process_single_file(input_path) }
91
- end
92
-
93
- desc 'validate [PATHS]', 'Validate LutaML DSL syntax'
94
- long_desc <<-DESC
95
- Validate the syntax of one or more LutaML DSL files.
96
-
97
- Checks for syntax errors and structural issues in the DSL files
98
- without generating output.
99
-
100
- Examples:
101
- lutaml lml validate model.lutaml
102
-
103
- lutaml lml validate model.lutaml project.lutaml
104
- DESC
105
- method_option :input_format, type: :string, aliases: '-i',
106
- desc: 'Input format (lutaml, yaml, exp)'
107
- method_option :format, type: :string, enum: %w[text json], default: 'text',
108
- desc: 'Report format: text (default) or json'
109
- def validate(*paths)
110
- assert_input_paths(paths)
111
- @input_format = options[:input_format] || DEFAULT_INPUT_FORMAT
112
- diagnostics = paths.flat_map { |path| validate_single_file(path) }
113
- report_validation(diagnostics)
114
- end
115
-
116
- desc 'format [PATHS]', 'Render LML files in canonical form'
117
- long_desc <<-DESC
118
- Rewrite LML files into the RS 3001 canonical form (deterministic
119
- member order, two-space layout, canonical attribute forms and
120
- quoting).
121
-
122
- With --check, files are not modified: each non-canonical file is
123
- reported with a diff and the command exits 1.
124
-
125
- Examples:
126
- lutaml lml format model.lml
127
-
128
- lutaml lml format model.lml --check
129
- DESC
130
- method_option :check, type: :boolean, default: false,
131
- desc: 'Report non-canonical files instead of rewriting them'
132
- def format(*paths)
133
- assert_input_paths(paths)
134
-
135
- noncanonical = paths.reject { |path| file_canonical?(path) }
136
- return if noncanonical.empty?
137
-
138
- exit 1
139
- end
140
-
141
- desc 'compile PATH', 'Compile LML model definitions into Ruby classes'
142
- long_desc <<-DESC
143
- Parse an LML file containing model definitions and compile them
144
- into Ruby classes that can be used to instantiate and validate data.
145
-
146
- With --namespace, classes are registered as constants in the given
147
- module (created if absent).
148
-
149
- With --schema, emits a JSON Schema or YAML Schema document for every
150
- compiled model instead of the compile listing. Schema output requires
151
- named classes, so --namespace defaults to Lutaml::Lml::Generated when omitted.
152
-
153
- Examples:
154
- lutaml lml compile models.lml
155
-
156
- lutaml lml compile models.lml --namespace MyModels
157
-
158
- lutaml lml compile models.lml --schema json -o schemas/
159
-
160
- lutaml lml compile models.lml --schema yaml -o widget.schema.yaml
161
- DESC
162
- method_option :namespace, type: :string, aliases: '-n',
163
- desc: 'Register compiled classes in a module (created if absent)'
164
- method_option :schema, type: :string, enum: %w[json yaml],
165
- desc: 'Emit JSON Schema or YAML Schema for the compiled models'
166
- method_option :output, type: :string, aliases: '-o',
167
- desc: 'Schema output path (file for one model, directory for many)'
168
- def compile(path)
169
- raise Thor::Error, "File does not exist: #{path}" unless File.exist?(path)
170
- raise Thor::Error, "Expected a file, got directory: #{path}" if File.directory?(path)
171
-
172
- ns = options[:namespace] || (::Lutaml::Lml::ModelCompiler::DEFAULT_NAMESPACE if options[:schema])
173
- result = File.open(path) { |file| Lutaml::Lml::ModelCompiler.new(namespace: ns).compile(file) }
174
- return emit_schemas(result) if options[:schema]
175
-
176
- report_compilation(result)
177
- end
178
-
179
- no_commands do
180
- def report_compilation(result)
181
- result.each_key do |name|
182
- say " compiled: #{name}", :green
183
- end
184
- say "\n#{result.size} class(es) compiled", :green
185
- end
186
-
187
- def emit_schemas(result)
188
- require 'lutaml/model/schema'
189
- if options[:output] && Pathname.new(options[:output]).directory?
190
- generator = "to_#{options[:schema]}"
191
- schemas = result.transform_values do |klass|
192
- Lutaml::Model::Schema.public_send(generator, klass, pretty: true)
193
- end
194
- write_schemas(schemas)
195
- else
196
- # One multi-root document covers every compiled model; each
197
- # root stays addressable under $defs by class name.
198
- schema = generate_multi_root_schema(result)
199
- return puts schema unless options[:output]
200
-
201
- File.write(options[:output], schema)
202
- say "Generated: #{options[:output]}", :green
203
- end
204
- end
205
-
206
- def generate_multi_root_schema(result)
207
- klass = options[:schema] == 'yaml' ? Lutaml::Model::Schema::YamlSchema : Lutaml::Model::Schema::JsonSchema
208
- klass.generate_many(result.values, pretty: true)
209
- end
210
-
211
- def write_schemas(schemas)
212
- target = Pathname.new(options[:output])
213
- if schemas.size > 1
214
- unless target.directory?
215
- raise Thor::Error,
216
- 'Output path must be a directory if multiple schemas ' \
217
- 'are generated'
218
- end
219
- write_schema_files(target, schemas)
220
- else
221
- target.write(schemas.values.first)
222
- say "Generated: #{target}", :green
223
- end
224
- end
225
-
226
- def write_schema_files(dir, schemas)
227
- schemas.each do |name, schema|
228
- file = dir.join("#{schema_file_stem(name)}.#{options[:schema]}")
229
- file.write(schema)
230
- say "Generated: #{file}", :green
231
- end
232
- end
233
-
234
- def schema_file_stem(name)
235
- name.split('::').last.gsub(/[^a-zA-Z0-9]+/, '_').downcase
236
- end
237
-
238
- def file_canonical?(path)
239
- input = Pathname.new(path)
240
- raise Thor::Error, "File does not exist: #{path}" unless input.exist?
241
-
242
- source = input.read
243
- canonical = canonical_source(source)
244
- return true if source == canonical
245
-
246
- if options[:check]
247
- say diff_source(source, canonical, path)
248
- say "#{path} is not canonical", :red
249
- false
250
- else
251
- input.write(canonical)
252
- say "Formatted: #{path}", :green
253
- true
254
- end
255
- end
256
-
257
- def canonical_source(source)
258
- document = Lutaml::Lml.parse_document(source)
259
- ::Lutaml::Formatter::Canonical.new(document).render
260
- end
261
-
262
- def diff_source(source, canonical, path)
263
- old_lines = source.lines.map(&:chomp)
264
- new_lines = canonical.lines.map(&:chomp)
265
- out = ["--- #{path}", "+++ #{path} (canonical)"]
266
- out.concat(diff_lines(old_lines, new_lines))
267
- out.join("\n")
268
- end
269
-
270
- def diff_lines(old_lines, new_lines)
271
- return ['(diff omitted: file too large)'] if old_lines.length * new_lines.length > 4_000_000
272
-
273
- table = Array.new(old_lines.length + 1) do
274
- Array.new(new_lines.length + 1, 0)
275
- end
276
- (old_lines.length - 1).downto(0) do |i|
277
- (new_lines.length - 1).downto(0) do |j|
278
- table[i][j] = if old_lines[i] == new_lines[j]
279
- table[i + 1][j + 1] + 1
280
- else
281
- [table[i + 1][j], table[i][j + 1]].max
282
- end
283
- end
284
- end
285
- walk_diff(old_lines, new_lines, table)
286
- end
287
-
288
- def walk_diff(old_lines, new_lines, table)
289
- result = []
290
- i = 0
291
- j = 0
292
- while i < old_lines.length && j < new_lines.length
293
- if old_lines[i] == new_lines[j]
294
- result << " #{old_lines[i]}"
295
- i += 1
296
- j += 1
297
- elsif table[i + 1][j] >= table[i][j + 1]
298
- result << "- #{old_lines[i]}"
299
- i += 1
300
- else
301
- result << "+ #{new_lines[j]}"
302
- j += 1
303
- end
304
- end
305
- while i < old_lines.length
306
- result << "- #{old_lines[i]}"
307
- i += 1
308
- end
309
- while j < new_lines.length
310
- result << "+ #{new_lines[j]}"
311
- j += 1
312
- end
313
- result
314
- end
315
-
316
- def assert_input_paths(paths)
317
- return unless paths.empty?
318
-
319
- raise Thor::Error,
320
- 'No input files provided. Please specify at least ' \
321
- 'one .lutaml file.'
322
- end
323
-
324
- def assert_output_path
325
- return unless @output_path&.file? && @paths.length > 1
326
-
327
- raise Thor::Error,
328
- 'Output path must be a directory if multiple input files ' \
329
- 'are given'
330
- end
331
-
332
- def process_single_file(input_path)
333
- raise Thor::Error, "File does not exist: #{input_path}" unless input_path.exist?
334
-
335
- document = parse_document(input_path)
336
- result = @formatter.format(document)
337
- write_output(input_path, result)
338
- end
339
-
340
- def resolve_output_path(input_path)
341
- return @out_object unless @output_path
342
-
343
- path = @output_path
344
- path = path.join("#{input_path.basename('.*')}.#{@formatter.type}") if path.directory?
345
- path
346
- end
347
-
348
- def write_output(input_path, result)
349
- target = resolve_output_path(input_path)
350
- if target.is_a?(Pathname)
351
- target.open('wb+') { |f| f.write(result) }
352
- say "Generated: #{target}", :green
353
- else
354
- target.puts(result)
355
- end
356
- end
357
-
358
- def validate_single_file(path_string)
359
- input_path = Pathname.new(path_string)
360
- return [missing_file_diagnostic(input_path)] unless input_path.exist?
361
-
362
- document = parse_document(input_path)
363
- diagnostics = Lutaml::Lml::Validator.diagnostics(document)
364
- if diagnostics.any?
365
- if text_output?
366
- diagnostics.each do |diagnostic|
367
- say diagnostic_line(diagnostic),
368
- (diagnostic.severity == 'warning' ? :yellow : :red)
369
- end
370
- end
371
- elsif text_output?
372
- say "✓ #{input_path}", :green
373
- end
374
- diagnostics
375
- rescue Thor::Error => e
376
- [contract_diagnostic('contract/LML-C02', e.message, input_path)]
377
- rescue Lutaml::Lml::Error => e
378
- [error_diagnostic(e, input_path)]
379
- rescue StandardError => e
380
- [contract_diagnostic('contract/LML-C03', e.message, input_path)]
381
- end
382
-
383
- def missing_file_diagnostic(input_path)
384
- contract_diagnostic('contract/LML-C01', 'File does not exist', input_path)
385
- end
386
-
387
- def contract_diagnostic(code, message, input_path)
388
- diagnostic = Diagnostic.build(code, message: message, file: input_path.to_s)
389
- say "✗ #{diagnostic}", :red if text_output?
390
- diagnostic
391
- end
392
-
393
- def error_diagnostic(error, input_path)
394
- diagnostic = error.diagnostic
395
- return diagnostic if diagnostic&.file
396
-
397
- Diagnostic.build(
398
- diagnostic&.code || 'parse/LML-001',
399
- message: diagnostic&.message || error.message.lines.first.to_s.strip,
400
- file: input_path.to_s,
401
- line: diagnostic&.line,
402
- column: diagnostic&.column,
403
- length: diagnostic&.length,
404
- )
405
- end
406
-
407
- def text_output?
408
- options[:format] != 'json'
409
- end
410
-
411
- def diagnostic_line(diagnostic)
412
- marker = diagnostic.severity == 'warning' ? '⚠' : '✗'
413
- "#{marker} #{diagnostic}"
414
- end
415
-
416
- def report_validation(diagnostics)
417
- errors = diagnostics.count { |diagnostic| diagnostic.severity != 'warning' }
418
- if options[:format] == 'json'
419
- puts JSON.pretty_generate(diagnostics.map(&:to_hash))
420
- exit 1 if errors.positive?
421
- return
422
- end
423
-
424
- if errors.positive?
425
- say "\nValidation failed with #{errors} error(s)", :red
426
- exit 1
427
- elsif diagnostics.any?
428
- say "\nAll files valid (#{diagnostics.size} warning(s))", :yellow
429
- else
430
- say "\nAll files valid!", :green
431
- end
432
- end
433
-
434
- def parse_document(input_path)
435
- handler = PARSE_HANDLERS[@input_format]
436
- return handler.call(input_path) if handler
437
-
438
- raise Thor::Error, "Unsupported input format: #{@input_format}"
439
- end
440
-
441
- def setup_options
442
- @formatter = options[:formatter] if options[:formatter]
443
- @output_path = Pathname.new(options[:output]) if options[:output]
444
- @input_format = options[:input_format] || DEFAULT_INPUT_FORMAT
445
- @type = resolve_output_type
446
-
447
- setup_formatter_options
448
- end
449
-
450
- def resolve_output_type
451
- return options[:type] if options[:type]
452
- return DEFAULT_OUTPUT_FORMAT unless @output_path
453
-
454
- ext = @output_path.extname.downcase
455
- EXTENSION_FORMAT_MAP.fetch(ext, DEFAULT_OUTPUT_FORMAT)
456
- end
457
-
458
- def parse_kv_string(str)
459
- str.split(',').to_h do |pair|
460
- key, value = pair.split('=', 2)
461
- [key.strip, value&.strip]
462
- end
463
- end
464
-
465
- def apply_kv_to_formatter(option_key, str)
466
- targets = option_key == :all ? FORMATTER_ATTR_TARGETS : [option_key]
467
- parse_kv_string(str).each do |key, value|
468
- targets.each { |t| @formatter.public_send(t)[key] = value }
469
- end
470
- end
471
-
472
- def setup_formatter_options
473
- return unless @formatter
474
-
475
- @formatter.type = @type if @type
476
-
477
- FORMATTER_ATTR_TARGETS.each do |target|
478
- apply_kv_to_formatter(target, options[target]) if options[target]
479
- end
480
- apply_kv_to_formatter(:all, options[:all]) if options[:all]
481
- end
482
- end
483
-
484
- def self.exit_on_failure?
485
- true
486
- end
487
- end
488
- end
489
- end
@@ -1,133 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Lutaml
4
- module Lml
5
- class DataProcessor
6
- # Normalizes attribute entries from the parse tree: merges
7
- # comma-form groups, extracts names/types/values, and applies the
8
- # nested attribute/property structure.
9
- module AttributeProcessing
10
- EXCLUDED_PASS_THROUGH_KEYS = %i[key value comments add attributes properties type_literal].freeze
11
-
12
- def process_attributes(obj)
13
- case obj
14
- when Array then process_attributes_array(obj)
15
- when Hash then process_attributes_hash(obj)
16
- end
17
- end
18
-
19
- def process_attributes_array(obj)
20
- return [] if obj.empty?
21
- return obj.map { |item| process_attributes(item) } unless named_attribute_entries?(obj)
22
- return merge_named_entries(obj) unless single_key_hashes?(obj)
23
-
24
- obj.each_with_object({}) { |item, hash| append_uniform_entry(hash, item) }
25
- end
26
-
27
- def append_uniform_entry(hash, item)
28
- hash[:properties] ||= []
29
- if item.key?(:properties)
30
- hash[:properties] << process_attributes(item[:properties])
31
- else
32
- hash.merge!(process_attributes(item))
33
- end
34
- end
35
-
36
- # Attribute-definition groups split across capture depths (e.g. the
37
- # comma form: {name, type} beside a nested {value_set}) combine into
38
- # one attribute entry. Key/value pair lists (import/export maps)
39
- # carry :key and are never merged.
40
- def named_attribute_entries?(obj)
41
- obj.any? { |e| e.is_a?(Hash) && e.key?(:name) } &&
42
- obj.all? { |e| e.is_a?(Hash) && !e.key?(:key) }
43
- end
44
-
45
- COLLECTION_KEYS = %i[properties attributes instances].freeze
46
-
47
- def merge_named_entries(obj)
48
- obj.each_with_object({}) do |item, hash|
49
- process_attributes(item).each do |key, value|
50
- hash[key] = merge_attribute_value(hash[key], key, value)
51
- end
52
- end
53
- end
54
-
55
- def merge_attribute_value(existing, key, value)
56
- return merge_collection_value(existing, value) if COLLECTION_KEYS.include?(key)
57
- return existing + value if existing.is_a?(Array) && value.is_a?(Array)
58
-
59
- value
60
- end
61
-
62
- def merge_collection_value(existing, value)
63
- current = if existing.is_a?(Array)
64
- existing
65
- else
66
- (existing ? [existing] : [])
67
- end
68
- current + (value.is_a?(Array) ? value : [value])
69
- end
70
-
71
- def process_attributes_hash(obj)
72
- result = extract_name_and_value(obj)
73
- convert_instance_type(result)
74
- result[:extended] = !obj[:add].nil? if obj.key?(:add)
75
- result[:literal] = true if obj.key?(:type_literal)
76
- apply_nested_attributes(result, obj)
77
- apply_remaining_keys(result, obj)
78
- translate_values_argument(result)
79
- result
80
- end
81
-
82
- def single_key_hashes?(arr)
83
- arr.all? { |e| e.is_a?(Hash) && e.keys.size == 1 }
84
- end
85
-
86
- def extract_name_and_value(obj)
87
- result = {}
88
- result[:name] = strip_argument_colon(obj[:key]) if obj.key?(:key)
89
-
90
- if obj.key?(:comments)
91
- result[:name] = 'Comment'
92
- result[:value] = process_value(obj[:comments]).last
93
- elsif obj.key?(:value)
94
- result[:type], result[:value] = process_value(obj[:value])
95
- end
96
-
97
- result
98
- end
99
-
100
- def convert_instance_type(result)
101
- return unless result[:type]&.start_with?('Instance')
102
-
103
- value = result.delete(:value)
104
- result[:instances] = value.is_a?(Array) ? value : [value]
105
- result.delete(:type)
106
- end
107
-
108
- def apply_nested_attributes(result, obj)
109
- result[:attributes] = process_attributes(obj[:attributes]) if obj.key?(:attributes)
110
- result[:properties] = process_attributes(obj[:properties]) if obj.key?(:properties)
111
- end
112
-
113
- def apply_remaining_keys(result, obj)
114
- obj.each do |key, value|
115
- next if EXCLUDED_PASS_THROUGH_KEYS.include?(key)
116
-
117
- value = strip_argument_colon(value) if key == :name
118
- result[key] = value unless result.key?(key)
119
- end
120
- end
121
-
122
- # `default: "Clear"` uses the colon separator (RS 3001 §Attribute);
123
- # the colon lands in the captured name and must not. A quoted key
124
- # is verbatim, so a trailing colon there is content.
125
- def strip_argument_colon(name)
126
- return name[:string] if name.is_a?(Hash) && name.key?(:string)
127
-
128
- name.to_s.sub(/:\z/, "")
129
- end
130
- end
131
- end
132
- end
133
- end
@@ -1,37 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Lutaml
4
- module Lml
5
- class DataProcessor
6
- module CollectionProcessing
7
- def process_collections(obj)
8
- obj.each_with_object({}) do |(key, value), result|
9
- result[key] = process_value(value).last
10
- end
11
- end
12
-
13
- def process_imports(obj)
14
- process_io_definitions(obj)
15
- end
16
-
17
- def process_exports(obj)
18
- process_io_definitions(obj)
19
- end
20
-
21
- private
22
-
23
- def process_io_definitions(obj)
24
- obj.map do |item|
25
- item.each_with_object({}) do |(key, value), result|
26
- result[key] = if key == :attributes
27
- process_attributes(value)
28
- else
29
- process_value(value).last
30
- end
31
- end
32
- end
33
- end
34
- end
35
- end
36
- end
37
- end