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
export type ID = {
pluginName: string,
export type Address = {
repositoryName: string,
name: string,
pluginName: string,
id: string,
};
export type GraphNode<T> = {
id: ID,
edges: ID[],
export type Node<T> = {
address: Address,
edges: Address[],
payload: T,
};
export type GraphEdge<T> = {
id: ID,
sourceId: ID,
destId: ID,
export type Edge<T> = {
address: Address,
sourceId: Address,
destId: Address,
weight: number,
payload: T,
};
export type Graph = {
nodes: {[stringID: string]: GraphNode<mixed>},
edges: {[stringID: string]: GraphEdge<mixed>},
nodes: {[stringAddress: string]: Node<mixed>},
edges: {[stringAddress: string]: Edge<mixed>},
};
export function idToString(id: ID) {
if (id.pluginName.includes("$")) {
const escaped = JSON.stringify(id.pluginName);
throw new Error(`id.pluginName must not include "\$": ${escaped}`);
export function addressToString(address: Address) {
if (address.pluginName.includes("$")) {
const escaped = JSON.stringify(address.pluginName);
throw new Error(`address.pluginName must not include "\$": ${escaped}`);
}
if (id.repositoryName.includes("$")) {
const escaped = JSON.stringify(id.repositoryName);
throw new Error(`id.repositoryName must not include "\$": ${escaped}`);
if (address.repositoryName.includes("$")) {
const escaped = JSON.stringify(address.repositoryName);
throw new Error(`address.repositoryName must not include "\$": ${escaped}`);
}
if (id.name.includes("$")) {
const escaped = JSON.stringify(id.name);
throw new Error(`id.name must not include "\$": ${escaped}`);
if (address.id.includes("$")) {
const escaped = JSON.stringify(address.id);
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("$");
if (parts.length !== 3) {
const escaped = JSON.stringify(string);
@ -50,6 +50,6 @@ export function stringToID(string: string) {
return {
pluginName: parts[0],
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("idToString", () => {
const makeSimpleID = () => ({
describe("addressToString", () => {
const makeSimpleAddress = () => ({
pluginName: "widgets",
repositoryName: "megacorp/megawidget",
name: "issue#123",
id: "issue#123",
});
it("stringifies a simple ID", () => {
const input = makeSimpleID();
it("stringifies a simple Address", () => {
const input = makeSimpleAddress();
const expected = "widgets$megacorp/megawidget$issue#123";
expect(idToString(input)).toEqual(expected);
expect(addressToString(input)).toEqual(expected);
});
function expectRejection(attribute, value) {
const input = {...makeSimpleID(), [attribute]: value};
expect(() => idToString(input)).toThrow(RegExp(attribute));
const input = {...makeSimpleAddress(), [attribute]: value};
expect(() => addressToString(input)).toThrow(RegExp(attribute));
// (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");
});
it("rejects an ID with $-signs in repository name", () => {
it("rejects an Address with $-signs in repository name", () => {
expectRejection("repositoryName", "megacorp$megawidget");
});
it("rejects an ID with $-signs in name", () => {
expectRejection("name", "issue$123");
it("rejects an Address with $-signs in id", () => {
expectRejection("id", "issue$123");
});
});
describe("stringToID", () => {
it("parses a simple ID-string", () => {
describe("stringToAddress", () => {
it("parses a simple Address-string", () => {
const input = "widgets$megacorp/megawidget$issue#123";
const expected = {
pluginName: "widgets",
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) => {
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 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 = () => [
{
object: {
pluginName: "widgets",
repositoryName: "megacorp/megawidget",
name: "issue#123",
id: "issue#123",
},
string: "widgets$megacorp/megawidget$issue#123",
},
];
examples().forEach((example, index) => {
describe(`for example at 0-index ${index}`, () => {
it("has stringToID a left identity for idToString", () => {
expect(stringToID(idToString(example.object))).toEqual(
it("has stringToAddress a left identity for addressToString", () => {
expect(stringToAddress(addressToString(example.object))).toEqual(
example.object
);
});
it("has stringToID a right identity for idToString", () => {
expect(idToString(stringToID(example.string))).toEqual(
it("has stringToAddress a right identity for addressToString", () => {
expect(addressToString(stringToAddress(example.string))).toEqual(
example.string
);
});