Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion cmd/wasm/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,16 +181,25 @@ type IncompleteOverlayErrorMessage struct {
}

func applyOverlayJSONPathIncomplete(result []*yaml.Node, node *yaml.Node) (string, error) {
yamlResult, err := yaml.Marshal(result)
var data interface{}
if len(result) == 1 {
data = result[0]
} else {
data = result
}

yamlResult, err := yaml.Marshal(data)
if err != nil {
return "", err
}

out, err := json.Marshal(IncompleteOverlayErrorMessage{
Type: "incomplete",
Line: node.Line,
Col: node.Column,
Result: string(yamlResult),
})

return string(out), err
}

Expand Down
12 changes: 9 additions & 3 deletions web/src/Playground.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
} from "react-resizable-panels";
import posthog from "posthog-js";
import { useDebounceCallback, useMediaQuery } from "usehooks-ts";
import { formatDocument, guessDocumentLanguage } from "./lib/utils";

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

const original = useRef(petstore);
const originalLang = useRef<DocumentLanguage>("yaml");
const changed = useRef("");
const [changedLoading, setChangedLoading] = useState(false);
const [applyOverlayMode, setApplyOverlayMode] = useState<
Expand Down Expand Up @@ -97,14 +99,14 @@ function Playground() {
);
if (response.type == "success") {
setApplyOverlayMode("original+overlay");
changed.current = response.result || "";
changed.current = formatDocument(response.result);
setError("");
setOverlayMarkers([]);
const info = await GetInfo(changed.current, false);
tryHandlePageTitle(JSON.parse(info));
} else if (response.type == "incomplete") {
setApplyOverlayMode("jsonpathexplorer");
changed.current = response.result || "";
changed.current = formatDocument(response.result);
setError("");
setOverlayMarkers([]);
} else if (response.type == "error") {
Expand Down Expand Up @@ -206,7 +208,7 @@ function Playground() {
false,
);
if (changedNew.type == "success") {
changed.current = changedNew.result;
changed.current = formatDocument(changedNew.result);
}
} catch (e: unknown) {
if (e instanceof Error) {
Expand All @@ -226,6 +228,7 @@ function Playground() {
try {
setResultLoading(true);
original.current = value || "";
originalLang.current = guessDocumentLanguage(original.current);
const res = await CalculateOverlay(
value || "",
changed.current,
Expand Down Expand Up @@ -419,6 +422,7 @@ function Playground() {
title="Original"
index={0}
maxOnClick={maxLayout}
language={originalLang.current}
/>
</div>
</Panel>
Expand All @@ -438,6 +442,7 @@ function Playground() {
title={appliedPanelTitle}
index={1}
maxOnClick={maxLayout}
language={originalLang.current}
/>
</div>
</Panel>
Expand All @@ -453,6 +458,7 @@ function Playground() {
title={"Overlay"}
index={2}
maxOnClick={maxLayout}
language="yaml"
/>
</div>
</Panel>
Expand Down
Binary file modified web/src/assets/wasm/lib.wasm
Binary file not shown.
3 changes: 2 additions & 1 deletion web/src/components/Editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export interface EditorComponentProps {
value: string | undefined,
ev: editor.IModelContentChangedEvent,
) => void;
language: DocumentLanguage;
}

const minLoadingTime = 150;
Expand Down Expand Up @@ -216,7 +217,7 @@ export function Editor(props: EditorComponentProps) {
originalModelPath={encodedTitle + "/original"}
modifiedModelPath={encodedTitle + "/modified"}
theme={"vscode-dark"}
language="yaml"
language={props.language}
options={options}
/>
</div>
Expand Down
24 changes: 21 additions & 3 deletions web/src/lib/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
import { clsx, type ClassValue } from "clsx"
import { twMerge } from "tailwind-merge"
import { clsx, type ClassValue } from "clsx";
import { twMerge } from "tailwind-merge";

export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs))
return twMerge(clsx(inputs));
}

export function guessDocumentLanguage(content: string): DocumentLanguage {
try {
JSON.parse(content);
return "json";
} catch {
return "yaml";
}
}

export function formatDocument(doc: string, indentWidth: number = 2): string {
const docLang = guessDocumentLanguage(doc);

if (docLang === "json")
return JSON.stringify(JSON.parse(doc), null, indentWidth);

return doc;
}
2 changes: 2 additions & 0 deletions web/src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ declare class Go {
mem: DataView;
run(instance: WebAssembly.Instance): Promise<void>;
}

declare type DocumentLanguage = "json" | "yaml";
Loading