5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-08 02:50:00 +08:00
wails/v3/internal/generator/render/templates/models.js.tmpl
2025-02-26 16:57:16 +01:00

208 lines
7.6 KiB
Cheetah

{{$module := .}}
{{- $runtime := $module.Runtime}}
{{- $models := (fixext $module.ModelsFile)}}
{{- $useInterfaces := .UseInterfaces}}
{{- $imports := $module.Imports -}}
// @ts-check
// Cynhyrchwyd y ffeil hon yn awtomatig. PEIDIWCH Â MODIWL
// This file is automatically generated. DO NOT EDIT
{{if not $useInterfaces}}
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore: Unused imports
import { Create as $Create } from "{{js $runtime}}";
{{end -}}
{{range $imports.External}}
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore: Unused imports
import * as {{jsimport .}} from "{{js .RelPath}}/{{js $models}}";
{{- end}}{{if $imports.External}}
{{end}}
{{- range $model := .Models}}
{{- $info := modelinfo $model $useInterfaces }}
{{- $template := $info.Template }}
{{- if or $template.ParamList (hasdoc $model.Decl.Doc) (hasdoc $model.Doc) $info.IsEnum $info.IsTypeAlias $info.IsInterface}}
/**
{{- if hasdoc $model.Decl.Doc}}
{{- jsdoc $model.Decl.Doc.Text ""}}{{if hasdoc $model.Doc}}
*{{end}}
{{- end}}
{{- if hasdoc $model.Doc}}
{{- jsdoc $model.Doc.Text ""}}
{{- end}}
{{- if and $template.ParamList (not $info.IsClassAlias)}}
* @template {{$template.Params}}
{{- end}}
{{- if $info.IsEnum}}
* @readonly
* @enum { {{- $module.JSType $model.Type -}} }
{{- else if $info.IsTypeAlias}}
* @typedef { {{- $module.JSType $model.Type -}} } {{jsid $model.Name}}
{{- else if $info.IsInterface}}
{{- if isjsdocobj $model}}
* @typedef {Object} {{jsid $model.Name}}
{{- range $i, $decl := $model.Fields}}{{range $j, $field := $decl}}
* @property { {{- $module.JSFieldType $field.StructField -}} }
{{- if $field.Optional}} [{{else}} {{end}}{{$field.JsonName}}{{if $field.Optional}}]{{end}}
{{- if hasdoc $field.Decl.Doc}} - {{jsdocline $field.Decl.Doc.Text}}{{end}}
{{- end}}{{end}}
{{- else}}
* @typedef { {
{{- range $i, $decl := $model.Fields}}{{range $j, $field := $decl}}
* "{{js $field.JsonName}}"{{if $field.Optional}}?{{end}}: {{$module.JSFieldType $field.StructField}},
{{- end}}{{end}}
* } } {{jsid $model.Name}}
{{- end}}
{{- end}}
*/
{{- end}}
{{- if $info.HasValues}}
{{- if not $info.IsEnum}}
/**
* Predefined constants for type {{jsid $model.Name}}.
* @namespace
*/
{{- end}}
export const {{jsid $model.Name}} = {
{{- if $info.IsEnum}}
/**
* The Go zero value for the underlying type of the enum.
*/
$zero: {{$module.JSDefault $model.Type false}},
{{end}}
{{- range $i, $decl := $model.Values}}{{range $j, $spec := $decl}}{{range $k, $value := $spec}}
{{- if and (ne $i 0) (eq $j 0) (eq $k 0)}}
{{end}}
{{- if or (and (eq $j 0) (eq $k 0) (hasdoc $value.Decl.Doc)) (and (eq $k 0) (hasdoc $value.Spec.Doc))}}
{{- if gt $j 0}}
{{end}}
/**
{{- if and (eq $j 0) (eq $k 0) (hasdoc $value.Decl.Doc)}}
{{- jsdoc $value.Decl.Doc.Text " "}}{{if and (eq $k 0) (hasdoc $value.Spec.Doc)}}
*{{end}}
{{- end}}
{{- if and (eq $k 0) (hasdoc $value.Spec.Doc)}}
{{- jsdoc $value.Spec.Doc.Text " "}}
{{- end}}
*/
{{- end}}
{{jsid $value.Name}}: {{jsvalue $value.Value}},
{{- end}}{{end}}{{end}}
};
{{else if $info.IsClassAlias}}
export const {{jsid $model.Name}} = {{if istpalias $model.Type -}}
{{$module.JSType (unalias $model.Type).Origin}};
{{- else -}}
{{$module.JSType $model.Type.Origin}};
{{- end}}
/**
{{- if hasdoc $model.Decl.Doc}}
{{- jsdoc $model.Decl.Doc.Text ""}}{{if hasdoc $model.Doc}}
*{{end}}
{{- end}}
{{- if hasdoc $model.Doc}}
{{- jsdoc $model.Doc.Text ""}}
{{- end}}
{{- if $template.ParamList}}
* @template {{$template.Params}}
{{- end}}
* @typedef { {{- $module.JSType $model.Type -}} } {{jsid $model.Name}}
*/
{{else if and $info.IsClass}}
export class {{jsid $model.Name}} {
/**
* Creates a new {{jsid $model.Name}} instance.
* @param {Partial<{{jsid $model.Name}}{{$template.ParamList}}>} [$$source = {}] - The source object to create the {{jsid $model.Name}}.
*/
constructor($$source = {}) {
{{- range $decl := $model.Fields}}{{range $j, $field := $decl}}
{{- /*
In JS we need to set all properties explicitly
because JSDoc has no support for arbitrary property names yet.
See https://github.com/jsdoc/jsdoc/issues/1468
For optional fields we make the initialization code unreachable
and cast the false condition to any to prevent any complaint from Typescript.
*/}}
if ({{if $field.Optional}}/** @type {any} */(false){{else}}!("{{js $field.JsonName}}" in $$source){{end}}) {
/**
{{- if and (eq $j 0) (hasdoc $field.Decl.Doc)}}
{{- jsdoc $field.Decl.Doc.Text " "}}
{{- end}}
* @member
* @type { {{- $module.JSFieldType $field.StructField}}{{if $field.Optional}} | undefined{{end -}} }
*/
this["{{js $field.JsonName}}"] = {{if $field.Optional}}undefined{{else}}{{$module.JSDefault $field.Type $field.Quoted}}{{end}};
}
{{- end}}{{end}}
Object.assign(this, $$source);
}
/**
{{- if $template.ParamList}}
* Given creation functions for each type parameter,
* returns a creation function for a concrete instance
* of the generic class {{jsid $model.Name}}.
{{- range $i, $param := $model.TypeParams}}
{{- $param = (typeparam $i $param)}}
* @template [{{$param}}=any]
{{- end}}
{{- range $i, $param := $model.TypeParams}}
{{- $param = (typeparam $i $param)}}
* @param {(source: any) => {{$param -}} } $$createParam{{$param}}
{{- end}}
* @returns {($$source?: any) => {{jsid $model.Name}}{{$template.ParamList -}} }
{{- else}}
* Creates a new {{jsid $model.Name}} instance from a string or object.
* @param {any} [$$source = {}]
* @returns { {{- jsid $model.Name -}} }
{{- end}}
*/
static createFrom{{if $template.ParamList}}{{$template.CreateList}}{{else}}($$source = {}){{end}} {
{{- range $i, $spec := $model.Fields}}{{range $j, $field := $spec}}
{{- $create := ($module.JSCreateWithParams $field.Type $template.CreateList)}}
{{- if ne $create "$Create.Any"}}
const $$createField{{$i}}_{{$j}} = {{$create}};
{{- end}}
{{- end}}{{end}}
{{- $indent := ""}}
{{- if $template.ParamList}}
{{- $indent = " "}}
return ($$source = {}) => {
{{- end}}
{{$indent}}let $$parsedSource = typeof $$source === 'string' ? JSON.parse($$source) : $$source;
{{- range $i, $spec := $model.Fields}}{{range $j, $field := $spec}}
{{- if $module.NeedsCreate $field.Type}}
{{$indent}}if ("{{js $field.JsonName}}" in $$parsedSource) {
{{$indent}} $$parsedSource["{{js $field.JsonName}}"] = $$createField{{$i}}_{{$j}}($$parsedSource["{{js $field.JsonName}}"]);
{{$indent -}} }
{{- end}}
{{- end}}{{end}}
{{$indent}}return new {{jsid $model.Name}}(/** @type {Partial<{{jsid $model.Name}}{{$template.ParamList}}>} */($$parsedSource));
{{- if $template.ParamList}}
};
{{- end}}
}
}
{{else}}
{{- /* Rendered as a @typedef */}}
{{end}}
{{- end}}
{{- $postponed := $module.PostponedCreates}}
{{- if $postponed}}
// Private type creation functions
{{- range $i, $create := $postponed}}
{{if and (ge (len $create) 54) (eq (slice $create 39 54) "function $$init")}}var {{else}}const {{end -}}
$$createType{{$i}} = {{$create}};
{{- end}}
{{end}}
{{- if $useInterfaces}}
// In interface mode, this file is likely to contain just comments.
// We add a dummy export statement to ensure it is recognised as an ES module.
export {};
{{end -}}