@dcl/ecs 7.1.4 → 7.1.5

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.
@@ -21,7 +21,7 @@ export type JsonArray = Array<JsonPrimitive | JsonMap | JsonArray>;
21
21
  */
22
22
  export type JsonSchemaExtended = {
23
23
  type: 'object' | 'number' | 'integer' | 'string' | 'array' | 'boolean';
24
- serializationType: 'boolean' | 'enum-int' | 'enum-string' | 'int8' | 'int16' | 'int32' | 'int64' | 'float32' | 'float64' | 'vector3' | 'color3' | 'quaternion' | 'color4' | 'map' | 'optional' | 'entity' | 'array' | 'utf8-string' | 'protocol-buffer' | 'transform' | 'unknown';
24
+ serializationType: 'boolean' | 'enum-int' | 'enum-string' | 'int8' | 'int16' | 'int32' | 'int64' | 'float32' | 'float64' | 'vector3' | 'color3' | 'quaternion' | 'color4' | 'map' | 'optional' | 'entity' | 'array' | 'utf8-string' | 'protocol-buffer' | 'transform' | 'one-of' | 'unknown';
25
25
  } & JsonMap;
26
26
  /**
27
27
  * @public
@@ -0,0 +1,10 @@
1
+ import { ISchema } from './ISchema';
2
+ import { Spec } from './Map';
3
+ type OneOfType<T extends Spec> = {
4
+ [K in keyof T]: {
5
+ readonly $case: K;
6
+ readonly value: ReturnType<T[K]['deserialize']>;
7
+ };
8
+ }[keyof T];
9
+ export declare const IOneOf: <T extends Spec>(specs: T) => ISchema<OneOfType<T>>;
10
+ export {};
@@ -0,0 +1,31 @@
1
+ export const IOneOf = (specs) => {
2
+ const specKeys = Object.keys(specs);
3
+ const keyToIndex = specKeys.reduce((dict, key, index) => {
4
+ dict[key] = index;
5
+ return dict;
6
+ }, {});
7
+ const specReflection = specKeys.reduce((specReflection, currentKey) => {
8
+ specReflection[currentKey] = specs[currentKey].jsonSchema;
9
+ return specReflection;
10
+ }, {});
11
+ return {
12
+ serialize({ $case, value }, builder) {
13
+ const _value = keyToIndex[$case.toString()] + 1;
14
+ builder.writeUint8(_value);
15
+ specs[$case].serialize(value, builder);
16
+ },
17
+ deserialize(reader) {
18
+ const $case = specKeys[reader.readInt8() - 1];
19
+ const value = specs[$case].deserialize(reader);
20
+ return { $case, value };
21
+ },
22
+ create() {
23
+ return {};
24
+ },
25
+ jsonSchema: {
26
+ type: 'object',
27
+ properties: specReflection,
28
+ serializationType: 'one-of'
29
+ }
30
+ };
31
+ };
@@ -1,4 +1,4 @@
1
- import { ISchema, JsonSchemaExtended } from './ISchema';
1
+ import { ISchema, JsonSchemaExtended } from '../ISchema';
2
2
  /**
3
3
  * Create an ISchema object from the json-schema
4
4
  * @param jsonSchema
@@ -1,16 +1,18 @@
1
- import { IArray } from './Array';
2
- import { Bool } from './basic/Boolean';
3
- import { IntEnum, StringEnum } from './basic/Enum';
4
- import { Float32, Float64 } from './basic/Float';
5
- import { Int16, Int32, Int64, Int8 } from './basic/Integer';
6
- import { EcsString } from './basic/String';
7
- import { Color3Schema } from './custom/Color3';
8
- import { Color4Schema } from './custom/Color4';
9
- import { EntitySchema } from './custom/Entity';
10
- import { QuaternionSchema } from './custom/Quaternion';
11
- import { Vector3Schema } from './custom/Vector3';
12
- import { IMap } from './Map';
13
- import { IOptional } from './Optional';
1
+ import { IArray } from '../Array';
2
+ import { Bool } from '../basic/Boolean';
3
+ import { IntEnum, StringEnum } from '../basic/Enum';
4
+ import { Float32, Float64 } from '../basic/Float';
5
+ import { Int16, Int32, Int64, Int8 } from '../basic/Integer';
6
+ import { EcsString } from '../basic/String';
7
+ import { Color3Schema } from '../custom/Color3';
8
+ import { Color4Schema } from '../custom/Color4';
9
+ import { EntitySchema } from '../custom/Entity';
10
+ import { QuaternionSchema } from '../custom/Quaternion';
11
+ import { Vector3Schema } from '../custom/Vector3';
12
+ import { IMap } from '../Map';
13
+ import { IOneOf } from '../OneOf';
14
+ import { IOptional } from '../Optional';
15
+ import { getTypeAndValue, isCompoundType } from './utils';
14
16
  const primitiveSchemas = {
15
17
  [Bool.jsonSchema.serializationType]: Bool,
16
18
  [EcsString.jsonSchema.serializationType]: EcsString,
@@ -59,35 +61,45 @@ export function jsonSchemaToSchema(jsonSchema) {
59
61
  const enumJsonSchema = jsonSchema;
60
62
  return StringEnum(enumJsonSchema.enumObject, enumJsonSchema.default);
61
63
  }
64
+ if (jsonSchema.serializationType === 'one-of') {
65
+ const oneOfJsonSchema = jsonSchema;
66
+ const spec = {};
67
+ for (const key in oneOfJsonSchema.properties) {
68
+ spec[key] = jsonSchemaToSchema(oneOfJsonSchema.properties[key]);
69
+ }
70
+ return IOneOf(spec);
71
+ }
62
72
  throw new Error(`${jsonSchema.serializationType} is not supported as reverse schema generation.`);
63
73
  }
64
74
  export function mutateValues(jsonSchema, value, mutateFn) {
65
75
  if (jsonSchema.serializationType === 'map') {
66
- const mapJsonSchema = jsonSchema;
67
- const mapValue = value;
68
- for (const key in mapJsonSchema.properties) {
69
- const valueType = mapJsonSchema.properties[key];
70
- if (valueType.serializationType === 'array' || valueType.serializationType === 'map') {
71
- mutateValues(mapJsonSchema.properties[key], mapValue[key], mutateFn);
76
+ const { properties } = jsonSchema;
77
+ const typedValue = value;
78
+ for (const key in properties) {
79
+ const { type, value: mapValue } = getTypeAndValue(properties, typedValue, key);
80
+ if (type.serializationType === 'unknown')
81
+ continue;
82
+ if (isCompoundType(type)) {
83
+ mutateValues(type, mapValue, mutateFn);
72
84
  }
73
85
  else {
74
- const newValue = mutateFn(mapValue[key], valueType);
86
+ const newValue = mutateFn(mapValue, type);
75
87
  if (newValue.changed) {
76
- mapValue[key] = newValue.value;
88
+ typedValue[key] = newValue.value;
77
89
  }
78
90
  }
79
91
  }
80
92
  }
81
93
  else if (jsonSchema.serializationType === 'array') {
82
- const withItemsJsonSchema = jsonSchema;
94
+ const { items } = jsonSchema;
83
95
  const arrayValue = value;
84
- const nestedMutateValues = withItemsJsonSchema.items.serializationType === 'array' || withItemsJsonSchema.items.serializationType === 'map';
85
96
  for (let i = 0, n = arrayValue.length; i < n; i++) {
86
- if (nestedMutateValues) {
87
- mutateValues(withItemsJsonSchema.items, arrayValue[i], mutateFn);
97
+ const { type, value } = getTypeAndValue({ items: items }, { items: arrayValue[i] }, 'items');
98
+ if (isCompoundType(type)) {
99
+ mutateValues(type, value, mutateFn);
88
100
  }
89
101
  else {
90
- const newValue = mutateFn(arrayValue[i], withItemsJsonSchema.items);
102
+ const newValue = mutateFn(value, type);
91
103
  if (newValue.changed) {
92
104
  arrayValue[i] = newValue.value;
93
105
  }
@@ -0,0 +1,5 @@
1
+ import { JsonSchemaExtended } from '../ISchema';
2
+ export type UnknownSchema = {
3
+ type: JsonSchemaExtended;
4
+ value: unknown;
5
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,12 @@
1
+ import { JsonSchemaExtended } from '../ISchema';
2
+ import { UnknownSchema } from './types';
3
+ export declare const isSchemaType: (value: JsonSchemaExtended, types: JsonSchemaExtended['serializationType'][]) => boolean;
4
+ export declare const isOneOfJsonSchema: (type: JsonSchemaExtended) => type is {
5
+ type: "string" | "number" | "boolean" | "object" | "integer" | "array";
6
+ serializationType: "boolean" | "map" | "array" | "enum-int" | "enum-string" | "int8" | "int16" | "int32" | "int64" | "float32" | "float64" | "vector3" | "color3" | "quaternion" | "color4" | "optional" | "entity" | "utf8-string" | "protocol-buffer" | "transform" | "one-of" | "unknown";
7
+ } & import("../ISchema").JsonMap & {
8
+ properties: Record<string, JsonSchemaExtended>;
9
+ };
10
+ export declare const getUnknownSchema: () => UnknownSchema;
11
+ export declare const isCompoundType: (type: JsonSchemaExtended) => boolean;
12
+ export declare const getTypeAndValue: (properties: Record<string, JsonSchemaExtended>, value: Record<string, unknown>, key: string) => UnknownSchema;
@@ -0,0 +1,22 @@
1
+ export const isSchemaType = (value, types) => types.includes(value.serializationType);
2
+ export const isOneOfJsonSchema = (type) => isSchemaType(type, ['one-of']);
3
+ export const getUnknownSchema = () => ({
4
+ type: { type: 'object', serializationType: 'unknown' },
5
+ value: undefined
6
+ });
7
+ export const isCompoundType = (type) => isSchemaType(type, ['array', 'map']);
8
+ export const getTypeAndValue = (properties, value, key) => {
9
+ const type = properties[key];
10
+ const valueKey = value[key];
11
+ if (isOneOfJsonSchema(type)) {
12
+ const typedMapValue = valueKey;
13
+ if (!typedMapValue.$case)
14
+ return getUnknownSchema();
15
+ const propType = type.properties[typedMapValue.$case];
16
+ // transform { $case: string; value: unknown } => { [$case]: value }
17
+ if (isCompoundType(propType))
18
+ value[key] = { [typedMapValue.$case]: typedMapValue.value };
19
+ return { type: propType, value: typedMapValue.value };
20
+ }
21
+ return { type, value: valueKey };
22
+ };
@@ -49,6 +49,11 @@ export declare namespace Schemas {
49
49
  const Map: <T extends import("./Map").Spec>(spec: T, defaultValue?: Partial<import("./Map").MapResult<T>> | undefined) => ISchema<import("./Map").MapResult<T>>;
50
50
  /** @public */
