Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# This workflow will build a golang project
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go

name: Go

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: '1.24'

- name: Build
run: go build -v ./...

test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: '1.24'

- name: Test
run: go test -coverprofile=coverage.out ./...

- name: Upload coverage report
uses: codecov/codecov-action@v5
with:
token: ${{ secrets.CODECOV_TOKEN }}
files: coverage.out
fail_ci_if_error: true

lint:
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: '1.24'

- name: Install golangci-lint
run: |
go install github.com/golangci/golangci-lint/cmd/[email protected]

- name: Run golangci-lint
run: $(go env GOPATH)/bin/golangci-lint run
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea
152 changes: 152 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
linters-settings:
gosec:
excludes:
- G306
- G115
depguard:
# new configuration
rules:
logger:
deny:
# logging is allowed only by logutils.Log,
# logrus is allowed to use only in logutils package.
- pkg: "github.com/sirupsen/logrus"
desc: logging is allowed only by logutils.Log
dupl:
threshold: 100
funlen:
lines: -1 # the number of lines (code + empty lines) is not a right metric and leads to code without empty line or one-liner.
statements: 75
goconst:
min-len: 2
min-occurrences: 3
gocritic:
enabled-tags:
- diagnostic
- experimental
- opinionated
- performance
- style
disabled-checks:
- dupImport # https://github.com/go-critic/go-critic/issues/845
- ifElseChain
- octalLiteral
- whyNoLint
gocyclo:
min-complexity: 15
gofmt:
rewrite-rules:
- pattern: 'interface{}'
replacement: 'any'
goimports:
local-prefixes: github.com/golangci/golangci-lint

govet:
settings:
printf:
funcs:
- (github.com/golangci/golangci-lint/pkg/logutils.Log).Infof
- (github.com/golangci/golangci-lint/pkg/logutils.Log).Warnf
- (github.com/golangci/golangci-lint/pkg/logutils.Log).Errorf
- (github.com/golangci/golangci-lint/pkg/logutils.Log).Fatalf
enable:
- nilness
- shadow
errorlint:
asserts: false
lll:
line-length: 140
nolintlint:
allow-unused: false # report any unused nolint directives
require-explanation: false # don't require an explanation for nolint directives
require-specific: false # don't require nolint directives to be specific about which linter is being skipped
revive:
rules:
- name: unexported-return
disabled: true
- name: unused-parameter

linters:
disable-all: true
enable:
- bodyclose
- depguard
- dogsled
- dupl
- errcheck
- errorlint
- funlen
- gocheckcompilerdirectives
- gochecknoinits
- goconst
- gocritic
- gocyclo
- gofmt
- goimports
- goprintffuncname
- gosec
- gosimple
- govet
- ineffassign
- lll
- nakedret
- noctx
- nolintlint
- revive
- staticcheck
- stylecheck
- typecheck
- unconvert
- unparam
- unused
- whitespace

# don't enable:
# - asciicheck
# - scopelint
# - gochecknoglobals
# - gocognit
# - godot
# - godox
# - goerr113
# - interfacer
# - maligned
# - nestif
# - prealloc
# - testpackage
# - wsl

issues:
# Excluding configuration per-path, per-linter, per-text and per-source
exclude-rules:
- path: pkg/golinters/errcheck.go
text: "SA1019: errCfg.Exclude is deprecated: use ExcludeFunctions instead"
- path: pkg/commands/run.go
text: "SA1019: lsc.Errcheck.Exclude is deprecated: use ExcludeFunctions instead"
- path: pkg/commands/run.go
text: "SA1019: e.cfg.Run.Deadline is deprecated: Deadline exists for historical compatibility and should not be used."

- path: pkg/golinters/gofumpt.go
text: "SA1019: settings.LangVersion is deprecated: use the global `run.go` instead."
- path: pkg/golinters/staticcheck_common.go
text: "SA1019: settings.GoVersion is deprecated: use the global `run.go` instead."
- path: pkg/lint/lintersdb/manager.go
text: "SA1019: (.+).(GoVersion|LangVersion) is deprecated: use the global `run.go` instead."
- path: pkg/golinters/unused.go
text: "rangeValCopy: each iteration copies 160 bytes \\(consider pointers or indexing\\)"
- path: test/(fix|linters)_test.go
text: "string `gocritic.go` has 3 occurrences, make it a constant"

# Due to a change inside go-critic v0.10.0, some reports have been removed,
# but as we run analysis with the previous version of golangci-lint this leads to a paradoxical situation.
# This exclusion will be removed when the next version of golangci-lint (v1.56.0) will be released.
- path: pkg/golinters/nolintlint/nolintlint.go
text: "hugeParam: (i|b) is heavy \\(\\d+ bytes\\); consider passing it by pointer"
exclude-dirs:
- test/testdata_etc # test files
- internal/cache # extracted from Go code
- internal/renameio # extracted from Go code
- internal/robustio # extracted from Go code

run:
timeout: 5m
52 changes: 52 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# go-inject

## Description

`go-inject` is a dependency injection library that support contextual scope.

## Example

```go
package myapp

import (
"context"

"github.com/illuin-tech/goinject"
)

type key int
const myScopeKey key = 0

const MyScope = "MyScope"

// define function to declare your own scope in context
func WithMyScopeEnabled(ctx context.Context) context.Context {
return goinject.WithContextualScopeEnabled(ctx, myScopeKey)
}

func ShutdownMyContextScoped(ctx context.Context) {
goinject.ShutdownContextualScope(ctx, myScopeKey)
}

// define injection modules
var Module = goinject.Module("myModule",
goinject.RegisterScope(MyScope, goinject.NewContextualScope(myScopeKey)),
goinject.Provide(func() string {
return "Hello world from scope"
}, goinject.In(MyScope)),
)

func main() {
ctx := context.Background()

// enable scope
ctx = WithMyScopeEnabled(ctx)
defer ShutdownMyContextScoped(ctx)

injector, _ := goinject.NewInjector(Module)
_ = injector.Invoke(ctx, func(hello string) {
println(hello)
})
}
```
36 changes: 36 additions & 0 deletions binding.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package goinject

import (
"context"
"fmt"
"reflect"
)

// binding defines a type mapped to a more concrete type
type binding struct {
typeof reflect.Type
provider reflect.Value
providedType reflect.Type
annotatedWith string
scope string
destroyMethod func(value reflect.Value)
}

func (b *binding) create(ctx context.Context, injector *Injector) (reflect.Value, error) {
res, err := injector.callFunctionWithArgumentInstance(ctx, b.provider)
if err != nil {
return reflect.Value{},
fmt.Errorf("failed to call provider function for type %q: %w", b.providedType.String(), err)
}
if b.provider.Type().NumOut() == 2 {
errValue := res[1].Interface()
if errValue != nil {
err, _ = errValue.(error)
}
}
if err != nil {
return res[0], fmt.Errorf("provider for type %q returned error: %w", b.providedType.String(), err)
} else {
return res[0], nil
}
}
29 changes: 29 additions & 0 deletions condition.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package goinject

import "os"

type Conditional interface {
evaluate() bool
}

type environmentVariableConditional struct {
name string
havingValue string
matchIfMissing bool
}

func (c *environmentVariableConditional) evaluate() bool {
val, ok := os.LookupEnv(c.name)
if !ok {
return c.matchIfMissing
}
return val == c.havingValue
}

func OnEnvironmentVariable(name, havingValue string, matchIfMissing bool) Conditional {
return &environmentVariableConditional{
name: name,
havingValue: havingValue,
matchIfMissing: matchIfMissing,
}
}
Loading