@lionweb/core 0.8.0-beta.3 → 0.8.0-beta.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.
Files changed (62) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/dist/deserializer.d.ts +43 -6
  3. package/dist/deserializer.d.ts.map +1 -1
  4. package/dist/deserializer.js +142 -129
  5. package/dist/deserializer.js.map +1 -1
  6. package/dist/functions.d.ts +6 -1
  7. package/dist/functions.d.ts.map +1 -1
  8. package/dist/functions.js +11 -0
  9. package/dist/functions.js.map +1 -1
  10. package/dist/index.d.ts +1 -1
  11. package/dist/index.d.ts.map +1 -1
  12. package/dist/index.js +1 -1
  13. package/dist/index.js.map +1 -1
  14. package/dist/m3/builtins.js +3 -3
  15. package/dist/m3/builtins.js.map +1 -1
  16. package/dist/m3/deserializer.d.ts +7 -3
  17. package/dist/m3/deserializer.d.ts.map +1 -1
  18. package/dist/m3/deserializer.js +14 -6
  19. package/dist/m3/deserializer.js.map +1 -1
  20. package/dist/m3/functions.d.ts +2 -2
  21. package/dist/m3/functions.d.ts.map +1 -1
  22. package/dist/m3/functions.js.map +1 -1
  23. package/dist/m3/reference-checker.js +2 -2
  24. package/dist/m3/reference-checker.js.map +1 -1
  25. package/dist/m3/serializer.d.ts.map +1 -1
  26. package/dist/m3/serializer.js +2 -2
  27. package/dist/m3/serializer.js.map +1 -1
  28. package/dist/m3/types.d.ts.map +1 -1
  29. package/dist/m3/types.js +4 -4
  30. package/dist/m3/types.js.map +1 -1
  31. package/dist/reading.d.ts +5 -4
  32. package/dist/reading.d.ts.map +1 -1
  33. package/dist/references.d.ts +14 -1
  34. package/dist/references.d.ts.map +1 -1
  35. package/dist/references.js +9 -0
  36. package/dist/references.js.map +1 -1
  37. package/dist/reporter.d.ts +49 -0
  38. package/dist/reporter.d.ts.map +1 -0
  39. package/dist/{handler.js → reporter.js} +15 -7
  40. package/dist/reporter.js.map +1 -0
  41. package/dist/serializer.d.ts +29 -7
  42. package/dist/serializer.d.ts.map +1 -1
  43. package/dist/serializer.js +14 -15
  44. package/dist/serializer.js.map +1 -1
  45. package/package.json +3 -3
  46. package/src/deserializer.ts +223 -168
  47. package/src/functions.ts +15 -1
  48. package/src/index.ts +1 -1
  49. package/src/m3/builtins.ts +3 -3
  50. package/src/m3/deserializer.ts +17 -12
  51. package/src/m3/functions.ts +2 -2
  52. package/src/m3/reference-checker.ts +2 -2
  53. package/src/m3/serializer.ts +2 -2
  54. package/src/m3/types.ts +4 -4
  55. package/src/reading.ts +5 -4
  56. package/src/references.ts +20 -1
  57. package/src/reporter.ts +78 -0
  58. package/src/serializer.ts +39 -23
  59. package/dist/handler.d.ts +0 -33
  60. package/dist/handler.d.ts.map +0 -1
  61. package/dist/handler.js.map +0 -1
  62. package/src/handler.ts +0 -57
package/src/m3/types.ts CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  import { LionWebId, LionWebJsonMetaPointer, LionWebKey } from "@lionweb/json"
7
7
  import { ResolveInfoDeducer } from "../reading.js"
8
- import { MultiRef, SingleRef, unresolved } from "../references.js"
8
+ import { MultiRef, SingleRef, referenceToSet } from "../references.js"
9
9
  import { Node } from "../types.js"
10
10
 
11
11
 
