collect

package
v3.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 6, 2026 License: MIT Imports: 25 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsAny

func IsAny(typ types.Type) bool

IsAny returns true if the given type is guaranteed to render as the TS any type or equivalent.

It might return false negatives for generic aliases, hence should only be used with instantiated types or in contexts where false negatives are acceptable.

func IsClass

func IsClass(typ types.Type) bool

IsClass returns true if the given type will be rendered as a JS/TS model class (or interface).

func IsDirective

func IsDirective(comment string, directive string) bool

IsDirective returns true if the given comment is a directive of the form //wails: + directive.

func IsKnownEvent

func IsKnownEvent(name string) bool

func IsMapKey

func IsMapKey(typ types.Type) bool

IsMapKey returns true if the given type is accepted as a map key by encoding/json.

func IsNumberAlias

func IsNumberAlias(typ types.Type) bool

IsNumberAlias returns true when either typ will be rendered to JS/TS as an alias for the TS type `number`, or typ itself (not its underlying type) is a pointer whose element type satisfies the property described above.

This predicate is only safe to use either with map keys, where pointers are treated in an ad-hoc way by [json.Marshal], or when typ IS ALREADY KNOWN to be either types.Alias or types.Named.

Otherwise, the result might be incorrect: IsNumberAlias MUST NOT be used to check whether an arbitrary instance of types.Type renders as a JS/TS number type.

Notice that IsNumberAlias returns false for all type parameters: detecting those that must be always instantiated as number aliases is technically possible, but very difficult.

func IsParametric

func IsParametric(typ types.Type) bool

IsParametric returns true if the given type contains unresolved type parameters.

func IsStringAlias

func IsStringAlias(typ types.Type) bool

IsStringAlias returns true when either typ will be rendered to JS/TS as an alias for the TS type `string`, or typ itself (not its underlying type) is a pointer whose element type satisfies the property described above.

This predicate is only safe to use either with map keys, where pointers are treated in an ad-hoc way by [json.Marshal], or when typ IS ALREADY KNOWN to be either types.Alias or types.Named.

Otherwise, the result might be incorrect: IsStringAlias MUST NOT be used to check whether an arbitrary instance of types.Type renders as a JS/TS string type.

Notice that IsStringAlias returns false for all type parameters: detecting those that must be always instantiated as string aliases is technically possible, but very difficult.

func IsTypeParam

func IsTypeParam(typ types.Type) bool

IsTypeParam returns true when the given type is either a TypeParam or a pointer to a TypeParam.

func ParseDirective

func ParseDirective(comment string, directive string) string

ParseDirective extracts the argument portion of a //wails: + directive comment.

Types

type Collector

type Collector struct {
	// contains filtered or unexported fields
}

Collector wraps all bookkeeping data structures that are needed to collect data about a set of packages, bindings and models.

func NewCollector

func NewCollector(pkgs []*packages.Package, registerEvent types.Object, systemPaths *config.SystemPaths, options *flags.GenerateBindingsOptions, scheduler Scheduler, logger config.Logger) *Collector

NewCollector initialises a new Collector instance for the given package set.

func (*Collector) Const

func (collector *Collector) Const(obj *types.Const) *ConstInfo

Const returns the unique ConstInfo instance associated to the given object within a collector.

Const is safe for concurrent use.

func (*Collector) EventMap

func (collector *Collector) EventMap() *EventMap

EventMap returns the unique event map associated with the given collector, or nil if event collection is disabled.

func (*Collector) Field

func (collector *Collector) Field(obj *types.Var) *FieldInfo

Field returns the unique FieldInfo instance associated to the given object within a collector.

Field is safe for concurrent use.

func (*Collector) IsVoidAlias

func (collector *Collector) IsVoidAlias(typOrObj any) bool

IsVoidAlias returns true when the given type or object is the application.Void named type that stands in for the void TS type.

func (*Collector) Iterate

func (collector *Collector) Iterate(yield func(pkg *PackageInfo) bool)

Iterate calls yield sequentially for each PackageInfo instance registered with the collector. If yield returns false, Iterate stops the iteration.

Iterate is safe for concurrent use.

func (*Collector) Method

func (collector *Collector) Method(obj *types.Func) *MethodInfo

Method returns the unique MethodInfo instance associated to the given object within a collector.

Method is safe for concurrent use.

func (*Collector) Model

