asciichem-model 0.3.3 → 0.3.4

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e583ad055e61c85690c0695745bfd843e1d8b9388d4e52ddba341c76eb11bcf7
4
- data.tar.gz: aa9a72d5bff30957f7240610a05c1e4aa3fbc2e91fcf9f28dc7fa92559e64558
3
+ metadata.gz: a317393d3390a18749059e74632ab0b120603dac7256559c1320981ee492c04b
4
+ data.tar.gz: cefda3468895028b7f6be495eaa689c801c161d5caefaf1a8155cf1ada08f7d5
5
5
  SHA512:
6
- metadata.gz: 1b4a70b3d2b5fd1448613c01d4b40438d1688ce30c3ab63bcdcd9102cfad4f9a67e2300f047039bf92b69a8c99805a2cfe6a95744ec40b93743318eef48251b2
7
- data.tar.gz: 3ce86f29c6c7f2db05e996389c8e306aad65c5586e093a9b6cdd45162a8e23f95fb77c6292430e2a77fea93d3ef394d062be46151af0e1e29a3a9f0608e313af
6
+ metadata.gz: 3faba1bad26e083955dbeee7ad1c7876e81ae01490af7d2bd49f0f9bd9e42414425c74806b44eb453e156038b4df80ca6c1c86445100256c2d4a1231499d854b
7
+ data.tar.gz: d8add49fc3d4185f97602c786e3966a4725ad37161fc901370e291e0291b21b7d9720bd09ffeb9cb8b490a90b6d543a364155f3fbad6d8d8da9908219ccbffae
@@ -113,12 +113,23 @@ module AsciiChemModel
113
113
  items = prop["items"]
114
114
  inner = if items.key?("anyOf")
115
115
  "(#{items["anyOf"].map { |sub| ts_type(sub, scope) }.uniq.join(" | ")})"
116
+ elsif items.key?("if")
117
+ "(#{ladder_refs(items).uniq.join(" | ")})"
116
118
  else
117
119
  ts_type(items, scope)
118
120
  end
119
121
  "#{inner}[]"
120
122
  end
121
123
 
124
+ # if/then/else discriminator ladders: collect the $ref of each
125
+ # then-branch plus the terminal else $ref.
126
+ def ladder_refs(items)
127
+ return [ts_ref(items["$ref"], nil)] if items.key?("$ref")
128
+
129
+ refs = [ts_ref(items["then"]["$ref"], nil)]
130
+ refs.concat(items.key?("else") ? ladder_refs(items["else"]) : [])
131
+ end
132
+
122
133
  def ts_inline_object(prop, scope)
123
134
  required = prop.fetch("required", [])
124
135
  members = prop.fetch("properties", {}).map do |key, sub|
@@ -24,12 +24,23 @@ module AsciiChemModel
24
24
  end
25
25
  end
26
26
 
27
+ # Sibling schemas are loaded exactly once and the SAME object is
28
+ # returned for every $ref resolution. json_schemer retains one
29
+ # compiled subschema per resolved object; returning a fresh
30
+ # YAML.load per lookup made retention grow with every
31
+ # validation x ref hit (gigabytes on corpus-scale runs).
32
+ def loaded_schema(basename)
33
+ @loaded_schemas ||= {}
34
+ @loaded_schemas[basename] ||= YAML.safe_load_file(schema_file(basename))
35
+ end
36
+
27
37
  def schema_names
28
38
  schemers.keys.sort
29
39
  end
30
40
 
31
41
  def schema_file(name)
32
- File.join(SCHEMAS_DIR, "#{name}.yaml")
42
+ base = name.end_with?(".yaml") ? name : "#{name}.yaml"
43
+ File.join(SCHEMAS_DIR, base)
33
44
  end
34
45
 
35
46
  # Validates a wire-form node hash against its node schema
@@ -69,8 +80,8 @@ module AsciiChemModel
69
80
  # Sibling-file $refs ("atom.yaml") resolve against the schemas
70
81
  # directory; external pointers are not supported by design.
71
82
  def resolve_ref(uri)
72
- candidate = File.join(SCHEMAS_DIR, File.basename(uri.to_s))
73
- return YAML.safe_load_file(candidate) if File.exist?(candidate)
83
+ basename = File.basename(uri.to_s)
84
+ return loaded_schema(basename) if File.exist?(schema_file(basename))
74
85
 
75
86
  raise KeyError, "unresolvable $ref #{uri} (only sibling schema files are supported)"
76
87
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module AsciiChemModel
4
- VERSION = "0.3.3"
4
+ VERSION = "0.3.4"
5
5
  end
@@ -1,13 +1,14 @@
1
- %YAML 1.2
2
1
  ---
3
- $id: "https://github.com/asciichem/asciichem-model/v1/formula"
2
+ "$id": https://github.com/asciichem/asciichem-model/v1/formula
4
3
  title: AsciiChem Formula node (document root)
5
4
  description: |
6
5
  The root of a parsed AsciiChem document: an ordered sequence of