@@ -94,7 +94,7 @@ class Property extends Feature {
94
94
  metaType(): string {
95
95
  return "Property"
96
96
  }
97
- type: SingleRef<DataType> = unresolved // (reference)
97
+ type: SingleRef<DataType> = referenceToSet() // (reference)
98
98
  ofType(type: DataType): Property {
99
99
  this.type = type
100
100
  return this
@@ -103,7 +103,7 @@ class Property extends Feature {
103
103
 
104
104
  abstract class Link extends Feature {
105
105
  multiple /*: boolean */ = false
106
- type: SingleRef<Classifier> = unresolved // (reference)
106
+ type: SingleRef<Classifier> = referenceToSet() // (reference)
107
107
  isMultiple() {
108
108
  this.multiple = true
109
109
  return this
@@ -182,7 +182,7 @@ class Annotation extends Classifier {
182
182
  }
183
183
  extends?: SingleRef<Annotation> // (reference)
184
184
  implements: MultiRef<Interface> = [] // (reference)
185
- annotates: SingleRef<Classifier> = unresolved // (reference)
185
+ annotates: SingleRef<Classifier> = referenceToSet() // (reference)
186
186
  constructor(language: Language, name: string, key: LionWebKey, id: LionWebId, extends_?: SingleRef<Annotation>) {
187
187
  super(language, name, key, id)
188
188
  this.extends = extends_
package/src/reading.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { Classifier, Enumeration, EnumerationLiteral, Feature } from "./m3/index.js"
1
+ import { Classifier, Enumeration, EnumerationLiteral, Feature, Reference } from "./m3/index.js"
2
2
  import { Node } from "./types.js"
3
3
 
4
4
 
@@ -9,10 +9,11 @@ export type ClassifierDeducer<NT extends Node> = (node: NT) => Classifier
9
9
 
10
10
  /**
11
11
  * Type def. for functions that deduce the string value of the `resolveInfo` field of a
12
- * {@link LionWebJsonReferenceTarget serialized reference target}, or {@code undefined}
13
- * to indicate that no `resolveInfo` could be derived.
12
+ * {@link LionWebJsonReferenceTarget serialized reference target},
13
+ * or {@code undefined} to indicate that no `resolveInfo` could be derived.
14
+ * The function’s arguments are the target {@link Node} and its containing {@link Reference}.
14
15
  */
15
- export type ResolveInfoDeducer<NT extends Node> = (node: NT) => string | undefined
16
+ export type ResolveInfoDeducer<NT extends Node> = (node: NT, reference: Reference) => string | undefined
16
17
 
17
18
  /**
18
19
  * An interface that's used to parametrize generic serialization of
package/src/references.ts CHANGED
@@ -4,15 +4,27 @@ import { Node } from "./types.js"
4
4
  /**
5
5
  * The `unresolved` symbol indicates a reference value which hasn't been resolved yet.
6
6
  * It differs from an unset (`undefined`) value.
7
+ * This value shouldn’t be manipulated/compared to directly!
7
8
  */
8
9
  export const unresolved = null
9
10
 
11
+ /**
12
+ * Type for unresolved references.
13
+ */
14
+ export type UnresolvedReference = typeof unresolved
15
+
16
+ /**
17
+ * @return a value of {@link UnresolvedReference} that’s a placeholder for a yet-to-set reference.
18
+ */
19
+ export const referenceToSet = () =>
20
+ unresolved
21
+
10
22
  /**
11
23
  * A type definition for a reference value that can be unresolved.
12
24
  * Note: this type is primarily meant to be used to type nodes’ properties,
13
25
  * but should be avoided as a return type for “auxiliary” functions.
14
26
  */
15
- export type SingleRef<NT extends Node> = typeof unresolved | NT
27
+ export type SingleRef<NT extends Node> = NT | UnresolvedReference
16
28
 
17
29
  /**
18
30
  * @return whether a given (at most) single-valued reference actually refers to something.
@@ -20,6 +32,13 @@ export type SingleRef<NT extends Node> = typeof unresolved | NT
20
32
  export const isRef = <NT extends Node>(ref?: SingleRef<NT>): ref is NT =>
21
33
  ref !== undefined && ref !== unresolved
22
34
 
35
+ /**
36
+ * Type function for the {@link UnresolvedReference} type.
37
+ */
38
+ export const isUnresolvedReference = <NT extends Node>(ref?: SingleRef<NT>): ref is UnresolvedReference =>
39
+ ref === unresolved
40
+
41
+
23
42
  /**
24
43
  * A type alias for a multi-valued reference, to make it look consistent with {@link SingleRef}.
25
44
  * Note: this type is primarily meant to be used to type nodes’ properties,
@@ -0,0 +1,78 @@
1
+ /**
2
+ * Problems that arise during deserialization, are reported as plain text by calling the `reportProblem` function.
3
+ * This type is an interface rather than, say, a function definition
4
+ */
5
+ export interface ProblemReporter {
6
+ reportProblem: (message: string) => void
7
+ }
8
+
9
+ /**
10
+ * Legacy alias for {@link ProblemReporter}, kept for backward compatibility, and to be deprecated and removed later.
11
+ */
12
+ export type SimplisticHandler = ProblemReporter
13
+
14
+
15
+ /**
16
+ * A default {@link ProblemReporter} that just outputs everything of the console.
17
+ */
18
+ export const consoleProblemReporter: ProblemReporter = {
19
+ reportProblem: (message) => {
20
+ console.log(message)
21
+ }
22
+ }
23
+
24
+ /**
25
+ * Legacy alias for {@link consoleProblemReporter}, kept for backward compatibility, and to be deprecated and removed later.
26
+ */
27
+ export const defaultSimplisticHandler = consoleProblemReporter
28
+
29
+
30
+ /**
31
+ * A {@link ProblemReporter} that just accumulates problems (in terms of their messages).
32
+ */
33
+ export class AccumulatingProblemReporter implements ProblemReporter {
34
+ private _allProblems: string[] = []
35
+ reportProblem(message: string) {
36
+ this._allProblems.push(message)
37
+ }
38
+ get allProblems() {
39
+ return this._allProblems
40
+ }
41
+ }
42
+
43
+ /**
44
+ * Legacy alias for {@link AccumulatingProblemReporter}, kept for backward compatibility, and to be deprecated and removed later.
45
+ */
46
+ export const AccumulatingSimplisticHandler = AccumulatingProblemReporter
47
+
48
+
49
+ /**
50
+ * A {@link ProblemReporter} that aggregates problems by their message.
51
+ * This is convenient for problems that arise many times during deserialization
52
+ * but produce the exact same message every time.
53
+ */
54
+ export class AggregatingProblemReporter implements ProblemReporter {
55
+ private messageByCount: { [message: string]: number } = {}
56
+ reportProblem(message: string) {
57
+ this.messageByCount[message] = (this.messageByCount[message] ?? 0) + 1
58
+ }
59
+ reportAllProblemsOnConsole(asTable = false) {
60
+ if (asTable) {
61
+ console.table(this.messageByCount)
62
+ } else {
63
+ Object.entries(this.messageByCount)
64
+ .forEach(([message, count]) => {
65
+ console.log(`${message} (${count})`)
66
+ })
67
+ }
68
+ }
69
+ allProblems() {
70
+ return { ...this.messageByCount }
71
+ }
72
+ }
73
+
74
+ /**
75
+ * Legacy alias for {@link AggregatingProblemReporter}, kept for backward compatibility, and to be deprecated and removed later.
76
+ */
77
+ export type AggregatingSimplisticHandler = AggregatingProblemReporter
78
+
package/src/serializer.ts CHANGED
@@ -1,12 +1,6 @@
1
- import {
2
- currentSerializationFormatVersion,
3
- LionWebId,
4
- LionWebJsonChunk,
5
- LionWebJsonMetaPointer,
6
- LionWebJsonNode
7
- } from "@lionweb/json"
1
+ import { currentSerializationFormatVersion, LionWebId, LionWebJsonChunk, LionWebJsonNode } from "@lionweb/json"
8
2
  import { asArray, keepDefineds, lazyMapGet, Nested3Map, uniquesAmong } from "@lionweb/ts-utils"
9
- import { asIds } from "./functions.js"
3
+ import { asIds, metaPointerFor } from "./functions.js"
10
4
  import { Reader } from "./reading.js"
11
5
  import { Node } from "./types.js"
12
6
  import { builtinPropertyValueSerializer } from "./m3/builtins.js"
@@ -24,6 +18,12 @@ import {
24
18
  } from "./m3/types.js"
25
19
 
26
20
 
21
+ /**
22
+ * Type definition for functions that serializes nodes as a {@link LionWebJsonChunk serialization chunk}.
23
+ */
24
+ export type Serializer<NT extends Node> = (nodes: NT[]) => LionWebJsonChunk
25
+
26
+
27
27
  /**
28
28
  * Interface for objects that expose a method to serialize a property's value.
29
29
  */
@@ -56,7 +56,7 @@ export type SerializationOptions = Partial<{
56
56
 
57
57
  /**
58
58
  * A {@link PropertyValueSerializer} implementation.
59
- * Default = DefaultPropertyValueSerializer.
59
+ * Default = {@link builtinPropertyValueSerializer}.
60
60
  */
61
61
  propertyValueSerializer: PropertyValueSerializer
62
62
 
@@ -67,26 +67,42 @@ export type SerializationOptions = Partial<{
67
67
 
68
68
  }>
69
69
 
70
+
70
71
  /**
71
- * @return the {@link LionWebJsonMetaPointer} for the given {@link Feature}.
72
+ * Type for objects to configure {@link Serializer node serializers} with.
73
+ * The `reader` property is mandatory,
74
+ * and the `serializeEmptyFeatures`, `propertyValueSerializer`,
75
+ * and `primitiveTypeSerializer` (which is a legacy alias for `propertyValueSerializer`)
76
+ * properties are optional, with defined defaults.
72
77
  */
73
- export const metaPointerFor = (feature: Feature): LionWebJsonMetaPointer => {
74
- const { language } = feature.classifier
75
- return {
76
- language: language.key,
77
- version: language.version,
78
- key: feature.key
79
- }
80
- }
78
+ export type SerializerConfiguration<NT extends Node> = {
79
+ /**
80
+ * An interface with functions to “read” – i.e., introspect – nodes.
81
+ */
82
+ reader: Reader<NT>
83
+ } & SerializationOptions
84
+
85
+
86
+ /**
87
+ * @return a {@link Serializer} function that serializes the {@link Node nodes} passed to it,
88
+ * configured through a `reader` {@link Reader} instance,
89
+ * and (optionally) a `serializationOptions` {@link SerializationOptions} object.
90
+ *
91
+ * This is a legacy version of {@link serializerWith}, kept for backward compatibility, and to be deprecated and removed later.
92
+ */
93
+ export const nodeSerializer = <NT extends Node>(reader: Reader<NT>, serializationOptions?: SerializationOptions): Serializer<NT> =>
94
+ serializerWith({ reader, ...serializationOptions })
81
95
 
82
96
 
83
97
  /**
84
- * @return a function that serializes the {@link Node nodes} passed to it.
98
+ * @return a {@link Serializer} function that serializes the {@link Node nodes} passed to it,
99
+ * configured through a `configuration` {@link SerializerConfiguration} object.
85
100
  */
86
- export const nodeSerializer = <NT extends Node>(reader: Reader<NT>, serializationOptions?: SerializationOptions) => {
101
+ export const serializerWith = <NT extends Node>(configuration: SerializerConfiguration<NT>): Serializer<NT> => {
102
+ const { reader } = configuration
87
103
  const propertyValueSerializer =
88
- serializationOptions?.propertyValueSerializer ?? serializationOptions?.primitiveTypeSerializer ?? builtinPropertyValueSerializer
89
- const serializeEmptyFeatures = serializationOptions?.serializeEmptyFeatures ?? true
104
+ configuration.propertyValueSerializer ?? configuration.primitiveTypeSerializer ?? builtinPropertyValueSerializer
105
+ const serializeEmptyFeatures = configuration.serializeEmptyFeatures ?? true
90
106
 
91
107
  const languageKey2version2classifierKey2allFeatures: Nested3Map<Feature[]> = {}
92
108
  const memoisedAllFeaturesOf = (classifier: Classifier): Feature[] =>
@@ -199,7 +215,7 @@ export const nodeSerializer = <NT extends Node>(reader: Reader<NT>, serializatio
199
215
  .map(t => t as NT)
200
216
  .map(t => ({
201
217
  resolveInfo:
202
- (reader.resolveInfoFor ? reader.resolveInfoFor(t) : simpleNameDeducer(t)) ?? null,
218
+ (reader.resolveInfoFor ? reader.resolveInfoFor(t, feature) : simpleNameDeducer(t, feature)) ?? null,
203
219
  reference: t.id
204
220
  }))
205
221
  })
package/dist/handler.d.ts DELETED
@@ -1,33 +0,0 @@
1
- /**
2
- * A simplistic handler to which problems that arise during deserialization,
3
- * are reported as plain text by calling the `reportProblem` function.
4
- */
5
- export interface SimplisticHandler {
6
- reportProblem: (message: string) => void;
7
- }
8
- /**
9
- * A default simplistic handler that just outputs everything of the console.
10
- */
11
- export declare const defaultSimplisticHandler: SimplisticHandler;
12
- /**
13
- * A simplistic handler that just accumulates problems (in terms of their messages).
14
- */
15
- export declare class AccumulatingSimplisticHandler implements SimplisticHandler {
16
- private _allProblems;
17
- reportProblem(message: string): void;
18
- get allProblems(): string[];
19
- }
20
- /**
21
- * A simplistic handler that aggregates problems by their message.
22
- * This is convenient for problems that arise many times during deserialization
23
- * but produce the exact same message every time.
24
- */
25
- export declare class AggregatingSimplisticHandler implements SimplisticHandler {
26
- private messageByCount;
27
- reportProblem(message: string): void;
28
- reportAllProblemsOnConsole(asTable?: boolean): void;
29
- allProblems(): {
30
- [message: string]: number;
31
- };
32
- }
33
- //# sourceMappingURL=handler.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"handler.d.ts","sourceRoot":"","sources":["../src/handler.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,WAAW,iBAAiB;IAC9B,aAAa,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,IAAI,CAAA;CAC3C;AAED;;GAEG;AACH,eAAO,MAAM,wBAAwB,EAAE,iBAItC,CAAA;AAGD;;GAEG;AACH,qBAAa,6BAA8B,YAAW,iBAAiB;IACnE,OAAO,CAAC,YAAY,CAAe;IACnC,aAAa,CAAC,OAAO,EAAE,MAAM;IAG7B,IAAI,WAAW,aAEd;CACJ;AAGD;;;;GAIG;AACH,qBAAa,4BAA6B,YAAW,iBAAiB;IAClE,OAAO,CAAC,cAAc,CAAoC;IAC1D,aAAa,CAAC,OAAO,EAAE,MAAM;IAG7B,0BAA0B,CAAC,OAAO,UAAQ;IAU1C,WAAW;;;CAGd"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"handler.js","sourceRoot":"","sources":["../src/handler.ts"],"names":[],"mappings":"AAQA;;GAEG;AACH,MAAM,CAAC,MAAM,wBAAwB,GAAsB;IACvD,aAAa,EAAE,CAAC,OAAO,EAAE,EAAE;QACvB,OAAO,CAAC,GAAG,CAAC,OAAO,CAAC,CAAA;IACxB,CAAC;CACJ,CAAA;AAGD;;GAEG;AACH,MAAM,OAAO,6BAA6B;IAA1C;QACY,iBAAY,GAAa,EAAE,CAAA;IAOvC,CAAC;IANG,aAAa,CAAC,OAAe;QACzB,IAAI,CAAC,YAAY,CAAC,IAAI,CAAC,OAAO,CAAC,CAAA;IACnC,CAAC;IACD,IAAI,WAAW;QACX,OAAO,IAAI,CAAC,YAAY,CAAA;IAC5B,CAAC;CACJ;AAGD;;;;GAIG;AACH,MAAM,OAAO,4BAA4B;IAAzC;QACY,mBAAc,GAAkC,EAAE,CAAA;IAiB9D,CAAC;IAhBG,aAAa,CAAC,OAAe;QACzB,IAAI,CAAC,cAAc,CAAC,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,cAAc,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,GAAG,CAAC,CAAA;IAC1E,CAAC;IACD,0BAA0B,CAAC,OAAO,GAAG,KAAK;QACtC,IAAI,OAAO,EAAE,CAAC;YACV,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC,cAAc,CAAC,CAAA;QACtC,CAAC;aAAM,CAAC;YACJ,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,cAAc,CAAC;iBAC9B,OAAO,CAAC,CAAC,CAAC,OAAO,EAAE,KAAK,CAAC,EAAE,EAAE;gBAC1B,OAAO,CAAC,GAAG,CAAC,GAAG,OAAO,KAAK,KAAK,GAAG,CAAC,CAAA;YACxC,CAAC,CAAC,CAAA;QACV,CAAC;IACL,CAAC;IACD,WAAW;QACP,OAAO,EAAE,GAAG,IAAI,CAAC,cAAc,EAAE,CAAA;IACrC,CAAC;CACJ"}
package/src/handler.ts DELETED
@@ -1,57 +0,0 @@
1
- /**
2
- * A simplistic handler to which problems that arise during deserialization,
3
- * are reported as plain text by calling the `reportProblem` function.
4
- */
5
- export interface SimplisticHandler {
6
- reportProblem: (message: string) => void
7
- }
8
-
9
- /**
10
- * A default simplistic handler that just outputs everything of the console.
11
- */
12
- export const defaultSimplisticHandler: SimplisticHandler = {
13
- reportProblem: (message) => {
14
- console.log(message)
15
- }
16
- }
17
-
18
-
19
- /**
20
- * A simplistic handler that just accumulates problems (in terms of their messages).
21
- */
22
- export class AccumulatingSimplisticHandler implements SimplisticHandler {
23
- private _allProblems: string[] = []
24
- reportProblem(message: string) {
25
- this._allProblems.push(message)
26
- }
27
- get allProblems() {
28
- return this._allProblems
29
- }
30
- }
31
-
32
-
33
- /**
34
- * A simplistic handler that aggregates problems by their message.
35
- * This is convenient for problems that arise many times during deserialization
36
- * but produce the exact same message every time.
37
- */
38
- export class AggregatingSimplisticHandler implements SimplisticHandler {
39
- private messageByCount: { [message: string]: number } = {}
40
- reportProblem(message: string) {
41
- this.messageByCount[message] = (this.messageByCount[message] ?? 0) + 1
42
- }
43
- reportAllProblemsOnConsole(asTable = false) {
44
- if (asTable) {
45
- console.table(this.messageByCount)
46
- } else {
47
- Object.entries(this.messageByCount)
48
- .forEach(([message, count]) => {
49
- console.log(`${message} (${count})`)
50
- })
51
- }
52
- }
53
- allProblems() {
54
- return { ...this.messageByCount }
55
- }
56
- }
57
-