5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-04 12:40:11 +08:00
wails/v3/internal/generator/collect/index.go
Fabio Massaioli f01b4b9a21
[v3] Fix binding generator bugs (#4001)
* Add some clarifying comments

* Remove special handling of window parameters

* Improve internal method exclusion

* Add test for internal method exclusion

* Remove useless blank field from app options

This is a leftover from an older version of the static analyser. It should have been removed long ago.

* Remove redundant godebug setting

gotypesalias=1 is the default starting with go1.23

* Use new range for syntax to simplify code

* Remove generator dependency on github.com/samber/lo

* Ensure generator testing tasks do not use the test cache

* Rename cyclic types test

* Test for cyclic imports

* Fix import cycle between model files

* Sort class aliases after their aliased class

* Test class aliases

* Fix length of default value for array types

* Test array initialization

* Add changelog

* Update changelog

* Fix contrived marking technique in model sorting algorithm

* Update binding example

* Update test data

---------

Co-authored-by: Lea Anthony <lea.anthony@gmail.com>
2025-01-17 18:56:07 +11:00

113 lines
2.7 KiB
Go

package collect
import (
"slices"
"strings"
)
// PackageIndex lists all services, models and unexported models
// to be generated from a single package.
//
// When obtained through a call to [PackageInfo.Index],
// each service and model appears at most once;
// services are sorted by name;
// exported models precede all unexported ones
// and both ranges are sorted by name.
type PackageIndex struct {
Package *PackageInfo
Services []*ServiceInfo
Models []*ModelInfo
HasExportedModels bool // If true, there is at least one exported model.
}
// Index computes a [PackageIndex] for the selected language from the list
// of generated services and models and regenerates cached stats.
//
// Services and models appear at most once in the returned slices;
// services are sorted by name;
// exported models precede all unexported ones
// and both ranges are sorted by name.
//
// Index calls info.Collect, and therefore provides the same guarantees.
// It is safe for concurrent use.
func (info *PackageInfo) Index(TS bool) (index *PackageIndex) {
// Init index.
index = &PackageIndex{
Package: info.Collect(),
}
// Init stats
stats := &Stats{
NumPackages: 1,
}
// Gather services.
for _, value := range info.services.Range {
service := value.(*ServiceInfo)
if !service.IsEmpty() {
if service.Object().Exported() {
// Publish non-internal service on the local index.
index.Services = append(index.Services, service)
}
// Update service stats.
stats.NumServices++
stats.NumMethods += len(service.Methods)
}
}
// Sort services by name.
slices.SortFunc(index.Services, func(b1 *ServiceInfo, b2 *ServiceInfo) int {
if b1 == b2 {
return 0
}
return strings.Compare(b1.Name, b2.Name)
})
// Gather models.
for _, value := range info.models.Range {
model := value.(*ModelInfo)
index.Models = append(index.Models, model)
// Mark presence of exported models
if model.Object().Exported() {
index.HasExportedModels = true
}
// Update model stats.
if len(model.Values) > 0 {
stats.NumEnums++
} else {
stats.NumModels++
}
}
// Sort models by exported property (exported first), then by name.
slices.SortFunc(index.Models, func(m1 *ModelInfo, m2 *ModelInfo) int {
if m1 == m2 {
return 0
}
m1e, m2e := m1.Object().Exported(), m2.Object().Exported()
if m1e != m2e {
if m1e {
return -1
} else {
return 1
}
}
return strings.Compare(m1.Name, m2.Name)
})
// Cache stats
info.stats.Store(stats)
return
}
// IsEmpty returns true if the given index
// contains no data for the selected language.
func (index *PackageIndex) IsEmpty() bool {
return len(index.Package.Injections) == 0 && len(index.Services) == 0 && len(index.Models) == 0
}