Skip to content

Commit

Permalink
Add equality functions for comparing message definitions / -fields (#1)
Browse files Browse the repository at this point in the history
### Public-Facing Changes
Add equality functions for comparing message definitions / -fields

### Description
- Changes the build to use typescript
- Adds equality functions for comparing message definitions / -fields
- Add some test cases
  • Loading branch information
achim-k authored Jul 26, 2023
1 parent 83d1ea1 commit 848b45e
Show file tree
Hide file tree
Showing 12 changed files with 2,083 additions and 19 deletions.
7 changes: 7 additions & 0 deletions .eslintrc.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
env:
es2020: true

ignorePatterns:
- dist

plugins:
- jest

extends:
- plugin:@foxglove/base
- plugin:@foxglove/jest

overrides:
- files: ["*.ts", "*.tsx"]
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ jobs:

- run: yarn install --frozen-lockfile
- run: yarn run lint:ci
- run: yarn run build
- run: yarn run test

- name: Publish to NPM
if: ${{ startsWith(github.ref, 'refs/tags/v') }}
Expand Down
6 changes: 6 additions & 0 deletions jest.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"testMatch": ["<rootDir>/src/**/*.test.ts"],
"transform": {
"^.+\\.ts$": "ts-jest"
}
}
18 changes: 14 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,36 @@
"email": "[email protected]"
},
"homepage": "https://github.com/foxglove/message-definition",
"main": "src/index.js",
"typings": "src/index.d.ts",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"files": [
"dist",
"src"
],
"scripts": {
"build": "yarn clean && tsc -b ./tsconfig.build.json",
"clean": "rm -rf dist tsconfig.build.tsbuildinfo",
"lint:ci": "eslint --report-unused-disable-directives .",
"lint": "eslint --report-unused-disable-directives --fix .",
"prepublishOnly": "yarn lint:ci"
"prepack": "yarn build",
"prepublishOnly": "yarn lint:ci",
"test": "jest"
},
"devDependencies": {
"@foxglove/eslint-plugin": "0.21.0",
"@types/jest": "^29.5.3",
"@typescript-eslint/eslint-plugin": "5.54.0",
"@typescript-eslint/parser": "5.54.0",
"eslint": "8.35.0",
"eslint-config-prettier": "8.6.0",
"eslint-plugin-es": "4.1.0",
"eslint-plugin-filenames": "1.3.2",
"eslint-plugin-import": "2.27.5",
"eslint-plugin-jest": "^27.2.3",
"eslint-plugin-prettier": "4.2.1",
"prettier": "2.8.4"
"jest": "^29.6.1",
"prettier": "2.8.4",
"ts-jest": "^29.1.1",
"typescript": "^5.1.6"
}
}
2 changes: 0 additions & 2 deletions src/index.js

This file was deleted.

2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./types";
export * from "./utils";
File renamed without changes.
63 changes: 63 additions & 0 deletions src/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { MessageDefinition } from "./types";
import { isMsgDefEqual } from "./utils";

describe("isMsgDefEqual", () => {
it("Should return true for definition for same msg parsed from .msg and .idl", () => {
const definition1: MessageDefinition = {
definitions: [
{
name: "sec",
type: "int32",
isComplex: false,
isArray: false,
},
{
name: "nanosec",
type: "uint32",
isComplex: false,
isArray: false,
},
],
};
const definition2: MessageDefinition = {
name: "builtin_interfaces/msg/Time",
definitions: [
{
type: "int32",
isComplex: false,
name: "sec",
},
{
type: "uint32",
isComplex: false,
name: "nanosec",
},
],
};
expect(isMsgDefEqual(definition1, definition2)).toBe(true);
});

it("should return true for identical definitions", () => {
const definition1: MessageDefinition = {
name: "definition",
definitions: [
{ type: "string", name: "field1" },
{ type: "number", name: "field2" },
],
};
const definition2: MessageDefinition = { ...definition1 };
expect(isMsgDefEqual(definition1, definition2)).toBe(true);
});

it("should return false for different definitions", () => {
const definition1: MessageDefinition = {
name: "definition",
definitions: [{ type: "string", name: "field1" }],
};
const definition2: MessageDefinition = {
...definition1,
definitions: [{ type: "string", name: "fieldWithDifferentName" }],
};
expect(isMsgDefEqual(definition1, definition2)).toBe(false);
});
});
28 changes: 28 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { MessageDefinition, MessageDefinitionField } from "./types";

export function isMsgDefFieldEqual(
lhs: MessageDefinitionField,
rhs: MessageDefinitionField,
): boolean {
return (
lhs.type === rhs.type &&
lhs.name === rhs.name &&
(lhs.isComplex ?? false) === (rhs.isComplex ?? false) &&
(lhs.isArray ?? false) === (rhs.isArray ?? false) &&
lhs.arrayLength === rhs.arrayLength &&
(lhs.isConstant ?? false) === (rhs.isConstant ?? false) &&
lhs.value === rhs.value &&
lhs.valueText === rhs.valueText &&
lhs.upperBound === rhs.upperBound &&
lhs.arrayUpperBound === rhs.arrayUpperBound &&
lhs.defaultValue === rhs.defaultValue
);
}

export function isMsgDefEqual(lhs: MessageDefinition, rhs: MessageDefinition): boolean {
return (
(lhs.name == undefined || rhs.name == undefined || lhs.name === rhs.name) &&
lhs.definitions.length === rhs.definitions.length &&
lhs.definitions.every((def, i) => isMsgDefFieldEqual(def, rhs.definitions[i]!))
);
}
4 changes: 4 additions & 0 deletions tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "./tsconfig.json",
"exclude": ["**/**.test.ts"]
}
16 changes: 13 additions & 3 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
{
"include": ["./src/**/*.ts"],
"compilerOptions": {
"noEmit": true,
"module": "commonjs",
"target": "es2020",
"lib": ["es2020"],
"rootDir": "./src",
"outDir": "./dist",

"moduleResolution": "node",
"esModuleInterop": true,
"isolatedModules": true,
"allowJs": false,
"newLine": "lf",
"skipLibCheck": false,
"allowJs": true,
"resolveJsonModule": true,
"skipLibCheck": true,

"declaration": true,
"declarationMap": true,
"sourceMap": true,

// be as strict as possible, but no stricter
"strict": true,
Expand Down
Loading

0 comments on commit 848b45e

Please sign in to comment.