Skip to content

Commit 2eff4a4

Browse files
authored
add json formatting formatting and lang discrimination (#16)
1 parent a7cf5be commit 2eff4a4

File tree

6 files changed

+44
-8
lines changed

6 files changed

+44
-8
lines changed

cmd/wasm/functions.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,16 +181,25 @@ type IncompleteOverlayErrorMessage struct {
181181
}
182182

183183
func applyOverlayJSONPathIncomplete(result []*yaml.Node, node *yaml.Node) (string, error) {
184-
yamlResult, err := yaml.Marshal(result)
184+
var data interface{}
185+
if len(result) == 1 {
186+
data = result[0]
187+
} else {
188+
data = result
189+
}
190+
191+
yamlResult, err := yaml.Marshal(data)
185192
if err != nil {
186193
return "", err
187194
}
195+
188196
out, err := json.Marshal(IncompleteOverlayErrorMessage{
189197
Type: "incomplete",
190198
Line: node.Line,
191199
Col: node.Column,
192200
Result: string(yamlResult),
193201
})
202+
194203
return string(out), err
195204
}
196205

web/src/Playground.tsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
} from "react-resizable-panels";
2626
import posthog from "posthog-js";
2727
import { useDebounceCallback, useMediaQuery } from "usehooks-ts";
28+
import { formatDocument, guessDocumentLanguage } from "./lib/utils";
2829

2930
const Link = ({ children, href }: { children: ReactNode; href: string }) => (
3031
<a
@@ -59,6 +60,7 @@ function Playground() {
5960
const [ready, setReady] = useState(false);
6061

6162
const original = useRef(petstore);
63+
const originalLang = useRef<DocumentLanguage>("yaml");
6264
const changed = useRef("");
6365
const [changedLoading, setChangedLoading] = useState(false);
6466
const [applyOverlayMode, setApplyOverlayMode] = useState<
@@ -97,14 +99,14 @@ function Playground() {
9799
);
98100
if (response.type == "success") {
99101
setApplyOverlayMode("original+overlay");
100-
changed.current = response.result || "";
102+
changed.current = formatDocument(response.result);
101103
setError("");
102104
setOverlayMarkers([]);
103105
const info = await GetInfo(changed.current, false);
104106
tryHandlePageTitle(JSON.parse(info));
105107
} else if (response.type == "incomplete") {
106108
setApplyOverlayMode("jsonpathexplorer");
107-
changed.current = response.result || "";
109+
changed.current = formatDocument(response.result);
108110
setError("");
109111
setOverlayMarkers([]);
110112
} else if (response.type == "error") {
@@ -206,7 +208,7 @@ function Playground() {
206208
false,
207209
);
208210
if (changedNew.type == "success") {
209-
changed.current = changedNew.result;
211+
changed.current = formatDocument(changedNew.result);
210212
}
211213
} catch (e: unknown) {
212214
if (e instanceof Error) {
@@ -226,6 +228,7 @@ function Playground() {
226228
try {
227229
setResultLoading(true);
228230
original.current = value || "";
231+
originalLang.current = guessDocumentLanguage(original.current);
229232
const res = await CalculateOverlay(
230233
value || "",
231234
changed.current,
@@ -419,6 +422,7 @@ function Playground() {
419422
title="Original"
420423
index={0}
421424
maxOnClick={maxLayout}
425+
language={originalLang.current}
422426
/>
423427
</div>
424428
</Panel>
@@ -438,6 +442,7 @@ function Playground() {
438442
title={appliedPanelTitle}
439443
index={1}
440444
maxOnClick={maxLayout}
445+
language={originalLang.current}
441446
/>
442447
</div>
443448
</Panel>
@@ -453,6 +458,7 @@ function Playground() {
453458
title={"Overlay"}
454459
index={2}
455460
maxOnClick={maxLayout}
461+
language="yaml"
456462
/>
457463
</div>
458464
</Panel>

web/src/assets/wasm/lib.wasm

305 KB
Binary file not shown.

web/src/components/Editor.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export interface EditorComponentProps {
2828
value: string | undefined,
2929
ev: editor.IModelContentChangedEvent,
3030
) => void;
31+
language: DocumentLanguage;
3132
}
3233

3334
const minLoadingTime = 150;
@@ -216,7 +217,7 @@ export function Editor(props: EditorComponentProps) {
216217
originalModelPath={encodedTitle + "/original"}
217218
modifiedModelPath={encodedTitle + "/modified"}
218219
theme={"vscode-dark"}
219-
language="yaml"
220+
language={props.language}
220221
options={options}
221222
/>
222223
</div>

web/src/lib/utils.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,24 @@
1-
import { clsx, type ClassValue } from "clsx"
2-
import { twMerge } from "tailwind-merge"
1+
import { clsx, type ClassValue } from "clsx";
2+
import { twMerge } from "tailwind-merge";
33

44
export function cn(...inputs: ClassValue[]) {
5-
return twMerge(clsx(inputs))
5+
return twMerge(clsx(inputs));
6+
}
7+
8+
export function guessDocumentLanguage(content: string): DocumentLanguage {
9+
try {
10+
JSON.parse(content);
11+
return "json";
12+
} catch {
13+
return "yaml";
14+
}
15+
}
16+
17+
export function formatDocument(doc: string, indentWidth: number = 2): string {
18+
const docLang = guessDocumentLanguage(doc);
19+
20+
if (docLang === "json")
21+
return JSON.stringify(JSON.parse(doc), null, indentWidth);
22+
23+
return doc;
624
}

web/src/types.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,5 @@ declare class Go {
77
mem: DataView;
88
run(instance: WebAssembly.Instance): Promise<void>;
99
}
10+
11+
declare type DocumentLanguage = "json" | "yaml";

0 commit comments

Comments
 (0)