func (collector *Collector) Model(obj *types.TypeName) *ModelInfo

Model retrieves the the unique ModelInfo instance associated to the given type object within a Collector. If none is present, Model initialises a new one registers it for code generation and schedules background collection activity.

Model is safe for concurrent use.

func (*Collector) Package

func (collector *Collector) Package(pkg *types.Package) *PackageInfo

Package retrieves the unique PackageInfo instance, if any, associated to the given package object within a Collector.

Package is safe for concurrent use.

func (*Collector) Service

func (collector *Collector) Service(obj *types.TypeName) *ServiceInfo

Service returns the unique ServiceInfo instance associated to the given object within a collector and registers it for code generation.

Service is safe for concurrent use.

func (*Collector) Struct

func (collector *Collector) Struct(typ *types.Struct) *StructInfo

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) Type

func (collector *Collector) Type(obj *types.TypeName) *TypeInfo

Type returns the unique TypeInfo instance associated to the given object within a collector.

Type is safe for concurrent use.

type Condition

type Condition struct {
	JS         bool
	TS         bool
	Classes    bool
	Interfaces bool
}

func ParseCondition

func ParseCondition(argument string) (string, Condition, error)

ParseCondition parses an optional two-character condition prefix for include or inject directives. It returns the argument stripped of the prefix and the resulting condition. If the condition is malformed, ParseCondition returns a non-nil error.

func (Condition) Satisfied

func (cond Condition) Satisfied(options *flags.GenerateBindingsOptions) bool

Satisfied returns true when the condition described by the receiver is satisfied by the given configuration.

type ConstInfo

type ConstInfo struct {
	Name  string
	Value any

	Pos  token.Pos
	Spec *GroupInfo
	Decl *GroupInfo
	// contains filtered or unexported fields
}

ConstInfo records information about a constant declaration.

Read accesses to any public field are only safe if a call to ConstInfo.Collect has completed before the access, for example by calling it in the accessing goroutine or before spawning the accessing goroutine.

func (*ConstInfo) Collect

func (info *ConstInfo) Collect() *ConstInfo

Collect gathers information about the constant described by its receiver. It can be called concurrently by multiple goroutines; the computation will be performed just once.

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 (*ConstInfo) Node

func (info *ConstInfo) Node() ast.Node

func (*ConstInfo) Object

func (info *ConstInfo) Object() types.Object

func (*ConstInfo) Type

func (info *ConstInfo) Type() types.Type

type EventInfo

type EventInfo struct {
	// Name is the name of the event.
	Name string

	// Data is the data type the event has been registered with.
	// It may be nil in case of conflicting definitions.
	Data types.Type

	// Pos records the position
	// of the first discovered definition for this event.
	Pos token.Position
}

EventInfo holds information about a single event definition.

type EventMap

type EventMap struct {
	Imports *ImportMap
	Defs    []*EventInfo
	// contains filtered or unexported fields
}

EventMap holds information about a set of custom events and their associated data types.

Read accesses to any public field are only safe if a call to EventMap.Collect has completed before the access, for example by calling it in the accessing goroutine or before spawning the accessing goroutine.

func (*EventMap) Collect

func (em *EventMap) Collect() *EventMap

Collect gathers information for the event map described by its receiver. It can be called concurrently by multiple goroutines; the computation will be performed just once.

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 (*EventMap) Stats

func (em *EventMap) Stats() *Stats

Stats returns statistics for this event map. It is an error to call stats before a call to EventMap.Collect has completed.

type FieldInfo

type FieldInfo struct {
	Name     string
	Blank    bool
	Embedded bool

	Pos  token.Pos
	Decl *GroupInfo
	// contains filtered or unexported fields
}

FieldInfo records information about a struct field declaration.

Read accesses to any public field are only safe if a call to FieldInfo.Collect has completed before the access, for example by calling it in the accessing goroutine or before spawning the accessing goroutine.

func (*FieldInfo) Collect

func (info *FieldInfo) Collect() *FieldInfo

Collect gathers information about the struct field described by its receiver. It can be called concurrently by multiple goroutines; the computation will be performed just once.

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 (*FieldInfo) Node

func (info *FieldInfo) Node() ast.Node

func (*FieldInfo) Object

func (info *FieldInfo) Object() types.Object

func (*FieldInfo) Type

func (info *FieldInfo) Type() types.Type

type GroupInfo

