Skip to content

Commit

Permalink
Merge pull request #369 from AykutSarac/feat/go-struct-generator
Browse files Browse the repository at this point in the history
feat: add go struct generator
  • Loading branch information
AykutSarac authored Dec 17, 2023
2 parents cf733f7 + da55330 commit 8366489
Show file tree
Hide file tree
Showing 4 changed files with 459 additions and 6 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"allotment": "^1.19.3",
"axios": "^1.5.0",
"dayjs": "^1.11.10",
"gofmt.js": "^0.0.2",
"html-to-image": "^1.11.11",
"jq-in-the-browser": "^0.7.2",
"jq-web": "^0.5.1",
Expand Down
38 changes: 32 additions & 6 deletions src/containers/Modals/TypeModal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,55 @@ import { Stack, Modal, ModalProps, ScrollArea, Select } from "@mantine/core";
import { Prism } from "@mantine/prism";
import vsDark from "prism-react-renderer/themes/vsDark";
import vsLight from "prism-react-renderer/themes/vsLight";
import useJsonQuery from "src/hooks/useJsonQuery";
import useJson from "src/store/useJson";

enum Language {
TypeScript = "typescript",
Go = "go",
}

const typeOptions = [
{
label: "TypeScript",
value: "typescript",
value: Language.TypeScript,
},
{
label: "Go",
value: Language.Go,
},
];

export const TypeModal: React.FC<ModalProps> = ({ opened, onClose }) => {
const { getJsonType } = useJsonQuery();
const getJson = useJson(state => state.getJson);
const [type, setType] = React.useState("");
const [selectedType, setSelectedType] = React.useState<Language>(Language.TypeScript);

React.useEffect(() => {
if (opened) {
setType(getJsonType());
if (selectedType === Language.TypeScript) {
import("json-to-ts").then(jts => {
const types = jts.default(JSON.parse(getJson()));
setType(types.flat().join("\n\n"));
});
} else if (selectedType === Language.Go) {
import("src/lib/utils/json2go").then(jtg => {
import("gofmt.js").then(gofmt => {
const types = jtg.default(getJson());
setType(gofmt.default(types.go));
});
});
}
}
}, [getJsonType, opened]);
}, [getJson, opened, selectedType]);

return (
<Modal title="Generate Types" size="auto" opened={opened} onClose={onClose} centered>
<Stack py="sm">
<Select value="typescript" data={typeOptions} />
<Select
value={selectedType}
data={typeOptions}
onChange={e => setSelectedType(e as Language)}
/>
<ScrollArea>
<Prism
miw={350}
Expand Down
Loading

0 comments on commit 8366489

Please sign in to comment.