Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into refactor/optional-err…
Browse files Browse the repository at this point in the history
…handler
  • Loading branch information
ahmetcanozcan committed Sep 1, 2024
2 parents dc4f089 + 227cb12 commit 98dc728
Show file tree
Hide file tree
Showing 82 changed files with 1,287 additions and 1,100 deletions.
33 changes: 33 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: build
on:
push:
branches:
- '**'

permissions:
contents: read

jobs:
build:
name: Lint, Build, and Test
runs-on: ubuntu-latest
timeout-minutes: 2
strategy:
matrix:
go-version: [1.21, 1.22, 1.23]
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: ${{ matrix.go-version }}
- name: Install Dependencies
run: go mod download
- name: Lint
uses: golangci/golangci-lint-action@v6
with:
version: v1.60
args: --timeout=5m -v
- name: Build
run: go build -v ./...
- name: Test
run: go test -v ./...
23 changes: 0 additions & 23 deletions .github/workflows/golangci-lint.yml

This file was deleted.

1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
test

vendor
.idea
.vscode
48 changes: 48 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
linters-settings:
lll:
line-length: 140
funlen:
lines: 70

linters:
disable-all: true
enable:
- bodyclose
- errcheck
- dupl
- exhaustive
- funlen
- goconst
- gocritic
- gocyclo
- gosimple
- govet
- gosec
- ineffassign
- lll
- misspell
- nakedret
- gofumpt
- staticcheck
- stylecheck
- typecheck
- unconvert
- unparam
- unused
- whitespace
- prealloc

issues:
exclude-rules:
- path: '(.+)_test\.go'
linters:
- funlen
- goconst
- lll
exclude-dirs:
- example

service:
golangci-lint-version: 1.60.3
prepare:
- echo "here I can run custom commands, but no preparation needed for this repo"
238 changes: 119 additions & 119 deletions app.go
Original file line number Diff line number Diff line change
@@ -1,119 +1,119 @@
package chaki

import (
"github.com/Trendyol/chaki/internal/di"
"github.com/Trendyol/chaki/module"
"github.com/Trendyol/chaki/util/slc"
)

type App struct {
injector *di.Injector
options []Option
provides []any
invokes []any
hooks []module.ProvideHook
modules []*module.Module
}

func New() *App {
app := &App{
injector: di.NewInjector(),
}

return app
}

func (app *App) WithOption(opts ...Option) *App {
app.options = append(app.options, opts...)
return app
}

func (app *App) Provide(ctr ...any) *App {
ctr = slc.Map(ctr, app.wrapProvideCtr)
app.provides = append(app.provides, ctr...)
return app
}

func (app *App) wrapProvideCtr(ctr any) any {
for _, a := range app.hooks {
if a.Match(ctr) {
return a.Wrap(ctr)
}
}

return ctr
}

func (app *App) Invoke(ctr ...any) *App {
app.invokes = append(app.invokes, ctr...)
return app
}

func (app *App) Use(modules ...*module.Module) {
for _, m := range modules {
h := m.Meta().ProvideHooks
app.hooks = append(app.hooks, h...)
}

app.modules = append(app.modules, modules...)
}

func (app *App) applyStdModules(o *options) error {
cfgm, err := configModule(o.configOptions)
if err != nil {
return err
}

app.Use(
cfgm,
loggerModule(),
)

return nil
}

func (app *App) Start(opts ...Option) error {
opts = append(app.options, opts...)
o := getOptions(opts...)

if err := app.applyStdModules(o); err != nil {
return err
}

modules := slc.Map(app.modules, func(m *module.Module) string {
return m.Name()
})

for _, m := range app.modules {
meta := m.Meta(modules...)
app.injector.Provide(slc.Map(meta.Provides, app.wrapProvideCtr)...)
app.injector.Invoke(meta.Invokes...)
}

app.injector.Provide(app.provides...)
app.injector.Invoke(app.invokes...)

return app.injector.Start(o.timeout)
}

var defaultApp *App = New()

func Start(p ...Option) error {
return defaultApp.Start(p...)
}

func Provide(ctr ...any) {
defaultApp.Provide(ctr...)
}

func Invoke(ctr ...any) {
defaultApp.Invoke(ctr...)
}

func Replacer[T any](t T) func(T) T {
return func(_ T) T { return t }
}

func Valuer[T any](t T) func() T {
return func() T { return t }
}
package chaki

import (
"github.com/Trendyol/chaki/internal/di"
"github.com/Trendyol/chaki/module"
"github.com/Trendyol/chaki/util/slc"
)

type App struct {
injector *di.Injector
options []Option
provides []any
invokes []any
hooks []module.ProvideHook
modules []*module.Module
}

func New() *App {
app := &App{
injector: di.NewInjector(),
}

return app
}

func (app *App) WithOption(opts ...Option) *App {
app.options = append(app.options, opts...)
return app
}

func (app *App) Provide(ctr ...any) *App {
ctr = slc.Map(ctr, app.wrapProvideCtr)
app.provides = append(app.provides, ctr...)
return app
}

func (app *App) wrapProvideCtr(ctr any) any {
for _, a := range app.hooks {
if a.Match(ctr) {
return a.Wrap(ctr)
}
}

return ctr
}

func (app *App) Invoke(ctr ...any) *App {
app.invokes = append(app.invokes, ctr...)
return app
}

func (app *App) Use(modules ...*module.Module) {
for _, m := range modules {
h := m.Meta().ProvideHooks
app.hooks = append(app.hooks, h...)
}

app.modules = append(app.modules, modules...)
}

func (app *App) applyStdModules(o *options) error {
cfgm, err := configModule(o.configOptions)
if err != nil {
return err
}

app.Use(
cfgm,
loggerModule(),
)

return nil
}

func (app *App) Start(opts ...Option) error {
opts = append(app.options, opts...)
o := getOptions(opts...)

if err := app.applyStdModules(o); err != nil {
return err
}

modules := slc.Map(app.modules, func(m *module.Module) string {
return m.Name()
})

for _, m := range app.modules {
meta := m.Meta(modules...)
app.injector.Provide(slc.Map(meta.Provides, app.wrapProvideCtr)...)
app.injector.Invoke(meta.Invokes...)
}

app.injector.Provide(app.provides...)
app.injector.Invoke(app.invokes...)

return app.injector.Start(o.timeout)
}

var defaultApp *App = New()

func Start(p ...Option) error {
return defaultApp.Start(p...)
}

func Provide(ctr ...any) {
defaultApp.Provide(ctr...)
}

func Invoke(ctr ...any) {
defaultApp.Invoke(ctr...)
}

func Replacer[T any](t T) func(T) T {
return func(_ T) T { return t }
}

func Valuer[T any](t T) func() T {
return func() T { return t }
}
2 changes: 0 additions & 2 deletions as/as_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ func (f *fooImpl) Bar() string {
}

func Test_asser_Match(t *testing.T) {

t.Run("it should match struct type correctly when correct constructor provided", func(t *testing.T) {
// Given
asser := Struct[string]("testf")
Expand Down Expand Up @@ -65,5 +64,4 @@ func Test_asser_Match(t *testing.T) {
// Then
assert.Equal(t, true, res)
})

}
Loading

0 comments on commit 98dc728

Please sign in to comment.