lutaml-lml 0.3.6 → 0.3.8
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.
- checksums.yaml +4 -4
- data/lib/lutaml-lml.rb +5 -0
- data/lib/lutaml_lml/version.rb +6 -0
- metadata +14 -166
- data/exe/lutaml-lml +0 -6
- data/exe/lutaml-lml-lsp +0 -11
- data/lib/lutaml/lml/association_label_resolver.rb +0 -52
- data/lib/lutaml/lml/cli.rb +0 -489
- data/lib/lutaml/lml/data_processor/attribute_processing.rb +0 -133
- data/lib/lutaml/lml/data_processor/collection_processing.rb +0 -37
- data/lib/lutaml/lml/data_processor/instance_processing.rb +0 -77
- data/lib/lutaml/lml/data_processor/value_declaration_processing.rb +0 -70
- data/lib/lutaml/lml/data_processor/value_processing.rb +0 -103
- data/lib/lutaml/lml/data_processor/view_processing.rb +0 -26
- data/lib/lutaml/lml/data_processor.rb +0 -59
- data/lib/lutaml/lml/diagnostic.rb +0 -149
- data/lib/lutaml/lml/document_builder.rb +0 -481
- data/lib/lutaml/lml/entity_types.rb +0 -30
- data/lib/lutaml/lml/executor/adapter_helpers.rb +0 -45
- data/lib/lutaml/lml/executor/condition_evaluator.rb +0 -169
- data/lib/lutaml/lml/executor/csv_adapter.rb +0 -88
- data/lib/lutaml/lml/executor/format_adapter.rb +0 -54
- data/lib/lutaml/lml/executor/xml_adapter.rb +0 -102
- data/lib/lutaml/lml/executor.rb +0 -96
- data/lib/lutaml/lml/format/adapter/document.rb +0 -11
- data/lib/lutaml/lml/format/adapter/mapping.rb +0 -31
- data/lib/lutaml/lml/format/adapter/standard_adapter.rb +0 -162
- data/lib/lutaml/lml/format/adapter/transform.rb +0 -11
- data/lib/lutaml/lml/format.rb +0 -29
- data/lib/lutaml/lml/formatter/base.rb +0 -79
- data/lib/lutaml/lml/formatter/canonical.rb +0 -736
- data/lib/lutaml/lml/formatter/elk.rb +0 -45
- data/lib/lutaml/lml/formatter/graphviz/document_formatter.rb +0 -89
- data/lib/lutaml/lml/formatter/graphviz/html_builder.rb +0 -72
- data/lib/lutaml/lml/formatter/graphviz/node_formatter.rb +0 -89
- data/lib/lutaml/lml/formatter/graphviz/relationship_formatter.rb +0 -130
- data/lib/lutaml/lml/formatter/graphviz.rb +0 -90
- data/lib/lutaml/lml/formatter/svg_pdf/path_parser.rb +0 -134
- data/lib/lutaml/lml/formatter/svg_pdf.rb +0 -285
- data/lib/lutaml/lml/formatter.rb +0 -12
- data/lib/lutaml/lml/grammar/lml.artifact.json +0 -14993
- data/lib/lutaml/lml/grammar/lml.parg +0 -389
- data/lib/lutaml/lml/grammar.rb +0 -60
- data/lib/lutaml/lml/has_attributes.rb +0 -14
- data/lib/lutaml/lml/import_resolver.rb +0 -96
- data/lib/lutaml/lml/layout/elk_engine.rb +0 -15
- data/lib/lutaml/lml/layout/elk_graph_builder.rb +0 -129
- data/lib/lutaml/lml/layout/elk_svg_markers.rb +0 -45
- data/lib/lutaml/lml/layout/elk_svg_renderer.rb +0 -119
- data/lib/lutaml/lml/layout/engine.rb +0 -17
- data/lib/lutaml/lml/layout/graph_viz_engine.rb +0 -19
- data/lib/lutaml/lml/layout.rb +0 -12
- data/lib/lutaml/lml/literal_value.rb +0 -32
- data/lib/lutaml/lml/lsp/conversions.rb +0 -78
- data/lib/lutaml/lml/lsp/keywords.rb +0 -43
- data/lib/lutaml/lml/lsp/server.rb +0 -378
- data/lib/lutaml/lml/lsp/store.rb +0 -68
- data/lib/lutaml/lml/lsp/transport.rb +0 -53
- data/lib/lutaml/lml/lsp.rb +0 -16
- data/lib/lutaml/lml/model_compiler/enums.rb +0 -172
- data/lib/lutaml/lml/model_compiler/hydration.rb +0 -123
- data/lib/lutaml/lml/model_compiler/serialization_mappings.rb +0 -188
- data/lib/lutaml/lml/model_compiler/string_formats.rb +0 -42
- data/lib/lutaml/lml/model_compiler/validation.rb +0 -65
- data/lib/lutaml/lml/model_compiler.rb +0 -323
- data/lib/lutaml/lml/models/action.rb +0 -10
- data/lib/lutaml/lml/models/association.rb +0 -29
- data/lib/lutaml/lml/models/cardinality.rb +0 -10
- data/lib/lutaml/lml/models/collection.rb +0 -11
- data/lib/lutaml/lml/models/constraint.rb +0 -23
- data/lib/lutaml/lml/models/data_type.rb +0 -41
- data/lib/lutaml/lml/models/derived_field.rb +0 -14
- data/lib/lutaml/lml/models/diagram.rb +0 -20
- data/lib/lutaml/lml/models/document.rb +0 -84
- data/lib/lutaml/lml/models/enum.rb +0 -38
- data/lib/lutaml/lml/models/fidelity.rb +0 -10
- data/lib/lutaml/lml/models/group.rb +0 -11
- data/lib/lutaml/lml/models/instance.rb +0 -26
- data/lib/lutaml/lml/models/instance_collection.rb +0 -16
- data/lib/lutaml/lml/models/instances_export.rb +0 -10
- data/lib/lutaml/lml/models/instances_import.rb +0 -11
- data/lib/lutaml/lml/models/mapping.rb +0 -15
- data/lib/lutaml/lml/models/mapping_rule.rb +0 -16
- data/lib/lutaml/lml/models/member_field.rb +0 -14
- data/lib/lutaml/lml/models/namespace.rb +0 -16
- data/lib/lutaml/lml/models/operation.rb +0 -26
- data/lib/lutaml/lml/models/operation_parameter.rb +0 -12
- data/lib/lutaml/lml/models/package.rb +0 -26
- data/lib/lutaml/lml/models/primitive_type.rb +0 -33
- data/lib/lutaml/lml/models/reference.rb +0 -23
- data/lib/lutaml/lml/models/serialization_mapping.rb +0 -19
- data/lib/lutaml/lml/models/string_format.rb +0 -16
- data/lib/lutaml/lml/models/top_element_attribute.rb +0 -116
- data/lib/lutaml/lml/models/uml_class.rb +0 -85
- data/lib/lutaml/lml/models/value.rb +0 -15
- data/lib/lutaml/lml/models/view_filter.rb +0 -11
- data/lib/lutaml/lml/models/view_import.rb +0 -11
- data/lib/lutaml/lml/parser.rb +0 -19
- data/lib/lutaml/lml/pipeline.rb +0 -73
- data/lib/lutaml/lml/positioned.rb +0 -13
- data/lib/lutaml/lml/preprocessor.rb +0 -130
- data/lib/lutaml/lml/quoting.rb +0 -60
- data/lib/lutaml/lml/source.rb +0 -49
- data/lib/lutaml/lml/source_position.rb +0 -20
- data/lib/lutaml/lml/symbol_index.rb +0 -200
- data/lib/lutaml/lml/transform.rb +0 -119
- data/lib/lutaml/lml/types/text_type.rb +0 -28
- data/lib/lutaml/lml/types.rb +0 -10
- data/lib/lutaml/lml/validator.rb +0 -458
- data/lib/lutaml/lml/version.rb +0 -7
- data/lib/lutaml/lml/view_resolution.rb +0 -32
- data/lib/lutaml/lml/view_resolver.rb +0 -48
- data/lib/lutaml/lml/yaml_parser.rb +0 -17
- data/lib/lutaml/lml.rb +0 -130
|
@@ -1,123 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
module Lutaml
|
|
4
|
-
module Lml
|
|
5
|
-
class ModelCompiler
|
|
6
|
-
# Builds typed instances from parsed instance documents
|
|
7
|
-
# against the compiled class set.
|
|
8
|
-
module Hydration
|
|
9
|
-
private
|
|
10
|
-
|
|
11
|
-
def hydrate_instance(instance)
|
|
12
|
-
inner = instance.instance
|
|
13
|
-
return hydrate_instance(inner) if inner && Array(instance.attributes).empty?
|
|
14
|
-
|
|
15
|
-
type_name = resolve_instance_type(instance)
|
|
16
|
-
klass = @compiled[type_name]
|
|
17
|
-
return hydrate_as_untyped(instance, type_name) unless klass
|
|
18
|
-
|
|
19
|
-
attrs = extract_instance_attributes(instance, klass)
|
|
20
|
-
klass.new(**attrs)
|
|
21
|
-
end
|
|
22
|
-
|
|
23
|
-
# An inline map's cargo is {key_value_map: [{key:, value:}, ...]} with
|
|
24
|
-
# literal-shaped keys/values ({string: ..}, {number: ..}, {boolean: ..});
|
|
25
|
-
# materialize it as a plain Hash (issue #17: maps hydrate as data).
|
|
26
|
-
|
|
27
|
-
# An inline map's cargo is {key_value_map: [{key:, value:}, ...]} with
|
|
28
|
-
# literal-shaped keys/values ({string: ..}, {number: ..}, {boolean: ..});
|
|
29
|
-
# materialize it as a plain Hash (issue #17: maps hydrate as data).
|
|
30
|
-
def hydrate_map_cargo(cargo)
|
|
31
|
-
cargo[:key_value_map].to_h do |pair|
|
|
32
|
-
[hydrate_scalar(pair[:key]).to_s, hydrate_scalar(pair[:value])]
|
|
33
|
-
end
|
|
34
|
-
end
|
|
35
|
-
|
|
36
|
-
def hydrate_scalar(literal)
|
|
37
|
-
literal = literal.value if literal.respond_to?(:value)
|
|
38
|
-
literal.is_a?(Hash) ? literal.values.first : literal
|
|
39
|
-
end
|
|
40
|
-
|
|
41
|
-
def resolve_instance_type(instance)
|
|
42
|
-
# L6: the explicit isa keyword is the type override; a `type`
|
|
43
|
-
# attribute is ordinary data (pubid's identifier-kind field)
|
|
44
|
-
return demodulize(instance.isa.to_s) if instance.isa
|
|
45
|
-
|
|
46
|
-
demodulize(instance.type.to_s)
|
|
47
|
-
end
|
|
48
|
-
|
|
49
|
-
def demodulize(name)
|
|
50
|
-
name.split('::').last.to_s
|
|
51
|
-
end
|
|
52
|
-
|
|
53
|
-
def hydrate_as_untyped(instance, type_name)
|
|
54
|
-
{ _name: type_name, **extract_raw_attributes(instance) }
|
|
55
|
-
end
|
|
56
|
-
|
|
57
|
-
def extract_raw_attributes(instance)
|
|
58
|
-
instance.each_attribute.with_object({}) do |(name, value, nested), hash|
|
|
59
|
-
hash[name.to_sym] = resolve_instance_value(value, nested)
|
|
60
|
-
end
|
|
61
|
-
end
|
|
62
|
-
|
|
63
|
-
def extract_instance_attributes(instance, klass)
|
|
64
|
-
schema_keys = klass.attributes.keys.to_set
|
|
65
|
-
instance.each_attribute.with_object({}) do |(name, value, nested), hash|
|
|
66
|
-
key = name.to_sym
|
|
67
|
-
next unless schema_keys.include?(key)
|
|
68
|
-
|
|
69
|
-
hash[key] = resolve_instance_value(value, nested, klass.attributes[key])
|
|
70
|
-
end
|
|
71
|
-
end
|
|
72
|
-
|
|
73
|
-
def resolve_instance_value(value, nested, attr_def = nil)
|
|
74
|
-
value = value.value if value.respond_to?(:value)
|
|
75
|
-
return hydrate_map_cargo(value) if value.is_a?(Hash) && value.key?(:key_value_map)
|
|
76
|
-
|
|
77
|
-
if nested.any?
|
|
78
|
-
# A scalar attribute with a single nested instance hydrates to
|
|
79
|
-
# the object itself; the declaration is the truth (issue #17).
|
|
80
|
-
return hydrate_typed(nested.first, attr_def) if !attr_def&.collection && nested.one?
|
|
81
|
-
|
|
82
|
-
nested.map { |i| hydrate_typed(i, attr_def) }
|
|
83
|
-
# The Array guard must run before the enum branch: an array
|
|
84
|
-
# value under an enum-typed attribute stays a raw array.
|
|
85
|
-
# rubocop:disable Lint/DuplicateBranch
|
|
86
|
-
elsif value.is_a?(Array)
|
|
87
|
-
value
|
|
88
|
-
elsif (enum_class = attr_def && enum_class_for(attr_def.type))
|
|
89
|
-
value_name = enum_value_name(value.to_s)
|
|
90
|
-
description = enum_class.values
|
|
91
|
-
.find { |v| v.name == value_name }&.description
|
|
92
|
-
enum_class.new(value: value_name, description: description)
|
|
93
|
-
elsif !value.nil?
|
|
94
|
-
value
|
|
95
|
-
# rubocop:enable Lint/DuplicateBranch
|
|
96
|
-
end
|
|
97
|
-
end
|
|
98
|
-
|
|
99
|
-
# An untyped nested instance takes the enclosing attribute's type;
|
|
100
|
-
# hydrate_as_untyped would otherwise emit a raw hash polluted with
|
|
101
|
-
# a `_name` key, which also defeats union member key-coverage.
|
|
102
|
-
|
|
103
|
-
# An untyped nested instance takes the enclosing attribute's type;
|
|
104
|
-
# hydrate_as_untyped would otherwise emit a raw hash polluted with
|
|
105
|
-
# a `_name` key, which also defeats union member key-coverage.
|
|
106
|
-
def hydrate_typed(instance, attr_def)
|
|
107
|
-
return hydrate_instance(instance) if instance.isa || !instance.type.to_s.empty?
|
|
108
|
-
|
|
109
|
-
type = attr_def&.type
|
|
110
|
-
return hydrate_instance(instance) unless type.is_a?(Class)
|
|
111
|
-
|
|
112
|
-
if type.include?(Lutaml::Model::Serialize)
|
|
113
|
-
type.new(**extract_instance_attributes(instance, type))
|
|
114
|
-
elsif type == Lutaml::Model::Type::Union
|
|
115
|
-
extract_raw_attributes(instance)
|
|
116
|
-
else
|
|
117
|
-
hydrate_instance(instance)
|
|
118
|
-
end
|
|
119
|
-
end
|
|
120
|
-
end
|
|
121
|
-
end
|
|
122
|
-
end
|
|
123
|
-
end
|
|
@@ -1,188 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
module Lutaml
|
|
4
|
-
module Lml
|
|
5
|
-
class ModelCompiler
|
|
6
|
-
# Applies declared LML serialization mappings
|
|
7
|
-
# (xml / key_value wire names, namespaces) onto compiled classes.
|
|
8
|
-
module SerializationMappings
|
|
9
|
-
private
|
|
10
|
-
|
|
11
|
-
# In-class `mapping <format> { … }` is the shortcut for a sibling
|
|
12
|
-
# mapping targeting the enclosing class; hoist them so emission is
|
|
13
|
-
# uniform.
|
|
14
|
-
def hoist_class_serialization_mappings(doc)
|
|
15
|
-
(Array(doc.classes) + Array(doc.data_types)).each do |klass_def|
|
|
16
|
-
Array(klass_def.serialization_mappings).each do |mapping|
|
|
17
|
-
mapping.target = klass_def.name.to_s
|
|
18
|
-
@serialization_mappings << mapping
|
|
19
|
-
end
|
|
20
|
-
end
|
|
21
|
-
end
|
|
22
|
-
|
|
23
|
-
# Emit the lutaml-model mapping blocks declared in the LML source:
|
|
24
|
-
# mapping yaml { map "full-name", to: "name" }
|
|
25
|
-
# becomes klass.yml do |m| m.map "full-name", to: :name end
|
|
26
|
-
def apply_declared_mappings(compiled_klass, def_obj)
|
|
27
|
-
return unless def_obj.is_a?(UmlClass) || def_obj.is_a?(DataType)
|
|
28
|
-
|
|
29
|
-
def_obj.wire_mappings.to_a.group_by(&:format).each do |format, entries|
|
|
30
|
-
next unless format && Lutaml::Model::FormatRegistry.registered?(format.to_sym)
|
|
31
|
-
|
|
32
|
-
compiled_klass.public_send(format) do |mapping|
|
|
33
|
-
entries.each { |entry| mapping.map(entry.wire, to: entry.to.to_sym) }
|
|
34
|
-
end
|
|
35
|
-
end
|
|
36
|
-
end
|
|
37
|
-
|
|
38
|
-
# Apply a lutaml-model XML mapping to each compiled class so they
|
|
39
|
-
# support from_xml/to_xml. Runs after forward references are resolved
|
|
40
|
-
# so all attributes (immediate + deferred) get element mappings.
|
|
41
|
-
def apply_xml_mappings
|
|
42
|
-
@compiled.each do |name, klass|
|
|
43
|
-
next if @enum_names.include?(name)
|
|
44
|
-
|
|
45
|
-
custom = serialization_mapping_for(name, 'xml')
|
|
46
|
-
next apply_custom_xml_mapping(klass, custom) if custom
|
|
47
|
-
|
|
48
|
-
apply_xml_mapping(klass, name)
|
|
49
|
-
end
|
|
50
|
-
end
|
|
51
|
-
|
|
52
|
-
# Default XML mapping: every attribute an element, except those
|
|
53
|
-
# declared `as :attribute`; an in-class `namespace "uri"` becomes
|
|
54
|
-
# the class's XML namespace (issue #49).
|
|
55
|
-
def apply_xml_mapping(klass, root_name)
|
|
56
|
-
attr_names = klass.attributes.keys
|
|
57
|
-
def_obj = @class_defs[root_name]
|
|
58
|
-
roles = xml_roles_of(def_obj)
|
|
59
|
-
ns_class = inline_namespace_class(def_obj)
|
|
60
|
-
klass.xml do |mapping|
|
|
61
|
-
mapping.element(root_name)
|
|
62
|
-
mapping.namespace(ns_class) if ns_class
|
|
63
|
-
attr_names.each do |attr_name|
|
|
64
|
-
if roles[attr_name] == 'attribute'
|
|
65
|
-
mapping.map_attribute(attr_name.to_s, to: attr_name)
|
|
66
|
-
else
|
|
67
|
-
mapping.map_element(attr_name.to_s, to: attr_name)
|
|
68
|
-
end
|
|
69
|
-
end
|
|
70
|
-
end
|
|
71
|
-
end
|
|
72
|
-
|
|
73
|
-
def xml_roles_of(def_obj)
|
|
74
|
-
Array(def_obj&.attributes).each_with_object({}) do |attr, roles|
|
|
75
|
-
roles[attr.name.to_sym] = attr.xml_role if attr.xml_role
|
|
76
|
-
end
|
|
77
|
-
end
|
|
78
|
-
|
|
79
|
-
def inline_namespace_class(def_obj)
|
|
80
|
-
decl = def_obj.xml_namespace if def_obj.is_a?(UmlClass)
|
|
81
|
-
return nil unless decl&.uri
|
|
82
|
-
|
|
83
|
-
build_namespace_class("#{decl.uri}|#{decl.prefix}", decl.uri, decl.prefix.to_s)
|
|
84
|
-
end
|
|
85
|
-
|
|
86
|
-
# RS 3010 extension: key-value mapping blocks for the declared
|
|
87
|
-
# formats (yaml/json/toml share lutaml-model's key_value model).
|
|
88
|
-
|
|
89
|
-
# RS 3010 extension: key-value mapping blocks for the declared
|
|
90
|
-
# formats (yaml/json/toml share lutaml-model's key_value model).
|
|
91
|
-
def apply_key_value_mappings
|
|
92
|
-
@compiled.each do |name, klass|
|
|
93
|
-
next if @enum_names.include?(name)
|
|
94
|
-
|
|
95
|
-
custom = serialization_mapping_for(name, 'key_value')
|
|
96
|
-
apply_custom_key_value_mapping(klass, custom) if custom
|
|
97
|
-
end
|
|
98
|
-
end
|
|
99
|
-
|
|
100
|
-
def serialization_mapping_for(target, format)
|
|
101
|
-
@serialization_mappings.find do |m|
|
|
102
|
-
m.target.to_s == target && serialization_format(m.format) == format
|
|
103
|
-
end
|
|
104
|
-
end
|
|
105
|
-
|
|
106
|
-
def serialization_format(format)
|
|
107
|
-
%w[yaml json toml].include?(format.to_s) ? 'key_value' : format.to_s
|
|
108
|
-
end
|
|
109
|
-
|
|
110
|
-
def apply_custom_xml_mapping(klass, mapping)
|
|
111
|
-
check_namespace_ambiguity(mapping)
|
|
112
|
-
ns_class = namespace_class_for(mapping.namespace_ref) if mapping.namespace_ref
|
|
113
|
-
rule_ns = mapping.rules.select { |r| r.kind == 'namespace' }
|
|
114
|
-
.to_h { |r| [r.field, namespace_class_for(r.field)] }
|
|
115
|
-
klass.xml do |m|
|
|
116
|
-
m.element(mapping.element_name) if mapping.element_name
|
|
117
|
-
m.namespace(ns_class) if ns_class
|
|
118
|
-
m.mixed_content if mapping.mixed_content
|
|
119
|
-
mapping.rules.each do |rule|
|
|
120
|
-
case rule.kind
|
|
121
|
-
when 'element' then m.map_element(rule.wire, to: rule.field.to_sym)
|
|
122
|
-
when 'attribute' then m.map_attribute(rule.wire, to: rule.field.to_sym)
|
|
123
|
-
when 'content' then m.map_content(to: rule.field.to_sym)
|
|
124
|
-
when 'namespace' then m.namespace(rule_ns[rule.field])
|
|
125
|
-
end
|
|
126
|
-
end
|
|
127
|
-
end
|
|
128
|
-
end
|
|
129
|
-
|
|
130
|
-
def apply_custom_key_value_mapping(klass, mapping)
|
|
131
|
-
formats = mapping.format.to_s == 'key_value' ? %w[yaml json toml] : [mapping.format.to_s]
|
|
132
|
-
klass.instance_eval do
|
|
133
|
-
formats.each do |format|
|
|
134
|
-
public_send(format) do |m|
|
|
135
|
-
mapping.rules.each do |rule|
|
|
136
|
-
case rule.kind
|
|
137
|
-
when 'map' then m.map(rule.wire, to: rule.field.to_sym)
|
|
138
|
-
when 'key' then m.map_key(to_instance: rule.field.to_sym)
|
|
139
|
-
when 'value'
|
|
140
|
-
if rule.mode == 'as_attribute'
|
|
141
|
-
m.map_value(as_attribute: rule.field.to_sym)
|
|
142
|
-
else
|
|
143
|
-
m.map_value(to_instance: rule.field.to_sym)
|
|
144
|
-
end
|
|
145
|
-
end
|
|
146
|
-
end
|
|
147
|
-
end
|
|
148
|
-
end
|
|
149
|
-
end
|
|
150
|
-
end
|
|
151
|
-
|
|
152
|
-
def check_namespace_ambiguity(mapping)
|
|
153
|
-
return unless mapping.format.to_s == 'xml'
|
|
154
|
-
return if mapping.namespace_ref || @declared_namespaces.length < 2
|
|
155
|
-
|
|
156
|
-
raise ArgumentError,
|
|
157
|
-
"mapping #{mapping.format} #{mapping.target}: ambiguous default " \
|
|
158
|
-
"namespace — #{@declared_namespaces.length} namespaces in scope " \
|
|
159
|
-
"(#{@declared_namespaces.map(&:name).join(', ')}); declare one with " \
|
|
160
|
-
"'namespace <name>'"
|
|
161
|
-
end
|
|
162
|
-
|
|
163
|
-
def namespace_class_for(ref)
|
|
164
|
-
return nil if ref.nil?
|
|
165
|
-
|
|
166
|
-
require 'lutaml/xml/namespace' unless defined?(Lutaml::Xml::Namespace)
|
|
167
|
-
|
|
168
|
-
ns_class = @namespace_classes[ref]
|
|
169
|
-
return ns_class if ns_class
|
|
170
|
-
|
|
171
|
-
declared = @declared_namespaces.find { |ns| ns.name.to_s == ref }
|
|
172
|
-
raise ArgumentError, "unknown namespace '#{ref}'" unless declared
|
|
173
|
-
|
|
174
|
-
build_namespace_class(ref, declared.uri, declared.prefix.to_s)
|
|
175
|
-
end
|
|
176
|
-
|
|
177
|
-
def build_namespace_class(key, uri, prefix)
|
|
178
|
-
require 'lutaml/xml/namespace' unless defined?(Lutaml::Xml::Namespace)
|
|
179
|
-
|
|
180
|
-
@namespace_classes[key] ||= Class.new(Lutaml::Xml::Namespace).tap do |ns_class|
|
|
181
|
-
ns_class.define_singleton_method(:uri) { uri }
|
|
182
|
-
ns_class.define_singleton_method(:prefix_default) { prefix }
|
|
183
|
-
end
|
|
184
|
-
end
|
|
185
|
-
end
|
|
186
|
-
end
|
|
187
|
-
end
|
|
188
|
-
end
|
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
module Lutaml
|
|
4
|
-
module Lml
|
|
5
|
-
class ModelCompiler
|
|
6
|
-
# Registers declared string_format handlers on
|
|
7
|
-
# compiled classes.
|
|
8
|
-
module StringFormats
|
|
9
|
-
private
|
|
10
|
-
|
|
11
|
-
# L5: register grammar-backed string formats. Class-level
|
|
12
|
-
# declarations win; a models-block default applies to classes
|
|
13
|
-
# without their own. Artifact resolution: the compiler's
|
|
14
|
-
# artifacts map (name => path), else the string as a path.
|
|
15
|
-
def register_string_formats(doc)
|
|
16
|
-
defaults = doc.packages.flat_map(&:default_string_formats)
|
|
17
|
-
doc.classes.each do |class_def|
|
|
18
|
-
declared = Array(class_def.string_formats)
|
|
19
|
-
declared.each { |fmt| register_string_format(class_def.name.to_s, fmt) }
|
|
20
|
-
|
|
21
|
-
declared_formats = declared.map(&:format)
|
|
22
|
-
defaults.reject { |fmt| declared_formats.include?(fmt.format) }
|
|
23
|
-
.each { |fmt| register_string_format(class_def.name.to_s, fmt) }
|
|
24
|
-
end
|
|
25
|
-
end
|
|
26
|
-
|
|
27
|
-
def register_string_format(class_name, fmt)
|
|
28
|
-
artifact_path = artifact_path_for(fmt.artifact.to_s)
|
|
29
|
-
return unless artifact_path
|
|
30
|
-
|
|
31
|
-
compiled = @compiled[class_name]
|
|
32
|
-
return unless compiled
|
|
33
|
-
|
|
34
|
-
Parsanol::PARG::Lutaml.register(compiled,
|
|
35
|
-
format_name: fmt.format.to_sym,
|
|
36
|
-
artifact: artifact_path,
|
|
37
|
-
entry: fmt.root.to_s)
|
|
38
|
-
end
|
|
39
|
-
end
|
|
40
|
-
end
|
|
41
|
-
end
|
|
42
|
-
end
|
|
@@ -1,65 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
module Lutaml
|
|
4
|
-
module Lml
|
|
5
|
-
class ModelCompiler
|
|
6
|
-
# Evaluates hydrated instances against their compiled
|
|
7
|
-
# models (unknown attributes, enum membership, nesting).
|
|
8
|
-
module Validation
|
|
9
|
-
private
|
|
10
|
-
|
|
11
|
-
def validate_instance(instance, errors, path = 'root')
|
|
12
|
-
if instance.instance && Array(instance.attributes).empty?
|
|
13
|
-
validate_instance(instance.instance, errors, "#{path}.instance")
|
|
14
|
-
return
|
|
15
|
-
end
|
|
16
|
-
|
|
17
|
-
attrs = Array(instance.attributes)
|
|
18
|
-
type_attr = attrs.find { |a| a.name == 'type' }
|
|
19
|
-
type_name = type_attr ? type_attr.value.to_s : instance.type
|
|
20
|
-
klass = @compiled[demodulize(type_name)]
|
|
21
|
-
unless klass
|
|
22
|
-
errors << "#{path}: unknown type '#{type_name}'"
|
|
23
|
-
return
|
|
24
|
-
end
|
|
25
|
-
|
|
26
|
-
schema_attrs = klass.attributes.keys.map(&:to_s)
|
|
27
|
-
present_attrs = attrs.map(&:name)
|
|
28
|
-
schema_set = schema_attrs.to_set
|
|
29
|
-
present_set = present_attrs.to_set
|
|
30
|
-
|
|
31
|
-
unknown = present_set - schema_set - Set.new(%w[type])
|
|
32
|
-
unknown.each do |name|
|
|
33
|
-
errors << "#{path}.#{name}: attribute not defined on #{type_name}"
|
|
34
|
-
end
|
|
35
|
-
|
|
36
|
-
required = klass.attributes.values
|
|
37
|
-
.reject { |v| v.collection? || v.options.key?(:default) }
|
|
38
|
-
.to_set { |v| v.name.to_s }
|
|
39
|
-
|
|
40
|
-
missing = required - present_set
|
|
41
|
-
missing.each do |name|
|
|
42
|
-
errors << "#{path}.#{name}: required attribute missing (cardinality min >= 1)"
|
|
43
|
-
end
|
|
44
|
-
|
|
45
|
-
attrs.each do |attr|
|
|
46
|
-
next unless schema_attrs.include?(attr.name)
|
|
47
|
-
|
|
48
|
-
attr_def = klass.attributes[attr.name.to_sym]
|
|
49
|
-
next unless attr_def
|
|
50
|
-
|
|
51
|
-
next unless attr_def.collection? && attr.instances.any?
|
|
52
|
-
|
|
53
|
-
attr.instances.each_with_index do |nested, i|
|
|
54
|
-
validate_instance(nested, errors, "#{path}.#{attr.name}[#{i}]")
|
|
55
|
-
end
|
|
56
|
-
end
|
|
57
|
-
|
|
58
|
-
return unless instance.instance
|
|
59
|
-
|
|
60
|
-
validate_instance(instance.instance, errors, "#{path}.instance")
|
|
61
|
-
end
|
|
62
|
-
end
|
|
63
|
-
end
|
|
64
|
-
end
|
|
65
|
-
end
|