-
-
Notifications
You must be signed in to change notification settings - Fork 259
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
e3976a6
commit ea65a43
Showing
3 changed files
with
150 additions
and
0 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,49 @@ | ||
import { Root } from 'protobufjs/light'; | ||
|
||
type Definitions = Record<string, unknown>; | ||
|
||
export const loadDefinitions = async ( | ||
messages: Root, | ||
packageName: string, | ||
packageLoader: () => Definitions | Promise<Definitions>, | ||
) => { | ||
// check if package already exists | ||
try { | ||
const pkg = messages.lookup(packageName); | ||
if (pkg) { | ||
return; | ||
} | ||
} catch {} | ||
|
||
// get current MessageType enum | ||
let enumType; | ||
try { | ||
enumType = messages.lookupEnum('MessageType'); | ||
} catch {} | ||
|
||
// load definitions | ||
const packageMessages = await packageLoader(); | ||
const pkg = messages.define(packageName, packageMessages); | ||
// get package MessageType enum | ||
let packageEnumType; | ||
try { | ||
packageEnumType = pkg.lookupEnum('MessageType'); | ||
} catch {} | ||
|
||
// merge MessageType enums | ||
if (enumType && packageEnumType) { | ||
try { | ||
// move values from nested enum to top level | ||
Object.keys(packageEnumType.values).forEach(key => { | ||
enumType.add(key, packageEnumType.values[key]); | ||
}); | ||
// remove nested enum | ||
pkg.remove(packageEnumType); | ||
} catch (e) { | ||
// remove whole package on merge error | ||
messages.remove(pkg); | ||
|
||
throw e; | ||
} | ||
} | ||
}; |
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,100 @@ | ||
import * as ProtoBuf from 'protobufjs/light'; | ||
|
||
import { loadDefinitions } from '../src/load-definitions'; | ||
|
||
describe('loadDefinitions', () => { | ||
const createProtobufRoot = () => { | ||
return ProtoBuf.Root.fromJSON({ | ||
nested: { | ||
MessageType: { | ||
values: { | ||
Initialize: 0, | ||
}, | ||
}, | ||
}, | ||
}); | ||
}; | ||
|
||
it('merge MessageType enum', async () => { | ||
const root = createProtobufRoot(); | ||
await loadDefinitions(root, 'bitcoin', () => { | ||
return Promise.resolve({ | ||
MessageType: { | ||
values: { | ||
GetAddress: 29, | ||
}, | ||
}, | ||
}); | ||
}); | ||
|
||
const messageType = root.lookupEnum('MessageType')?.values; | ||
expect(messageType).toEqual({ | ||
Initialize: 0, | ||
GetAddress: 29, | ||
}); | ||
}); | ||
|
||
it('throw on merge MessageType enum', async () => { | ||
const root1 = createProtobufRoot(); | ||
await expect( | ||
loadDefinitions(root1, 'bitcoin', () => { | ||
return Promise.resolve({ | ||
MessageType: { | ||
values: { | ||
GetAddress: 0, | ||
}, | ||
}, | ||
}); | ||
}), | ||
).rejects.toThrow('duplicate id 0'); | ||
expect(root1.lookup('bitcoin')).toBe(null); | ||
|
||
await expect( | ||
loadDefinitions(createProtobufRoot(), 'bitcoin', () => { | ||
return Promise.resolve({ | ||
MessageType: { | ||
values: { | ||
Initialize: 1, | ||
}, | ||
}, | ||
}); | ||
}), | ||
).rejects.toThrow('duplicate name'); | ||
}); | ||
|
||
it('create MessageType enum', async () => { | ||
const root = createProtobufRoot(); | ||
root.remove(root.lookupEnum('MessageType')); | ||
|
||
await loadDefinitions(root, 'bitcoin', () => { | ||
return Promise.resolve({ | ||
MessageType: { | ||
values: { | ||
GetAddress: 29, | ||
}, | ||
}, | ||
}); | ||
}); | ||
|
||
const messageType = root.lookupEnum('MessageType')?.values; | ||
expect(messageType).toEqual({ | ||
GetAddress: 29, | ||
}); | ||
}); | ||
|
||
it('already loaded', async () => { | ||
const root = createProtobufRoot(); | ||
root.define('bitcoin', { | ||
MessageType: { | ||
values: { | ||
GetAddress: 29, | ||
}, | ||
}, | ||
}); | ||
|
||
const spy = jest.fn(); | ||
await loadDefinitions(root, 'bitcoin', spy); | ||
|
||
expect(spy).toHaveBeenCalledTimes(0); | ||
}); | ||
}); |