Skip to content

Commit

Permalink
change iters
Browse files Browse the repository at this point in the history
  • Loading branch information
mkideal committed Aug 17, 2024
1 parent 8a66cbf commit 1229499
Show file tree
Hide file tree
Showing 15 changed files with 679 additions and 119 deletions.
4 changes: 3 additions & 1 deletion builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,12 @@ type buildInfo struct {
DateTime string
}

var runtimeVersion = runtime.Version

// String returns a formatted string containing all build information.
func (info buildInfo) String() string {
return fmt.Sprintf("%s %s(%s: %s) built at %s by %s",
info.Name, info.Version, info.Branch, info.Commit, info.DateTime, runtime.Version())
info.Name, info.Version, info.Branch, info.Commit, info.DateTime, runtimeVersion())
}

// Info returns a struct containing the build information.
Expand Down
82 changes: 82 additions & 0 deletions builder/builder_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package builder

import "testing"

func TestInfo_String(t *testing.T) {
runtimeVersion = func() string {
return "go.test"
}
tests := []struct {
name string
info buildInfo
want string
}{
{
name: "empty",
info: buildInfo{},
want: " (: ) built at by go.test",
},
{
name: "full",
info: buildInfo{
Name: "app",
Version: "v1.0.0",
Branch: "main",
Commit: "abcdefg",
DateTime: "2021-01-01T00:00:00Z",
},
want: "app v1.0.0(main: abcdefg) built at 2021-01-01T00:00:00Z by go.test",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.info.String(); got != tt.want {
t.Errorf("buildInfo.String() = %q, want %q", got, tt.want)
}
})
}
}

func TestInfo(t *testing.T) {
defaultAppName := appName()
tests := []struct {
name string
want buildInfo
}{
{
name: "empty",
want: buildInfo{
Name: "",
Version: "",
Branch: "",
Commit: "",
DateTime: "",
},
},
{
name: "full",
want: buildInfo{
Name: "app",
Version: "v1.0.0",
Branch: "main",
Commit: "abcdefg",
DateTime: "2021-01-01T00:00:00Z",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
name = tt.want.Name
version = tt.want.Version
branch = tt.want.Branch
commit = tt.want.Commit
datetime = tt.want.DateTime
if name == "" {
tt.want.Name = defaultAppName
}
if got := Info(); got != tt.want {
t.Errorf("Info() = %v, want %v", got, tt.want)
}
})
}
}
79 changes: 65 additions & 14 deletions component/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,28 +77,20 @@ type Resolver interface {
Resolve(Container) error
}

// BaseComponent provides a basic implementation of the Component interface.
type BaseComponent[T any] struct {
lifecycle.BaseLifecycle

options T
type simpleComponent struct {
funcs lifecycle.Funcs
identifier string
container Container
logger atomic.Pointer[slog.Logger]
}

// Options returns a pointer to the component's options.
func (c *BaseComponent[T]) Options() *T {
return &c.options
}

// String implements the fmt.Stringer interface.
func (c *BaseComponent[T]) String() string {
func (c *simpleComponent) String() string {
return c.identifier
}

// Logger implements the Component Logger method.
func (c *BaseComponent[T]) Logger() *slog.Logger {
func (c *simpleComponent) Logger() *slog.Logger {
currentLogger := c.logger.Load()
latestLogger := c.container.Logger()
if currentLogger != latestLogger {
Expand All @@ -109,7 +101,7 @@ func (c *BaseComponent[T]) Logger() *slog.Logger {
}

// Setup implements the Component Setup method.
func (c *BaseComponent[T]) Setup(container Container, config Config) error {
func (c *simpleComponent) Setup(container Container, config Config) error {
c.container = container
if config.UUID != "" {
if strings.Contains(config.UUID, config.Name) {
Expand All @@ -120,7 +112,57 @@ func (c *BaseComponent[T]) Setup(container Container, config Config) error {
} else {
c.identifier = config.Name
}
return nil
}

// Init implements the Component Init method.
func (c *simpleComponent) Init(ctx context.Context) error {
if c.funcs.Init != nil {
return c.funcs.Init(ctx)
}
return nil
}

// Start implements the Component Start method.
func (c *simpleComponent) Start(ctx context.Context) error {
if c.funcs.Start != nil {
return c.funcs.Start(ctx)
}
return nil
}

// Shutdown implements the Component Shutdown method.
func (c *simpleComponent) Shutdown(ctx context.Context) error {
if c.funcs.Shutdown != nil {
return c.funcs.Shutdown(ctx)
}
return nil
}

// Uninit implements the Component Uninit method.
func (c *simpleComponent) Uninit(ctx context.Context) error {
if c.funcs.Uninit != nil {
return c.funcs.Uninit(ctx)
}
return nil
}

// BaseComponent provides a basic implementation of the Component interface.
type BaseComponent[T any] struct {
simpleComponent
options T
}

// Options returns a pointer to the component's options.
func (c *BaseComponent[T]) Options() *T {
return &c.options
}

// Setup implements the Component Setup method.
func (c *BaseComponent[T]) Setup(container Container, config Config) error {
if err := c.simpleComponent.Setup(container, config); err != nil {
return err
}
if err := config.Options.Decode(json.Unmarshal, &c.options); err != nil {
return fmt.Errorf("failed to unmarshal options: %w", err)
}
Expand All @@ -136,8 +178,8 @@ func (c *BaseComponent[T]) Setup(container Container, config Config) error {

// Reference represents a reference to another component.
type Reference[T any] struct {
component T
uuid string
component T
}

// Ref creates a reference to a component with the given UUID.
Expand Down Expand Up @@ -395,6 +437,15 @@ func Register(name string, creator func() Component) {
creators[name] = creator
}

// RegisterFuncs registers a component creator with lifecycle functions.
func RegisterFuncs(name string, funcs lifecycle.Funcs) {
Register(name, func() Component {
return &simpleComponent{
funcs: funcs,
}
})
}

// Create creates a new component by its name.
func Create(name string) (Component, error) {
creatorsMu.RLock()
Expand Down
5 changes: 5 additions & 0 deletions constraints/constraints.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,8 @@ type Number interface {
type Field interface {
Float | Complex
}

// Addable is a constraint that permits any number or string type.
type Addable interface {
Number | string
}
23 changes: 23 additions & 0 deletions constraints/constraints_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package constraints_test

import (
"fmt"

"github.com/gopherd/core/constraints"
)

func sum[T constraints.Addable](values ...T) T {
var result T
for _, v := range values {
result += v
}
return result
}

func ExampleAddable() {
fmt.Println(sum(1, 2))
fmt.Println(sum("hello", " ", "world"))
// Output:
// 3
// hello world
}
Loading

0 comments on commit 1229499

Please sign in to comment.