diff --git a/.gitea/ISSUE_TEMPLATE/bug_report.md b/.gitea/ISSUE_TEMPLATE/bug_report.md new file mode 100644 index 00000000..a77fdfdf --- /dev/null +++ b/.gitea/ISSUE_TEMPLATE/bug_report.md @@ -0,0 +1,18 @@ +--- +name: Bug report +about: For reporting bugs in micro +title: "[BUG]" +labels: '' +assignees: '' + +--- + +**Describe the bug** + +1. What are you trying to do? +2. What did you expect to happen? +3. What happens instead? + +**How to reproduce the bug:** + +If possible, please include a minimal code snippet here. diff --git a/.gitea/ISSUE_TEMPLATE/feature-request---enhancement.md b/.gitea/ISSUE_TEMPLATE/feature-request---enhancement.md new file mode 100644 index 00000000..c722ea6f --- /dev/null +++ b/.gitea/ISSUE_TEMPLATE/feature-request---enhancement.md @@ -0,0 +1,17 @@ +--- +name: Feature request / Enhancement +about: If you have a need not served by micro +title: "[FEATURE]" +labels: '' +assignees: '' + +--- + +**Is your feature request related to a problem? Please describe.** +A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] + +**Describe the solution you'd like** +A clear and concise description of what you want to happen. + +**Additional context** +Add any other context or screenshots about the feature request here. \ No newline at end of file diff --git a/.gitea/ISSUE_TEMPLATE/question.md b/.gitea/ISSUE_TEMPLATE/question.md new file mode 100644 index 00000000..3d65c721 --- /dev/null +++ b/.gitea/ISSUE_TEMPLATE/question.md @@ -0,0 +1,8 @@ +--- +name: Question +about: Ask a question about micro +title: '' +labels: '' +assignees: '' + +--- \ No newline at end of file diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.gitea/PULL_REQUEST_TEMPLATE.md similarity index 100% rename from .github/PULL_REQUEST_TEMPLATE.md rename to .gitea/PULL_REQUEST_TEMPLATE.md diff --git a/.gitea/autoapprove.yml b/.gitea/autoapprove.yml new file mode 100644 index 00000000..ef43e490 --- /dev/null +++ b/.gitea/autoapprove.yml @@ -0,0 +1,28 @@ +name: "autoapprove" + +on: + pull_request_target: + types: [assigned, opened, synchronize, reopened] + workflow_run: + workflows: ["prbuild"] + types: + - completed + +permissions: + pull-requests: write + contents: write + +jobs: + autoapprove: + runs-on: ubuntu-latest + steps: + - name: approve + run: [ "curl -o tea https://dl.gitea.com/tea/main/tea-main-linux-amd64", + "chmod +x ./tea", + "./tea login add --name unistack --token ${{ secrets.GITHUB_TOKEN }} --url https://git.unistack.org", + "./tea pr --repo ${{ github.event.repository.name }}" + ] + if: github.actor == 'vtolstov' + id: approve + with: + github-token: ${{ secrets.GITHUB_TOKEN }} diff --git a/.gitea/workflows/lint.yml b/.gitea/workflows/lint.yml new file mode 100644 index 00000000..8da88945 --- /dev/null +++ b/.gitea/workflows/lint.yml @@ -0,0 +1,24 @@ +name: lint +on: + pull_request: + branches: + - master + - v3 +jobs: + lint: + name: lint + runs-on: ubuntu-latest + steps: + - name: setup-go + uses: https://gitea.com/actions/setup-go@v3 + with: + go-version: 1.18 + - name: checkout + uses: https://gitea.com/actions/checkout@v3 + - name: deps + run: go get -v -d ./... + - name: lint + uses: https://github.com/golangci/golangci-lint-action@v3.4.0 + continue-on-error: true + with: + version: v1.52 \ No newline at end of file diff --git a/.gitea/workflows/pr.yml b/.gitea/workflows/pr.yml new file mode 100644 index 00000000..ba947f0c --- /dev/null +++ b/.gitea/workflows/pr.yml @@ -0,0 +1,23 @@ +name: pr +on: + pull_request: + branches: + - master + - v3 +jobs: + test: + name: test + runs-on: ubuntu-latest + steps: + - name: checkout + uses: https://gitea.com/actions/checkout@v3 + - name: setup-go + uses: https://gitea.com/actions/setup-go@v3 + with: + go-version: 1.18 + - name: deps + run: go get -v -t -d ./... + - name: test + env: + INTEGRATION_TESTS: yes + run: go test -mod readonly -v ./... \ No newline at end of file diff --git a/.github/ISSUE_TEMPLATE/bug_report.md b/.github.old/ISSUE_TEMPLATE/bug_report.md similarity index 100% rename from .github/ISSUE_TEMPLATE/bug_report.md rename to .github.old/ISSUE_TEMPLATE/bug_report.md diff --git a/.github/ISSUE_TEMPLATE/feature-request---enhancement.md b/.github.old/ISSUE_TEMPLATE/feature-request---enhancement.md similarity index 100% rename from .github/ISSUE_TEMPLATE/feature-request---enhancement.md rename to .github.old/ISSUE_TEMPLATE/feature-request---enhancement.md diff --git a/.github/ISSUE_TEMPLATE/question.md b/.github.old/ISSUE_TEMPLATE/question.md similarity index 100% rename from .github/ISSUE_TEMPLATE/question.md rename to .github.old/ISSUE_TEMPLATE/question.md diff --git a/.github.old/PULL_REQUEST_TEMPLATE.md b/.github.old/PULL_REQUEST_TEMPLATE.md new file mode 100644 index 00000000..cba3cbcc --- /dev/null +++ b/.github.old/PULL_REQUEST_TEMPLATE.md @@ -0,0 +1,9 @@ +## Pull Request template +Please, go through these steps before clicking submit on this PR. + +1. Give a descriptive title to your PR. +2. Provide a description of your changes. +3. Make sure you have some relevant tests. +4. Put `closes #XXXX` in your comment to auto-close the issue that your PR fixes (if applicable). + +**PLEASE REMOVE THIS TEMPLATE BEFORE SUBMITTING** diff --git a/.github/dependabot.yml b/.github.old/dependabot.yml similarity index 100% rename from .github/dependabot.yml rename to .github.old/dependabot.yml diff --git a/.github/workflows/autoapprove.yml b/.github.old/workflows/autoapprove.yml similarity index 100% rename from .github/workflows/autoapprove.yml rename to .github.old/workflows/autoapprove.yml diff --git a/.github/workflows/automerge.yml b/.github.old/workflows/automerge.yml similarity index 100% rename from .github/workflows/automerge.yml rename to .github.old/workflows/automerge.yml diff --git a/.github/workflows/build.yml b/.github.old/workflows/build.yml similarity index 100% rename from .github/workflows/build.yml rename to .github.old/workflows/build.yml diff --git a/.github/workflows/codecov.yml b/.github.old/workflows/codecov.yml similarity index 100% rename from .github/workflows/codecov.yml rename to .github.old/workflows/codecov.yml diff --git a/.github/workflows/codeql-analysis.yml b/.github.old/workflows/codeql-analysis.yml similarity index 100% rename from .github/workflows/codeql-analysis.yml rename to .github.old/workflows/codeql-analysis.yml diff --git a/.github/workflows/dependabot-automerge.yml b/.github.old/workflows/dependabot-automerge.yml similarity index 100% rename from .github/workflows/dependabot-automerge.yml rename to .github.old/workflows/dependabot-automerge.yml diff --git a/.github/workflows/pr.yml b/.github.old/workflows/pr.yml similarity index 62% rename from .github/workflows/pr.yml rename to .github.old/workflows/pr.yml index f313ebe8..c03221c3 100644 --- a/.github/workflows/pr.yml +++ b/.github.old/workflows/pr.yml @@ -37,11 +37,4 @@ jobs: uses: golangci/golangci-lint-action@v3.4.0 continue-on-error: true with: - # Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version. - version: v1.30 - # Optional: working directory, useful for monorepos - # working-directory: somedir - # Optional: golangci-lint command line arguments. - # args: --issues-exit-code=0 - # Optional: show only new issues if it's a pull request. The default value is `false`. - # only-new-issues: true + version: v1.30 \ No newline at end of file diff --git a/go.mod b/go.mod index e82ed741..55853a55 100644 --- a/go.mod +++ b/go.mod @@ -1,10 +1,9 @@ module go.unistack.org/micro/v3 -go 1.16 +go 1.19 require ( - github.com/imdario/mergo v0.3.13 + github.com/imdario/mergo v0.3.14 github.com/patrickmn/go-cache v2.1.0+incompatible github.com/silas/dag v0.0.0-20211117232152-9d50aa809f35 - gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 60067f79..40151841 100644 --- a/go.sum +++ b/go.sum @@ -1,10 +1,9 @@ -github.com/imdario/mergo v0.3.13 h1:lFzP57bqS/wsqKssCGmtLAb8A0wKjLGrve2q3PPVcBk= -github.com/imdario/mergo v0.3.13/go.mod h1:4lJ1jqUDcsbIECGy0RUJAXNIhg+6ocWgb1ALK2O4oXg= +github.com/imdario/mergo v0.3.14 h1:fOqeC1+nCuuk6PKQdg9YmosXX7Y7mHX6R/0ZldI9iHo= +github.com/imdario/mergo v0.3.14/go.mod h1:WBLT9ZmE3lPoWsEzCh9LPo3TiwVN+ZKEjmz+hD27ysY= github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc= github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ= github.com/silas/dag v0.0.0-20211117232152-9d50aa809f35 h1:4mohWoM/UGg1BvFFiqSPRl5uwJY3rVV0HQX0ETqauqQ= github.com/silas/dag v0.0.0-20211117232152-9d50aa809f35/go.mod h1:7RTUFBdIRC9nZ7/3RyRNH1bdqIShrDejd1YbLwgPS+I= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v3 v3.0.0/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/options.go b/options.go index 95636364..37164a71 100644 --- a/options.go +++ b/options.go @@ -211,6 +211,7 @@ func Stores(s ...store.Store) Option { } // Logger set the logger to use +// //nolint:gocyclo func Logger(l logger.Logger, opts ...LoggerOption) Option { return func(o *Options) error { @@ -329,6 +330,7 @@ func Meters(m ...meter.Meter) Option { // Register sets the register for the service // and the underlying components +// //nolint:gocyclo func Register(r register.Register, opts ...RegisterOption) Option { return func(o *Options) error { @@ -403,6 +405,7 @@ func RegisterBroker(n string) RegisterOption { } // Tracer sets the tracer +// //nolint:gocyclo func Tracer(t tracer.Tracer, opts ...TracerOption) Option { return func(o *Options) error { diff --git a/options/hooks.go b/options/hooks.go new file mode 100644 index 00000000..48a45ad2 --- /dev/null +++ b/options/hooks.go @@ -0,0 +1,31 @@ +package options // import "go.unistack.org/micro/v3/options" + +// Hook func interface +type Hook interface{} + +// Hooks func slice +type Hooks []Hook + +// Append is used to add hooks +func (hs *Hooks) Append(h ...Hook) { + *hs = append(*hs, h...) +} + +// Replace is used to set hooks +func (hs *Hooks) Replace(h ...Hook) { + *hs = h +} + +// EachNext is used to itearate over hooks forward +func (hs *Hooks) EachNext(fn func(Hook)) { + for idx := 0; idx < len(*hs); idx++ { + fn((*hs)[idx]) + } +} + +// EachPrev is used to iterate over hooks backward +func (hs *Hooks) EachPrev(fn func(Hook)) { + for idx := len(*hs) - 1; idx >= 0; idx-- { + fn((*hs)[idx]) + } +} diff --git a/options/hooks_test.go b/options/hooks_test.go new file mode 100644 index 00000000..be84d3bd --- /dev/null +++ b/options/hooks_test.go @@ -0,0 +1,65 @@ +package options + +import "testing" + +func TestHooks_Append(t *testing.T) { + fn1 := func() {} + fn2 := func() {} + hs := &Hooks{} + hs.Append(fn1, fn2) + if len(*hs) != 2 { + t.Fatalf("unexpected Append error") + } +} + +func TestHooks_Replace(t *testing.T) { + fn1 := func() {} + fn2 := func() {} + hs := &Hooks{} + hs.Append(fn1, fn2, fn1) + if len(*hs) != 3 { + t.Fatalf("unexpected Append error") + } + hs.Replace(fn1, fn2) + if len(*hs) != 2 { + t.Fatalf("unexpected Replace error") + } +} + +func TestHooks_EachNext(t *testing.T) { + n := 5 + fn1 := func() { + n *= 2 + } + fn2 := func() { + n -= 10 + } + hs := &Hooks{} + hs.Append(fn1, fn2) + + hs.EachNext(func(h Hook) { + h.(func())() + }) + if n != 0 { + t.Fatalf("unexpected EachNext") + } +} + +func TestHooks_EachPrev(t *testing.T) { + n := 5 + fn1 := func() { + n *= 2 + } + fn2 := func() { + n -= 10 + } + hs := &Hooks{} + hs.Append(fn2, fn1) + + hs.EachPrev(func(h Hook) { + h.(func())() + }) + if n != 0 { + t.Fatalf("unexpected EachPrev") + } +} diff --git a/server/options.go b/server/options.go index cf269772..8ab8c0d2 100644 --- a/server/options.go +++ b/server/options.go @@ -13,6 +13,7 @@ import ( "go.unistack.org/micro/v3/metadata" "go.unistack.org/micro/v3/meter" "go.unistack.org/micro/v3/network/transport" + "go.unistack.org/micro/v3/options" "go.unistack.org/micro/v3/register" "go.unistack.org/micro/v3/tracer" "go.unistack.org/micro/v3/util/id" @@ -83,6 +84,8 @@ type Options struct { MaxConn int // DeregisterAttempts holds the number of deregister attempts before error DeregisterAttempts int + // Hooks may contains SubscriberWrapper, HandlerWrapper or Server func wrapper + Hooks options.Hooks } // NewOptions returns new options struct with default or passed values