Graph concept renames

- ID -> Address
- ID.name -> Address.id
- GraphEdge -> Edge
- GraphNode -> Node
This commit is contained in:
Dandelion Mané 2018-02-28 17:25:27 -08:00
parent 791cad9059
commit 2992a31157
2 changed files with 50 additions and 50 deletions

View File

@ -1,47 +1,47 @@
// @flow // @flow
export type ID = { export type Address = {
pluginName: string,
repositoryName: string, repositoryName: string,
name: string, pluginName: string,
id: string,
}; };
export type GraphNode<T> = { export type Node<T> = {
id: ID, address: Address,
edges: ID[], edges: Address[],
payload: T, payload: T,
}; };
export type GraphEdge<T> = { export type Edge<T> = {
id: ID, address: Address,
sourceId: ID, sourceId: Address,
destId: ID, destId: Address,
weight: number, weight: number,
payload: T, payload: T,
}; };
export type Graph = { export type Graph = {
nodes: {[stringID: string]: GraphNode<mixed>}, nodes: {[stringAddress: string]: Node<mixed>},
edges: {[stringID: string]: GraphEdge<mixed>}, edges: {[stringAddress: string]: Edge<mixed>},
}; };
export function idToString(id: ID) { export function addressToString(address: Address) {
if (id.pluginName.includes("$")) { if (address.pluginName.includes("$")) {
const escaped = JSON.stringify(id.pluginName); const escaped = JSON.stringify(address.pluginName);
throw new Error(`id.pluginName must not include "\$": ${escaped}`); throw new Error(`address.pluginName must not include "\$": ${escaped}`);
} }
if (id.repositoryName.includes("$")) { if (address.repositoryName.includes("$")) {
const escaped = JSON.stringify(id.repositoryName); const escaped = JSON.stringify(address.repositoryName);
throw new Error(`id.repositoryName must not include "\$": ${escaped}`); throw new Error(`address.repositoryName must not include "\$": ${escaped}`);
} }
if (id.name.includes("$")) { if (address.id.includes("$")) {
const escaped = JSON.stringify(id.name); const escaped = JSON.stringify(address.id);
throw new Error(`id.name must not include "\$": ${escaped}`); throw new Error(`address.id must not include "\$": ${escaped}`);
} }
return `${id.pluginName}\$${id.repositoryName}\$${id.name}`; return `${address.pluginName}\$${address.repositoryName}\$${address.id}`;
} }
export function stringToID(string: string) { export function stringToAddress(string: string) {
const parts = string.split("$"); const parts = string.split("$");
if (parts.length !== 3) { if (parts.length !== 3) {
const escaped = JSON.stringify(string); const escaped = JSON.stringify(string);
@ -50,6 +50,6 @@ export function stringToID(string: string) {
return { return {
pluginName: parts[0], pluginName: parts[0],
repositoryName: parts[1], repositoryName: parts[1],
name: parts[2], id: parts[2],
}; };
} }

View File

@ -1,72 +1,72 @@
import {idToString, stringToID} from "./graph"; import {addressToString, stringToAddress} from "./graph";
describe("graph", () => { describe("graph", () => {
describe("idToString", () => { describe("addressToString", () => {
const makeSimpleID = () => ({ const makeSimpleAddress = () => ({
pluginName: "widgets", pluginName: "widgets",
repositoryName: "megacorp/megawidget", repositoryName: "megacorp/megawidget",
name: "issue#123", id: "issue#123",
}); });
it("stringifies a simple ID", () => { it("stringifies a simple Address", () => {
const input = makeSimpleID(); const input = makeSimpleAddress();
const expected = "widgets$megacorp/megawidget$issue#123"; const expected = "widgets$megacorp/megawidget$issue#123";
expect(idToString(input)).toEqual(expected); expect(addressToString(input)).toEqual(expected);
}); });
function expectRejection(attribute, value) { function expectRejection(attribute, value) {
const input = {...makeSimpleID(), [attribute]: value}; const input = {...makeSimpleAddress(), [attribute]: value};
expect(() => idToString(input)).toThrow(RegExp(attribute)); expect(() => addressToString(input)).toThrow(RegExp(attribute));
// (escaping regexp in JavaScript is a nightmare; ignore it) // (escaping regexp in JavaScript is a nightmare; ignore it)
} }
it("rejects an ID with $-signs in plugin name", () => { it("rejects an Address with $-signs in plugin name", () => {
expectRejection("pluginName", "widg$ets"); expectRejection("pluginName", "widg$ets");
}); });
it("rejects an ID with $-signs in repository name", () => { it("rejects an Address with $-signs in repository name", () => {
expectRejection("repositoryName", "megacorp$megawidget"); expectRejection("repositoryName", "megacorp$megawidget");
}); });
it("rejects an ID with $-signs in name", () => { it("rejects an Address with $-signs in id", () => {
expectRejection("name", "issue$123"); expectRejection("id", "issue$123");
}); });
}); });
describe("stringToID", () => { describe("stringToAddress", () => {
it("parses a simple ID-string", () => { it("parses a simple Address-string", () => {
const input = "widgets$megacorp/megawidget$issue#123"; const input = "widgets$megacorp/megawidget$issue#123";
const expected = { const expected = {
pluginName: "widgets", pluginName: "widgets",
repositoryName: "megacorp/megawidget", repositoryName: "megacorp/megawidget",
name: "issue#123", id: "issue#123",
}; };
expect(stringToID(input)).toEqual(expected); expect(stringToAddress(input)).toEqual(expected);
}); });
[0, 1, 3, 4].forEach((n) => { [0, 1, 3, 4].forEach((n) => {
it(`rejects an ID-string with ${n} occurrences of "\$"`, () => { it(`rejects an Address-string with ${n} occurrences of "\$"`, () => {
const dollars = Array(n + 1).join("$"); const dollars = Array(n + 1).join("$");
const input = `mega${dollars}corp`; const input = `mega${dollars}corp`;
expect(() => stringToID(input)).toThrow(/exactly two \$s/); expect(() => stringToAddress(input)).toThrow(/exactly two \$s/);
}); });
}); });
}); });
describe("stringToID and idToString interop", () => { describe("stringToAddress and addressToString interop", () => {
const examples = () => [ const examples = () => [
{ {
object: { object: {
pluginName: "widgets", pluginName: "widgets",
repositoryName: "megacorp/megawidget", repositoryName: "megacorp/megawidget",
name: "issue#123", id: "issue#123",
}, },
string: "widgets$megacorp/megawidget$issue#123", string: "widgets$megacorp/megawidget$issue#123",
}, },
]; ];
examples().forEach((example, index) => { examples().forEach((example, index) => {
describe(`for example at 0-index ${index}`, () => { describe(`for example at 0-index ${index}`, () => {
it("has stringToID a left identity for idToString", () => { it("has stringToAddress a left identity for addressToString", () => {
expect(stringToID(idToString(example.object))).toEqual( expect(stringToAddress(addressToString(example.object))).toEqual(
example.object example.object
); );
}); });
it("has stringToID a right identity for idToString", () => { it("has stringToAddress a right identity for addressToString", () => {
expect(idToString(stringToID(example.string))).toEqual( expect(addressToString(stringToAddress(example.string))).toEqual(
example.string example.string
); );
}); });