type GroupInfo struct {
	Pos token.Pos
	Doc *ast.CommentGroup
	// contains filtered or unexported fields
}

GroupInfo records information about a group of type, field or constant declarations. This may be either a list of distinct specifications wrapped in parentheses, or a single specification declaring multiple fields or constants.

Read accesses to any public field are only safe if a call to GroupInfo.Collect has completed before the access, for example by calling it in the accessing goroutine or before spawning the accessing goroutine.

func (*GroupInfo) Collect

func (info *GroupInfo) Collect() *GroupInfo

Collect gathers information about the declaration group described by its receiver. It can be called concurrently by multiple goroutines; the computation will be performed just once.

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 (*GroupInfo) Node

func (info *GroupInfo) Node() ast.Node

func (*GroupInfo) Object

func (*GroupInfo) Object() types.Object

func (*GroupInfo) Type

func (*GroupInfo) Type() types.Type

type ImportInfo

type ImportInfo struct {
	Name    string
	Index   int // Progressive number for identically named imports, starting from 0 for each distinct name.
	RelPath string
}

ImportInfo records information about a single import.

type ImportMap

type ImportMap struct {
	// Self records the path of the importing package.
	Self string

	// ImportModels records whether models from the current package may be needed.
	ImportModels bool

	// External records information about each imported package,
	// keyed by package path.
	External map[string]ImportInfo
	// contains filtered or unexported fields
}

ImportMap records deduplicated imports by a binding or models module. It computes relative import paths and assigns import names, taking care to avoid collisions.

func NewImportMap

func NewImportMap(importer *PackageInfo) *ImportMap

NewImportMap initialises an import map for the given importer package. The argument may be nil, in which case import paths will be relative to the root output directory.

func (*ImportMap) Add

func (imports *ImportMap) Add(pkg *PackageInfo)

Add adds the given package to the import map if not already present, choosing import names so as to avoid collisions.

Add does not support unsynchronised concurrent calls on the same receiver.

func (*ImportMap) AddType

func (imports *ImportMap) AddType(typ types.Type)

AddType adds all dependencies of the given type to the import map and marks all referenced named types as models.

It is a runtime error to call AddType on an ImportMap created with nil importing package.

AddType does not support unsynchronised concurrent calls on the same receiver.

func (*ImportMap) Merge

func (imports *ImportMap) Merge(other *ImportMap)

Merge merges the given import map into the receiver. The importing package must be the same.

type Info

type Info interface {
	Object() types.Object
	Type() types.Type
	Node() ast.Node
}

Info instances provide information about either a type-checker object, a struct type or a group of declarations.

type MarshalerKind

type MarshalerKind byte

MarshalerKind values describe whether and how a type implements a marshaler interface. For any one of the two marshaler interfaces, a type is

  • a NonMarshaler if it does not implement it;
  • an ImplicitMarshaler if it inherits the implementation from its underlying type;
  • an ExplicitMarshaler if it defines the relevant method explicitly.
const (
	NonMarshaler MarshalerKind = iota
	ImplicitMarshaler
	ExplicitMarshaler
)

func IsJSONMarshaler

func IsJSONMarshaler(typ types.Type) MarshalerKind

IsJSONMarshaler queries whether and how the given type implements the [json.Marshaler] interface.

func IsTextMarshaler

func IsTextMarshaler(typ types.Type) MarshalerKind

IsTextMarshaler queries whether and how the given type implements the encoding.TextMarshaler interface.

func MaybeJSONMarshaler

func MaybeJSONMarshaler(typ types.Type) MarshalerKind

MaybeJSONMarshaler queries whether and how the given type implements the [json.Marshaler] interface for at least one receiver form.

func MaybeTextMarshaler

func MaybeTextMarshaler(typ types.Type) MarshalerKind

MaybeTextMarshaler queries whether and how the given type implements the encoding.TextMarshaler interface for at least one receiver form.

type MethodInfo

type MethodInfo struct {
	Name string

	// Abstract is true when the described method belongs to an interface.
	Abstract bool

	Doc  *ast.CommentGroup
	Decl *GroupInfo
	// contains filtered or unexported fields
}

MethodInfo records information about a method declaration.

Read accesses to any public field are only safe if a call to MethodInfo.Collect has completed before the access, for example by calling it in the accessing goroutine or before spawning the accessing goroutine.

func (*MethodInfo) Collect

func (info *MethodInfo) Collect() *MethodInfo

