-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
108fa71
commit f6a337a
Showing
22 changed files
with
222 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { DeepReadonly } from '../engine/readonly' | ||
import { ByteBuffer } from '../serialization/ByteBuffer' | ||
import { ISchema } from './ISchema' | ||
import { Spec } from './Map' | ||
|
||
type OneOfType<T extends Spec> = { | ||
[K in keyof T]: { | ||
readonly $case: K | ||
readonly value: ReturnType<T[K]['deserialize']> | ||
} | ||
}[keyof T] | ||
|
||
export const IOneOf = <T extends Spec>(specs: T): ISchema<OneOfType<T>> => { | ||
const specKeys = Object.keys(specs) | ||
const keyToIndex = specKeys.reduce((dict: Record<string, number>, key, index) => { | ||
dict[key] = index | ||
return dict | ||
}, {}) | ||
const specReflection = specKeys.reduce((specReflection, currentKey) => { | ||
specReflection[currentKey] = specs[currentKey].jsonSchema | ||
return specReflection | ||
}, {} as Record<string, any>) | ||
|
||
return { | ||
serialize({ $case, value }: DeepReadonly<OneOfType<T>>, builder: ByteBuffer): void { | ||
const _value = keyToIndex[$case.toString()] + 1 | ||
builder.writeUint8(_value) | ||
;(specs as any)[$case].serialize(value, builder) | ||
}, | ||
deserialize(reader: ByteBuffer) { | ||
const $case = specKeys[reader.readInt8() - 1] | ||
const value = specs[$case].deserialize(reader) | ||
return { $case, value } | ||
}, | ||
create() { | ||
return {} as OneOfType<T> | ||
}, | ||
jsonSchema: { | ||
type: 'object', | ||
properties: specReflection, | ||
serializationType: 'one-of' | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { JsonSchemaExtended } from '../ISchema' | ||
|
||
export type UnknownSchema = { type: JsonSchemaExtended; value: unknown } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { JsonSchemaExtended } from '../ISchema' | ||
import { IOneOf } from '../OneOf' | ||
import { UnknownSchema } from './types' | ||
|
||
export const isSchemaType = (value: JsonSchemaExtended, types: JsonSchemaExtended['serializationType'][]) => | ||
types.includes(value.serializationType) | ||
|
||
export const isOneOfJsonSchema = ( | ||
type: JsonSchemaExtended | ||
): type is JsonSchemaExtended & { properties: Record<string, JsonSchemaExtended> } => isSchemaType(type, ['one-of']) | ||
|
||
export const getUnknownSchema = (): UnknownSchema => ({ | ||
type: { type: 'object', serializationType: 'unknown' }, | ||
value: undefined | ||
}) | ||
|
||
export const isCompoundType = (type: JsonSchemaExtended): boolean => isSchemaType(type, ['array', 'map']) | ||
|
||
export const getTypeAndValue = ( | ||
properties: Record<string, JsonSchemaExtended>, | ||
value: Record<string, unknown>, | ||
key: string | ||
): UnknownSchema => { | ||
const type = properties[key] | ||
const valueKey = value[key] | ||
|
||
if (isOneOfJsonSchema(type)) { | ||
const typedMapValue = valueKey as ReturnType<ReturnType<typeof IOneOf>['deserialize']> | ||
if (!typedMapValue.$case) return getUnknownSchema() | ||
|
||
const propType = type.properties[typedMapValue.$case] | ||
|
||
// transform { $case: string; value: unknown } => { [$case]: value } | ||
if (isCompoundType(propType)) value[key] = { [typedMapValue.$case]: typedMapValue.value } | ||
|
||
return { type: propType, value: typedMapValue.value } | ||
} | ||
|
||
return { type, value: valueKey } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.