mirror of
https://github.com/windmill-labs/windmill.git
synced 2026-10-20 16:03:21 +00:00
feat(go): Resource Types
This commit is contained in:
@@ -7,7 +7,7 @@ use gosyn::{
|
||||
use itertools::Itertools;
|
||||
|
||||
use regex::Regex;
|
||||
use windmill_parser::{Arg, MainArgSignature, ObjectProperty, Typ};
|
||||
use windmill_parser::{to_snake_case, Arg, MainArgSignature, ObjectProperty, Typ};
|
||||
|
||||
lazy_static::lazy_static! {
|
||||
pub static ref REQUIRE_PARSE: Regex = Regex::new(r"//require (.*)\n").unwrap();
|
||||
@@ -99,7 +99,7 @@ fn parse_go_typ(typ: &Expression) -> (Option<String>, Typ) {
|
||||
"int64" => Typ::Int,
|
||||
"string" => Typ::Str(None),
|
||||
"bool" => Typ::Bool,
|
||||
_ => Typ::Unknown,
|
||||
_ => Typ::Resource(to_snake_case(name.as_ref())),
|
||||
},
|
||||
),
|
||||
Expression::TypeSlice(slice_type) => {
|
||||
@@ -163,6 +163,55 @@ mod tests {
|
||||
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_parse_go_sig_resources() -> anyhow::Result<()> {
|
||||
let code = r#"
|
||||
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type MyResource struct {
|
||||
A string `json:"a"`
|
||||
B *int64 `json:"b,omitempty"`
|
||||
C []string `json:"c,omitempty"`
|
||||
}
|
||||
|
||||
func main(x int, a MyResource) {
|
||||
fmt.Println("hello world")
|
||||
}
|
||||
|
||||
"#;
|
||||
assert_eq!(
|
||||
parse_go_sig(code)?,
|
||||
MainArgSignature {
|
||||
star_args: false,
|
||||
star_kwargs: false,
|
||||
args: vec![
|
||||
Arg {
|
||||
otyp: Some("int".to_string()),
|
||||
name: "x".to_string(),
|
||||
typ: Typ::Int,
|
||||
has_default: false,
|
||||
default: None,
|
||||
oidx: None
|
||||
},
|
||||
Arg {
|
||||
otyp: Some("MyResource".to_string()),
|
||||
name: "a".to_string(),
|
||||
typ: Typ::Resource("my_resource".into()),
|
||||
default: None,
|
||||
has_default: false,
|
||||
oidx: None
|
||||
},
|
||||
],
|
||||
no_main_func: Some(false),
|
||||
has_preprocessor: None
|
||||
}
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
#[test]
|
||||
fn test_parse_go_sig() -> anyhow::Result<()> {
|
||||
let code = r#"
|
||||
|
||||
Generated
+778
-41
File diff suppressed because it is too large
Load Diff
@@ -81,12 +81,12 @@
|
||||
"dependencies": {
|
||||
"@aws-crypto/sha256-js": "^4.0.0",
|
||||
"@codingame/monaco-vscode-configuration-service-override": "~16.1.1",
|
||||
"@codingame/monaco-vscode-editor-api": "~16.1.1",
|
||||
"@codingame/monaco-vscode-standalone-css-language-features": "~16.1.1",
|
||||
"@codingame/monaco-vscode-standalone-html-language-features": "^16.1.1",
|
||||
"@codingame/monaco-vscode-standalone-json-language-features": "~16.1.1",
|
||||
"@codingame/monaco-vscode-standalone-languages": "~16.1.1",
|
||||
"@codingame/monaco-vscode-standalone-typescript-language-features": "~16.1.1",
|
||||
"@codingame/monaco-vscode-editor-api": "~16.1.1",
|
||||
"@json2csv/plainjs": "^7.0.6",
|
||||
"@leeoniya/ufuzzy": "^1.0.8",
|
||||
"@popperjs/core": "^2.11.6",
|
||||
@@ -124,6 +124,8 @@
|
||||
"p-limit": "^6.1.0",
|
||||
"panzoom": "^9.4.3",
|
||||
"pdfjs-dist": "4.8.69",
|
||||
"quicktype": "^23.2.6",
|
||||
"quicktype-core": "^23.2.6",
|
||||
"quill": "^1.3.7",
|
||||
"rehype-github-alerts": "^3.0.0",
|
||||
"rehype-raw": "^7.0.0",
|
||||
|
||||
@@ -283,6 +283,13 @@
|
||||
}
|
||||
}
|
||||
|
||||
export function insertAtCurrentLine(code: string): void {
|
||||
if (editor) {
|
||||
console.log();
|
||||
insertAtLine(code, editor.getPosition()?.lineNumber ?? 0);
|
||||
}
|
||||
}
|
||||
|
||||
export function arrowDown(): void {
|
||||
if (editor) {
|
||||
let pos = editor.getPosition()
|
||||
|
||||
@@ -24,6 +24,7 @@
|
||||
import { getScriptByPath, scriptLangToEditorLang } from '$lib/scripts'
|
||||
import Toggle from './Toggle.svelte'
|
||||
|
||||
|
||||
import {
|
||||
DiffIcon,
|
||||
DollarSign,
|
||||
@@ -46,6 +47,12 @@
|
||||
import ResourceEditorDrawer from './ResourceEditorDrawer.svelte'
|
||||
import type { EditorBarUi } from './custom_ui'
|
||||
import EditorSettings from './EditorSettings.svelte'
|
||||
import {
|
||||
quicktype,
|
||||
InputData,
|
||||
JSONSchemaInput,
|
||||
FetchingJSONSchemaStore
|
||||
} from "quicktype-core";
|
||||
|
||||
export let lang: SupportedLanguage | 'bunnative' | undefined
|
||||
export let editor: Editor | undefined
|
||||
@@ -136,7 +143,8 @@
|
||||
$: showResourceTypePicker =
|
||||
['typescript', 'javascript'].includes(scriptLangToEditorLang(lang)) ||
|
||||
lang === 'python3' ||
|
||||
lang === 'php'
|
||||
lang === 'php' ||
|
||||
lang === 'go'
|
||||
|
||||
let codeViewer: Drawer
|
||||
let codeObj: { language: SupportedLanguage; content: string } | undefined = undefined
|
||||
@@ -207,6 +215,26 @@
|
||||
return rec(schema.properties, true)
|
||||
}
|
||||
|
||||
|
||||
async function quicktypeJSONSchema(targetLanguage, typeName, jsonSchemaString) {
|
||||
const schemaInput = new JSONSchemaInput(new FetchingJSONSchemaStore());
|
||||
|
||||
// We could add multiple schemas for multiple types,
|
||||
// but here we're just making one type from JSON schema.
|
||||
await schemaInput.addSource({ name: typeName, schema: jsonSchemaString });
|
||||
|
||||
const inputData = new InputData();
|
||||
inputData.addInput(schemaInput);
|
||||
|
||||
return await quicktype({
|
||||
inputData,
|
||||
lang: targetLanguage,
|
||||
|
||||
rendererOptions: {
|
||||
"just-types": "Plain types only"
|
||||
}
|
||||
});
|
||||
}
|
||||
async function resourceTypePickCallback(name: string) {
|
||||
if (!editor) return
|
||||
const resourceType = await ResourceService.getResourceType({
|
||||
@@ -228,6 +256,11 @@
|
||||
editor.insertAtCursor(`if (!class_exists('${rtName}')) {\nclass ${rtName} {\n${phpSchema}\n`)
|
||||
editor.backspace()
|
||||
editor.insertAtCursor('}')
|
||||
} else if (lang === 'go') {
|
||||
const { lines: lines } = await quicktypeJSONSchema("go", name, JSON.stringify(resourceType.schema));
|
||||
editor.insertAtCurrentLine(lines.join("\n"))
|
||||
|
||||
|
||||
} else {
|
||||
const tsSchema = compile(resourceType.schema as any)
|
||||
editor.insertAtCursor(`type ${toCamel(capitalize(name))} = ${tsSchema}`)
|
||||
|
||||
Reference in New Issue
Block a user