Skip to content

Commit 033ad76

Browse files
docs: Add comprehensive docstrings to Go files (#31)
This commit adds comprehensive docstrings to all Go files in the root of the repository, achieving 100% documentation coverage. Examples have been included where appropriate to improve clarity. Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
1 parent 1013d49 commit 033ad76

File tree

5 files changed

+86
-2
lines changed

5 files changed

+86
-2
lines changed

actions.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@ package core
22

33
import "github.com/wailsapp/wails/v3/pkg/application"
44

5+
// ActionServiceStartup is a message sent when the application's services are starting up.
6+
// This provides a hook for services to perform initialization tasks.
57
type ActionServiceStartup struct{}
68

9+
// ActionServiceShutdown is a message sent when the application is shutting down.
10+
// This allows services to perform cleanup tasks, such as saving state or closing resources.
711
type ActionServiceShutdown struct{}
812

913
// ActionDisplayOpenWindow is a structured message for requesting a new window.

core.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@ import (
1212
)
1313

1414
// New initialises a Core instance using the provided options and performs the necessary setup.
15+
// It is the primary entry point for creating a new Core application.
16+
//
17+
// Example:
18+
//
19+
// core, err := core.New(
20+
// core.WithService(&MyService{}),
21+
// core.WithAssets(assets),
22+
// )
1523
func New(opts ...Option) (*Core, error) {
1624
c := &Core{
1725
services: make(map[string]any),
@@ -37,6 +45,20 @@ func New(opts ...Option) (*Core, error) {
3745
// WithService creates an Option that registers a service. It automatically discovers
3846
// the service name from its package path and registers its IPC handler if it
3947
// implements a method named `HandleIPCEvents`.
48+
//
49+
// Example:
50+
//
51+
// // In myapp/services/calculator.go
52+
// package services
53+
//
54+
// type Calculator struct{}
55+
//
56+
// func (s *Calculator) Add(a, b int) int { return a + b }
57+
//
58+
// // In main.go
59+
// import "myapp/services"
60+
//
61+
// core.New(core.WithService(services.NewCalculator))
4062
func WithService(factory func(*Core) (any, error)) Option {
4163
return func(c *Core) error {
4264
serviceInstance, err := factory(c)
@@ -83,20 +105,27 @@ func WithName(name string, factory func(*Core) (any, error)) Option {
83105
}
84106
}
85107

108+
// WithWails creates an Option that injects the Wails application instance into the Core.
109+
// This is essential for services that need to interact with the Wails runtime.
86110
func WithWails(app *application.App) Option {
87111
return func(c *Core) error {
88112
c.App = app
89113
return nil
90114
}
91115
}
92116

117+
// WithAssets creates an Option that registers the application's embedded assets.
118+
// This is necessary for the application to be able to serve its frontend.
93119
func WithAssets(fs embed.FS) Option {
94120
return func(c *Core) error {
95121
c.assets = fs
96122
return nil
97123
}
98124
}
99125

126+
// WithServiceLock creates an Option that prevents any further services from being
127+
// registered after the Core has been initialized. This is a security measure to
128+
// prevent late-binding of services that could have unintended consequences.
100129
func WithServiceLock() Option {
101130
return func(c *Core) error {
102131
c.serviceLock = true
@@ -106,14 +135,20 @@ func WithServiceLock() Option {
106135

107136
// --- Core Methods ---
108137

138+
// ServiceStartup is the entry point for the Core service's startup lifecycle.
139+
// It is called by Wails when the application starts.
109140
func (c *Core) ServiceStartup(ctx context.Context, options application.ServiceOptions) error {
110141
return c.ACTION(ActionServiceStartup{})
111142
}
112143

144+
// ServiceShutdown is the entry point for the Core service's shutdown lifecycle.
145+
// It is called by Wails when the application shuts down.
113146
func (c *Core) ServiceShutdown(ctx context.Context) error {
114147
return c.ACTION(ActionServiceShutdown{})
115148
}
116149

150+
// ACTION dispatches a message to all registered IPC handlers.
151+
// This is the primary mechanism for services to communicate with each other.
117152
func (c *Core) ACTION(msg Message) error {
118153
c.ipcMu.RLock()
119154
handlers := append([]func(*Core, Message) error(nil), c.ipcHandlers...)
@@ -128,18 +163,21 @@ func (c *Core) ACTION(msg Message) error {
128163
return agg
129164
}
130165

166+
// RegisterAction adds a new IPC handler to the Core.
131167
func (c *Core) RegisterAction(handler func(*Core, Message) error) {
132168
c.ipcMu.Lock()
133169
c.ipcHandlers = append(c.ipcHandlers, handler)
134170
c.ipcMu.Unlock()
135171
}
136172

173+
// RegisterActions adds multiple IPC handlers to the Core.
137174
func (c *Core) RegisterActions(handlers ...func(*Core, Message) error) {
138175
c.ipcMu.Lock()
139176
c.ipcHandlers = append(c.ipcHandlers, handlers...)
140177
c.ipcMu.Unlock()
141178
}
142179

180+
// RegisterService adds a new service to the Core.
143181
func (c *Core) RegisterService(name string, api any) error {
144182
if c.servicesLocked {
145183
return fmt.Errorf("core: service %q is not permitted by the serviceLock setting", name)
@@ -156,6 +194,8 @@ func (c *Core) RegisterService(name string, api any) error {
156194
return nil
157195
}
158196

197+
// Service retrieves a registered service by name.
198+
// It returns nil if the service is not found.
159199
func (c *Core) Service(name string) any {
160200
c.serviceMu.RLock()
161201
api, ok := c.services[name]
@@ -191,6 +231,7 @@ func MustServiceFor[T any](c *Core, name string) T {
191231
}
192232

193233
// App returns the global application instance.
234+
// It panics if the Core has not been initialized.
194235
func App() *application.App {
195236
if instance == nil {
196237
panic("core.App() called before core.Setup() was successfully initialized")
@@ -210,8 +251,10 @@ func (c *Core) Display() Display {
210251
return display
211252
}
212253

254+
// Core returns the Core instance itself.
213255
func (c *Core) Core() *Core { return c }
214256

257+
// Assets returns the embedded filesystem containing the application's assets.
215258
func (c *Core) Assets() embed.FS {
216259
return c.assets
217260
}

interfaces.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,19 @@ import (
1212
// in the Core framework. Services depend on these interfaces, not on
1313
// concrete implementations.
1414

15+
// Contract specifies the operational guarantees that the Core and its services must adhere to.
16+
// This is used for configuring panic handling and other resilience features.
1517
type Contract struct {
16-
DontPanic bool
18+
// DontPanic, if true, instructs the Core to recover from panics and return an error instead.
19+
DontPanic bool
20+
// DisableLogging, if true, disables all logging from the Core and its services.
1721
DisableLogging bool
1822
}
1923

2024
// Features provides a way to check if a feature is enabled.
25+
// This is used for feature flagging and conditional logic.
2126
type Features struct {
27+
// Flags is a list of enabled feature flags.
2228
Flags []string
2329
}
2430

@@ -31,8 +37,16 @@ func (f *Features) IsEnabled(feature string) bool {
3137
}
3238
return false
3339
}
40+
41+
// Option is a function that configures the Core.
42+
// This is used to apply settings and register services during initialization.
3443
type Option func(*Core) error
44+
45+
// Message is the interface for all messages that can be sent through the Core's IPC system.
46+
// Any struct can be a message, allowing for structured data to be passed between services.
3547
type Message interface{}
48+
49+
// Core is the central application object that manages services, assets, and communication.
3650
type Core struct {
3751
once sync.Once
3852
initErr error
@@ -51,7 +65,9 @@ var instance *Core
5165

5266
// Config provides access to application configuration.
5367
type Config interface {
68+
// Get retrieves a configuration value by key and stores it in the 'out' variable.
5469
Get(key string, out any) error
70+
// Set stores a configuration value by key.
5571
Set(key string, v any) error
5672
}
5773

@@ -66,23 +82,29 @@ type WindowConfig struct {
6682

6783
// WindowOption configures window creation.
6884
type WindowOption interface {
85+
// Apply applies the window option to the given configuration.
6986
Apply(*WindowConfig)
7087
}
7188

7289
// Display manages windows and UI.
7390
type Display interface {
91+
// OpenWindow creates and displays a new window with the given options.
7492
OpenWindow(opts ...WindowOption) error
7593
}
7694

7795
// Help manages the in-app documentation and help system.
7896
type Help interface {
97+
// Show displays the main help topic.
7998
Show() error
99+
// ShowAt displays the help topic for the given anchor.
80100
ShowAt(anchor string) error
81101
}
82102

83103
// Crypt provides cryptographic functions.
84104
type Crypt interface {
105+
// EncryptPGP encrypts data using PGP and writes the result to the given writer.
85106
EncryptPGP(writer io.Writer, recipientPath, data string, signerPath, signerPassphrase *string) (string, error)
107+
// DecryptPGP decrypts a PGP message.
86108
DecryptPGP(recipientPath, message, passphrase string, signerPath *string) (string, error)
87109
}
88110

@@ -96,8 +118,12 @@ type I18n interface {
96118

97119
// Workspace manages user workspaces.
98120
type Workspace interface {
121+
// CreateWorkspace creates a new workspace with the given identifier and password.
99122
CreateWorkspace(identifier, password string) (string, error)
123+
// SwitchWorkspace changes the active workspace.
100124
SwitchWorkspace(name string) error
125+
// WorkspaceFileGet retrieves the content of a file from the current workspace.
101126
WorkspaceFileGet(filename string) (string, error)
127+
// WorkspaceFileSet writes content to a file in the current workspace.
102128
WorkspaceFileSet(filename, content string) error
103129
}

runtime.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,28 @@
11
package core
22

33
// ServiceRuntime is a helper struct embedded in services to provide access to the core application.
4+
// It is generic and can be parameterized with a service-specific options struct.
45
type ServiceRuntime[T any] struct {
56
core *Core
67
opts T
78
}
89

910
// NewServiceRuntime creates a new ServiceRuntime instance for a service.
11+
// This is typically called by a service's constructor.
1012
func NewServiceRuntime[T any](c *Core, opts T) *ServiceRuntime[T] {
1113
return &ServiceRuntime[T]{
1214
core: c,
1315
opts: opts,
1416
}
1517
}
1618

17-
// Core returns the central core instance.
19+
// Core returns the central core instance, providing access to all registered services.
1820
func (r *ServiceRuntime[T]) Core() *Core {
1921
return r.core
2022
}
2123

2224
// Config returns the registered Config service from the core application.
25+
// This is a convenience method for accessing the application's configuration.
2326
func (r *ServiceRuntime[T]) Config() Config {
2427
return r.core.Config()
2528
}

runtime_pkg.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,19 @@ import (
1010

1111
// Runtime is the container that holds all instantiated services.
1212
// Its fields are the concrete types, allowing Wails to bind them directly.
13+
// This struct is the primary entry point for the Wails application.
1314
type Runtime struct {
1415
app *application.App
1516
Core *Core
1617
}
1718

1819
// ServiceFactory defines a function that creates a service instance.
20+
// This is used to decouple the service creation from the runtime initialization.
1921
type ServiceFactory func() (any, error)
2022

2123
// NewWithFactories creates a new Runtime instance using the provided service factories.
24+
// This is the most flexible way to create a new Runtime, as it allows for
25+
// the registration of any number of services.
2226
func NewWithFactories(app *application.App, factories map[string]ServiceFactory) (*Runtime, error) {
2327
services := make(map[string]any)
2428
coreOpts := []Option{
@@ -58,6 +62,8 @@ func NewWithFactories(app *application.App, factories map[string]ServiceFactory)
5862
}
5963

6064
// NewRuntime creates and wires together all application services.
65+
// This is the simplest way to create a new Runtime, but it does not allow for
66+
// the registration of any custom services.
6167
func NewRuntime(app *application.App) (*Runtime, error) {
6268
return NewWithFactories(app, map[string]ServiceFactory{})
6369
}
@@ -68,11 +74,13 @@ func (r *Runtime) ServiceName() string {
6874
}
6975

7076
// ServiceStartup is called by Wails at application startup.
77+
// This is where the Core's startup lifecycle is initiated.
7178
func (r *Runtime) ServiceStartup(ctx context.Context, options application.ServiceOptions) {
7279
r.Core.ServiceStartup(ctx, options)
7380
}
7481

7582
// ServiceShutdown is called by Wails at application shutdown.
83+
// This is where the Core's shutdown lifecycle is initiated.
7684
func (r *Runtime) ServiceShutdown(ctx context.Context) {
7785
if r.Core != nil {
7886
r.Core.ServiceShutdown(ctx)

0 commit comments

Comments
 (0)