7
6
  top-level nodes. This is the canonical wire form every
8
7
  implementation emits under the envelope's `root` key.
9
8
  type: object
10
- required: [type, nodes]
9
+ required:
10
+ - type
11
+ - nodes
11
12
  properties:
12
13
  type:
13
14
  const: formula
@@ -19,21 +20,140 @@ properties:
19
20
  spectra, calculations, z-matrices, mechanisms, (annotated)
20
21
  molecules, groups, atoms, embedded math, and text.
21
22
  items:
22
- anyOf:
23
- - $ref: "atom.yaml"
24
- - $ref: "molecule.yaml"
25
- - $ref: "group.yaml"
26
- - $ref: "bond.yaml"
27
- - $ref: "reaction.yaml"
28
- - $ref: "reaction-cascade.yaml"
29
- - $ref: "electron-configuration.yaml"
30
- - $ref: "embedded-math.yaml"
31
- - $ref: "text.yaml"
32
- - $ref: "name.yaml"
33
- - $ref: "identifier.yaml"
34
- - $ref: "mechanism.yaml"
35
- - $ref: "spectrum.yaml"
36
- - $ref: "crystal.yaml"
37
- - $ref: "zmatrix.yaml"
38
- - $ref: "calculation.yaml"
23
+ if:
24
+ properties:
25
+ type:
26
+ const: molecule
27
+ required:
28
+ - type
29
+ then:
30
+ "$ref": molecule.yaml
31
+ else:
32
+ if:
33
+ properties:
34
+ type:
35
+ const: reaction
36
+ required:
37
+ - type
38
+ then:
39
+ "$ref": reaction.yaml
40
+ else:
41
+ if:
42
+ properties:
43
+ type:
44
+ const: reaction-cascade
45
+ required:
46
+ - type
47
+ then:
48
+ "$ref": reaction-cascade.yaml
49
+ else:
50
+ if:
51
+ properties:
52
+ type:
53
+ const: group
54
+ required:
55
+ - type
56
+ then:
57
+ "$ref": group.yaml
58
+ else:
59
+ if:
60
+ properties:
61
+ type:
62
+ const: atom
63
+ required:
64
+ - type
65
+ then:
66
+ "$ref": atom.yaml
67
+ else:
68
+ if:
69
+ properties:
70
+ type:
71
+ const: bond
72
+ required:
73
+ - type
74
+ then:
75
+ "$ref": bond.yaml
76
+ else:
77
+ if:
78
+ properties:
79
+ type:
80
+ const: electron-configuration
81
+ required:
82
+ - type
83
+ then:
84
+ "$ref": electron-configuration.yaml
85
+ else:
86
+ if:
87
+ properties:
88
+ type:
89
+ const: embedded-math
90
+ required:
91
+ - type
92
+ then:
93
+ "$ref": embedded-math.yaml
94
+ else:
95
+ if:
96
+ properties:
97
+ type:
98
+ const: text
99
+ required:
100
+ - type
101
+ then:
102
+ "$ref": text.yaml
103
+ else:
104
+ if:
105
+ properties:
106
+ type:
107
+ const: name
108
+ required:
109
+ - type
110
+ then:
111
+ "$ref": name.yaml
112
+ else:
113
+ if:
114
+ properties:
115
+ type:
116
+ const: identifier
117
+ required:
118
+ - type
119
+ then:
120
+ "$ref": identifier.yaml
121
+ else:
122
+ if:
123
+ properties:
124
+ type:
125
+ const: mechanism
126
+ required:
127
+ - type
128
+ then:
129
+ "$ref": mechanism.yaml
130
+ else:
131
+ if:
132
+ properties:
133
+ type:
134
+ const: spectrum
135
+ required:
136
+ - type
137
+ then:
138
+ "$ref": spectrum.yaml
139
+ else:
140
+ if:
141
+ properties:
142
+ type:
143
+ const: crystal
144
+ required:
145
+ - type
146
+ then:
147
+ "$ref": crystal.yaml
148
+ else:
149
+ if:
150
+ properties:
151
+ type:
152
+ const: zmatrix
153
+ required:
154
+ - type
155
+ then:
156
+ "$ref": zmatrix.yaml
157
+ else:
158
+ "$ref": calculation.yaml
39
159
  additionalProperties: false
@@ -1,12 +1,13 @@
1
- %YAML 1.2
2
1
  ---
3
- $id: "https://github.com/asciichem/asciichem-model/v1/group"
2
+ "$id": https://github.com/asciichem/asciichem-model/v1/group
4
3
  title: AsciiChem Group node
5
4
  description: |
6
5
  A bracketed sub-formula with an optional multiplier that applies to
7
6
  the whole group (`(OH)_2`).
8
7
  type: object
9
- required: [type, nodes]
8
+ required:
9
+ - type
10
+ - nodes
10
11
  properties:
11
12
  type:
12
13
  const: group
@@ -14,16 +15,43 @@ properties:
14
15
  type: array
