refactor: Flatten repository structure (#28)
- Move Go files from core, e, and runtime directories to the project root. - Unify package declarations to a single 'core' package. - Update go.work to exclude the cmd directory from the main build. - Resolve naming conflicts and update import paths. - Fix tests to work with the new structure. Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
This commit is contained in:
parent
67a38acc7c
commit
003996b148
11 changed files with 47 additions and 52 deletions
|
|
@ -52,7 +52,7 @@ func WithService(factory func(*Core) (any, error)) Option {
|
|||
}
|
||||
pkgPath := typeOfService.PkgPath()
|
||||
parts := strings.Split(pkgPath, "/")
|
||||
name := parts[len(parts)-1]
|
||||
name := strings.ToLower(parts[len(parts)-1])
|
||||
|
||||
// --- IPC Handler Discovery ---
|
||||
instanceValue := reflect.ValueOf(serviceInstance)
|
||||
|
|
@ -1,25 +0,0 @@
|
|||
package core
|
||||
|
||||
// Runtime is a helper struct embedded in services to provide access to the core application.
|
||||
type Runtime[T any] struct {
|
||||
core *Core
|
||||
opts T
|
||||
}
|
||||
|
||||
// NewRuntime creates a new Runtime instance for a service.
|
||||
func NewRuntime[T any](c *Core, opts T) *Runtime[T] {
|
||||
return &Runtime[T]{
|
||||
core: c,
|
||||
opts: opts,
|
||||
}
|
||||
}
|
||||
|
||||
// Core returns the central core instance.
|
||||
func (r *Runtime[T]) Core() *Core {
|
||||
return r.core
|
||||
}
|
||||
|
||||
// Config returns the registered Config service from the core application.
|
||||
func (r *Runtime[T]) Config() Config {
|
||||
return r.core.Config()
|
||||
}
|
||||
|
|
@ -53,14 +53,14 @@ func TestCore_WithWails_Good(t *testing.T) {
|
|||
assert.Equal(t, app, c.App)
|
||||
}
|
||||
|
||||
//go:embed testdata
|
||||
//go:embed core/testdata
|
||||
var testFS embed.FS
|
||||
|
||||
func TestCore_WithAssets_Good(t *testing.T) {
|
||||
c, err := New(WithAssets(testFS))
|
||||
assert.NoError(t, err)
|
||||
assets := c.Assets()
|
||||
file, err := assets.Open("testdata/test.txt")
|
||||
file, err := assets.Open("core/testdata/test.txt")
|
||||
assert.NoError(t, err)
|
||||
defer file.Close()
|
||||
content, err := io.ReadAll(file)
|
||||
|
|
@ -13,7 +13,7 @@
|
|||
// that is more informative than a raw stack trace.
|
||||
// - Consistent error handling: Encourages a uniform approach to error
|
||||
// handling across the entire codebase.
|
||||
package e
|
||||
package core
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package e
|
||||
package core
|
||||
|
||||
import (
|
||||
"errors"
|
||||
3
go.work
3
go.work
|
|
@ -2,7 +2,4 @@ go 1.25
|
|||
|
||||
use (
|
||||
.
|
||||
./cmd/core
|
||||
./cmd/core-gui
|
||||
./cmd/examples/core-static-di
|
||||
)
|
||||
|
|
|
|||
25
runtime.go
Normal file
25
runtime.go
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
package core
|
||||
|
||||
// ServiceRuntime is a helper struct embedded in services to provide access to the core application.
|
||||
type ServiceRuntime[T any] struct {
|
||||
core *Core
|
||||
opts T
|
||||
}
|
||||
|
||||
// NewServiceRuntime creates a new ServiceRuntime instance for a service.
|
||||
func NewServiceRuntime[T any](c *Core, opts T) *ServiceRuntime[T] {
|
||||
return &ServiceRuntime[T]{
|
||||
core: c,
|
||||
opts: opts,
|
||||
}
|
||||
}
|
||||
|
||||
// Core returns the central core instance.
|
||||
func (r *ServiceRuntime[T]) Core() *Core {
|
||||
return r.core
|
||||
}
|
||||
|
||||
// Config returns the registered Config service from the core application.
|
||||
func (r *ServiceRuntime[T]) Config() Config {
|
||||
return r.core.Config()
|
||||
}
|
||||
|
|
@ -1,10 +1,9 @@
|
|||
package runtime
|
||||
package core
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/Snider/Core/core"
|
||||
"github.com/wailsapp/wails/v3/pkg/application"
|
||||
)
|
||||
|
||||
|
|
@ -12,7 +11,7 @@ import (
|
|||
// Its fields are the concrete types, allowing Wails to bind them directly.
|
||||
type Runtime struct {
|
||||
app *application.App
|
||||
Core *core.Core
|
||||
Core *Core
|
||||
}
|
||||
|
||||
// ServiceFactory defines a function that creates a service instance.
|
||||
|
|
@ -21,8 +20,8 @@ type ServiceFactory func() (any, error)
|
|||
// NewWithFactories creates a new Runtime instance using the provided service factories.
|
||||
func NewWithFactories(app *application.App, factories map[string]ServiceFactory) (*Runtime, error) {
|
||||
services := make(map[string]any)
|
||||
coreOpts := []core.Option{
|
||||
core.WithWails(app),
|
||||
coreOpts := []Option{
|
||||
WithWails(app),
|
||||
}
|
||||
|
||||
for _, name := range []string{} {
|
||||
|
|
@ -36,10 +35,10 @@ func NewWithFactories(app *application.App, factories map[string]ServiceFactory)
|
|||
}
|
||||
services[name] = svc
|
||||
svcCopy := svc
|
||||
coreOpts = append(coreOpts, core.WithName(name, func(c *core.Core) (any, error) { return svcCopy, nil }))
|
||||
coreOpts = append(coreOpts, WithName(name, func(c *Core) (any, error) { return svcCopy, nil }))
|
||||
}
|
||||
|
||||
coreInstance, err := core.New(coreOpts...)
|
||||
coreInstance, err := New(coreOpts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
@ -54,8 +53,8 @@ func NewWithFactories(app *application.App, factories map[string]ServiceFactory)
|
|||
return rt, nil
|
||||
}
|
||||
|
||||
// New creates and wires together all application services.
|
||||
func New(app *application.App) (*Runtime, error) {
|
||||
// NewRuntime creates and wires together all application services.
|
||||
func NewRuntime(app *application.App) (*Runtime, error) {
|
||||
return NewWithFactories(app, map[string]ServiceFactory{})
|
||||
}
|
||||
|
||||
|
|
@ -1,28 +1,27 @@
|
|||
package runtime_test
|
||||
package core
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/Snider/Core/runtime"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/wailsapp/wails/v3/pkg/application"
|
||||
)
|
||||
|
||||
func TestNew(t *testing.T) {
|
||||
func TestNewRuntime(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
app *application.App
|
||||
factories map[string]runtime.ServiceFactory
|
||||
factories map[string]ServiceFactory
|
||||
expectErr bool
|
||||
expectErrStr string
|
||||
checkRuntime func(*testing.T, *runtime.Runtime)
|
||||
checkRuntime func(*testing.T, *Runtime)
|
||||
}{
|
||||
{
|
||||
name: "Good path",
|
||||
app: nil,
|
||||
factories: map[string]runtime.ServiceFactory{},
|
||||
factories: map[string]ServiceFactory{},
|
||||
expectErr: false,
|
||||
checkRuntime: func(t *testing.T, rt *runtime.Runtime) {
|
||||
checkRuntime: func(t *testing.T, rt *Runtime) {
|
||||
assert.NotNil(t, rt)
|
||||
assert.NotNil(t, rt.Core)
|
||||
},
|
||||
|
|
@ -30,9 +29,9 @@ func TestNew(t *testing.T) {
|
|||
{
|
||||
name: "With non-nil app",
|
||||
app: &application.App{},
|
||||
factories: map[string]runtime.ServiceFactory{},
|
||||
factories: map[string]ServiceFactory{},
|
||||
expectErr: false,
|
||||
checkRuntime: func(t *testing.T, rt *runtime.Runtime) {
|
||||
checkRuntime: func(t *testing.T, rt *Runtime) {
|
||||
assert.NotNil(t, rt)
|
||||
assert.NotNil(t, rt.Core)
|
||||
assert.NotNil(t, rt.Core.App)
|
||||
|
|
@ -42,7 +41,7 @@ func TestNew(t *testing.T) {
|
|||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
rt, err := runtime.NewWithFactories(tc.app, tc.factories)
|
||||
rt, err := NewRuntime(tc.app)
|
||||
|
||||
if tc.expectErr {
|
||||
assert.Error(t, err)
|
||||
Loading…
Add table
Reference in a new issue