Collect gathers information about the method described by its receiver. It can be called concurrently by multiple goroutines; the computation will be performed just once.

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 (*MethodInfo) Node

func (info *MethodInfo) Node() ast.Node

func (*MethodInfo) Object

func (info *MethodInfo) Object() types.Object

func (*MethodInfo) Type

func (info *MethodInfo) Type() types.Type

type ModelFieldInfo

type ModelFieldInfo struct {
	*StructField
	*FieldInfo
}

ModelFieldInfo holds extended information about a struct field in a model type.

type ModelInfo

type ModelInfo struct {
	*TypeInfo

	// Internal records whether the model
	// should be exported by the index file.
	Internal bool

	// Imports records dependencies for this model.
	Imports *ImportMap

	// Type records the target type for an alias or derived model,
	// the underlying type for an enum.
	Type types.Type

	// Fields records the property list for a class or struct alias model,
	// in order of declaration and grouped by their declaring [ast.Field].
	Fields [][]*ModelFieldInfo

	// Values records the value list for an enum model,
	// in order of declaration and grouped
	// by their declaring [ast.GenDecl] and [ast.ValueSpec].
	Values [][][]*ConstInfo

	// TypeParams records type parameter names for generic models.
	TypeParams []string

	// Predicates caches the value of all type predicates for this model.
	//
	// WARN: whenever working with a generic uninstantiated model type,
	// use these instead of invoking predicate functions,
	// which may incur a large performance penalty.
	Predicates Predicates
	// contains filtered or unexported fields
}

ModelInfo records all information that is required to render JS/TS code for a model type.

Read accesses to exported fields are only safe if a call to ModelInfo.Collect has completed before the access, for example by calling it in the accessing goroutine or before spawning the accessing goroutine.

func (*ModelInfo) Collect

func (info *ModelInfo) Collect() *ModelInfo

Collect gathers information for the model described by its receiver. It can be called concurrently by multiple goroutines; the computation will be performed just once.

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.

type PackageIndex

type PackageIndex struct {
	Package *PackageInfo

	Services            []*ServiceInfo
	HasExportedServices bool // If true, there is at least one exported service.

	Models            []*ModelInfo
	HasExportedModels bool // If true, there is at least one exported model.
}

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 and models are sorted by internal property (all exported first), then by name.

func (*PackageIndex) IsEmpty

func (index *PackageIndex) IsEmpty() bool

IsEmpty returns true if the given index contains no data for the selected language.

type PackageInfo

type PackageInfo struct {
	// Path holds the canonical path of the described package.
	Path string

	// Name holds the import name of the described package.
	Name string

	// IsOrImportsApp is true if this package is, or depends upon, the Wails application package.
	IsOrImportsApp bool

	// Types and TypesInfo hold type information for this package.
	Types     *types.Package
	TypesInfo *types.Info

	// Fset holds the FileSet that was used to parse this package.
	Fset *token.FileSet

	// Files holds parsed files for this package,
	// ordered by start position to support binary search.
	Files []*ast.File

	// Docs holds package doc comments.
	Docs []*ast.CommentGroup

	// Includes holds a list of additional files to include
	// with the generated bindings.
	// It maps file names to their paths on disk.
	Includes map[string]string

	// Injections holds a list of code lines to be injected
	// into the package index file.
	Injections []string
	// contains filtered or unexported fields
}

PackageInfo records information about a package.

Read accesses to fields Path, Name, IsOrImportsApp, Types, TypesInfo, Fset, are safe at any time without any synchronisation.

Read accesses to all other fields are only safe if a call to PackageInfo.Collect has completed before the access, for example by calling it in the accessing goroutine or before spawning the accessing goroutine.

Concurrent write accesses are only allowed through the provided methods.

func (*PackageInfo) Collect

func (info *PackageInfo) Collect() *PackageInfo

Collect gathers information about the package described by its receiver. It can be called concurrently by multiple goroutines; the computation will be performed just once.

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 (*PackageInfo) Index

func (info *PackageInfo) Index(TS bool) (index *PackageIndex)

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 (*PackageInfo) Stats

func (info *PackageInfo) Stats() *Stats

Stats returns cached statistics for this package. If PackageInfo.Index has not been called yet, it returns nil.

Stats is safe for unsynchronised concurrent calls.

type ParamInfo

type ParamInfo struct {
	Name     string
	Type     types.Type
	Blank    bool
	Variadic bool
}

