5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-08 09:20:13 +08:00
wails/v3/internal/generator/render/templates/models.ts.tmpl
Fabio Massaioli 37673eb24d
[v3] Fix binding generator bugs and prepare for Go 1.24 (#4045)
* Rename predicates source file

* Overhaul and document type predicates

* Fix model collection logic for named types

* Fix map key type rendering

* Fix map creation code

* Fix rendering of structs that implement marshaler interfaces

* Fix type cycle detection to take type args into account

* Fix enum and typeparam field initialisation

* Improve unsupported type warnings

* Remove internal models file

* Deduplicate template code

* Accept generic aliases in static analyser

* Support new `encoding/json` flag `omitzero`

* Handle special cases when rendering generic aliases

* Update npm test dependencies

* Test class aliases and implicit private dependencies

* Test marshaler combinations

* Test map key types

* Remove bad map keys from unrelated tests

* Test service discovery through generic aliases

* Test generic aliases

* Test warning messages

* Disable go1.24 tests

* Update changelog

* Restore rendering of injected lines in index file

* Test directives

* Add wails:ignore directive

* Fix typo

* Move injections to the bottom of service files

* Handle errors from closing files

* Do not emit messages when services define only lifecycle methods

* Add internal directive for services and models

* Update changelog

* Fix error in service templates

* Test internal directive on services/models

* Fix error in index template

* Base testdata updates

* Testdata for class aliases and implicit private dependencies

* Testdata for marshaler combinations

* Testdata for map key types

* Testdata for bad map key fixes

* Add weakly typed enums aka alias constants

* Testdata for enum and typeparam field fixes

* Testdata for generic aliases

* Testdata for warning messages

* Testdata for directives

* Testdata for weakly typed enums

* Update binding example

* Update services example

* Remove go1.24 testdata

* Update cli doc

* Fix analyser tests

* Fix windows tests... hopefully

* go mod tidy on examples

* Update bindings guide

---------

Co-authored-by: Lea Anthony <lea.anthony@gmail.com>
2025-02-09 09:44:34 +11:00

185 lines
6.6 KiB
Cheetah

{{$module := .}}
{{- $runtime := $module.Runtime}}
{{- $models := (fixext $module.ModelsFile)}}
{{- $useInterfaces := .UseInterfaces}}
{{- $imports := $module.Imports -}}
// 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 (hasdoc $model.Decl.Doc) (hasdoc $model.Doc)}}
/**
{{- 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}}
*/
{{- end}}
{{- if $info.IsEnum}}
export enum {{jsid $model.Name}} {
/**
* The Go zero value for the underlying type of the enum.
*/
$zero = {{$module.JSDefault $model.Type false}},
{{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 or (hasdoc $model.Decl.Doc) (hasdoc $model.Doc)}}
/**
{{- 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}}
*/
{{- end}}
export type {{jsid $model.Name}}{{$template.ParamList}} = {{$module.JSType $model.Type}};
{{else if $info.IsTypeAlias}}
export type {{jsid $model.Name}}{{$template.ParamList}} = {{$module.JSType $model.Type}};
{{- if $info.HasValues}}
/**
* Predefined constants for type {{jsid $model.Name}}.
* @namespace
*/
export const {{jsid $model.Name}} = {
{{- 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}}
};
{{- end}}
{{else if $info.IsClassOrInterface}}
export {{if $info.IsInterface}}interface{{else}}class{{end}} {{jsid $model.Name}}{{$template.ParamList}} {
{{- range $i, $decl := $model.Fields}}{{range $j, $field := $decl}}
{{- if and (eq $j 0) (hasdoc $field.Decl.Doc)}}
{{- if gt $i 0}}
{{end}}
/**
{{- jsdoc $field.Decl.Doc.Text " "}}
*/
{{- end}}
"{{js $field.JsonName}}"{{if $field.Optional}}?{{end}}: {{$module.JSFieldType $field.StructField}};
{{- end}}{{end}}
{{- if $info.IsClass}}
/** Creates a new {{jsid $model.Name}} instance. */
constructor($$source: Partial<{{jsid $model.Name}}{{$template.ParamList}}> = {}) {
{{- range $spec := $model.Fields}}{{range $i, $field := $spec}}{{if not $field.Optional}}
if (!("{{js $field.JsonName}}" in $$source)) {
this["{{js $field.JsonName}}"] = {{$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}}.
{{- else}}
* Creates a new {{jsid $model.Name}} instance from a string or object.
{{- end}}
*/
static createFrom{{$template.ParamList}}({{if $template.ParamList}}
{{- range $i, $param := $model.TypeParams}}
{{- $param = (typeparam $i $param)}}
{{- if gt $i 0}}, {{end -}}
$$createParam{{$param}}: (source: any) => {{$param}}{{end -}}
{{else}}$$source: any = {}{{end}}):
{{- if $template.ParamList}} ($$source?: any) =>{{end}} {{jsid $model.Name}}{{$template.ParamList}} {
{{- 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: any = {}) => {
{{- 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}}{{$template.ParamList}}($$parsedSource as Partial<{{jsid $model.Name}}{{$template.ParamList}}>);
{{- if $template.ParamList}}
};
{{- end}}
}
{{- end}}
}
{{end}}
{{- end}}
{{- $postponed := $module.PostponedCreates}}
{{- if $postponed}}
// Private type creation functions
{{- range $i, $create := $postponed}}
{{if and (ge (len $create) 16) (eq (slice $create 1 16) "function $$init")}}var {{else}}const {{end -}}
$$createType{{$i}} = {{$create}};
{{- end}}
{{end -}}