15
16
  minItems: 1
16
17
  items:
17
- anyOf:
18
- - $ref: "atom.yaml"
19
- - $ref: "molecule.yaml"
20
- - $ref: "group.yaml"
18
+ if:
19
+ properties:
20
+ type:
21
+ const: molecule
22
+ required:
23
+ - type
24
+ then:
25
+ "$ref": molecule.yaml
26
+ else:
27
+ if:
28
+ properties:
29
+ type:
30
+ const: group
31
+ required:
32
+ - type
33
+ then:
34
+ "$ref": group.yaml
35
+ else:
36
+ if:
37
+ properties:
38
+ type:
39
+ const: bond
40
+ required:
41
+ - type
42
+ then:
43
+ "$ref": bond.yaml
44
+ else:
45
+ "$ref": atom.yaml
21
46
  multiplicity:
22
47
  type: string
23
48
  pattern: "^\\d+$"
24
49
  description: Multiplier applied to the group ("2" in (OH)_2).
25
50
  bracket:
26
51
  default: paren
27
- enum: [paren, square, brace]
52
+ enum:
53
+ - paren
54
+ - square
55
+ - brace
28
56
  description: Bracket kind — paren (), square [], brace {}.
29
57
  additionalProperties: false
@@ -1,12 +1,13 @@
1
- %YAML 1.2
2
1
  ---
3
- $id: "https://github.com/asciichem/asciichem-model/v1/molecule"
2
+ "$id": https://github.com/asciichem/asciichem-model/v1/molecule
4
3
  title: AsciiChem Molecule node
5
4
  description: |
6
5
  An ordered sequence of atoms/groups with optional coefficient and
7
6
  attached substance identifiers (CML-level annotations).
8
7
  type: object
9
- required: [type, nodes]
8
+ required:
9
+ - type
10
+ - nodes
10
11
  properties:
11
12
  type:
12
13
  const: molecule
@@ -14,11 +15,34 @@ properties:
14
15
  type: array
15
16
  description: Ordered child nodes (atoms, bonds, groups, nested molecules).
16
17
  items:
17
- anyOf:
18
- - $ref: "atom.yaml"
19
- - $ref: "bond.yaml"
20
- - $ref: "group.yaml"
21
- - $ref: "molecule.yaml"
18
+ if:
19
+ properties:
20
+ type:
21
+ const: molecule
22
+ required:
23
+ - type
24
+ then:
25
+ "$ref": molecule.yaml
26
+ else:
27
+ if:
28
+ properties:
29
+ type:
30
+ const: group
31
+ required:
32
+ - type
33
+ then:
34
+ "$ref": group.yaml
35
+ else:
36
+ if:
37
+ properties:
38
+ type:
39
+ const: bond
40
+ required:
41
+ - type
42
+ then:
43
+ "$ref": bond.yaml
44
+ else:
45
+ "$ref": atom.yaml
22
46
  minItems: 1
23
47
  coefficient:
24
48
  type: string
@@ -28,5 +52,5 @@ properties:
28
52
  type: array
29
53
  description: Substance identifiers attached to this molecule.
30
54
  items:
31
- $ref: "identifier.yaml"
55
+ "$ref": identifier.yaml
32
56
  additionalProperties: false
@@ -1,5 +1,5 @@
1
1
  // Generated from schemas/v1/formula.yaml — do not edit; regenerate.
2
2
  export interface Formula {
3
3
  readonly type: "formula";
4
- readonly nodes: (Atom | Molecule | Group | Bond | Reaction | ReactionCascade | ElectronConfiguration | EmbeddedMath | Text | Name | Identifier | Mechanism | Spectrum | Crystal | ZMatrix | Calculation)[];
4
+ readonly nodes: (Molecule | Reaction | ReactionCascade | Group | Atom | Bond | ElectronConfiguration | EmbeddedMath | Text | Name | Identifier | Mechanism | Spectrum | Crystal | ZMatrix | Calculation)[];
5
5
  }
@@ -1,7 +1,7 @@
1
1
  // Generated from schemas/v1/group.yaml — do not edit; regenerate.
2
2
  export interface Group {
3
3
  readonly type: "group";
4
- readonly nodes: (Atom | Molecule | Group)[];
4
+ readonly nodes: (Molecule | Group | Bond | Atom)[];
5
5
  readonly multiplicity?: string;
6
6
  readonly bracket?: "paren" | "square" | "brace";
7
7
  }
@@ -1,7 +1,7 @@
1
1
  // Generated from schemas/v1/molecule.yaml — do not edit; regenerate.
2
2
  export interface Molecule {
3
3
  readonly type: "molecule";
4
- readonly nodes: (Atom | Bond | Group | Molecule)[];
4
+ readonly nodes: (Molecule | Group | Bond | Atom)[];
5
5
  readonly coefficient?: string;
6
6
  readonly identifiers?: Identifier[];
7
7
  }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: asciichem-model
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.3
4
+ version: 0.3.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - AsciiChem contributors