Skip to content

Commit

Permalink
add sort init messagesJson before init microavia#67
Browse files Browse the repository at this point in the history
  • Loading branch information
meded7 committed Mar 31, 2023
1 parent becc6ae commit 64aa539
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 6 deletions.
16 changes: 12 additions & 4 deletions port/js/src/messgen.js
Original file line number Diff line number Diff line change
Expand Up @@ -561,11 +561,11 @@ export function initializeMessages(messagesJson, headerSchema) {
HEADER_STRUCT: headerSchema ? new Struct(headerSchema) : HEADER_STRUCT
};

for (let m in messagesJson) {
for (let m in getKeysWithSortById(messagesJson)) {

let name = m.trim(),
msg = "MSG_" + name.toUpperCase(),
id = messagesJson[m].id;
msg = "MSG_" + name.toUpperCase(),
id = messagesJson[m].id;

if (!res.__id__[id]) {
let msg_struct = new Struct(messagesJson[m], res);
Expand All @@ -576,9 +576,17 @@ export function initializeMessages(messagesJson, headerSchema) {
res[name] = msg_struct;

} else {
console.warn(`Warning: message ${id} ${msg} already exists.`);
console.warn(`Warning: message ${ id } ${ msg } already exists.`);
}
}

return res;
}

const getKeysWithSortById = (obj) => {
let keys = Object.keys(obj);
keys.sort((a, b) => {
return obj[a].id - obj[b].id;
});
return keys;
}
27 changes: 25 additions & 2 deletions port/js/tests/messgen.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ describe('Serialization deserialization tests', () => {

let schema = {
"MyYZ": {
id: 555,
id: 100,
fields: [
{ "name": "y", "type": "Int8" },
{ "name": "z", "type": "Int16" },
Expand Down Expand Up @@ -409,6 +409,7 @@ describe('Serialization deserialization tests', () => {

it('Error if wrong order of complex type', () => {

// wrong order
let schema = {
"First": {
id: 1,
Expand All @@ -428,6 +429,7 @@ describe('Serialization deserialization tests', () => {
initializeMessages(schema)
}).toThrow('Unknown type');

// missed type
let schema1 = {
"First": {
id: 1,
Expand All @@ -438,9 +440,10 @@ describe('Serialization deserialization tests', () => {
};

expect(() => {
initializeMessages(schema)
initializeMessages(schema1)
}).toThrow('Unknown type');

// correct location
let schema2 = {
"First": {
id: 1,
Expand All @@ -460,6 +463,26 @@ describe('Serialization deserialization tests', () => {
initializeMessages(schema2)
}).not.toThrow('Unknown type');

// change order
let schema3 = {
"Second": {
"id": 2,
"fields": [
{ "name": "xxx", "type": "First" }
]
},
"First": {
id: 1,
fields: [
{ "name": "x", "type": "Uint8" },
]
},
};

expect(() => {
initializeMessages(schema3)
}).not.toThrow('Unknown type');

})

it('TODO: compose tests for not equal size arrays messages', () => {
Expand Down

0 comments on commit 64aa539

Please sign in to comment.