ParamInfo records all information that is required to render JS/TS code for a service method parameter.

type Predicates

type Predicates struct {
	IsJSONMarshaler    MarshalerKind
	MaybeJSONMarshaler MarshalerKind
	IsTextMarshaler    MarshalerKind
	MaybeTextMarshaler MarshalerKind
	IsMapKey           bool
	IsTypeParam        bool
	IsStringAlias      bool
	IsNumberAlias      bool
	IsClass            bool
	IsAny              bool
}

Predicates caches the value of all type predicates.

type Scheduler

type Scheduler interface {
	// Schedule should run the given function according
	// to the implementation's preferred strategy.
	//
	// Scheduled tasks may call Schedule again;
	// therefore, if tasks run concurrently,
	// the implementation must support concurrent calls.
	Schedule(task func())
}

Scheduler instances provide task scheduling for collection activities.

type ServiceInfo

type ServiceInfo struct {
	*TypeInfo

	// Internal records whether the service
	// should be exported by the index file.
	Internal bool

	Imports *ImportMap
	Methods []*ServiceMethodInfo

	// HasInternalMethods records whether the service
	// defines lifecycle or http server methods.
	HasInternalMethods bool

	// Injections stores a list of JS code lines
	// that should be injected into the generated file.
	Injections []string
	// contains filtered or unexported fields
}

ServiceInfo records all information that is required to render JS/TS code for a service type.

Read accesses to any public field are only safe if a call to ServiceInfo.Collect has completed before the access, for example by calling it in the accessing goroutine or before spawning the accessing goroutine.

func (*ServiceInfo) Collect

func (info *ServiceInfo) Collect() *ServiceInfo

Collect gathers information about the service described by its receiver. It can be called concurrently by multiple goroutines; the computation will be performed just once.

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 (*ServiceInfo) IsEmpty

func (info *ServiceInfo) IsEmpty() bool

IsEmpty returns true if no methods or code injections are present for this service, for the selected language.

type ServiceMethodInfo

type ServiceMethodInfo struct {
	*MethodInfo
	FQN      string
	ID       string
	Internal bool
	Params   []*ParamInfo
	Results  []types.Type
}

ServiceMethodInfo records all information that is required to render JS/TS code for a service method.

type Stats

type Stats struct {
	NumPackages int
	NumServices int
	NumMethods  int
	NumEnums    int
	NumModels   int
	NumEvents   int
	StartTime   time.Time
	EndTime     time.Time
}

func (*Stats) Add

func (stats *Stats) Add(other *Stats)

func (*Stats) Elapsed

func (stats *Stats) Elapsed() time.Duration

func (*Stats) Start

func (stats *Stats) Start()

func (*Stats) Stop

func (stats *Stats) Stop()

type StructField

type 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
}

FieldInfo represents a single field in a struct.

type StructInfo

type StructInfo struct {
	Fields []*StructField
	// contains filtered or unexported fields
}

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.

func (*StructInfo) Collect

func (info *StructInfo) Collect() *StructInfo

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 (*StructInfo) Node

func (*StructInfo) Node() ast.Node

func (*StructInfo) Object

func (*StructInfo) Object() types.Object

func (*StructInfo) Type

func (info *StructInfo) Type() types.Type

type TypeInfo

type TypeInfo struct {
	Name string

	// Alias is true for type aliases.
	Alias bool

	Doc  *ast.CommentGroup
	Decl *GroupInfo
	// contains filtered or unexported fields
}

TypeInfo records information about a type declaration.

Read accesses to any public field are only safe if a call to TypeInfo.Collect has completed before the access, for example by calling it in the accessing goroutine or before spawning the accessing goroutine.

func (*TypeInfo) Collect

func (info *TypeInfo) Collect() *TypeInfo

Collect gathers information about the type described by its receiver. It can be called concurrently by multiple goroutines; the computation will be performed just once.

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 (*TypeInfo) Node

func (info *TypeInfo) Node() ast.Node

func (*TypeInfo) Object

func (info *TypeInfo) Object() types.Object

func (*TypeInfo) Type

func (info *TypeInfo) Type() types.Type

Directories

Path Synopsis
This example explores exhaustively the behaviour of encoding/json when handling types that implement marshaler interfaces.
This example explores exhaustively the behaviour of encoding/json when handling types that implement marshaler interfaces.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL