Skip to content

Commit 722277e

Browse files
authored
Add Java Pair goody (#160)
Hypothetically we could use [`javafx.util.Pair`](https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html) but I don't love that the fields are named key and value. So let's reinvent the wheel!
1 parent b325331 commit 722277e

File tree

14 files changed

+68
-29
lines changed

14 files changed

+68
-29
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package pair;
2+
3+
public record Pair<TFirst, TSecond>(TFirst first, TSecond second) {}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"name": "Pair"
3+
}

tools/adventure-pack/src/app/__tests__/__snapshots__/equip-test.ts.snap

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`App can equip single goody: Java Pair 1`] = `
4+
"////////////////////////// BEGIN ADVENTURE PACK CODE ///////////////////////////
5+
// Adventure Pack commit fake-commit-hash
6+
// Running at: https://example.com/
7+
8+
record Pair<TFirst, TSecond>(TFirst first, TSecond second) {}
9+
10+
/////////////////////////// END ADVENTURE PACK CODE ////////////////////////////"
11+
`;
12+
313
exports[`App can equip single goody: Java UnionFind 1`] = `
414
"////////////////////////// BEGIN ADVENTURE PACK CODE ///////////////////////////
515
// Adventure Pack commit fake-commit-hash

tools/adventure-pack/src/app/__tests__/__snapshots__/render-test.ts.snap

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`App can render goody: Java Pair 1`] = `
4+
"package pair;
5+
6+
record Pair<TFirst, TSecond>(TFirst first, TSecond second) {}"
7+
`;
8+
39
exports[`App can render goody: Java UnionFind 1`] = `
410
"package union_find;
511

tools/adventure-pack/src/app/__tests__/render-test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { describe, expect, it } from "@jest/globals";
33
import { LANGUAGE_NAMES } from "../constants";
44
import type { Language } from "../Language";
55
import { readAllGoodies } from "../../scripts/package-goodies/readAllGoodies";
6-
import { goodyToText } from "../goodyToText";
6+
import { stringifyGoody } from "../stringifyGoody";
77

88
describe("App", () => {
99
it("can render goody", async () => {
@@ -12,7 +12,7 @@ describe("App", () => {
1212
for (const language of Object.keys(goodiesByLanguage) as Language[]) {
1313
const goodies = goodiesByLanguage[language];
1414
for (const goody of Object.values(goodies)) {
15-
const code = goodyToText(goody);
15+
const code = stringifyGoody(goody);
1616

1717
expect(code).toMatchSnapshot(
1818
`${LANGUAGE_NAMES[language]} ${goody.name}`,

tools/adventure-pack/src/app/components/GoodyCard.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from "react";
22

33
import type { Goody } from "../Goody";
4-
import { goodyToText } from "../goodyToText";
4+
import { stringifyGoody } from "../stringifyGoody";
55
import { HighlightedCode } from "./HighlightedCode";
66

77
type Props = {
@@ -13,7 +13,7 @@ export function GoodyCard({ goody }: Props) {
1313
<div>
1414
<h2>{goody.name}</h2>
1515
<HighlightedCode language={goody.language}>
16-
{goodyToText(goody)}
16+
{stringifyGoody(goody)}
1717
</HighlightedCode>
1818
</div>
1919
);

tools/adventure-pack/src/app/mergeCode.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,11 @@ export function mergeCode({
127127
for (const [moduleName, interfaceDeclarations] of Object.entries(
128128
goody.moduleDeclarations,
129129
)) {
130-
for (const [interfaceName, codeGroups] of Object.entries(
130+
for (const [interfaceName, codeSections] of Object.entries(
131131
interfaceDeclarations,
132132
)) {
133133
((mergedDeclarations[moduleName] ??= {})[interfaceName] ??=
134-
[]).push(...codeGroups);
134+
[]).push(...codeSections);
135135
}
136136
}
137137
}

tools/adventure-pack/src/app/mergeJavaCode.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,18 @@ import type { JavaGoody } from "./parsers/javaGoodyParser";
66
const ADVENTURE_PACK_CLASS_NAME = "AP";
77

88
export function mergeJavaCode(goodies: Iterable<ReadonlyDeep<JavaGoody>>) {
9-
const classes: Record<string, { code: string[]; modifiers: Set<string> }> =
10-
{};
9+
const classes: Record<string, { code: string[]; declaration: string }> = {};
1110
for (const goody of goodies) {
1211
for (const className of Object.keys(goody.codeByClass)) {
1312
invariant(
1413
classes[className] == null || className === ADVENTURE_PACK_CLASS_NAME,
1514
`Only the ${ADVENTURE_PACK_CLASS_NAME} class can exist in multiple goodies!`,
1615
);
1716

18-
classes[className] ??= { code: [], modifiers: new Set() };
19-
for (const modifier of goody.codeByClass[className].modifiers) {
20-
classes[className].modifiers.add(modifier);
21-
}
17+
classes[className] ??= {
18+
code: [],
19+
declaration: goody.codeByClass[className].declaration,
20+
};
2221
classes[className].code.push(goody.codeByClass[className].code);
2322
}
2423
}
@@ -32,8 +31,13 @@ export function mergeJavaCode(goodies: Iterable<ReadonlyDeep<JavaGoody>>) {
3231

3332
const res: string[] = [];
3433
for (const className of Object.keys(classes)) {
34+
const classData = classes[className];
35+
const codeSections = classData.code.filter(Boolean);
36+
3537
res.push(
36-
`${[...classes[className].modifiers, "class", className].join(" ")} {\n${classes[className].code.map((section) => section + "\n").join("\n")}}`,
38+
codeSections.length > 0
39+
? `${classData.declaration}\n${codeSections.map((codeSection) => codeSection + "\n").join("\n")}}`
40+
: classData.declaration + "}",
3741
);
3842
}
3943

tools/adventure-pack/src/app/parsers/javaGoodyParser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export const javaGoodyParser = goodyBaseParser
1111
z
1212
.object({
1313
code: z.string(),
14-
modifiers: z.array(nonBlankStringParser),
14+
declaration: nonBlankStringParser,
1515
})
1616
.strict(),
1717
),

tools/adventure-pack/src/app/sortTypeScriptModuleAndInterfaceDeclarations.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ export function sortTypeScriptModuleAndInterfaceDeclarations(
1212
): Record<string, Record<string, string[]>> {
1313
return sortObjectKeysRecursive(
1414
mapObjectValues(moduleDeclarations, (interfaceDeclarations) =>
15-
mapObjectValues(interfaceDeclarations, (codeGroups) =>
16-
[...codeGroups].sort(compareStringsCaseInsensitive),
15+
mapObjectValues(interfaceDeclarations, (codeSections) =>
16+
[...codeSections].sort(compareStringsCaseInsensitive),
1717
),
1818
),
1919
compareStringsCaseInsensitive,

0 commit comments

Comments
 (0)