5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-05 23:30:10 +08:00
wails/v3/internal/generator/render/create.go
Fabio Massaioli 90b7ea944d
[v3] New binding generator (#3468)
* Support variadic arguments and slice, pointer types

* Fix computation of type namespaces

* Improve comments and general formatting

* Set default values correctly for composite types

* Add templates for bindings

Additionally:
* fixes generation of tuple return type
* improves imports and namespacing in JS mode
* general cleanup of generated code

* Simplify import list construction

* Refactor type generation code

Improves support for unknown types (encoded as any) and maps (using
Typescript index signatures)

* Support slices with pointer elements

* Match encoding/json behaviour in struct parser

* Update tests and example

* Add tests for complex method signatures and json tag parsing

* Add test `function_multiple_files`

* Attempt looking up idents with missing denotation

* Update test data

* fix quoted bool field

* Test quoted booleans

* Delete old parser code

* Remove old test data

* Update bindgen flags

* Makes call by ID the default

* Add package loading code

* Add static analyser

* Temporarily ignore binding generation code

* Add complex slice expressions test

* Fix variable reference analysis

* Unwrap casts to interface types

* Complete code comments

* Refactor static analyser

* Restrict options struct usage

* Update tests

* Fix method selector sink and source processing

* Improve Set API

* Add package info collector

* Rename analyser package to analyse

* Improve template functions

* Add index file templates

* Add glue code for binding generation

* Refactor collection and rendering code

* Implement binding generator

* Implement global index generation

* Improve marshaler and alias handling

* Use package path in binding calls by name

* Implement model collection and rendering

* Fix wrong exit condition in analyser

* Fix enum rendering

* Generate shortcuts for all packages.

* Implement generator tests

* Ignore non-pointer bound types

* Treat main package specially

* Compute stats

* Plug new API into generate command

* Support all named types

* Update JS runtime

* Report dual role types

* Remove go1.22 syntax

* Fix type assertion in TS bindings

* encoding/json compliance for arrays and slices

* Ignore got files in testdata

* Cleanup type rendering mechanism

* Update JS runtime

* Implement generic models

* Add missing field in renderer initialisation

* Improve generic creation code

* Add generic model test

* Add error reporting infrastructure

* Support configurable file names

* Detect file naming collisions

* Print final error report

* New shortcut file structure + collision detection

* Update test layout and data

* Autoconfiguration for analyser tests

* Live progress reporting

* Update code comments

* Fix model doc rendering

* Simplify name resolution

* Add test for out of tree types

* Fix generic creation code

* Fix potential collisions between methods and models

* Fix generic class alias rendering

* Report model discovery in debug mode

* Add interface mode for JS

* Collect interface method comments

* Add interface methods test

* Unwrap generic instantiations in method receivers

* Fix rendering of nullable types in interface mode

* Fix rendering of class aliases

* Expose promise cancel method to typescript

* Update test data

* Update binding example

* Fix rendering of aliased quoted type params

* Move to strongly typed bindings

* Implement lightweight analyser

* Update test cases

* Update binding example

* Add complex instantiation test

* Load full dependency tree

* Rewrite collector

* Update renderer to match new collector

* Update generator to match new collector

* Update test data

* Update binding example

* Configure includes and injections by language

* Improve system path resolution

* Support rich conditions in inject/include directives

* Fix error handling in Generator.Generate

* Retrieve compiled go file paths from fileset

* Do not rely on struct info in struct flattening algorithm

* Fix doc comment for findDeclaraion

* Fix bugs in embedded field handling

* Fix bugs and comments in package collection

* Remove useless fields from ServiceInfo

* Fix empty line at the beginning of TS indexes

* Remove global index and shortcuts

* Remove generation tests for individual packages

* Enforce lower-case file names

* Update test data

* Improve error reporting

* Update binding example

* Reintroduce go1.22 syntax

* Improve relative import path computation

* Improve alias support

* Add alias test

* Update test data

* Remove no services error

* Rename global analyser test

* Add workaround and test for bug in typeutil.Map

* Update test data

* Do not split fully qualified names

* Update typeutil package and remove workaround

* Unify alias/named type handling

* Fix rendering of generic named class aliases

* Fix rendering of array types

* Minor tweaks and cleanups

* Rmove namespaced export construct

* Update test data

* Update binding example

* Break type cycles

* Fix typo in comment

* Fix creation code for cyclic types

* Fix type of variadic params in interface mode

* Update test data

* Fix bad whitespace

* Refactor type assertions inside bound methods

* Update test data

* Rename field application.Options.Bind to Services

* Rename parser package to generator

* Update binding example

* Update test data

* Update generator readme

* Add typescript test harness

* Move test output to new subfolder

* Fix code generation bugs

* Use .js extensions in TS mode imports

* Update test data

* Revert default generator output dir to frontend/bindings

* Bump runtime package version

* Update templates

* Update changelog

* Improve newline handling

---------

Co-authored-by: Andreas Bichinger <andreas.bichinger@gmail.com>
2024-05-19 20:40:44 +10:00

380 lines
9.4 KiB
Go

package render
import (
"fmt"
"go/types"
"strings"
"text/template"
"github.com/wailsapp/wails/v3/internal/generator/collect"
)
// SkipCreate returns true if the given array of types needs no creation code.
func (m *module) SkipCreate(ts []types.Type) bool {
for _, typ := range ts {
if m.NeedsCreate(typ) {
return false
}
}
return true
}
// NeedsCreate returns true if the given type needs some creation code.
func (m *module) NeedsCreate(typ types.Type) bool {
return m.needsCreateImpl(typ, make(map[*types.TypeName]bool))
}
// needsCreateImpl provides the actual implementation of NeedsCreate.
// The visited parameter is used to break cycles.
func (m *module) needsCreateImpl(typ types.Type, visited map[*types.TypeName]bool) bool {
switch t := typ.(type) {
case *types.Alias, *types.Named:
obj := typ.(interface{ Obj() *types.TypeName }).Obj()
if visited[obj] {
return false
}
visited[obj] = true
if obj.Pkg() == nil {
// Builtin alias or named type: render underlying type.
return m.needsCreateImpl(t.Underlying(), visited)
}
if collect.IsAny(t) || collect.IsString(t) {
break
} else if collect.IsClass(t) {
return true
} else if _, isAlias := typ.(*types.Alias); isAlias {
return m.needsCreateImpl(types.Unalias(t), visited)
} else {
return m.needsCreateImpl(t.Underlying(), visited)
}
case *types.Array, *types.Pointer:
return m.needsCreateImpl(typ.(interface{ Elem() types.Type }).Elem(), visited)
case *types.Map, *types.Slice:
return true
case *types.Struct:
info := m.collector.Struct(t)
info.Collect()
for _, field := range info.Fields {
if m.needsCreateImpl(field.Type, visited) {
return true
}
}
case *types.TypeParam:
return true
}
return false
}
// JSCreate renders JS/TS code that creates an instance
// of the given type from JSON data.
//
// JSCreate's output may be incorrect
// if m.Imports.AddType has not been called for the given type.
func (m *module) JSCreate(typ types.Type) string {
return m.JSCreateWithParams(typ, "")
}
// JSCreateWithParams renders JS/TS code that creates an instance
// of the given type from JSON data. For generic types,
// it renders parameterised code.
//
// JSCreateWithParams's output may be incorrect
// if m.Imports.AddType has not been called for the given type.
func (m *module) JSCreateWithParams(typ types.Type, params string) string {
if len(params) > 0 && !m.hasTypeParams(typ) {
// Forget params for non-generic types.
params = ""
}
switch t := typ.(type) {
case *types.Alias:
return m.JSCreateWithParams(types.Unalias(typ), params)
case *types.Array, *types.Pointer:
pp, ok := m.postponedCreates.At(typ).(*postponed)
if ok {
return fmt.Sprintf("$$createType%d%s", pp.index, params)
}
createElement := m.JSCreateWithParams(typ.(interface{ Elem() types.Type }).Elem(), params)
if createElement != "$Create.Any" {
pp = &postponed{m.postponedCreates.Len(), params}
m.postponedCreates.Set(typ, pp)
return fmt.Sprintf("$$createType%d%s", pp.index, params)
}
case *types.Map:
pp, ok := m.postponedCreates.At(typ).(*postponed)
if !ok {
m.JSCreateWithParams(t.Key(), params)
m.JSCreateWithParams(t.Elem(), params)
pp = &postponed{m.postponedCreates.Len(), params}
m.postponedCreates.Set(typ, pp)
}
return fmt.Sprintf("$$createType%d%s", pp.index, params)
case *types.Named:
if t.Obj().Pkg() == nil {
// Builtin named type: render underlying type.
return m.JSCreateWithParams(t.Underlying(), params)
}
if collect.IsAny(typ) || collect.IsString(typ) || !m.NeedsCreate(typ) {
break
}
pp, ok := m.postponedCreates.At(typ).(*postponed)
if !ok {
if t.TypeArgs() != nil && t.TypeArgs().Len() > 0 {
// Postpone type args.
for i := range t.TypeArgs().Len() {
m.JSCreateWithParams(t.TypeArgs().At(i), params)
}
}
pp = &postponed{m.postponedCreates.Len(), params}
m.postponedCreates.Set(typ, pp)
if !collect.IsClass(typ) {
m.JSCreateWithParams(t.Underlying(), params)
}
}
return fmt.Sprintf("$$createType%d%s", pp.index, params)
case *types.Slice:
if types.Identical(typ, typeByteSlice) {
return "$Create.ByteSlice"
}
pp, ok := m.postponedCreates.At(typ).(*postponed)
if !ok {
m.JSCreateWithParams(t.Elem(), params)
pp = &postponed{m.postponedCreates.Len(), params}
m.postponedCreates.Set(typ, pp)
}
return fmt.Sprintf("$$createType%d%s", pp.index, params)
case *types.Struct:
pp, ok := m.postponedCreates.At(typ).(*postponed)
if ok {
return fmt.Sprintf("$$createType%d%s", pp.index, params)
}
info := m.collector.Struct(t)
info.Collect()
postpone := false
for _, field := range info.Fields {
if m.JSCreateWithParams(field.Type, params) != "$Create.Any" {
postpone = true
}
}
if postpone {
pp = &postponed{m.postponedCreates.Len(), params}
m.postponedCreates.Set(typ, pp)
return fmt.Sprintf("$$createType%d%s", pp.index, params)
}
case *types.TypeParam:
return fmt.Sprintf("$$createParam%s", typeparam(t.Index(), t.Obj().Name()))
}
return "$Create.Any"
}
// PostponedCreates returns the list of postponed create functions
// for the given module.
func (m *module) PostponedCreates() []string {
result := make([]string, m.postponedCreates.Len())
m.postponedCreates.Iterate(func(key types.Type, value any) {
pp := value.(*postponed)
pre := ""
if pp.params != "" {
pre = pp.params + " => "
}
switch t := key.(type) {
case *types.Array, *types.Slice:
result[pp.index] = fmt.Sprintf("%s$Create.Array(%s)", pre, m.JSCreateWithParams(t.(interface{ Elem() types.Type }).Elem(), pp.params))
case *types.Map:
result[pp.index] = fmt.Sprintf(
"%s$Create.Map(%s, %s)",
pre,
m.JSCreateWithParams(t.Key(), pp.params),
m.JSCreateWithParams(t.Elem(), pp.params),
)
case *types.Named:
if !collect.IsClass(key) {
// Creation function for non-struct named types
// require an indirect assignment to break cycles.
// Typescript cannot infer the return type on its own: add hints.
cast, returnType := "", ""
if m.TS {
returnType = ": any"
} else {
cast = "/** @type {(...args: any[]) => any} */"
}
result[pp.index] = fmt.Sprintf(`
%s(function $$initCreateType%d(...args)%s {
if ($$createType%d === $$initCreateType%d) {
$$createType%d = %s%s;
}
return $$createType%d(...args);
})`,
cast, pp.index, returnType,
pp.index, pp.index,
pp.index, pre, m.JSCreateWithParams(t.Underlying(), pp.params),
pp.index,
)[1:] // Remove initial newline.
// We're done.
break
}
var builder strings.Builder
builder.WriteString(pre)
if t.Obj().Pkg().Path() == m.Imports.Self {
if t.Obj().Exported() && m.Imports.ImportModels {
builder.WriteString("$models.")
} else if !t.Obj().Exported() && m.Imports.ImportInternal {
builder.WriteString("$internal.")
}
} else {
builder.WriteString(jsimport(m.Imports.External[t.Obj().Pkg().Path()]))
builder.WriteRune('.')
}
builder.WriteString(jsid(t.Obj().Name()))
builder.WriteString(".createFrom")
if t.TypeArgs() != nil && t.TypeArgs().Len() > 0 {
builder.WriteString("(")
for i := range t.TypeArgs().Len() {
if i > 0 {
builder.WriteString(", ")
}
builder.WriteString(m.JSCreateWithParams(t.TypeArgs().At(i), pp.params))
}
builder.WriteString(")")
}
result[pp.index] = builder.String()
case *types.Pointer:
result[pp.index] = fmt.Sprintf("%s$Create.Nullable(%s)", pre, m.JSCreateWithParams(t.Elem(), pp.params))
case *types.Struct:
info := m.collector.Struct(t)
info.Collect()
var builder strings.Builder
builder.WriteString(pre)
builder.WriteString("$Create.Struct({")
for _, field := range info.Fields {
createField := m.JSCreateWithParams(field.Type, pp.params)
if createField == "$Create.Any" {
continue
}
builder.WriteString("\n \"")
template.JSEscape(&builder, []byte(field.JsonName))
builder.WriteString("\": ")
builder.WriteString(createField)
builder.WriteRune(',')
}
if len(info.Fields) > 0 {
builder.WriteRune('\n')
}
builder.WriteString("})")
result[pp.index] = builder.String()
default:
result[pp.index] = pre + "$Create.Any"
}
})
if newline != "\n" {
// Replace newlines according to local git config.
for i := range result {
result[i] = strings.ReplaceAll(result[i], "\n", newline)
}
}
return result
}
type postponed struct {
index int
params string
}
// hasTypeParams returns true if the given type depends upon type parameters.
func (m *module) hasTypeParams(typ types.Type) bool {
switch t := typ.(type) {
case *types.Alias:
if t.Obj().Pkg() == nil {
// Builtin alias: these are never rendered as templates.
return false
}
return m.hasTypeParams(types.Unalias(typ))
case *types.Array, *types.Pointer, *types.Slice:
return m.hasTypeParams(typ.(interface{ Elem() types.Type }).Elem())
case *types.Map:
return m.hasTypeParams(t.Key()) || m.hasTypeParams(t.Elem())
case *types.Named:
if t.Obj().Pkg() == nil {
// Builtin named type: these are never rendered as templates.
return false
}
if targs := t.TypeArgs(); targs != nil {
for i := range targs.Len() {
if m.hasTypeParams(targs.At(i)) {
return true
}
}
}
case *types.Struct:
info := m.collector.Struct(t)
info.Collect()
for _, field := range info.Fields {
if m.hasTypeParams(field.Type) {
return true
}
}
case *types.TypeParam:
return true
}
return false
}