-
Notifications
You must be signed in to change notification settings - Fork 130
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add message branding with types (#147)
- Loading branch information
1 parent
710b516
commit 088283f
Showing
6 changed files
with
99 additions
and
7 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
41 changes: 41 additions & 0 deletions
41
packages/runtime/spec/reflection-contains-message-type.spec.ts
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,41 @@ | ||
import {MessageType, ScalarType, containsMessageType, MESSAGE_TYPE} from '../src'; | ||
|
||
describe('containsMessageType', () => { | ||
interface MyMessage { | ||
stringField: string; | ||
} | ||
|
||
const MyMessage: MessageType<MyMessage> = new MessageType<MyMessage>('.test.MyMessage', [ | ||
{no: 1, name: 'string_field', kind: "scalar", T: ScalarType.STRING}, | ||
]); | ||
|
||
it('allows to extract type after .create', () => { | ||
const msg = MyMessage.create({ | ||
stringField: "hello world", | ||
}); | ||
|
||
if (containsMessageType(msg)) { | ||
expect(msg[MESSAGE_TYPE]).toEqual(MyMessage); | ||
} else { | ||
fail("containsMessageType() must return true"); | ||
} | ||
}); | ||
|
||
it('allows to extract type after .clone', () => { | ||
const cloned = MyMessage.clone(MyMessage.create({ | ||
stringField: "hello world", | ||
})); | ||
|
||
if (containsMessageType(cloned)) { | ||
expect(cloned[MESSAGE_TYPE]).toEqual(MyMessage); | ||
} else { | ||
fail("containsMessageType() must return true"); | ||
} | ||
}); | ||
|
||
it('returns false if provided a non-message', () => { | ||
const msg: MyMessage = { stringField: "foo" }; | ||
|
||
expect(containsMessageType(msg)).toBeFalse(); | ||
}); | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import {IMessageType, MESSAGE_TYPE} from './message-type-contract'; | ||
|
||
/** | ||
* The interface that models storing type in a symbol property. | ||
*/ | ||
export interface MessageTypeContainer<T extends object> { | ||
[MESSAGE_TYPE]: IMessageType<T>; | ||
} | ||
|
||
/** | ||
* Check if the provided object is a proto message. | ||
*/ | ||
export function containsMessageType<T extends object>(msg: T): msg is (T & MessageTypeContainer<T>) { | ||
return (msg as MessageTypeContainer<T>)[MESSAGE_TYPE] != null; | ||
} |
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