Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
iwoplaza authored Jan 25, 2022
1 parent 711d9e7 commit c40ade9
Showing 1 changed file with 3 additions and 52 deletions.
55 changes: 3 additions & 52 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -337,57 +337,8 @@ console.log(JSON.stringify(Person.read(reader).address)); // { "city": "New York

# Recursive types
If you want an object type to be able to contain one of itself (recursion), then you have to apply the following pattern:
```ts
import { INT, STRING, object, Parsed, ParsedConcrete, typedGeneric, typedObject, TypeToken } from 'typed-binary';

interface ExpressionBase {}

interface MultiplyExpression extends ExpressionBase {
type: 'multiply';
a: Expression;
b: Expression;
}

interface NegateExpression extends ExpressionBase {
type: 'negate';
inner: Expression;
}

type IntLiteralExpression = ParsedConcrete<ExpressionBase, typeof IntLiteralExpression, 'int_literal'>;
const IntLiteralExpression = object({
value: INT,
});

type Expression = MultiplyExpression|NegateExpression|IntLiteralExpression;
const Expression = typedGeneric(new TypeToken<Expression>(), {
name: STRING,
}, {
'multiply': typedObject<MultiplyExpression>(() => ({
a: Expression,
b: Expression,
})),
'negate': typedObject<NegateExpression>(() => ({
inner: Expression,
})),
'int_literal': IntLiteralExpression
});

const expr: Parsed<typeof Expression> = {
type: 'multiply',
a: {
type: 'negate',
inner: {
type: 'int_literal',
value: 15,
}
},
b: {
type: 'int_literal',
value: 2,
},
};

```
> CURRENTLY WORKING ON A NEW PATTERN.
> THIS IS A WORK IN PROGRESS.
# Serialization and Deserialization
Each schema has the following methods:
Expand Down Expand Up @@ -415,4 +366,4 @@ const buffer = Buffer.alloc(64); // Or new ArrayBuffer(64); on browsers.

const reader = new BufferReader(buffer); // Implements ISerialInput
const writer = new BufferWriter(buffer); // Implements ISerialOutput
```
```

0 comments on commit c40ade9

Please sign in to comment.