51
51
  const Optional: <T>(spec: ISchema<T>) => ISchema<T | undefined>;
52
+ /** @public */
53
+ const OneOf: <T extends import("./Map").Spec>(specs: T) => ISchema<{ [K in keyof T]: {
54
+ readonly $case: K;
55
+ readonly value: ReturnType<T[K]["deserialize"]>;
56
+ }; }[keyof T]>;
52
57
  /**
53
58
  * @public Create an ISchema object from the json-schema
54
59
  * @param jsonSchema
@@ -11,6 +11,7 @@ import { QuaternionSchema } from './custom/Quaternion';
11
11
  import { Vector3Schema } from './custom/Vector3';
12
12
  import { IMap } from './Map';
13
13
  import { IOptional } from './Optional';
14
+ import { IOneOf } from './OneOf';
14
15
  import { jsonSchemaToSchema, mutateValues } from './buildSchema';
15
16
  /**
16
17
  * @public
@@ -55,6 +56,8 @@ export var Schemas;
55
56
  Schemas.Map = IMap;
56
57
  /** @public */
57
58
  Schemas.Optional = IOptional;
59
+ /** @public */
60
+ Schemas.OneOf = IOneOf;
58
61
  /**
59
62
  * @public Create an ISchema object from the json-schema
60
63
  * @param jsonSchema
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "@dcl/ecs",
3
3
  "description": "Decentraland ECS",
4
- "version": "7.1.4",
4
+ "version": "7.1.5",
5
5
  "author": "DCL",
6
6
  "bugs": "https://github.com/decentraland/ecs/issues",
7
7
  "dependencies": {
8
- "@dcl/js-runtime": "7.1.4"
8
+ "@dcl/js-runtime": "7.1.5"
9
9
  },
10
10
  "devDependencies": {
11
11
  "ts-proto": "^1.122.0"
@@ -34,5 +34,5 @@
34
34
  },
35
35
  "types": "./dist/index.d.ts",
36
36
  "typings": "./dist/index.d.ts",
37
- "commit": "3f2952c5e02d012a18b91605485925bc3dd6f756"
37
+ "commit": "f6a337a9f93f7738174df9448d5a735ebc9468a1"
38
38
  }