Add JSON serialization support for weights (#1147)

Test plan: New unit tests added.
This commit is contained in:
Dandelion Mané 2019-05-20 14:12:12 +03:00 committed by GitHub
parent 03a8ab679b
commit f6a4042342
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 96 additions and 0 deletions

View File

@ -1,6 +1,8 @@
// @flow // @flow
import * as MapUtil from "../util/map";
import {type NodeAddressT, type EdgeAddressT} from "../core/graph"; import {type NodeAddressT, type EdgeAddressT} from "../core/graph";
import {toCompat, fromCompat, type Compatible} from "../util/compat";
/** /**
* Represents the weight for a particular Node (or NodeType). * Represents the weight for a particular Node (or NodeType).
@ -45,3 +47,30 @@ export function defaultWeights(): Weights {
nodeManualWeights: new Map(), nodeManualWeights: new Map(),
}; };
} }
export type WeightsJSON = Compatible<{|
+nodeTypeWeights: {[NodeAddressT]: NodeWeight},
+edgeTypeWeights: {[EdgeAddressT]: EdgeWeight},
+nodeManualWeights: {[NodeAddressT]: NodeWeight},
|}>;
const COMPAT_INFO = {type: "sourcecred/weights", version: "0.1.0"};
export function toJSON(weights: Weights): WeightsJSON {
return toCompat(COMPAT_INFO, {
nodeTypeWeights: MapUtil.toObject(weights.nodeTypeWeights),
edgeTypeWeights: MapUtil.toObject(weights.edgeTypeWeights),
nodeManualWeights: MapUtil.toObject(weights.nodeManualWeights),
});
}
export function fromJSON(json: WeightsJSON): Weights {
const {nodeTypeWeights, edgeTypeWeights, nodeManualWeights} = fromCompat(
COMPAT_INFO,
json
);
return {
nodeTypeWeights: MapUtil.fromObject(nodeTypeWeights),
edgeTypeWeights: MapUtil.fromObject(edgeTypeWeights),
nodeManualWeights: MapUtil.fromObject(nodeManualWeights),
};
}

View File

@ -0,0 +1,67 @@
// @flow
import stringify from "json-stable-stringify";
import {NodeAddress, EdgeAddress} from "../core/graph";
import {toJSON, fromJSON, defaultWeights} from "./weights";
describe("analysis/weights", () => {
describe("toJSON/fromJSON", () => {
it("works for the default weights", () => {
const weights = defaultWeights();
const json = toJSON(weights);
const jsonString = stringify(json, {space: 4});
expect(jsonString).toMatchInlineSnapshot(`
"[
{
\\"type\\": \\"sourcecred/weights\\",
\\"version\\": \\"0.1.0\\"
},
{
\\"edgeTypeWeights\\": {
},
\\"nodeManualWeights\\": {
},
\\"nodeTypeWeights\\": {
}
}
]"
`);
expect(weights).toEqual(fromJSON(json));
});
it("works for non-default weights", () => {
const weights = defaultWeights();
weights.nodeTypeWeights.set(NodeAddress.empty, 32);
weights.edgeTypeWeights.set(EdgeAddress.empty, {
forwards: 7,
backwards: 9,
});
weights.nodeManualWeights.set(NodeAddress.fromParts(["foo"]), 42);
const json = toJSON(weights);
const jsonString = stringify(json, {space: 4});
expect(jsonString).toMatchInlineSnapshot(`
"[
{
\\"type\\": \\"sourcecred/weights\\",
\\"version\\": \\"0.1.0\\"
},
{
\\"edgeTypeWeights\\": {
\\"E\\\\u0000\\": {
\\"backwards\\": 9,
\\"forwards\\": 7
}
},
\\"nodeManualWeights\\": {
\\"N\\\\u0000foo\\\\u0000\\": 42
},
\\"nodeTypeWeights\\": {
\\"N\\\\u0000\\": 32
}
}
]"
`);
expect(weights).toEqual(fromJSON(json));
});
});
});