Skip to content

Commit

Permalink
feat: add support for yaml format as a string format
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenfiszel committed Jan 4, 2023
1 parent aaaece9 commit 5204e4a
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 13 deletions.
8 changes: 4 additions & 4 deletions frontend/src/lib/components/ArgInput.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@
bind:value={description}
placeholder="Field description"
/>
{#if type == 'string' && !contentEncoding && format != 'date-time'}
{#if type == 'string' && format != 'date-time'}
<StringTypeNarrowing bind:format bind:pattern bind:enum_ bind:contentEncoding />
{:else if type == 'object'}
<ObjectTypeNarrowing bind:format />
Expand Down Expand Up @@ -326,13 +326,13 @@
</select>
{:else if inputCat == 'date'}
<input {autofocus} class="inline-block" type="datetime-local" bind:value />
{:else if inputCat == 'sql'}
<div class="border rounded mb-4 w-full border-gray-700">
{:else if inputCat == 'sql' || inputCat == 'yaml'}
<div class="border mb-4 w-full border-gray-400">
<SimpleEditor
on:focus={() => dispatch('focus')}
on:blur={() => dispatch('blur')}
bind:this={editor}
lang="sql"
lang={inputCat}
bind:code={value}
class="few-lines-editor"
on:change={async () => {
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/lib/components/InputTransformForm.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
}
function isStaticTemplate(inputCat: InputCat) {
return inputCat === 'string' || inputCat === 'sql'
return inputCat === 'string' || inputCat === 'sql' || inputCat == 'yaml'
}
function connectProperty(rawValue: string) {
Expand Down
11 changes: 11 additions & 0 deletions frontend/src/lib/components/SimpleEditor.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@
import libStdContent from '$lib/es5.d.ts.txt?raw'
meditor.defineTheme('myTheme', {
base: 'vs',
inherit: true,
rules: [],
colors: {
'editorLineNumber.foreground': '#999',
'editorGutter.background': '#F9FAFB'
}
})
meditor.setTheme('myTheme')
languages.typescript.javascriptDefaults.setCompilerOptions({
target: languages.typescript.ScriptTarget.Latest,
allowNonTsExtensions: true,
Expand Down
39 changes: 31 additions & 8 deletions frontend/src/lib/components/StringTypeNarrowing.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
export let format: string | undefined
export let contentEncoding: 'base64' | 'binary' | undefined
let kind: 'none' | 'pattern' | 'enum' | 'resource' | 'format' | 'base64' = 'none'
let kind: 'none' | 'pattern' | 'enum' | 'resource' | 'format' | 'base64' = computeKind()
let patternStr: string = pattern ?? ''
let resource: string | undefined
Expand All @@ -18,7 +18,9 @@
'hostname',
'uri',
'uuid',
'ipv4'
'ipv4',
'yaml',
'sql'
// 'time',
// 'date',
// 'duration',
Expand All @@ -41,6 +43,25 @@
enum_ = undefined
}
}
function computeKind(): 'base64' | 'none' | 'pattern' | 'enum' | 'resource' | 'format' {
if (enum_ != undefined) {
return 'enum'
}
if (contentEncoding == 'base64') {
return 'base64'
}
if (pattern != undefined) {
return 'pattern'
}
if (format != undefined) {
if (format.startsWith('resource')) {
return 'resource'
}
return 'format'
}
return 'none'
}
</script>

<RadioButton
Expand Down Expand Up @@ -81,12 +102,14 @@
{:else if kind == 'enum'}
<label for="input" class="mb-2 text-gray-700 text-xs">
Enums
{#each enum_ || [] as e}
<div class="flex flex-row max-w-md">
<input id="input" type="text" bind:value={e} />
<Button size="sm" btnClasses="ml-6" on:click={() => remove(e)}>-</Button>
</div>
{/each}
<div class="flex flex-col gap-1">
{#each enum_ || [] as e}
<div class="flex flex-row max-w-md">
<input id="input" type="text" bind:value={e} />
<Button size="sm" btnClasses="ml-6" on:click={() => remove(e)}>-</Button>
</div>
{/each}
</div>
<div class="flex flex-row my-1">
<Button size="sm" on:click={add}>+</Button>
<Button variant="border" size="sm" btnClasses="ml-2" on:click={() => (enum_ = undefined)}>
Expand Down
2 changes: 2 additions & 0 deletions frontend/src/lib/editorUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ export function langToExt(lang: string): string {
return 'json'
case 'sql':
return 'sql'
case 'yaml':
return 'yaml'
case 'typescript':
return 'ts'
case 'python':
Expand Down
3 changes: 3 additions & 0 deletions frontend/src/lib/infer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ function argSigToJsonSchemaType(
} else if (t === 'sql') {
newS.type = 'string'
newS.format = 'sql'
} else if (t === 'yaml') {
newS.type = 'string'
newS.format = 'yaml'
} else if (t === 'bytes') {
newS.type = 'string'
newS.contentEncoding = 'base64'
Expand Down
3 changes: 3 additions & 0 deletions frontend/src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,7 @@ export type InputCat =
| 'resource-string'
| 'object'
| 'sql'
| 'yaml'

export function setInputCat(
type: string | undefined,
Expand All @@ -458,6 +459,8 @@ export function setInputCat(
return 'date'
} else if (type == 'string' && format == 'sql') {
return 'sql'
} else if (type == 'string' && format == 'yaml') {
return 'yaml'
} else if (type == 'string' && contentEncoding == 'base64') {
return 'base64'
} else if (type == 'string' && format?.startsWith('resource')) {
Expand Down

0 comments on commit 5204e4a

Please sign in to comment.