mirror of
https://github.com/wailsapp/wails.git
synced 2025-05-06 19:11:14 +08:00

* 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>
345 lines
8.5 KiB
Go
345 lines
8.5 KiB
Go
package collect
|
|
|
|
import (
|
|
"cmp"
|
|
"go/ast"
|
|
"go/types"
|
|
"reflect"
|
|
"slices"
|
|
"strings"
|
|
"sync"
|
|
"unicode"
|
|
)
|
|
|
|
type (
|
|
// StructInfo records the flattened field list for a struct type,
|
|
// taking into account JSON tags.
|
|
//
|
|
// The field list is initially empty. It will be populated
|
|
// upon calling [StructInfo.Collect] for the first time.
|
|
//
|
|
// Read accesses to the field list are only safe
|
|
// if a call to [StructInfo.Collect] has been completed before the access,
|
|
// for example by calling it in the accessing goroutine
|
|
// or before spawning the accessing goroutine.
|
|
StructInfo struct {
|
|
Fields []*StructField
|
|
|
|
typ *types.Struct
|
|
|
|
collector *Collector
|
|
once sync.Once
|
|
}
|
|
|
|
// FieldInfo represents a single field in a struct.
|
|
StructField struct {
|
|
JsonName string // Avoid collisions with [FieldInfo.Name].
|
|
Type types.Type
|
|
Optional bool
|
|
Quoted bool
|
|
|
|
// Object holds the described type-checker object.
|
|
Object *types.Var
|
|
}
|
|
)
|
|
|
|
func newStructInfo(collector *Collector, typ *types.Struct) *StructInfo {
|
|
return &StructInfo{
|
|
typ: typ,
|
|
collector: collector,
|
|
}
|
|
}
|
|
|
|
// Struct retrieves the unique [StructInfo] instance
|
|
// associated to the given type within a Collector.
|
|
// If none is present, a new one is initialised.
|
|
//
|
|
// Struct is safe for concurrent use.
|
|
func (collector *Collector) Struct(typ *types.Struct) *StructInfo {
|
|
// Cache by type pointer, do not use a typeutil.Map:
|
|
// - for models, it may result in incorrect comments;
|
|
// - for anonymous structs, it would probably bring little benefit
|
|
// because the probability of repetitions is much lower.
|
|
|
|
return collector.fromCache(typ).(*StructInfo)
|
|
}
|
|
|
|
func (*StructInfo) Object() types.Object {
|
|
return nil
|
|
}
|
|
|
|
func (info *StructInfo) Type() types.Type {
|
|
return info.typ
|
|
}
|
|
|
|
func (*StructInfo) Node() ast.Node {
|
|
return nil
|
|
}
|
|
|
|
// Collect gathers information for the structure described by its receiver.
|
|
// It can be called concurrently by multiple goroutines;
|
|
// the computation will be performed just once.
|
|
//
|
|
// The field list of the receiver is populated
|
|
// by the same flattening algorithm employed by encoding/json.
|
|
// JSON struct tags are accounted for.
|
|
//
|
|
// Collect returns the receiver for chaining.
|
|
// It is safe to call Collect with nil receiver.
|
|
//
|
|
// After Collect returns, the calling goroutine and all goroutines
|
|
// it might spawn afterwards are free to access
|
|
// the receiver's fields indefinitely.
|
|
func (info *StructInfo) Collect() *StructInfo {
|
|
if info == nil {
|
|
return nil
|
|
}
|
|
|
|
type fieldData struct {
|
|
*StructField
|
|
|
|
// Data for the encoding/json flattening algorithm.
|
|
nameFromTag bool
|
|
index []int
|
|
}
|
|
|
|
info.once.Do(func() {
|
|
// Flattened list of fields with additional information.
|
|
fields := make([]fieldData, 0, info.typ.NumFields())
|
|
|
|
// Queued embedded types for current and next level.
|
|
current := make([]fieldData, 0, info.typ.NumFields())
|
|
next := make([]fieldData, 1, max(1, info.typ.NumFields()))
|
|
|
|
// Count of queued embedded types for current and next level.
|
|
count := make(map[*types.Struct]int)
|
|
nextCount := make(map[*types.Struct]int)
|
|
|
|
// Set of visited types to avoid duplicating work.
|
|
visited := make(map[*types.Struct]bool)
|
|
|
|
next[0] = fieldData{
|
|
StructField: &StructField{
|
|
Type: info.typ,
|
|
},
|
|
}
|
|
|
|
for len(next) > 0 {
|
|
current, next = next, current[:0]
|
|
count, nextCount = nextCount, count
|
|
clear(nextCount)
|
|
|
|
for _, embedded := range current {
|
|
// Scan embedded type for fields to include.
|
|
estruct := embedded.Type.Underlying().(*types.Struct)
|
|
|
|
// Skip previously visited structs
|
|
if visited[estruct] {
|
|
continue
|
|
}
|
|
visited[estruct] = true
|
|
|
|
// WARNING: do not reuse cached info for embedded structs.
|
|
// It may lead to incorrect results for subtle reasons.
|
|
|
|
for i := range estruct.NumFields() {
|
|
field := estruct.Field(i)
|
|
|
|
// Retrieve type of field, following aliases conservatively
|
|
// and unwrapping exactly one pointer.
|
|
ftype := field.Type()
|
|
if ptr, ok := types.Unalias(ftype).(*types.Pointer); ok {
|
|
ftype = ptr.Elem()
|
|
}
|
|
|
|
// Detect struct alias and keep it.
|
|
fstruct, _ := types.Unalias(ftype).(*types.Struct)
|
|
if fstruct == nil {
|
|
// Not a struct alias, follow alias chain.
|
|
ftype = types.Unalias(ftype)
|
|
fstruct, _ = ftype.Underlying().(*types.Struct)
|
|
}
|
|
|
|
if field.Embedded() {
|
|
if !field.Exported() && fstruct == nil {
|
|
// Ignore embedded fields of unexported non-struct types.
|
|
continue
|
|
}
|
|
} else if !field.Exported() {
|
|
// Ignore unexported non-embedded fields.
|
|
continue
|
|
}
|
|
|
|
// Retrieve and parse json tag.
|
|
tag := reflect.StructTag(estruct.Tag(i)).Get("json")
|
|
name, optional, quoted, visible := parseTag(tag)
|
|
if !visible {
|
|
// Ignored by encoding/json.
|
|
continue
|
|
}
|
|
|
|
if !isValidFieldName(name) {
|
|
// Ignore alternative name if invalid.
|
|
name = ""
|
|
}
|
|
|
|
index := make([]int, len(embedded.index)+1)
|
|
copy(index, embedded.index)
|
|
index[len(embedded.index)] = i
|
|
|
|
if name != "" || !field.Embedded() || fstruct == nil {
|
|
// Tag name is non-empty,
|
|
// or field is not embedded,
|
|
// or field is not structure:
|
|
// add to field list.
|
|
|
|
finfo := fieldData{
|
|
StructField: &StructField{
|
|
JsonName: name,
|
|
Type: field.Type(),
|
|
Optional: optional,
|
|
Quoted: quoted,
|
|
|
|
Object: field,
|
|
},
|
|
nameFromTag: name != "",
|
|
index: index,
|
|
}
|
|
|
|
if name == "" {
|
|
finfo.JsonName = field.Name()
|
|
}
|
|
|
|
fields = append(fields, finfo)
|
|
if count[estruct] > 1 {
|
|
// The struct we are scanning
|
|
// appears multiple times at the current level.
|
|
// This means that all its fields are ambiguous
|
|
// and must disappear.
|
|
// Duplicate them so that the field selection phase
|
|
// below will erase them.
|
|
fields = append(fields, finfo)
|
|
}
|
|
|
|
continue
|
|
}
|
|
|
|
// Queue embedded field for next level.
|
|
// If it has been queued already, do not duplicate it.
|
|
nextCount[fstruct]++
|
|
if nextCount[fstruct] == 1 {
|
|
next = append(next, fieldData{
|
|
StructField: &StructField{
|
|
Type: ftype,
|
|
},
|
|
index: index,
|
|
})
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Prepare for field selection phase.
|
|
slices.SortFunc(fields, func(f1 fieldData, f2 fieldData) int {
|
|
// Sort by name first.
|
|
if diff := strings.Compare(f1.JsonName, f2.JsonName); diff != 0 {
|
|
return diff
|
|
}
|
|
|
|
// Break ties by depth of occurrence.
|
|
if diff := cmp.Compare(len(f1.index), len(f2.index)); diff != 0 {
|
|
return diff
|
|
}
|
|
|
|
// Break ties by presence of json tag (prioritize presence).
|
|
if f1.nameFromTag != f2.nameFromTag {
|
|
if f1.nameFromTag {
|
|
return -1
|
|
} else {
|
|
return 1
|
|
}
|
|
}
|
|
|
|
// Break ties by order of occurrence.
|
|
return slices.Compare(f1.index, f2.index)
|
|
})
|
|
|
|
fieldCount := 0
|
|
|
|
// Keep for each name the dominant field, drop those for which ties
|
|
// still exist (ignoring order of occurrence).
|
|
for i, j := 0, 1; j <= len(fields); j++ {
|
|
if j < len(fields) && fields[i].JsonName == fields[j].JsonName {
|
|
continue
|
|
}
|
|
|
|
// If there is only one field with the current name,
|
|
// or there is a dominant one, keep it.
|
|
if i+1 == j || len(fields[i].index) != len(fields[i+1].index) || fields[i].nameFromTag != fields[i+1].nameFromTag {
|
|
fields[fieldCount] = fields[i]
|
|
fieldCount++
|
|
}
|
|
|
|
i = j
|
|
}
|
|
|
|
fields = fields[:fieldCount]
|
|
|
|
// Sort by order of occurrence.
|
|
slices.SortFunc(fields, func(f1 fieldData, f2 fieldData) int {
|
|
return slices.Compare(f1.index, f2.index)
|
|
})
|
|
|
|
// Copy selected fields to receiver.
|
|
info.Fields = make([]*StructField, len(fields))
|
|
for i, field := range fields {
|
|
info.Fields[i] = field.StructField
|
|
}
|
|
|
|
info.typ = nil
|
|
})
|
|
|
|
return info
|
|
}
|
|
|
|
// parseTag parses a json field tag and extracts
|
|
// all options recognised by encoding/json.
|
|
func parseTag(tag string) (name string, optional bool, quoted bool, visible bool) {
|
|
if tag == "-" {
|
|
return "", false, false, false
|
|
} else {
|
|
visible = true
|
|
}
|
|
|
|
parts := strings.Split(tag, ",")
|
|
|
|
name = parts[0]
|
|
|
|
for _, option := range parts[1:] {
|
|
switch option {
|
|
case "omitempty":
|
|
optional = true
|
|
case "string":
|
|
quoted = true
|
|
}
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
// isValidFieldName determines whether a field name is valid
|
|
// according to encoding/json.
|
|
func isValidFieldName(name string) bool {
|
|
if name == "" {
|
|
return false
|
|
}
|
|
|
|
for _, c := range name {
|
|
if !strings.ContainsRune("!#$%&()*+-./:;<=>?@[]^_{|}~ ", c) && !unicode.IsLetter(c) && !unicode.IsDigit(c) {
|
|
return false
|
|
}
|
|
}
|
|
|
|
return true
|
|
}
|