Compare commits
46 Commits
v3.8.10
...
2287c65118
Author | SHA1 | Date | |
---|---|---|---|
2287c65118 | |||
b34bc7ffff | |||
|
2a0bf03d0a | ||
89114c291c | |||
|
b4b4320fac | ||
7b0d69115c | |||
f054beb6e8 | |||
9fb346594e | |||
|
cbf6fbd780 | ||
|
0392bff282 | ||
|
75b1fe5dc6 | ||
1f232ffba8 | |||
|
7f43b64fc2 | ||
1dda3f0dcc | |||
1abf5e7647 | |||
df8560bb6f | |||
58f03d05e7 | |||
60340a749b | |||
56b0df5b7a | |||
|
bb59d5a2fd | ||
797c0f822d | |||
8546140e22 | |||
8f7eebc24f | |||
b0def96d14 | |||
00450c9cc7 | |||
534bce2d20 | |||
53949be0cc | |||
d8fe2ff8b4 | |||
53b5ee2c6f | |||
dfd85cd871 | |||
1f3834e187 | |||
0354873c3a | |||
c26a7db47c | |||
74765b4c5f | |||
8bd7323af1 | |||
|
899dc8b3bc | ||
94929878fe | |||
8ce469a09e | |||
88788776d2 | |||
e143e2b547 | |||
a36f99e30b | |||
326ee53333 | |||
1244c5bb4d | |||
4ccc8a9c85 | |||
8a2e84d489 | |||
d29363b78d |
20
.github/workflows/autoapprove.yml
vendored
Normal file
20
.github/workflows/autoapprove.yml
vendored
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
name: "autoapprove"
|
||||||
|
|
||||||
|
on:
|
||||||
|
pull_request_target:
|
||||||
|
types: [assigned, opened, synchronize, reopened]
|
||||||
|
|
||||||
|
permissions:
|
||||||
|
pull-requests: write
|
||||||
|
contents: write
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
autoapprove:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: approve
|
||||||
|
uses: hmarr/auto-approve-action@v2
|
||||||
|
if: github.actor == 'vtolstov' || github.actor == 'dependabot[bot]'
|
||||||
|
id: approve
|
||||||
|
with:
|
||||||
|
github-token: ${{ secrets.GITHUB_TOKEN }}
|
21
.github/workflows/automerge.yml
vendored
Normal file
21
.github/workflows/automerge.yml
vendored
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
name: "automerge"
|
||||||
|
|
||||||
|
on:
|
||||||
|
pull_request_target:
|
||||||
|
types: [assigned, opened, synchronize, reopened]
|
||||||
|
|
||||||
|
permissions:
|
||||||
|
pull-requests: write
|
||||||
|
contents: write
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
automerge:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
if: github.actor == 'vtolstov'
|
||||||
|
steps:
|
||||||
|
- name: merge
|
||||||
|
id: merge
|
||||||
|
run: gh pr merge --auto --merge "$PR_URL"
|
||||||
|
env:
|
||||||
|
PR_URL: ${{github.event.pull_request.html_url}}
|
||||||
|
GITHUB_TOKEN: ${{secrets.TOKEN}}
|
32
.github/workflows/build.yml
vendored
32
.github/workflows/build.yml
vendored
@@ -1,6 +1,6 @@
|
|||||||
name: build
|
name: build
|
||||||
on:
|
on:
|
||||||
push:
|
push:
|
||||||
branches:
|
branches:
|
||||||
- master
|
- master
|
||||||
- v3
|
- v3
|
||||||
@@ -13,48 +13,32 @@ jobs:
|
|||||||
uses: actions/setup-go@v2
|
uses: actions/setup-go@v2
|
||||||
with:
|
with:
|
||||||
go-version: 1.16
|
go-version: 1.16
|
||||||
|
- name: checkout
|
||||||
|
uses: actions/checkout@v3
|
||||||
- name: cache
|
- name: cache
|
||||||
uses: actions/cache@v2
|
uses: actions/cache@v2
|
||||||
with:
|
with:
|
||||||
path: ~/go/pkg/mod
|
path: ~/go/pkg/mod
|
||||||
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
|
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
|
||||||
restore-keys: ${{ runner.os }}-go-
|
restore-keys: ${{ runner.os }}-go-
|
||||||
- name: sdk checkout
|
- name: deps
|
||||||
uses: actions/checkout@v2
|
|
||||||
- name: sdk deps
|
|
||||||
run: go get -v -t -d ./...
|
run: go get -v -t -d ./...
|
||||||
- name: sdk test
|
- name: test
|
||||||
env:
|
env:
|
||||||
INTEGRATION_TESTS: yes
|
INTEGRATION_TESTS: yes
|
||||||
run: go test -mod readonly -v ./...
|
run: go test -mod readonly -v ./...
|
||||||
- name: tests checkout
|
|
||||||
uses: actions/checkout@v2
|
|
||||||
with:
|
|
||||||
repository: unistack-org/micro-tests
|
|
||||||
ref: refs/heads/master
|
|
||||||
path: micro-tests
|
|
||||||
fetch-depth: 1
|
|
||||||
- name: tests deps
|
|
||||||
run: |
|
|
||||||
cd micro-tests
|
|
||||||
go mod edit -replace="github.com/unistack-org/micro/v3=../"
|
|
||||||
go get -v -t -d ./...
|
|
||||||
- name: tests test
|
|
||||||
env:
|
|
||||||
INTEGRATION_TESTS: yes
|
|
||||||
run: cd micro-tests && go test -mod readonly -v ./...
|
|
||||||
lint:
|
lint:
|
||||||
name: lint
|
name: lint
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: checkout
|
- name: checkout
|
||||||
uses: actions/checkout@v2
|
uses: actions/checkout@v3
|
||||||
- name: lint
|
- name: lint
|
||||||
uses: golangci/golangci-lint-action@v2
|
uses: golangci/golangci-lint-action@v3.1.0
|
||||||
continue-on-error: true
|
continue-on-error: true
|
||||||
with:
|
with:
|
||||||
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
|
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
|
||||||
version: v1.39
|
version: v1.30
|
||||||
# Optional: working directory, useful for monorepos
|
# Optional: working directory, useful for monorepos
|
||||||
# working-directory: somedir
|
# working-directory: somedir
|
||||||
# Optional: golangci-lint command line arguments.
|
# Optional: golangci-lint command line arguments.
|
||||||
|
2
.github/workflows/codeql-analysis.yml
vendored
2
.github/workflows/codeql-analysis.yml
vendored
@@ -43,7 +43,7 @@ jobs:
|
|||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: checkout
|
- name: checkout
|
||||||
uses: actions/checkout@v2
|
uses: actions/checkout@v3
|
||||||
- name: setup
|
- name: setup
|
||||||
uses: actions/setup-go@v2
|
uses: actions/setup-go@v2
|
||||||
with:
|
with:
|
||||||
|
14
.github/workflows/dependabot-automerge.yml
vendored
14
.github/workflows/dependabot-automerge.yml
vendored
@@ -1,4 +1,4 @@
|
|||||||
name: "prautomerge"
|
name: "dependabot-automerge"
|
||||||
|
|
||||||
on:
|
on:
|
||||||
pull_request_target:
|
pull_request_target:
|
||||||
@@ -9,21 +9,17 @@ permissions:
|
|||||||
contents: write
|
contents: write
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
dependabot:
|
automerge:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
if: ${{ github.actor == 'dependabot[bot]' }}
|
if: github.actor == 'dependabot[bot]'
|
||||||
steps:
|
steps:
|
||||||
- name: metadata
|
- name: metadata
|
||||||
id: metadata
|
id: metadata
|
||||||
uses: dependabot/fetch-metadata@v1.1.1
|
uses: dependabot/fetch-metadata@v1.3.0
|
||||||
with:
|
with:
|
||||||
github-token: "${{ secrets.TOKEN }}"
|
github-token: "${{ secrets.TOKEN }}"
|
||||||
- name: approve
|
|
||||||
run: gh pr review --approve "$PR_URL"
|
|
||||||
env:
|
|
||||||
PR_URL: ${{github.event.pull_request.html_url}}
|
|
||||||
GITHUB_TOKEN: ${{secrets.TOKEN}}
|
|
||||||
- name: merge
|
- name: merge
|
||||||
|
id: merge
|
||||||
if: ${{contains(steps.metadata.outputs.dependency-names, 'go.unistack.org')}}
|
if: ${{contains(steps.metadata.outputs.dependency-names, 'go.unistack.org')}}
|
||||||
run: gh pr merge --auto --merge "$PR_URL"
|
run: gh pr merge --auto --merge "$PR_URL"
|
||||||
env:
|
env:
|
||||||
|
32
.github/workflows/pr.yml
vendored
32
.github/workflows/pr.yml
vendored
@@ -13,48 +13,32 @@ jobs:
|
|||||||
uses: actions/setup-go@v2
|
uses: actions/setup-go@v2
|
||||||
with:
|
with:
|
||||||
go-version: 1.16
|
go-version: 1.16
|
||||||
|
- name: checkout
|
||||||
|
uses: actions/checkout@v3
|
||||||
- name: cache
|
- name: cache
|
||||||
uses: actions/cache@v2
|
uses: actions/cache@v2
|
||||||
with:
|
with:
|
||||||
path: ~/go/pkg
|
path: ~/go/pkg/mod
|
||||||
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
|
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
|
||||||
restore-keys: ${{ runner.os }}-go-
|
restore-keys: ${{ runner.os }}-go-
|
||||||
- name: sdk checkout
|
- name: deps
|
||||||
uses: actions/checkout@v2
|
|
||||||
- name: sdk deps
|
|
||||||
run: go get -v -t -d ./...
|
run: go get -v -t -d ./...
|
||||||
- name: sdk test
|
- name: test
|
||||||
env:
|
env:
|
||||||
INTEGRATION_TESTS: yes
|
INTEGRATION_TESTS: yes
|
||||||
run: go test -mod readonly -v ./...
|
run: go test -mod readonly -v ./...
|
||||||
- name: tests checkout
|
|
||||||
uses: actions/checkout@v2
|
|
||||||
with:
|
|
||||||
repository: unistack-org/micro-tests
|
|
||||||
ref: refs/heads/master
|
|
||||||
path: micro-tests
|
|
||||||
fetch-depth: 1
|
|
||||||
- name: tests deps
|
|
||||||
run: |
|
|
||||||
cd micro-tests
|
|
||||||
go mod edit -replace="github.com/unistack-org/micro/v3=../"
|
|
||||||
go get -v -t -d ./...
|
|
||||||
- name: tests test
|
|
||||||
env:
|
|
||||||
INTEGRATION_TESTS: yes
|
|
||||||
run: cd micro-tests && go test -mod readonly -v ./...
|
|
||||||
lint:
|
lint:
|
||||||
name: lint
|
name: lint
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: checkout
|
- name: checkout
|
||||||
uses: actions/checkout@v2
|
uses: actions/checkout@v3
|
||||||
- name: lint
|
- name: lint
|
||||||
uses: golangci/golangci-lint-action@v2
|
uses: golangci/golangci-lint-action@v3.1.0
|
||||||
continue-on-error: true
|
continue-on-error: true
|
||||||
with:
|
with:
|
||||||
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
|
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
|
||||||
version: v1.39
|
version: v1.30
|
||||||
# Optional: working directory, useful for monorepos
|
# Optional: working directory, useful for monorepos
|
||||||
# working-directory: somedir
|
# working-directory: somedir
|
||||||
# Optional: golangci-lint command line arguments.
|
# Optional: golangci-lint command line arguments.
|
||||||
|
@@ -1,4 +1,4 @@
|
|||||||
# Micro [](https://opensource.org/licenses/Apache-2.0) [](https://pkg.go.dev/github.com/unistack-org/micro/v3?tab=overview) [](https://github.com/unistack-org/micro/actions?query=workflow%3Abuild+branch%3Amaster+event%3Apush) [](https://goreportcard.com/report/github.com/unistack-org/micro) [](https://unistack-org.slack.com/messages/default)
|
# Micro [](https://opensource.org/licenses/Apache-2.0) [](https://pkg.go.dev/github.com/unistack-org/micro/v3?tab=overview) [](https://github.com/unistack-org/micro/actions?query=workflow%3Abuild+branch%3Amaster+event%3Apush) [](https://goreportcard.com/report/go.unistack.org/micro/v3) [](https://unistack-org.slack.com/messages/default)
|
||||||
|
|
||||||
Micro is a standard library for microservices.
|
Micro is a standard library for microservices.
|
||||||
|
|
||||||
|
@@ -49,6 +49,7 @@ type Message interface {
|
|||||||
Topic() string
|
Topic() string
|
||||||
Payload() interface{}
|
Payload() interface{}
|
||||||
ContentType() string
|
ContentType() string
|
||||||
|
Metadata() metadata.Metadata
|
||||||
}
|
}
|
||||||
|
|
||||||
// Request is the interface for a synchronous request used by Call or Stream
|
// Request is the interface for a synchronous request used by Call or Stream
|
||||||
@@ -91,10 +92,16 @@ type Stream interface {
|
|||||||
Send(msg interface{}) error
|
Send(msg interface{}) error
|
||||||
// Recv will decode and read a response
|
// Recv will decode and read a response
|
||||||
Recv(msg interface{}) error
|
Recv(msg interface{}) error
|
||||||
|
// SendMsg will encode and send a request
|
||||||
|
SendMsg(msg interface{}) error
|
||||||
|
// RecvMsg will decode and read a response
|
||||||
|
RecvMsg(msg interface{}) error
|
||||||
// Error returns the stream error
|
// Error returns the stream error
|
||||||
Error() error
|
Error() error
|
||||||
// Close closes the stream
|
// Close closes the stream
|
||||||
Close() error
|
Close() error
|
||||||
|
// CloseSend closes the send direction of the stream
|
||||||
|
CloseSend() error
|
||||||
}
|
}
|
||||||
|
|
||||||
// Option used by the Client
|
// Option used by the Client
|
||||||
|
@@ -12,7 +12,7 @@ import (
|
|||||||
type LookupFunc func(context.Context, Request, CallOptions) ([]string, error)
|
type LookupFunc func(context.Context, Request, CallOptions) ([]string, error)
|
||||||
|
|
||||||
// LookupRoute for a request using the router and then choose one using the selector
|
// LookupRoute for a request using the router and then choose one using the selector
|
||||||
func LookupRoute(ctx context.Context, req Request, opts CallOptions) ([]string, error) {
|
func LookupRoute(_ context.Context, req Request, opts CallOptions) ([]string, error) {
|
||||||
// check to see if an address was provided as a call option
|
// check to see if an address was provided as a call option
|
||||||
if len(opts.Address) > 0 {
|
if len(opts.Address) > 0 {
|
||||||
return opts.Address, nil
|
return opts.Address, nil
|
||||||
|
@@ -119,6 +119,14 @@ func (n *noopStream) Recv(interface{}) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (n *noopStream) SendMsg(interface{}) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n *noopStream) RecvMsg(interface{}) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (n *noopStream) Error() error {
|
func (n *noopStream) Error() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -127,6 +135,10 @@ func (n *noopStream) Close() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (n *noopStream) CloseSend() error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (n *noopMessage) Topic() string {
|
func (n *noopMessage) Topic() string {
|
||||||
return n.topic
|
return n.topic
|
||||||
}
|
}
|
||||||
@@ -139,6 +151,10 @@ func (n *noopMessage) ContentType() string {
|
|||||||
return n.opts.ContentType
|
return n.opts.ContentType
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (n *noopMessage) Metadata() metadata.Metadata {
|
||||||
|
return n.opts.Metadata
|
||||||
|
}
|
||||||
|
|
||||||
func (n *noopClient) newCodec(contentType string) (codec.Codec, error) {
|
func (n *noopClient) newCodec(contentType string) (codec.Codec, error) {
|
||||||
if cf, ok := n.opts.Codecs[contentType]; ok {
|
if cf, ok := n.opts.Codecs[contentType]; ok {
|
||||||
return cf, nil
|
return cf, nil
|
||||||
|
@@ -8,6 +8,7 @@ import (
|
|||||||
"go.unistack.org/micro/v3/broker"
|
"go.unistack.org/micro/v3/broker"
|
||||||
"go.unistack.org/micro/v3/codec"
|
"go.unistack.org/micro/v3/codec"
|
||||||
"go.unistack.org/micro/v3/logger"
|
"go.unistack.org/micro/v3/logger"
|
||||||
|
"go.unistack.org/micro/v3/metadata"
|
||||||
"go.unistack.org/micro/v3/meter"
|
"go.unistack.org/micro/v3/meter"
|
||||||
"go.unistack.org/micro/v3/network/transport"
|
"go.unistack.org/micro/v3/network/transport"
|
||||||
"go.unistack.org/micro/v3/register"
|
"go.unistack.org/micro/v3/register"
|
||||||
@@ -128,7 +129,7 @@ type PublishOptions struct {
|
|||||||
|
|
||||||
// NewMessageOptions creates message options struct
|
// NewMessageOptions creates message options struct
|
||||||
func NewMessageOptions(opts ...MessageOption) MessageOptions {
|
func NewMessageOptions(opts ...MessageOption) MessageOptions {
|
||||||
options := MessageOptions{}
|
options := MessageOptions{Metadata: metadata.New(1)}
|
||||||
for _, o := range opts {
|
for _, o := range opts {
|
||||||
o(&options)
|
o(&options)
|
||||||
}
|
}
|
||||||
@@ -137,7 +138,10 @@ func NewMessageOptions(opts ...MessageOption) MessageOptions {
|
|||||||
|
|
||||||
// MessageOptions holds client message options
|
// MessageOptions holds client message options
|
||||||
type MessageOptions struct {
|
type MessageOptions struct {
|
||||||
|
// Metadata additional metadata
|
||||||
|
Metadata metadata.Metadata
|
||||||
// ContentType specify content-type of message
|
// ContentType specify content-type of message
|
||||||
|
// deprecated
|
||||||
ContentType string
|
ContentType string
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -517,6 +521,7 @@ func WithSelectOptions(sops ...selector.SelectOption) CallOption {
|
|||||||
// Deprecated
|
// Deprecated
|
||||||
func WithMessageContentType(ct string) MessageOption {
|
func WithMessageContentType(ct string) MessageOption {
|
||||||
return func(o *MessageOptions) {
|
return func(o *MessageOptions) {
|
||||||
|
o.Metadata.Set(metadata.HeaderContentType, ct)
|
||||||
o.ContentType = ct
|
o.ContentType = ct
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -524,10 +529,18 @@ func WithMessageContentType(ct string) MessageOption {
|
|||||||
// MessageContentType sets the message content type
|
// MessageContentType sets the message content type
|
||||||
func MessageContentType(ct string) MessageOption {
|
func MessageContentType(ct string) MessageOption {
|
||||||
return func(o *MessageOptions) {
|
return func(o *MessageOptions) {
|
||||||
|
o.Metadata.Set(metadata.HeaderContentType, ct)
|
||||||
o.ContentType = ct
|
o.ContentType = ct
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// MessageMetadata sets the message metadata
|
||||||
|
func MessageMetadata(k, v string) MessageOption {
|
||||||
|
return func(o *MessageOptions) {
|
||||||
|
o.Metadata.Set(k, v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// StreamingRequest specifies that request is streaming
|
// StreamingRequest specifies that request is streaming
|
||||||
func StreamingRequest(b bool) RequestOption {
|
func StreamingRequest(b bool) RequestOption {
|
||||||
return func(o *RequestOptions) {
|
return func(o *RequestOptions) {
|
||||||
|
@@ -20,7 +20,7 @@ func RetryNever(ctx context.Context, req Request, retryCount int, err error) (bo
|
|||||||
}
|
}
|
||||||
|
|
||||||
// RetryOnError retries a request on a 500 or timeout error
|
// RetryOnError retries a request on a 500 or timeout error
|
||||||
func RetryOnError(ctx context.Context, req Request, retryCount int, err error) (bool, error) {
|
func RetryOnError(_ context.Context, _ Request, _ int, err error) (bool, error) {
|
||||||
if err == nil {
|
if err == nil {
|
||||||
return false, nil
|
return false, nil
|
||||||
}
|
}
|
||||||
|
@@ -5,29 +5,40 @@ type Frame struct {
|
|||||||
Data []byte
|
Data []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewFrame returns new frame with data
|
||||||
|
func NewFrame(data []byte) *Frame {
|
||||||
|
return &Frame{Data: data}
|
||||||
|
}
|
||||||
|
|
||||||
|
// MarshalJSON returns frame data
|
||||||
func (m *Frame) MarshalJSON() ([]byte, error) {
|
func (m *Frame) MarshalJSON() ([]byte, error) {
|
||||||
return m.Data, nil
|
return m.Marshal()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// UnmarshalJSON set frame data
|
||||||
func (m *Frame) UnmarshalJSON(data []byte) error {
|
func (m *Frame) UnmarshalJSON(data []byte) error {
|
||||||
m.Data = data
|
return m.Unmarshal(data)
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ProtoMessage noop func
|
||||||
func (m *Frame) ProtoMessage() {}
|
func (m *Frame) ProtoMessage() {}
|
||||||
|
|
||||||
|
// Reset resets frame
|
||||||
func (m *Frame) Reset() {
|
func (m *Frame) Reset() {
|
||||||
*m = Frame{}
|
*m = Frame{}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// String returns frame as string
|
||||||
func (m *Frame) String() string {
|
func (m *Frame) String() string {
|
||||||
return string(m.Data)
|
return string(m.Data)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Marshal returns frame data
|
||||||
func (m *Frame) Marshal() ([]byte, error) {
|
func (m *Frame) Marshal() ([]byte, error) {
|
||||||
return m.Data, nil
|
return m.Data, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Unmarshal set frame data
|
||||||
func (m *Frame) Unmarshal(data []byte) error {
|
func (m *Frame) Unmarshal(data []byte) error {
|
||||||
m.Data = data
|
m.Data = data
|
||||||
return nil
|
return nil
|
||||||
|
@@ -23,6 +23,8 @@ var (
|
|||||||
ErrInvalidStruct = errors.New("invalid struct specified")
|
ErrInvalidStruct = errors.New("invalid struct specified")
|
||||||
// ErrWatcherStopped is returned when source watcher has been stopped
|
// ErrWatcherStopped is returned when source watcher has been stopped
|
||||||
ErrWatcherStopped = errors.New("watcher stopped")
|
ErrWatcherStopped = errors.New("watcher stopped")
|
||||||
|
// ErrWatcherNotImplemented returned when config does not implement watch
|
||||||
|
ErrWatcherNotImplemented = errors.New("watcher not implemented")
|
||||||
)
|
)
|
||||||
|
|
||||||
// Config is an interface abstraction for dynamic configuration
|
// Config is an interface abstraction for dynamic configuration
|
||||||
|
@@ -2,7 +2,6 @@ package config
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
|
||||||
"reflect"
|
"reflect"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -271,7 +270,7 @@ func (c *defaultConfig) Name() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *defaultConfig) Watch(ctx context.Context, opts ...WatchOption) (Watcher, error) {
|
func (c *defaultConfig) Watch(ctx context.Context, opts ...WatchOption) (Watcher, error) {
|
||||||
return nil, fmt.Errorf("not implemented")
|
return nil, ErrWatcherNotImplemented
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewConfig returns new default config source
|
// NewConfig returns new default config source
|
||||||
|
151
errors/errors.go
151
errors/errors.go
@@ -3,9 +3,12 @@
|
|||||||
package errors // import "go.unistack.org/micro/v3/errors"
|
package errors // import "go.unistack.org/micro/v3/errors"
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -53,6 +56,22 @@ func (e *Error) Error() string {
|
|||||||
return string(b)
|
return string(b)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
// Generator struct holds id of error
|
||||||
|
type Generator struct {
|
||||||
|
id string
|
||||||
|
}
|
||||||
|
|
||||||
|
// Generator can emit new error with static id
|
||||||
|
func NewGenerator(id string) *Generator {
|
||||||
|
return &Generator{id: id}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *Generator) BadRequest(format string, args ...interface{}) error {
|
||||||
|
return BadRequest(g.id, format, args...)
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
// New generates a custom error
|
// New generates a custom error
|
||||||
func New(id, detail string, code int32) error {
|
func New(id, detail string, code int32) error {
|
||||||
return &Error{
|
return &Error{
|
||||||
@@ -66,130 +85,130 @@ func New(id, detail string, code int32) error {
|
|||||||
// Parse tries to parse a JSON string into an error. If that
|
// Parse tries to parse a JSON string into an error. If that
|
||||||
// fails, it will set the given string as the error detail.
|
// fails, it will set the given string as the error detail.
|
||||||
func Parse(err string) *Error {
|
func Parse(err string) *Error {
|
||||||
e := new(Error)
|
e := &Error{}
|
||||||
errr := json.Unmarshal([]byte(err), e)
|
nerr := json.Unmarshal([]byte(err), e)
|
||||||
if errr != nil {
|
if nerr != nil {
|
||||||
e.Detail = err
|
e.Detail = err
|
||||||
}
|
}
|
||||||
return e
|
return e
|
||||||
}
|
}
|
||||||
|
|
||||||
// BadRequest generates a 400 error.
|
// BadRequest generates a 400 error.
|
||||||
func BadRequest(id, format string, a ...interface{}) error {
|
func BadRequest(id, format string, args ...interface{}) error {
|
||||||
return &Error{
|
return &Error{
|
||||||
ID: id,
|
ID: id,
|
||||||
Code: 400,
|
Code: 400,
|
||||||
Detail: fmt.Sprintf(format, a...),
|
Detail: fmt.Sprintf(format, args...),
|
||||||
Status: http.StatusText(400),
|
Status: http.StatusText(400),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Unauthorized generates a 401 error.
|
// Unauthorized generates a 401 error.
|
||||||
func Unauthorized(id, format string, a ...interface{}) error {
|
func Unauthorized(id, format string, args ...interface{}) error {
|
||||||
return &Error{
|
return &Error{
|
||||||
ID: id,
|
ID: id,
|
||||||
Code: 401,
|
Code: 401,
|
||||||
Detail: fmt.Sprintf(format, a...),
|
Detail: fmt.Sprintf(format, args...),
|
||||||
Status: http.StatusText(401),
|
Status: http.StatusText(401),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Forbidden generates a 403 error.
|
// Forbidden generates a 403 error.
|
||||||
func Forbidden(id, format string, a ...interface{}) error {
|
func Forbidden(id, format string, args ...interface{}) error {
|
||||||
return &Error{
|
return &Error{
|
||||||
ID: id,
|
ID: id,
|
||||||
Code: 403,
|
Code: 403,
|
||||||
Detail: fmt.Sprintf(format, a...),
|
Detail: fmt.Sprintf(format, args...),
|
||||||
Status: http.StatusText(403),
|
Status: http.StatusText(403),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// NotFound generates a 404 error.
|
// NotFound generates a 404 error.
|
||||||
func NotFound(id, format string, a ...interface{}) error {
|
func NotFound(id, format string, args ...interface{}) error {
|
||||||
return &Error{
|
return &Error{
|
||||||
ID: id,
|
ID: id,
|
||||||
Code: 404,
|
Code: 404,
|
||||||
Detail: fmt.Sprintf(format, a...),
|
Detail: fmt.Sprintf(format, args...),
|
||||||
Status: http.StatusText(404),
|
Status: http.StatusText(404),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// MethodNotAllowed generates a 405 error.
|
// MethodNotAllowed generates a 405 error.
|
||||||
func MethodNotAllowed(id, format string, a ...interface{}) error {
|
func MethodNotAllowed(id, format string, args ...interface{}) error {
|
||||||
return &Error{
|
return &Error{
|
||||||
ID: id,
|
ID: id,
|
||||||
Code: 405,
|
Code: 405,
|
||||||
Detail: fmt.Sprintf(format, a...),
|
Detail: fmt.Sprintf(format, args...),
|
||||||
Status: http.StatusText(405),
|
Status: http.StatusText(405),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Timeout generates a 408 error.
|
// Timeout generates a 408 error.
|
||||||
func Timeout(id, format string, a ...interface{}) error {
|
func Timeout(id, format string, args ...interface{}) error {
|
||||||
return &Error{
|
return &Error{
|
||||||
ID: id,
|
ID: id,
|
||||||
Code: 408,
|
Code: 408,
|
||||||
Detail: fmt.Sprintf(format, a...),
|
Detail: fmt.Sprintf(format, args...),
|
||||||
Status: http.StatusText(408),
|
Status: http.StatusText(408),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Conflict generates a 409 error.
|
// Conflict generates a 409 error.
|
||||||
func Conflict(id, format string, a ...interface{}) error {
|
func Conflict(id, format string, args ...interface{}) error {
|
||||||
return &Error{
|
return &Error{
|
||||||
ID: id,
|
ID: id,
|
||||||
Code: 409,
|
Code: 409,
|
||||||
Detail: fmt.Sprintf(format, a...),
|
Detail: fmt.Sprintf(format, args...),
|
||||||
Status: http.StatusText(409),
|
Status: http.StatusText(409),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// InternalServerError generates a 500 error.
|
// InternalServerError generates a 500 error.
|
||||||
func InternalServerError(id, format string, a ...interface{}) error {
|
func InternalServerError(id, format string, args ...interface{}) error {
|
||||||
return &Error{
|
return &Error{
|
||||||
ID: id,
|
ID: id,
|
||||||
Code: 500,
|
Code: 500,
|
||||||
Detail: fmt.Sprintf(format, a...),
|
Detail: fmt.Sprintf(format, args...),
|
||||||
Status: http.StatusText(500),
|
Status: http.StatusText(500),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// NotImplemented generates a 501 error
|
// NotImplemented generates a 501 error
|
||||||
func NotImplemented(id, format string, a ...interface{}) error {
|
func NotImplemented(id, format string, args ...interface{}) error {
|
||||||
return &Error{
|
return &Error{
|
||||||
ID: id,
|
ID: id,
|
||||||
Code: 501,
|
Code: 501,
|
||||||
Detail: fmt.Sprintf(format, a...),
|
Detail: fmt.Sprintf(format, args...),
|
||||||
Status: http.StatusText(501),
|
Status: http.StatusText(501),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// BadGateway generates a 502 error
|
// BadGateway generates a 502 error
|
||||||
func BadGateway(id, format string, a ...interface{}) error {
|
func BadGateway(id, format string, args ...interface{}) error {
|
||||||
return &Error{
|
return &Error{
|
||||||
ID: id,
|
ID: id,
|
||||||
Code: 502,
|
Code: 502,
|
||||||
Detail: fmt.Sprintf(format, a...),
|
Detail: fmt.Sprintf(format, args...),
|
||||||
Status: http.StatusText(502),
|
Status: http.StatusText(502),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ServiceUnavailable generates a 503 error
|
// ServiceUnavailable generates a 503 error
|
||||||
func ServiceUnavailable(id, format string, a ...interface{}) error {
|
func ServiceUnavailable(id, format string, args ...interface{}) error {
|
||||||
return &Error{
|
return &Error{
|
||||||
ID: id,
|
ID: id,
|
||||||
Code: 503,
|
Code: 503,
|
||||||
Detail: fmt.Sprintf(format, a...),
|
Detail: fmt.Sprintf(format, args...),
|
||||||
Status: http.StatusText(503),
|
Status: http.StatusText(503),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// GatewayTimeout generates a 504 error
|
// GatewayTimeout generates a 504 error
|
||||||
func GatewayTimeout(id, format string, a ...interface{}) error {
|
func GatewayTimeout(id, format string, args ...interface{}) error {
|
||||||
return &Error{
|
return &Error{
|
||||||
ID: id,
|
ID: id,
|
||||||
Code: 504,
|
Code: 504,
|
||||||
Detail: fmt.Sprintf(format, a...),
|
Detail: fmt.Sprintf(format, args...),
|
||||||
Status: http.StatusText(504),
|
Status: http.StatusText(504),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -222,3 +241,81 @@ func FromError(err error) *Error {
|
|||||||
|
|
||||||
return Parse(err.Error())
|
return Parse(err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// MarshalJSON returns error data
|
||||||
|
func (e *Error) MarshalJSON() ([]byte, error) {
|
||||||
|
return e.Marshal()
|
||||||
|
}
|
||||||
|
|
||||||
|
// UnmarshalJSON set error data
|
||||||
|
func (e *Error) UnmarshalJSON(data []byte) error {
|
||||||
|
return e.Unmarshal(data)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ProtoMessage noop func
|
||||||
|
func (e *Error) ProtoMessage() {}
|
||||||
|
|
||||||
|
// Reset resets error
|
||||||
|
func (e *Error) Reset() {
|
||||||
|
*e = Error{}
|
||||||
|
}
|
||||||
|
|
||||||
|
// String returns error as string
|
||||||
|
func (e *Error) String() string {
|
||||||
|
return fmt.Sprintf(`{"id":"%s","detail":"%s","status":"%s","code":%d}`, addslashes(e.ID), addslashes(e.Detail), addslashes(e.Status), e.Code)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Marshal returns error data
|
||||||
|
func (e *Error) Marshal() ([]byte, error) {
|
||||||
|
return []byte(e.String()), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Unmarshal set error data
|
||||||
|
func (e *Error) Unmarshal(data []byte) error {
|
||||||
|
str := string(data)
|
||||||
|
if len(data) < 41 {
|
||||||
|
return fmt.Errorf("invalid data")
|
||||||
|
}
|
||||||
|
parts := strings.FieldsFunc(str[1:len(str)-1], func(r rune) bool {
|
||||||
|
return r == ','
|
||||||
|
})
|
||||||
|
for _, part := range parts {
|
||||||
|
nparts := strings.FieldsFunc(part, func(r rune) bool {
|
||||||
|
return r == ':'
|
||||||
|
})
|
||||||
|
for idx := 0; idx < len(nparts)/2; idx += 2 {
|
||||||
|
val := strings.Trim(nparts[idx+1], `"`)
|
||||||
|
if len(val) == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
switch {
|
||||||
|
case nparts[idx] == `"id"`:
|
||||||
|
e.ID = val
|
||||||
|
case nparts[idx] == `"detail"`:
|
||||||
|
e.Detail = val
|
||||||
|
case nparts[idx] == `"status"`:
|
||||||
|
e.Status = val
|
||||||
|
case nparts[idx] == `"code"`:
|
||||||
|
c, err := strconv.ParseInt(val, 10, 32)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
e.Code = int32(c)
|
||||||
|
}
|
||||||
|
idx++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func addslashes(str string) string {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
for _, char := range str {
|
||||||
|
switch char {
|
||||||
|
case '\'', '"', '\\':
|
||||||
|
buf.WriteRune('\\')
|
||||||
|
}
|
||||||
|
buf.WriteRune(char)
|
||||||
|
}
|
||||||
|
return buf.String()
|
||||||
|
}
|
||||||
|
31
errors/errors.proto
Normal file
31
errors/errors.proto
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
// Copyright 2021 Unistack LLC
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
syntax = "proto3";
|
||||||
|
|
||||||
|
package micro.errors;
|
||||||
|
|
||||||
|
option cc_enable_arenas = true;
|
||||||
|
option go_package = "go.unistack.org/micro/v3/errors;errors";
|
||||||
|
option java_multiple_files = true;
|
||||||
|
option java_outer_classname = "MicroErrors";
|
||||||
|
option java_package = "micro.errors";
|
||||||
|
option objc_class_prefix = "MERRORS";
|
||||||
|
|
||||||
|
message Error {
|
||||||
|
string id = 1;
|
||||||
|
string detail = 2;
|
||||||
|
string status = 3;
|
||||||
|
uint32 code = 4;
|
||||||
|
}
|
@@ -1,11 +1,34 @@
|
|||||||
package errors
|
package errors
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
er "errors"
|
er "errors"
|
||||||
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func TestMarshalJSON(t *testing.T) {
|
||||||
|
e := InternalServerError("id", "err: %v", fmt.Errorf("err: %v", `xxx: "UNIX_TIMESTAMP": invalid identifier`))
|
||||||
|
_, err := json.Marshal(e)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestEmpty(t *testing.T) {
|
||||||
|
msg := "test"
|
||||||
|
var err *Error
|
||||||
|
err = FromError(fmt.Errorf(msg))
|
||||||
|
if err.Detail != msg {
|
||||||
|
t.Fatalf("invalid error %v", err)
|
||||||
|
}
|
||||||
|
err = FromError(fmt.Errorf(`{"id":"","detail":"%s","status":"%s","code":0}`, msg, msg))
|
||||||
|
if err.Detail != msg || err.Status != msg {
|
||||||
|
t.Fatalf("invalid error %#+v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestFromError(t *testing.T) {
|
func TestFromError(t *testing.T) {
|
||||||
err := NotFound("go.micro.test", "%s", "example")
|
err := NotFound("go.micro.test", "%s", "example")
|
||||||
merr := FromError(err)
|
merr := FromError(err)
|
||||||
|
@@ -349,6 +349,7 @@ func (w *microWorkflow) Execute(ctx context.Context, req *Message, opts ...Execu
|
|||||||
return eid, err
|
return eid, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewFlow create new flow
|
||||||
func NewFlow(opts ...Option) Flow {
|
func NewFlow(opts ...Option) Flow {
|
||||||
options := NewOptions(opts...)
|
options := NewOptions(opts...)
|
||||||
return µFlow{opts: options}
|
return µFlow{opts: options}
|
||||||
@@ -574,11 +575,13 @@ func (s *microPublishStep) Execute(ctx context.Context, req *Message, opts ...Ex
|
|||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewCallStep create new step with client.Call
|
||||||
func NewCallStep(service string, name string, method string, opts ...StepOption) Step {
|
func NewCallStep(service string, name string, method string, opts ...StepOption) Step {
|
||||||
options := NewStepOptions(opts...)
|
options := NewStepOptions(opts...)
|
||||||
return µCallStep{service: service, method: name + "." + method, opts: options}
|
return µCallStep{service: service, method: name + "." + method, opts: options}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewPublishStep create new step with client.Publish
|
||||||
func NewPublishStep(topic string, opts ...StepOption) Step {
|
func NewPublishStep(topic string, opts ...StepOption) Step {
|
||||||
options := NewStepOptions(opts...)
|
options := NewStepOptions(opts...)
|
||||||
return µPublishStep{topic: topic, opts: options}
|
return µPublishStep{topic: topic, opts: options}
|
||||||
|
@@ -70,7 +70,7 @@ func Client(c client.Client) Option {
|
|||||||
|
|
||||||
// Context specifies a context for the service.
|
// Context specifies a context for the service.
|
||||||
// Can be used to signal shutdown of the flow
|
// Can be used to signal shutdown of the flow
|
||||||
// Can be used for extra option values.
|
// or can be used for extra option values.
|
||||||
func Context(ctx context.Context) Option {
|
func Context(ctx context.Context) Option {
|
||||||
return func(o *Options) {
|
return func(o *Options) {
|
||||||
o.Context = ctx
|
o.Context = ctx
|
||||||
@@ -91,7 +91,7 @@ func Store(s store.Store) Option {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// WorflowOption signature
|
// WorflowOption func signature
|
||||||
type WorkflowOption func(*WorkflowOptions)
|
type WorkflowOption func(*WorkflowOptions)
|
||||||
|
|
||||||
// WorkflowOptions holds workflow options
|
// WorkflowOptions holds workflow options
|
||||||
@@ -107,6 +107,7 @@ func WorkflowID(id string) WorkflowOption {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ExecuteOptions holds execute options
|
||||||
type ExecuteOptions struct {
|
type ExecuteOptions struct {
|
||||||
// Client holds the client.Client
|
// Client holds the client.Client
|
||||||
Client client.Client
|
Client client.Client
|
||||||
@@ -128,56 +129,66 @@ type ExecuteOptions struct {
|
|||||||
Async bool
|
Async bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ExecuteOption func signature
|
||||||
type ExecuteOption func(*ExecuteOptions)
|
type ExecuteOption func(*ExecuteOptions)
|
||||||
|
|
||||||
|
// ExecuteClient pass client.Client to ExecuteOption
|
||||||
func ExecuteClient(c client.Client) ExecuteOption {
|
func ExecuteClient(c client.Client) ExecuteOption {
|
||||||
return func(o *ExecuteOptions) {
|
return func(o *ExecuteOptions) {
|
||||||
o.Client = c
|
o.Client = c
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ExecuteTracer pass tracer.Tracer to ExecuteOption
|
||||||
func ExecuteTracer(t tracer.Tracer) ExecuteOption {
|
func ExecuteTracer(t tracer.Tracer) ExecuteOption {
|
||||||
return func(o *ExecuteOptions) {
|
return func(o *ExecuteOptions) {
|
||||||
o.Tracer = t
|
o.Tracer = t
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ExecuteLogger pass logger.Logger to ExecuteOption
|
||||||
func ExecuteLogger(l logger.Logger) ExecuteOption {
|
func ExecuteLogger(l logger.Logger) ExecuteOption {
|
||||||
return func(o *ExecuteOptions) {
|
return func(o *ExecuteOptions) {
|
||||||
o.Logger = l
|
o.Logger = l
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ExecuteMeter pass meter.Meter to ExecuteOption
|
||||||
func ExecuteMeter(m meter.Meter) ExecuteOption {
|
func ExecuteMeter(m meter.Meter) ExecuteOption {
|
||||||
return func(o *ExecuteOptions) {
|
return func(o *ExecuteOptions) {
|
||||||
o.Meter = m
|
o.Meter = m
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ExecuteContext pass context.Context ot ExecuteOption
|
||||||
func ExecuteContext(ctx context.Context) ExecuteOption {
|
func ExecuteContext(ctx context.Context) ExecuteOption {
|
||||||
return func(o *ExecuteOptions) {
|
return func(o *ExecuteOptions) {
|
||||||
o.Context = ctx
|
o.Context = ctx
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ExecuteReverse says that dag must be run in reverse order
|
||||||
func ExecuteReverse(b bool) ExecuteOption {
|
func ExecuteReverse(b bool) ExecuteOption {
|
||||||
return func(o *ExecuteOptions) {
|
return func(o *ExecuteOptions) {
|
||||||
o.Reverse = b
|
o.Reverse = b
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ExecuteTimeout pass timeout time.Duration for execution
|
||||||
func ExecuteTimeout(td time.Duration) ExecuteOption {
|
func ExecuteTimeout(td time.Duration) ExecuteOption {
|
||||||
return func(o *ExecuteOptions) {
|
return func(o *ExecuteOptions) {
|
||||||
o.Timeout = td
|
o.Timeout = td
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ExecuteAsync says that caller does not wait for execution complete
|
||||||
func ExecuteAsync(b bool) ExecuteOption {
|
func ExecuteAsync(b bool) ExecuteOption {
|
||||||
return func(o *ExecuteOptions) {
|
return func(o *ExecuteOptions) {
|
||||||
o.Async = b
|
o.Async = b
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewExecuteOptions create new ExecuteOptions struct
|
||||||
func NewExecuteOptions(opts ...ExecuteOption) ExecuteOptions {
|
func NewExecuteOptions(opts ...ExecuteOption) ExecuteOptions {
|
||||||
options := ExecuteOptions{
|
options := ExecuteOptions{
|
||||||
Client: client.DefaultClient,
|
Client: client.DefaultClient,
|
||||||
@@ -192,6 +203,7 @@ func NewExecuteOptions(opts ...ExecuteOption) ExecuteOptions {
|
|||||||
return options
|
return options
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// StepOptions holds step options
|
||||||
type StepOptions struct {
|
type StepOptions struct {
|
||||||
Context context.Context
|
Context context.Context
|
||||||
Fallback string
|
Fallback string
|
||||||
@@ -199,8 +211,10 @@ type StepOptions struct {
|
|||||||
Requires []string
|
Requires []string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// StepOption func signature
|
||||||
type StepOption func(*StepOptions)
|
type StepOption func(*StepOptions)
|
||||||
|
|
||||||
|
// NewStepOptions create new StepOptions struct
|
||||||
func NewStepOptions(opts ...StepOption) StepOptions {
|
func NewStepOptions(opts ...StepOption) StepOptions {
|
||||||
options := StepOptions{
|
options := StepOptions{
|
||||||
Context: context.Background(),
|
Context: context.Background(),
|
||||||
@@ -211,18 +225,21 @@ func NewStepOptions(opts ...StepOption) StepOptions {
|
|||||||
return options
|
return options
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// StepID sets the step id for dag
|
||||||
func StepID(id string) StepOption {
|
func StepID(id string) StepOption {
|
||||||
return func(o *StepOptions) {
|
return func(o *StepOptions) {
|
||||||
o.ID = id
|
o.ID = id
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// StepRequires specifies required steps
|
||||||
func StepRequires(steps ...string) StepOption {
|
func StepRequires(steps ...string) StepOption {
|
||||||
return func(o *StepOptions) {
|
return func(o *StepOptions) {
|
||||||
o.Requires = steps
|
o.Requires = steps
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// StepFallback set the step to run on error
|
||||||
func StepFallback(step string) StepOption {
|
func StepFallback(step string) StepOption {
|
||||||
return func(o *StepOptions) {
|
return func(o *StepOptions) {
|
||||||
o.Fallback = step
|
o.Fallback = step
|
||||||
|
@@ -1,3 +1,4 @@
|
|||||||
|
//go:build ignore
|
||||||
// +build ignore
|
// +build ignore
|
||||||
|
|
||||||
package micro
|
package micro
|
||||||
|
@@ -1,3 +1,4 @@
|
|||||||
|
//go:build ignore
|
||||||
// +build ignore
|
// +build ignore
|
||||||
|
|
||||||
package micro
|
package micro
|
||||||
|
7
go.mod
7
go.mod
@@ -4,11 +4,10 @@ go 1.16
|
|||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/ef-ds/deque v1.0.4
|
github.com/ef-ds/deque v1.0.4
|
||||||
github.com/golang-jwt/jwt/v4 v4.1.0
|
github.com/golang-jwt/jwt/v4 v4.3.0
|
||||||
github.com/imdario/mergo v0.3.12
|
github.com/imdario/mergo v0.3.12
|
||||||
github.com/patrickmn/go-cache v2.1.0+incompatible
|
github.com/patrickmn/go-cache v2.1.0+incompatible
|
||||||
github.com/silas/dag v0.0.0-20210626123444-3804bac2d6d4
|
github.com/silas/dag v0.0.0-20211117232152-9d50aa809f35
|
||||||
go.unistack.org/micro-proto/v3 v3.1.0
|
go.unistack.org/micro-proto/v3 v3.2.7
|
||||||
golang.org/x/net v0.0.0-20210928044308-7d9f5e0b762b
|
golang.org/x/net v0.0.0-20210928044308-7d9f5e0b762b
|
||||||
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
|
|
||||||
)
|
)
|
||||||
|
143
go.sum
143
go.sum
@@ -1,31 +1,162 @@
|
|||||||
|
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
|
||||||
|
cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
|
||||||
|
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
|
||||||
|
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
|
||||||
|
github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY=
|
||||||
|
github.com/buger/jsonparser v1.1.1/go.mod h1:6RYKKt7H4d4+iWqouImQ9R2FZql3VbhNgx27UK13J/0=
|
||||||
|
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
|
||||||
|
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
|
||||||
|
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
|
||||||
|
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
|
||||||
|
github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk=
|
||||||
|
github.com/cncf/xds/go v0.0.0-20210312221358-fbca930ec8ed/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs=
|
||||||
|
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815/go.mod h1:WwZ+bS3ebgob9U8Nd0kOddGdZWjyMGR8Wziv+TBNwSE=
|
||||||
github.com/ef-ds/deque v1.0.4 h1:iFAZNmveMT9WERAkqLJ+oaABF9AcVQ5AjXem/hroniI=
|
github.com/ef-ds/deque v1.0.4 h1:iFAZNmveMT9WERAkqLJ+oaABF9AcVQ5AjXem/hroniI=
|
||||||
github.com/ef-ds/deque v1.0.4/go.mod h1:gXDnTC3yqvBcHbq2lcExjtAcVrOnJCbMcZXmuj8Z4tg=
|
github.com/ef-ds/deque v1.0.4/go.mod h1:gXDnTC3yqvBcHbq2lcExjtAcVrOnJCbMcZXmuj8Z4tg=
|
||||||
github.com/golang-jwt/jwt/v4 v4.1.0 h1:XUgk2Ex5veyVFVeLm0xhusUTQybEbexJXrvPNOKkSY0=
|
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
|
||||||
github.com/golang-jwt/jwt/v4 v4.1.0/go.mod h1:/xlHOz8bRuivTWchD4jCa+NbatV+wEUSzwAxVc6locg=
|
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
|
||||||
|
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
|
||||||
|
github.com/envoyproxy/go-control-plane v0.9.9-0.20201210154907-fd9021fe5dad/go.mod h1:cXg6YxExXjJnVBQHBLXeUAgxn2UodCpnH306RInaBQk=
|
||||||
|
github.com/envoyproxy/go-control-plane v0.9.9-0.20210512163311-63b5d3c536b0/go.mod h1:hliV/p42l8fGbc6Y9bQ70uLwIvmJyVE5k4iMKlh8wCQ=
|
||||||
|
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
|
||||||
|
github.com/flowstack/go-jsonschema v0.1.1/go.mod h1:yL7fNggx1o8rm9RlgXv7hTBWxdBM0rVwpMwimd3F3N0=
|
||||||
|
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
|
||||||
|
github.com/golang-jwt/jwt/v4 v4.3.0 h1:kHL1vqdqWNfATmA0FNMdmZNMyZI1U6O31X4rlIPoBog=
|
||||||
|
github.com/golang-jwt/jwt/v4 v4.3.0/go.mod h1:/xlHOz8bRuivTWchD4jCa+NbatV+wEUSzwAxVc6locg=
|
||||||
|
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
|
||||||
|
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
|
||||||
|
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
||||||
|
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
||||||
|
github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw=
|
||||||
|
github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8=
|
||||||
|
github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA=
|
||||||
|
github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs=
|
||||||
|
github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w=
|
||||||
|
github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0=
|
||||||
|
github.com/golang/protobuf v1.4.1/go.mod h1:U8fpvMrcmy5pZrNK1lt4xCsGvpyWQ/VVv6QDs8UjoX8=
|
||||||
|
github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI=
|
||||||
|
github.com/golang/protobuf v1.4.3/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI=
|
||||||
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
|
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
|
||||||
|
github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw=
|
||||||
|
github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
|
||||||
|
github.com/google/gnostic v0.6.6 h1:MVSM2r2j9aRUvYNym66JGW96Ddd5MN4sTi59yktb6yk=
|
||||||
|
github.com/google/gnostic v0.6.6/go.mod h1:Nm8234We1lq6iB9OmlgNv3nH91XLLVZHCDayfA3xq+E=
|
||||||
|
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
|
||||||
|
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
|
||||||
|
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
|
||||||
|
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||||
|
github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||||
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
|
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
|
||||||
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||||
|
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||||
|
github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw=
|
||||||
github.com/imdario/mergo v0.3.12 h1:b6R2BslTbIEToALKP7LxUvijTsNI9TAe80pLWN2g/HU=
|
github.com/imdario/mergo v0.3.12 h1:b6R2BslTbIEToALKP7LxUvijTsNI9TAe80pLWN2g/HU=
|
||||||
github.com/imdario/mergo v0.3.12/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
|
github.com/imdario/mergo v0.3.12/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
|
||||||
|
github.com/kr/pretty v0.2.0 h1:s5hAObm+yFO5uHYt5dYjxi2rXrsnmRpJx4OYvIWUaQs=
|
||||||
|
github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
|
||||||
|
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
||||||
|
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
|
||||||
|
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
||||||
github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc=
|
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/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
|
||||||
github.com/silas/dag v0.0.0-20210626123444-3804bac2d6d4 h1:fOH64AB0C3ixGf9emky61STvPJL3smxJg+1Zwx1oCdg=
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
github.com/silas/dag v0.0.0-20210626123444-3804bac2d6d4/go.mod h1:7RTUFBdIRC9nZ7/3RyRNH1bdqIShrDejd1YbLwgPS+I=
|
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
|
||||||
go.unistack.org/micro-proto/v3 v3.1.0 h1:q39FwjFiRZn+Ux/tt+d3bJTmDtsQQWa+3SLYVo1vLfA=
|
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
|
||||||
go.unistack.org/micro-proto/v3 v3.1.0/go.mod h1:DpRhYCBXlmSJ/AAXTmntvlh7kQkYU6eFvlmYAx4BQS8=
|
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=
|
||||||
|
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
|
||||||
|
github.com/stoewer/go-strcase v1.2.0/go.mod h1:IBiWB2sKIp3wVVQ3Y035++gc+knqhUQag1KpM8ahLw8=
|
||||||
|
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||||
|
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||||
|
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
|
||||||
|
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU=
|
||||||
|
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415/go.mod h1:GwrjFmJcFw6At/Gs6z4yjiIwzuJ1/+UwLxMQDVQXShQ=
|
||||||
|
github.com/xeipuuv/gojsonschema v1.2.0/go.mod h1:anYRn/JVcOK2ZgGU+IjEV4nwlhoK5sQluxsYJ78Id3Y=
|
||||||
|
go.opentelemetry.io/proto/otlp v0.7.0/go.mod h1:PqfVotwruBrMGOCsRd/89rSnXhoiJIqeYNgFYFoEGnI=
|
||||||
|
go.unistack.org/micro-proto/v3 v3.2.7 h1:zG6d69kHc+oij2lwQ3AfrCgdjiEVRG2A7TlsxjusWs4=
|
||||||
|
go.unistack.org/micro-proto/v3 v3.2.7/go.mod h1:ZltVWNECD5yK+40+OCONzGw4OtmSdTpVi8/KFgo9dqM=
|
||||||
|
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||||
|
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
||||||
|
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
|
||||||
|
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
|
||||||
|
golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU=
|
||||||
|
golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
|
||||||
|
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||||
|
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||||
|
golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||||
|
golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||||
|
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||||
|
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||||
|
golang.org/x/net v0.0.0-20200822124328-c89045814202/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
|
||||||
|
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
|
||||||
|
golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
|
||||||
golang.org/x/net v0.0.0-20210928044308-7d9f5e0b762b h1:eB48h3HiRycXNy8E0Gf5e0hv7YT6Kt14L/D73G1fuwo=
|
golang.org/x/net v0.0.0-20210928044308-7d9f5e0b762b h1:eB48h3HiRycXNy8E0Gf5e0hv7YT6Kt14L/D73G1fuwo=
|
||||||
golang.org/x/net v0.0.0-20210928044308-7d9f5e0b762b/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
|
golang.org/x/net v0.0.0-20210928044308-7d9f5e0b762b/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
|
||||||
|
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
|
||||||
|
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
|
||||||
|
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
|
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
|
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
|
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
|
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||||
|
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||||
|
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||||
|
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||||
|
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||||
|
golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||||
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||||
|
golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||||
|
golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY=
|
||||||
|
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
|
||||||
|
golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
|
||||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||||
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
|
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
|
||||||
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||||
|
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
|
||||||
|
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
|
||||||
|
google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=
|
||||||
|
google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc=
|
||||||
|
google.golang.org/genproto v0.0.0-20200513103714-09dca8ec2884/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c=
|
||||||
|
google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013/go.mod h1:NbSheEEYHJ7i3ixzK3sjbqSGDJWnxyFXZblF3eUsNvo=
|
||||||
|
google.golang.org/genproto v0.0.0-20220107163113-42d7afdf6368/go.mod h1:5CzLGKJ67TSI2B9POpiiyGha0AjJvZIUgRMt1dSmuhc=
|
||||||
|
google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
|
||||||
|
google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg=
|
||||||
|
google.golang.org/grpc v1.25.1/go.mod h1:c3i+UQWmh7LiEpx4sFZnkU36qjEYZ0imhYfXVyQciAY=
|
||||||
|
google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk=
|
||||||
|
google.golang.org/grpc v1.33.1/go.mod h1:fr5YgcSWrqhRRxogOsw7RzIpsmvOZ6IcH4kBYTpR3n0=
|
||||||
|
google.golang.org/grpc v1.36.0/go.mod h1:qjiiYl8FncCW8feJPdyg3v6XW24KsRHe+dy9BAGRRjU=
|
||||||
|
google.golang.org/grpc v1.40.0/go.mod h1:ogyxbiOoUXAkP+4+xa6PZSE9DZgIHtSpzjDTB9KAK34=
|
||||||
|
google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8=
|
||||||
|
google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0=
|
||||||
|
google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM=
|
||||||
|
google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE=
|
||||||
|
google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo=
|
||||||
|
google.golang.org/protobuf v1.22.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
|
||||||
|
google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
|
||||||
|
google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
|
||||||
|
google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c=
|
||||||
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
|
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
|
||||||
|
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
|
||||||
google.golang.org/protobuf v1.27.1 h1:SnqbnDw1V7RiZcXPx5MEeqPv2s79L9i7BJUlG/+RurQ=
|
google.golang.org/protobuf v1.27.1 h1:SnqbnDw1V7RiZcXPx5MEeqPv2s79L9i7BJUlG/+RurQ=
|
||||||
google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
|
google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
|
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
|
||||||
|
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
|
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||||
|
gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||||
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
|
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
|
||||||
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||||
|
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
|
||||||
|
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
||||||
|
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
||||||
|
@@ -12,11 +12,11 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type defaultLogger struct {
|
type defaultLogger struct {
|
||||||
|
sync.RWMutex
|
||||||
enc *json.Encoder
|
enc *json.Encoder
|
||||||
logFunc LogFunc
|
logFunc LogFunc
|
||||||
logfFunc LogfFunc
|
logfFunc LogfFunc
|
||||||
opts Options
|
opts Options
|
||||||
sync.RWMutex
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Init(opts...) should only overwrite provided options
|
// Init(opts...) should only overwrite provided options
|
||||||
@@ -81,6 +81,8 @@ func (l *defaultLogger) Fields(fields ...interface{}) Logger {
|
|||||||
} else if len(fields)%2 != 0 {
|
} else if len(fields)%2 != 0 {
|
||||||
fields = fields[:len(fields)-1]
|
fields = fields[:len(fields)-1]
|
||||||
}
|
}
|
||||||
|
nl.logFunc = l.logFunc
|
||||||
|
nl.logfFunc = l.logfFunc
|
||||||
nl.opts.Fields = append(nl.opts.Fields, fields...)
|
nl.opts.Fields = append(nl.opts.Fields, fields...)
|
||||||
return nl
|
return nl
|
||||||
}
|
}
|
||||||
|
@@ -7,6 +7,37 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func TestContext(t *testing.T) {
|
||||||
|
ctx := context.TODO()
|
||||||
|
buf := bytes.NewBuffer(nil)
|
||||||
|
l := NewLogger(WithLevel(TraceLevel), WithOutput(buf))
|
||||||
|
if err := l.Init(); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
nl, ok := FromContext(NewContext(ctx, l.Fields("key", "val")))
|
||||||
|
if !ok {
|
||||||
|
t.Fatal("context without logger")
|
||||||
|
}
|
||||||
|
nl.Info(ctx, "message")
|
||||||
|
if !bytes.Contains(buf.Bytes(), []byte(`"key":"val"`)) {
|
||||||
|
t.Fatalf("logger fields not works, buf contains: %s", buf.Bytes())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFields(t *testing.T) {
|
||||||
|
ctx := context.TODO()
|
||||||
|
buf := bytes.NewBuffer(nil)
|
||||||
|
l := NewLogger(WithLevel(TraceLevel), WithOutput(buf))
|
||||||
|
if err := l.Init(); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
l.Fields("key", "val").Info(ctx, "message")
|
||||||
|
if !bytes.Contains(buf.Bytes(), []byte(`"key":"val"`)) {
|
||||||
|
t.Fatalf("logger fields not works, buf contains: %s", buf.Bytes())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestClone(t *testing.T) {
|
func TestClone(t *testing.T) {
|
||||||
ctx := context.TODO()
|
ctx := context.TODO()
|
||||||
buf := bytes.NewBuffer(nil)
|
buf := bytes.NewBuffer(nil)
|
||||||
|
@@ -10,6 +10,7 @@ type stdLogger struct {
|
|||||||
level Level
|
level Level
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewStdLogger returns new *log.Logger baked by logger.Logger implementation
|
||||||
func NewStdLogger(l Logger, level Level) *log.Logger {
|
func NewStdLogger(l Logger, level Level) *log.Logger {
|
||||||
return log.New(&stdLogger{l: l, level: level}, "" /* prefix */, 0 /* flags */)
|
return log.New(&stdLogger{l: l, level: level}, "" /* prefix */, 0 /* flags */)
|
||||||
}
|
}
|
||||||
@@ -20,6 +21,7 @@ func (sl *stdLogger) Write(p []byte) (int, error) {
|
|||||||
return len(p), nil
|
return len(p), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RedirectStdLogger replace *log.Logger with logger.Logger implementation
|
||||||
func RedirectStdLogger(l Logger, level Level) func() {
|
func RedirectStdLogger(l Logger, level Level) func() {
|
||||||
flags := log.Flags()
|
flags := log.Flags()
|
||||||
prefix := log.Prefix()
|
prefix := log.Prefix()
|
||||||
|
@@ -20,104 +20,104 @@ type Wrapper interface {
|
|||||||
Logf(LogfFunc) LogfFunc
|
Logf(LogfFunc) LogfFunc
|
||||||
}
|
}
|
||||||
|
|
||||||
var _ Logger = &OmitLogger{}
|
var _ Logger = &omitLogger{}
|
||||||
|
|
||||||
type OmitLogger struct {
|
type omitLogger struct {
|
||||||
l Logger
|
l Logger
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewOmitLogger(l Logger) Logger {
|
func NewOmitLogger(l Logger) Logger {
|
||||||
return &OmitLogger{l: l}
|
return &omitLogger{l: l}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *OmitLogger) Init(opts ...Option) error {
|
func (w *omitLogger) Init(opts ...Option) error {
|
||||||
return w.l.Init(append(opts, WrapLogger(NewOmitWrapper()))...)
|
return w.l.Init(append(opts, WrapLogger(NewOmitWrapper()))...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *OmitLogger) V(level Level) bool {
|
func (w *omitLogger) V(level Level) bool {
|
||||||
return w.l.V(level)
|
return w.l.V(level)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *OmitLogger) Level(level Level) {
|
func (w *omitLogger) Level(level Level) {
|
||||||
w.l.Level(level)
|
w.l.Level(level)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *OmitLogger) Clone(opts ...Option) Logger {
|
func (w *omitLogger) Clone(opts ...Option) Logger {
|
||||||
return w.l.Clone(opts...)
|
return w.l.Clone(opts...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *OmitLogger) Options() Options {
|
func (w *omitLogger) Options() Options {
|
||||||
return w.l.Options()
|
return w.l.Options()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *OmitLogger) Fields(fields ...interface{}) Logger {
|
func (w *omitLogger) Fields(fields ...interface{}) Logger {
|
||||||
return w.l.Fields(fields...)
|
return w.l.Fields(fields...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *OmitLogger) Info(ctx context.Context, args ...interface{}) {
|
func (w *omitLogger) Info(ctx context.Context, args ...interface{}) {
|
||||||
w.l.Info(ctx, args...)
|
w.l.Info(ctx, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *OmitLogger) Trace(ctx context.Context, args ...interface{}) {
|
func (w *omitLogger) Trace(ctx context.Context, args ...interface{}) {
|
||||||
w.l.Trace(ctx, args...)
|
w.l.Trace(ctx, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *OmitLogger) Debug(ctx context.Context, args ...interface{}) {
|
func (w *omitLogger) Debug(ctx context.Context, args ...interface{}) {
|
||||||
w.l.Debug(ctx, args...)
|
w.l.Debug(ctx, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *OmitLogger) Warn(ctx context.Context, args ...interface{}) {
|
func (w *omitLogger) Warn(ctx context.Context, args ...interface{}) {
|
||||||
w.l.Warn(ctx, args...)
|
w.l.Warn(ctx, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *OmitLogger) Error(ctx context.Context, args ...interface{}) {
|
func (w *omitLogger) Error(ctx context.Context, args ...interface{}) {
|
||||||
w.l.Error(ctx, args...)
|
w.l.Error(ctx, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *OmitLogger) Fatal(ctx context.Context, args ...interface{}) {
|
func (w *omitLogger) Fatal(ctx context.Context, args ...interface{}) {
|
||||||
w.l.Fatal(ctx, args...)
|
w.l.Fatal(ctx, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *OmitLogger) Infof(ctx context.Context, msg string, args ...interface{}) {
|
func (w *omitLogger) Infof(ctx context.Context, msg string, args ...interface{}) {
|
||||||
w.l.Infof(ctx, msg, args...)
|
w.l.Infof(ctx, msg, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *OmitLogger) Tracef(ctx context.Context, msg string, args ...interface{}) {
|
func (w *omitLogger) Tracef(ctx context.Context, msg string, args ...interface{}) {
|
||||||
w.l.Tracef(ctx, msg, args...)
|
w.l.Tracef(ctx, msg, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *OmitLogger) Debugf(ctx context.Context, msg string, args ...interface{}) {
|
func (w *omitLogger) Debugf(ctx context.Context, msg string, args ...interface{}) {
|
||||||
w.l.Debugf(ctx, msg, args...)
|
w.l.Debugf(ctx, msg, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *OmitLogger) Warnf(ctx context.Context, msg string, args ...interface{}) {
|
func (w *omitLogger) Warnf(ctx context.Context, msg string, args ...interface{}) {
|
||||||
w.l.Warnf(ctx, msg, args...)
|
w.l.Warnf(ctx, msg, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *OmitLogger) Errorf(ctx context.Context, msg string, args ...interface{}) {
|
func (w *omitLogger) Errorf(ctx context.Context, msg string, args ...interface{}) {
|
||||||
w.l.Errorf(ctx, msg, args...)
|
w.l.Errorf(ctx, msg, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *OmitLogger) Fatalf(ctx context.Context, msg string, args ...interface{}) {
|
func (w *omitLogger) Fatalf(ctx context.Context, msg string, args ...interface{}) {
|
||||||
w.l.Fatalf(ctx, msg, args...)
|
w.l.Fatalf(ctx, msg, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *OmitLogger) Log(ctx context.Context, level Level, args ...interface{}) {
|
func (w *omitLogger) Log(ctx context.Context, level Level, args ...interface{}) {
|
||||||
w.l.Log(ctx, level, args...)
|
w.l.Log(ctx, level, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *OmitLogger) Logf(ctx context.Context, level Level, msg string, args ...interface{}) {
|
func (w *omitLogger) Logf(ctx context.Context, level Level, msg string, args ...interface{}) {
|
||||||
w.l.Logf(ctx, level, msg, args...)
|
w.l.Logf(ctx, level, msg, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *OmitLogger) String() string {
|
func (w *omitLogger) String() string {
|
||||||
return w.l.String()
|
return w.l.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
type OmitWrapper struct{}
|
type omitWrapper struct{}
|
||||||
|
|
||||||
func NewOmitWrapper() Wrapper {
|
func NewOmitWrapper() Wrapper {
|
||||||
return &OmitWrapper{}
|
return &omitWrapper{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func getArgs(args []interface{}) []interface{} {
|
func getArgs(args []interface{}) []interface{} {
|
||||||
@@ -153,13 +153,13 @@ func getArgs(args []interface{}) []interface{} {
|
|||||||
return nargs
|
return nargs
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *OmitWrapper) Log(fn LogFunc) LogFunc {
|
func (w *omitWrapper) Log(fn LogFunc) LogFunc {
|
||||||
return func(ctx context.Context, level Level, args ...interface{}) {
|
return func(ctx context.Context, level Level, args ...interface{}) {
|
||||||
fn(ctx, level, getArgs(args)...)
|
fn(ctx, level, getArgs(args)...)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *OmitWrapper) Logf(fn LogfFunc) LogfFunc {
|
func (w *omitWrapper) Logf(fn LogfFunc) LogfFunc {
|
||||||
return func(ctx context.Context, level Level, msg string, args ...interface{}) {
|
return func(ctx context.Context, level Level, msg string, args ...interface{}) {
|
||||||
fn(ctx, level, msg, getArgs(args)...)
|
fn(ctx, level, msg, getArgs(args)...)
|
||||||
}
|
}
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
// Code generated by protoc-gen-go-micro. DO NOT EDIT.
|
// Code generated by protoc-gen-go-micro. DO NOT EDIT.
|
||||||
// protoc-gen-go-micro version: v3.5.2
|
// protoc-gen-go-micro version: v3.5.3
|
||||||
// source: handler.proto
|
// source: handler.proto
|
||||||
|
|
||||||
package handler
|
package handler
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
// Code generated by protoc-gen-go-micro. DO NOT EDIT.
|
// Code generated by protoc-gen-go-micro. DO NOT EDIT.
|
||||||
// protoc-gen-go-micro version: v3.5.2
|
// protoc-gen-go-micro version: v3.5.3
|
||||||
// source: handler.proto
|
// source: handler.proto
|
||||||
|
|
||||||
package handler
|
package handler
|
||||||
|
@@ -9,7 +9,7 @@ import (
|
|||||||
// Option powers the configuration for metrics implementations:
|
// Option powers the configuration for metrics implementations:
|
||||||
type Option func(*Options)
|
type Option func(*Options)
|
||||||
|
|
||||||
// Options for metrics implementations:
|
// Options for metrics implementations
|
||||||
type Options struct {
|
type Options struct {
|
||||||
// Logger used for logging
|
// Logger used for logging
|
||||||
Logger logger.Logger
|
Logger logger.Logger
|
||||||
@@ -102,6 +102,7 @@ func Logger(l logger.Logger) Option {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Labels sets the meter labels
|
||||||
func Labels(ls ...string) Option {
|
func Labels(ls ...string) Option {
|
||||||
return func(o *Options) {
|
return func(o *Options) {
|
||||||
o.Labels = append(o.Labels, ls...)
|
o.Labels = append(o.Labels, ls...)
|
||||||
|
@@ -11,25 +11,38 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
ClientRequestDurationSeconds = "client_request_duration_seconds"
|
// ClientRequestDurationSeconds specifies meter metric name
|
||||||
|
ClientRequestDurationSeconds = "client_request_duration_seconds"
|
||||||
|
// ClientRequestLatencyMicroseconds specifies meter metric name
|
||||||
ClientRequestLatencyMicroseconds = "client_request_latency_microseconds"
|
ClientRequestLatencyMicroseconds = "client_request_latency_microseconds"
|
||||||
ClientRequestTotal = "client_request_total"
|
// ClientRequestTotal specifies meter metric name
|
||||||
ClientRequestInflight = "client_request_inflight"
|
ClientRequestTotal = "client_request_total"
|
||||||
|
// ClientRequestInflight specifies meter metric name
|
||||||
ServerRequestDurationSeconds = "server_request_duration_seconds"
|
ClientRequestInflight = "client_request_inflight"
|
||||||
|
// ServerRequestDurationSeconds specifies meter metric name
|
||||||
|
ServerRequestDurationSeconds = "server_request_duration_seconds"
|
||||||
|
// ServerRequestLatencyMicroseconds specifies meter metric name
|
||||||
ServerRequestLatencyMicroseconds = "server_request_latency_microseconds"
|
ServerRequestLatencyMicroseconds = "server_request_latency_microseconds"
|
||||||
ServerRequestTotal = "server_request_total"
|
// ServerRequestTotal specifies meter metric name
|
||||||
ServerRequestInflight = "server_request_inflight"
|
ServerRequestTotal = "server_request_total"
|
||||||
|
// ServerRequestInflight specifies meter metric name
|
||||||
PublishMessageDurationSeconds = "publish_message_duration_seconds"
|
ServerRequestInflight = "server_request_inflight"
|
||||||
|
// PublishMessageDurationSeconds specifies meter metric name
|
||||||
|
PublishMessageDurationSeconds = "publish_message_duration_seconds"
|
||||||
|
// PublishMessageLatencyMicroseconds specifies meter metric name
|
||||||
PublishMessageLatencyMicroseconds = "publish_message_latency_microseconds"
|
PublishMessageLatencyMicroseconds = "publish_message_latency_microseconds"
|
||||||
PublishMessageTotal = "publish_message_total"
|
// PublishMessageTotal specifies meter metric name
|
||||||
PublishMessageInflight = "publish_message_inflight"
|
PublishMessageTotal = "publish_message_total"
|
||||||
|
// PublishMessageInflight specifies meter metric name
|
||||||
SubscribeMessageDurationSeconds = "subscribe_message_duration_seconds"
|
PublishMessageInflight = "publish_message_inflight"
|
||||||
|
// SubscribeMessageDurationSeconds specifies meter metric name
|
||||||
|
SubscribeMessageDurationSeconds = "subscribe_message_duration_seconds"
|
||||||
|
// SubscribeMessageLatencyMicroseconds specifies meter metric name
|
||||||
SubscribeMessageLatencyMicroseconds = "subscribe_message_latency_microseconds"
|
SubscribeMessageLatencyMicroseconds = "subscribe_message_latency_microseconds"
|
||||||
SubscribeMessageTotal = "subscribe_message_total"
|
// SubscribeMessageTotal specifies meter metric name
|
||||||
SubscribeMessageInflight = "subscribe_message_inflight"
|
SubscribeMessageTotal = "subscribe_message_total"
|
||||||
|
// SubscribeMessageInflight specifies meter metric name
|
||||||
|
SubscribeMessageInflight = "subscribe_message_inflight"
|
||||||
|
|
||||||
labelSuccess = "success"
|
labelSuccess = "success"
|
||||||
labelFailure = "failure"
|
labelFailure = "failure"
|
||||||
@@ -40,14 +53,17 @@ var (
|
|||||||
DefaultSkipEndpoints = []string{"Meter.Metrics"}
|
DefaultSkipEndpoints = []string{"Meter.Metrics"}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Options struct
|
||||||
type Options struct {
|
type Options struct {
|
||||||
Meter meter.Meter
|
Meter meter.Meter
|
||||||
lopts []meter.Option
|
lopts []meter.Option
|
||||||
SkipEndpoints []string
|
SkipEndpoints []string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Option func signature
|
||||||
type Option func(*Options)
|
type Option func(*Options)
|
||||||
|
|
||||||
|
// NewOptions creates new Options struct
|
||||||
func NewOptions(opts ...Option) Options {
|
func NewOptions(opts ...Option) Options {
|
||||||
options := Options{
|
options := Options{
|
||||||
Meter: meter.DefaultMeter,
|
Meter: meter.DefaultMeter,
|
||||||
@@ -60,30 +76,35 @@ func NewOptions(opts ...Option) Options {
|
|||||||
return options
|
return options
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ServiceName passes service name to meter label
|
||||||
func ServiceName(name string) Option {
|
func ServiceName(name string) Option {
|
||||||
return func(o *Options) {
|
return func(o *Options) {
|
||||||
o.lopts = append(o.lopts, meter.Labels("name", name))
|
o.lopts = append(o.lopts, meter.Labels("name", name))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ServiceVersion passes service version to meter label
|
||||||
func ServiceVersion(version string) Option {
|
func ServiceVersion(version string) Option {
|
||||||
return func(o *Options) {
|
return func(o *Options) {
|
||||||
o.lopts = append(o.lopts, meter.Labels("version", version))
|
o.lopts = append(o.lopts, meter.Labels("version", version))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ServiceID passes service id to meter label
|
||||||
func ServiceID(id string) Option {
|
func ServiceID(id string) Option {
|
||||||
return func(o *Options) {
|
return func(o *Options) {
|
||||||
o.lopts = append(o.lopts, meter.Labels("id", id))
|
o.lopts = append(o.lopts, meter.Labels("id", id))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Meter passes meter
|
||||||
func Meter(m meter.Meter) Option {
|
func Meter(m meter.Meter) Option {
|
||||||
return func(o *Options) {
|
return func(o *Options) {
|
||||||
o.Meter = m
|
o.Meter = m
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SkipEndpoint add endpoint to skip
|
||||||
func SkipEndoints(eps ...string) Option {
|
func SkipEndoints(eps ...string) Option {
|
||||||
return func(o *Options) {
|
return func(o *Options) {
|
||||||
o.SkipEndpoints = append(o.SkipEndpoints, eps...)
|
o.SkipEndpoints = append(o.SkipEndpoints, eps...)
|
||||||
@@ -96,6 +117,7 @@ type wrapper struct {
|
|||||||
opts Options
|
opts Options
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewClientWrapper create new client wrapper
|
||||||
func NewClientWrapper(opts ...Option) client.Wrapper {
|
func NewClientWrapper(opts ...Option) client.Wrapper {
|
||||||
return func(c client.Client) client.Client {
|
return func(c client.Client) client.Client {
|
||||||
handler := &wrapper{
|
handler := &wrapper{
|
||||||
@@ -106,6 +128,7 @@ func NewClientWrapper(opts ...Option) client.Wrapper {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewCallWrapper create new call wrapper
|
||||||
func NewCallWrapper(opts ...Option) client.CallWrapper {
|
func NewCallWrapper(opts ...Option) client.CallWrapper {
|
||||||
return func(fn client.CallFunc) client.CallFunc {
|
return func(fn client.CallFunc) client.CallFunc {
|
||||||
handler := &wrapper{
|
handler := &wrapper{
|
||||||
@@ -231,6 +254,7 @@ func (w *wrapper) Publish(ctx context.Context, p client.Message, opts ...client.
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewHandlerWrapper create new server handler wrapper
|
||||||
func NewHandlerWrapper(opts ...Option) server.HandlerWrapper {
|
func NewHandlerWrapper(opts ...Option) server.HandlerWrapper {
|
||||||
handler := &wrapper{
|
handler := &wrapper{
|
||||||
opts: NewOptions(opts...),
|
opts: NewOptions(opts...),
|
||||||
@@ -240,7 +264,7 @@ func NewHandlerWrapper(opts ...Option) server.HandlerWrapper {
|
|||||||
|
|
||||||
func (w *wrapper) HandlerFunc(fn server.HandlerFunc) server.HandlerFunc {
|
func (w *wrapper) HandlerFunc(fn server.HandlerFunc) server.HandlerFunc {
|
||||||
return func(ctx context.Context, req server.Request, rsp interface{}) error {
|
return func(ctx context.Context, req server.Request, rsp interface{}) error {
|
||||||
endpoint := req.Endpoint()
|
endpoint := req.Service() + "." + req.Endpoint()
|
||||||
for _, ep := range w.opts.SkipEndpoints {
|
for _, ep := range w.opts.SkipEndpoints {
|
||||||
if ep == endpoint {
|
if ep == endpoint {
|
||||||
return fn(ctx, req, rsp)
|
return fn(ctx, req, rsp)
|
||||||
@@ -270,6 +294,7 @@ func (w *wrapper) HandlerFunc(fn server.HandlerFunc) server.HandlerFunc {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewSubscribeWrapper create server subscribe wrapper
|
||||||
func NewSubscriberWrapper(opts ...Option) server.SubscriberWrapper {
|
func NewSubscriberWrapper(opts ...Option) server.SubscriberWrapper {
|
||||||
handler := &wrapper{
|
handler := &wrapper{
|
||||||
opts: NewOptions(opts...),
|
opts: NewOptions(opts...),
|
||||||
|
@@ -51,6 +51,4 @@ func TestExtractEndpoint(t *testing.T) {
|
|||||||
if endpoints[0].Response != "TestResponse" {
|
if endpoints[0].Response != "TestResponse" {
|
||||||
t.Fatalf("Expected TestResponse got %s", endpoints[0].Response)
|
t.Fatalf("Expected TestResponse got %s", endpoints[0].Response)
|
||||||
}
|
}
|
||||||
|
|
||||||
t.Logf("XXX %#+v\n", endpoints[0])
|
|
||||||
}
|
}
|
||||||
|
@@ -11,10 +11,11 @@ import (
|
|||||||
const (
|
const (
|
||||||
// WildcardDomain indicates any domain
|
// WildcardDomain indicates any domain
|
||||||
WildcardDomain = "*"
|
WildcardDomain = "*"
|
||||||
// DefaultDomain to use if none was provided in options
|
|
||||||
DefaultDomain = "micro"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// DefaultDomain to use if none was provided in options
|
||||||
|
var DefaultDomain = "micro"
|
||||||
|
|
||||||
var (
|
var (
|
||||||
// DefaultRegister is the global default register
|
// DefaultRegister is the global default register
|
||||||
DefaultRegister Register = NewRegister()
|
DefaultRegister Register = NewRegister()
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
// Code generated by protoc-gen-go-micro. DO NOT EDIT.
|
// Code generated by protoc-gen-go-micro. DO NOT EDIT.
|
||||||
// protoc-gen-go-micro version: v3.5.2
|
// protoc-gen-go-micro version: v3.5.3
|
||||||
// source: health.proto
|
// source: health.proto
|
||||||
|
|
||||||
package health
|
package health
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
// Code generated by protoc-gen-go-micro. DO NOT EDIT.
|
// Code generated by protoc-gen-go-micro. DO NOT EDIT.
|
||||||
// protoc-gen-go-micro version: v3.5.2
|
// protoc-gen-go-micro version: v3.5.3
|
||||||
// source: health.proto
|
// source: health.proto
|
||||||
|
|
||||||
package health
|
package health
|
||||||
|
@@ -16,6 +16,7 @@ import (
|
|||||||
"go.unistack.org/micro/v3/network/transport"
|
"go.unistack.org/micro/v3/network/transport"
|
||||||
"go.unistack.org/micro/v3/register"
|
"go.unistack.org/micro/v3/register"
|
||||||
"go.unistack.org/micro/v3/tracer"
|
"go.unistack.org/micro/v3/tracer"
|
||||||
|
"go.unistack.org/micro/v3/util/id"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Option func
|
// Option func
|
||||||
@@ -106,7 +107,7 @@ func NewOptions(opts ...Option) Options {
|
|||||||
Address: DefaultAddress,
|
Address: DefaultAddress,
|
||||||
Name: DefaultName,
|
Name: DefaultName,
|
||||||
Version: DefaultVersion,
|
Version: DefaultVersion,
|
||||||
ID: DefaultID,
|
ID: id.Must(),
|
||||||
Namespace: DefaultNamespace,
|
Namespace: DefaultNamespace,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -8,7 +8,6 @@ import (
|
|||||||
"go.unistack.org/micro/v3/codec"
|
"go.unistack.org/micro/v3/codec"
|
||||||
"go.unistack.org/micro/v3/metadata"
|
"go.unistack.org/micro/v3/metadata"
|
||||||
"go.unistack.org/micro/v3/register"
|
"go.unistack.org/micro/v3/register"
|
||||||
"go.unistack.org/micro/v3/util/id"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// DefaultServer default server
|
// DefaultServer default server
|
||||||
@@ -21,8 +20,6 @@ var (
|
|||||||
DefaultName = "server"
|
DefaultName = "server"
|
||||||
// DefaultVersion will be used if no version passed
|
// DefaultVersion will be used if no version passed
|
||||||
DefaultVersion = "latest"
|
DefaultVersion = "latest"
|
||||||
// DefaultID will be used if no id passed
|
|
||||||
DefaultID = id.Must()
|
|
||||||
// DefaultRegisterCheck holds func that run before register server
|
// DefaultRegisterCheck holds func that run before register server
|
||||||
DefaultRegisterCheck = func(context.Context) error { return nil }
|
DefaultRegisterCheck = func(context.Context) error { return nil }
|
||||||
// DefaultRegisterInterval holds interval for register
|
// DefaultRegisterInterval holds interval for register
|
||||||
@@ -126,11 +123,21 @@ type Response interface {
|
|||||||
// The last error will be left in Error().
|
// The last error will be left in Error().
|
||||||
// EOF indicates end of the stream.
|
// EOF indicates end of the stream.
|
||||||
type Stream interface {
|
type Stream interface {
|
||||||
|
// Context for the stream
|
||||||
Context() context.Context
|
Context() context.Context
|
||||||
|
// Request returns request
|
||||||
Request() Request
|
Request() Request
|
||||||
|
// Send will encode and send a request
|
||||||
Send(msg interface{}) error
|
Send(msg interface{}) error
|
||||||
|
// Recv will decode and read a response
|
||||||
Recv(msg interface{}) error
|
Recv(msg interface{}) error
|
||||||
|
// SendMsg will encode and send a request
|
||||||
|
SendMsg(msg interface{}) error
|
||||||
|
// RecvMsg will decode and read a response
|
||||||
|
RecvMsg(msg interface{}) error
|
||||||
|
// Error returns stream error
|
||||||
Error() error
|
Error() error
|
||||||
|
// Close closes the stream
|
||||||
Close() error
|
Close() error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -18,11 +18,11 @@ var (
|
|||||||
if md, ok := metadata.FromOutgoingContext(ctx); ok {
|
if md, ok := metadata.FromOutgoingContext(ctx); ok {
|
||||||
labels = make([]tracer.Label, 0, len(md))
|
labels = make([]tracer.Label, 0, len(md))
|
||||||
for k, v := range md {
|
for k, v := range md {
|
||||||
labels = append(labels, tracer.String(k, v))
|
labels = append(labels, tracer.LabelString(k, v))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
labels = append(labels, tracer.Bool("error", true))
|
labels = append(labels, tracer.LabelBool("error", true))
|
||||||
}
|
}
|
||||||
sp.SetLabels(labels...)
|
sp.SetLabels(labels...)
|
||||||
}
|
}
|
||||||
@@ -33,11 +33,11 @@ var (
|
|||||||
if md, ok := metadata.FromOutgoingContext(ctx); ok {
|
if md, ok := metadata.FromOutgoingContext(ctx); ok {
|
||||||
labels = make([]tracer.Label, 0, len(md))
|
labels = make([]tracer.Label, 0, len(md))
|
||||||
for k, v := range md {
|
for k, v := range md {
|
||||||
labels = append(labels, tracer.String(k, v))
|
labels = append(labels, tracer.LabelString(k, v))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
labels = append(labels, tracer.Bool("error", true))
|
labels = append(labels, tracer.LabelBool("error", true))
|
||||||
}
|
}
|
||||||
sp.SetLabels(labels...)
|
sp.SetLabels(labels...)
|
||||||
}
|
}
|
||||||
@@ -48,11 +48,11 @@ var (
|
|||||||
if md, ok := metadata.FromOutgoingContext(ctx); ok {
|
if md, ok := metadata.FromOutgoingContext(ctx); ok {
|
||||||
labels = make([]tracer.Label, 0, len(md))
|
labels = make([]tracer.Label, 0, len(md))
|
||||||
for k, v := range md {
|
for k, v := range md {
|
||||||
labels = append(labels, tracer.String(k, v))
|
labels = append(labels, tracer.LabelString(k, v))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
labels = append(labels, tracer.Bool("error", true))
|
labels = append(labels, tracer.LabelBool("error", true))
|
||||||
}
|
}
|
||||||
sp.SetLabels(labels...)
|
sp.SetLabels(labels...)
|
||||||
}
|
}
|
||||||
@@ -63,11 +63,11 @@ var (
|
|||||||
if md, ok := metadata.FromIncomingContext(ctx); ok {
|
if md, ok := metadata.FromIncomingContext(ctx); ok {
|
||||||
labels = make([]tracer.Label, 0, len(md))
|
labels = make([]tracer.Label, 0, len(md))
|
||||||
for k, v := range md {
|
for k, v := range md {
|
||||||
labels = append(labels, tracer.String(k, v))
|
labels = append(labels, tracer.LabelString(k, v))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
labels = append(labels, tracer.Bool("error", true))
|
labels = append(labels, tracer.LabelBool("error", true))
|
||||||
}
|
}
|
||||||
sp.SetLabels(labels...)
|
sp.SetLabels(labels...)
|
||||||
}
|
}
|
||||||
@@ -78,11 +78,11 @@ var (
|
|||||||
if md, ok := metadata.FromIncomingContext(ctx); ok {
|
if md, ok := metadata.FromIncomingContext(ctx); ok {
|
||||||
labels = make([]tracer.Label, 0, len(md))
|
labels = make([]tracer.Label, 0, len(md))
|
||||||
for k, v := range md {
|
for k, v := range md {
|
||||||
labels = append(labels, tracer.String(k, v))
|
labels = append(labels, tracer.LabelString(k, v))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
labels = append(labels, tracer.Bool("error", true))
|
labels = append(labels, tracer.LabelBool("error", true))
|
||||||
}
|
}
|
||||||
sp.SetLabels(labels...)
|
sp.SetLabels(labels...)
|
||||||
}
|
}
|
||||||
@@ -93,11 +93,11 @@ var (
|
|||||||
if md, ok := metadata.FromOutgoingContext(ctx); ok {
|
if md, ok := metadata.FromOutgoingContext(ctx); ok {
|
||||||
labels = make([]tracer.Label, 0, len(md))
|
labels = make([]tracer.Label, 0, len(md))
|
||||||
for k, v := range md {
|
for k, v := range md {
|
||||||
labels = append(labels, tracer.String(k, v))
|
labels = append(labels, tracer.LabelString(k, v))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
labels = append(labels, tracer.Bool("error", true))
|
labels = append(labels, tracer.LabelBool("error", true))
|
||||||
}
|
}
|
||||||
sp.SetLabels(labels...)
|
sp.SetLabels(labels...)
|
||||||
}
|
}
|
||||||
@@ -229,7 +229,10 @@ func (ot *tWrapper) Call(ctx context.Context, req client.Request, rsp interface{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
sp := tracer.SpanFromContext(ctx)
|
sp, ok := tracer.SpanFromContext(ctx)
|
||||||
|
if !ok {
|
||||||
|
ctx, sp = ot.opts.Tracer.Start(ctx, endpoint)
|
||||||
|
}
|
||||||
defer sp.Finish()
|
defer sp.Finish()
|
||||||
|
|
||||||
err := ot.Client.Call(ctx, req, rsp, opts...)
|
err := ot.Client.Call(ctx, req, rsp, opts...)
|
||||||
@@ -249,7 +252,10 @@ func (ot *tWrapper) Stream(ctx context.Context, req client.Request, opts ...clie
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
sp := tracer.SpanFromContext(ctx)
|
sp, ok := tracer.SpanFromContext(ctx)
|
||||||
|
if !ok {
|
||||||
|
ctx, sp = ot.opts.Tracer.Start(ctx, endpoint)
|
||||||
|
}
|
||||||
defer sp.Finish()
|
defer sp.Finish()
|
||||||
|
|
||||||
stream, err := ot.Client.Stream(ctx, req, opts...)
|
stream, err := ot.Client.Stream(ctx, req, opts...)
|
||||||
@@ -262,7 +268,10 @@ func (ot *tWrapper) Stream(ctx context.Context, req client.Request, opts ...clie
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (ot *tWrapper) Publish(ctx context.Context, msg client.Message, opts ...client.PublishOption) error {
|
func (ot *tWrapper) Publish(ctx context.Context, msg client.Message, opts ...client.PublishOption) error {
|
||||||
sp := tracer.SpanFromContext(ctx)
|
sp, ok := tracer.SpanFromContext(ctx)
|
||||||
|
if !ok {
|
||||||
|
ctx, sp = ot.opts.Tracer.Start(ctx, msg.Topic())
|
||||||
|
}
|
||||||
defer sp.Finish()
|
defer sp.Finish()
|
||||||
|
|
||||||
err := ot.Client.Publish(ctx, msg, opts...)
|
err := ot.Client.Publish(ctx, msg, opts...)
|
||||||
@@ -282,7 +291,10 @@ func (ot *tWrapper) ServerHandler(ctx context.Context, req server.Request, rsp i
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
sp := tracer.SpanFromContext(ctx)
|
sp, ok := tracer.SpanFromContext(ctx)
|
||||||
|
if !ok {
|
||||||
|
ctx, sp = ot.opts.Tracer.Start(ctx, fmt.Sprintf("%s.%s", req.Service(), req.Endpoint()))
|
||||||
|
}
|
||||||
defer sp.Finish()
|
defer sp.Finish()
|
||||||
|
|
||||||
err := ot.serverHandler(ctx, req, rsp)
|
err := ot.serverHandler(ctx, req, rsp)
|
||||||
@@ -295,7 +307,10 @@ func (ot *tWrapper) ServerHandler(ctx context.Context, req server.Request, rsp i
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (ot *tWrapper) ServerSubscriber(ctx context.Context, msg server.Message) error {
|
func (ot *tWrapper) ServerSubscriber(ctx context.Context, msg server.Message) error {
|
||||||
sp := tracer.SpanFromContext(ctx)
|
sp, ok := tracer.SpanFromContext(ctx)
|
||||||
|
if !ok {
|
||||||
|
ctx, sp = ot.opts.Tracer.Start(ctx, msg.Topic())
|
||||||
|
}
|
||||||
defer sp.Finish()
|
defer sp.Finish()
|
||||||
|
|
||||||
err := ot.serverSubscriber(ctx, msg)
|
err := ot.serverSubscriber(ctx, msg)
|
||||||
@@ -339,7 +354,10 @@ func (ot *tWrapper) ClientCallFunc(ctx context.Context, addr string, req client.
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
sp := tracer.SpanFromContext(ctx)
|
sp, ok := tracer.SpanFromContext(ctx)
|
||||||
|
if !ok {
|
||||||
|
ctx, sp = ot.opts.Tracer.Start(ctx, endpoint)
|
||||||
|
}
|
||||||
defer sp.Finish()
|
defer sp.Finish()
|
||||||
|
|
||||||
err := ot.clientCallFunc(ctx, addr, req, rsp, opts)
|
err := ot.clientCallFunc(ctx, addr, req, rsp, opts)
|
||||||
|
@@ -2,8 +2,12 @@ package buf // import "go.unistack.org/micro/v3/util/buf"
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"io"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var _ io.Closer = &Buffer{}
|
||||||
|
|
||||||
|
// Buffer bytes.Buffer wrapper to satisfie io.Closer interface
|
||||||
type Buffer struct {
|
type Buffer struct {
|
||||||
*bytes.Buffer
|
*bytes.Buffer
|
||||||
}
|
}
|
||||||
|
@@ -1,3 +1,4 @@
|
|||||||
|
//go:build ignore
|
||||||
// +build ignore
|
// +build ignore
|
||||||
|
|
||||||
package http
|
package http
|
||||||
|
@@ -45,8 +45,7 @@ var methodMap = map[string]methodTyp{
|
|||||||
http.MethodTrace: mTRACE,
|
http.MethodTrace: mTRACE,
|
||||||
}
|
}
|
||||||
|
|
||||||
// RegisterMethod adds support for custom HTTP method handlers, available
|
// RegisterMethod adds support for custom HTTP method handlers
|
||||||
// via Router#Method and Router#MethodFunc
|
|
||||||
func RegisterMethod(method string) error {
|
func RegisterMethod(method string) error {
|
||||||
if method == "" {
|
if method == "" {
|
||||||
return nil
|
return nil
|
||||||
@@ -75,13 +74,13 @@ const (
|
|||||||
ntCatchAll // /api/v1/*
|
ntCatchAll // /api/v1/*
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewTrie() *Node {
|
// NewTrie create new tree
|
||||||
return &Node{}
|
func NewTrie() *Trie {
|
||||||
|
return &Trie{}
|
||||||
}
|
}
|
||||||
|
|
||||||
type Trie = Node
|
// Trie holds nodes for path based tree search
|
||||||
|
type Trie struct {
|
||||||
type Node struct {
|
|
||||||
// regexp matcher for regexp nodes
|
// regexp matcher for regexp nodes
|
||||||
rex *regexp.Regexp
|
rex *regexp.Regexp
|
||||||
|
|
||||||
@@ -128,7 +127,8 @@ func (s endpoints) Value(method methodTyp) *endpoint {
|
|||||||
return mh
|
return mh
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n *Node) Insert(methods []string, pattern string, handler interface{}) error {
|
// Insert add elemenent to tree
|
||||||
|
func (n *Trie) Insert(methods []string, pattern string, handler interface{}) error {
|
||||||
var err error
|
var err error
|
||||||
for _, method := range methods {
|
for _, method := range methods {
|
||||||
if err = n.insert(methodMap[method], pattern, handler); err != nil {
|
if err = n.insert(methodMap[method], pattern, handler); err != nil {
|
||||||
@@ -138,8 +138,8 @@ func (n *Node) Insert(methods []string, pattern string, handler interface{}) err
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n *Node) insert(method methodTyp, pattern string, handler interface{}) error {
|
func (n *Trie) insert(method methodTyp, pattern string, handler interface{}) error {
|
||||||
var parent *Node
|
var parent *Trie
|
||||||
search := pattern
|
search := pattern
|
||||||
|
|
||||||
for {
|
for {
|
||||||
@@ -175,8 +175,8 @@ func (n *Node) insert(method methodTyp, pattern string, handler interface{}) err
|
|||||||
|
|
||||||
// No edge, create one
|
// No edge, create one
|
||||||
if n == nil {
|
if n == nil {
|
||||||
child := &Node{typ: ntStatic, label: label, tail: segTail, prefix: search}
|
child := &Trie{typ: ntStatic, label: label, tail: segTail, prefix: search}
|
||||||
var hn *Node
|
var hn *Trie
|
||||||
hn, err = parent.addChild(child, search)
|
hn, err = parent.addChild(child, search)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -205,7 +205,7 @@ func (n *Node) insert(method methodTyp, pattern string, handler interface{}) err
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Split the node
|
// Split the node
|
||||||
child := &Node{
|
child := &Trie{
|
||||||
typ: ntStatic,
|
typ: ntStatic,
|
||||||
prefix: search[:commonPrefix],
|
prefix: search[:commonPrefix],
|
||||||
}
|
}
|
||||||
@@ -227,12 +227,12 @@ func (n *Node) insert(method methodTyp, pattern string, handler interface{}) err
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Create a new edge for the node
|
// Create a new edge for the node
|
||||||
subchild := &Node{
|
subchild := &Trie{
|
||||||
typ: ntStatic,
|
typ: ntStatic,
|
||||||
label: search[0],
|
label: search[0],
|
||||||
prefix: search,
|
prefix: search,
|
||||||
}
|
}
|
||||||
var hn *Node
|
var hn *Trie
|
||||||
hn, err = child.addChild(subchild, search)
|
hn, err = child.addChild(subchild, search)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -245,7 +245,7 @@ func (n *Node) insert(method methodTyp, pattern string, handler interface{}) err
|
|||||||
// For a URL router like chi's, we split the static, param, regexp and wildcard segments
|
// For a URL router like chi's, we split the static, param, regexp and wildcard segments
|
||||||
// into different nodes. In addition, addChild will recursively call itself until every
|
// into different nodes. In addition, addChild will recursively call itself until every
|
||||||
// pattern segment is added to the url pattern tree as individual nodes, depending on type.
|
// pattern segment is added to the url pattern tree as individual nodes, depending on type.
|
||||||
func (n *Node) addChild(child *Node, prefix string) (*Node, error) {
|
func (n *Trie) addChild(child *Trie, prefix string) (*Trie, error) {
|
||||||
search := prefix
|
search := prefix
|
||||||
|
|
||||||
// handler leaf node added to the tree is the child.
|
// handler leaf node added to the tree is the child.
|
||||||
@@ -298,7 +298,7 @@ func (n *Node) addChild(child *Node, prefix string) (*Node, error) {
|
|||||||
|
|
||||||
search = search[segStartIdx:] // advance search position
|
search = search[segStartIdx:] // advance search position
|
||||||
|
|
||||||
nn := &Node{
|
nn := &Trie{
|
||||||
typ: ntStatic,
|
typ: ntStatic,
|
||||||
label: search[0],
|
label: search[0],
|
||||||
prefix: search,
|
prefix: search,
|
||||||
@@ -321,7 +321,7 @@ func (n *Node) addChild(child *Node, prefix string) (*Node, error) {
|
|||||||
// add the param edge node
|
// add the param edge node
|
||||||
search = search[segStartIdx:]
|
search = search[segStartIdx:]
|
||||||
|
|
||||||
nn := &Node{
|
nn := &Trie{
|
||||||
typ: segTyp,
|
typ: segTyp,
|
||||||
label: search[0],
|
label: search[0],
|
||||||
tail: segTail,
|
tail: segTail,
|
||||||
@@ -339,7 +339,7 @@ func (n *Node) addChild(child *Node, prefix string) (*Node, error) {
|
|||||||
return hn, nil
|
return hn, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n *Node) replaceChild(label, tail byte, child *Node) error {
|
func (n *Trie) replaceChild(label, tail byte, child *Trie) error {
|
||||||
for i := 0; i < len(n.children[child.typ]); i++ {
|
for i := 0; i < len(n.children[child.typ]); i++ {
|
||||||
if n.children[child.typ][i].label == label && n.children[child.typ][i].tail == tail {
|
if n.children[child.typ][i].label == label && n.children[child.typ][i].tail == tail {
|
||||||
n.children[child.typ][i] = child
|
n.children[child.typ][i] = child
|
||||||
@@ -351,7 +351,7 @@ func (n *Node) replaceChild(label, tail byte, child *Node) error {
|
|||||||
return fmt.Errorf("replacing missing child")
|
return fmt.Errorf("replacing missing child")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n *Node) getEdge(ntyp nodeTyp, label, tail byte, prefix string) *Node {
|
func (n *Trie) getEdge(ntyp nodeTyp, label, tail byte, prefix string) *Trie {
|
||||||
nds := n.children[ntyp]
|
nds := n.children[ntyp]
|
||||||
for i := 0; i < len(nds); i++ {
|
for i := 0; i < len(nds); i++ {
|
||||||
if nds[i].label == label && nds[i].tail == tail {
|
if nds[i].label == label && nds[i].tail == tail {
|
||||||
@@ -364,7 +364,7 @@ func (n *Node) getEdge(ntyp nodeTyp, label, tail byte, prefix string) *Node {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n *Node) setEndpoint(method methodTyp, handler interface{}, pattern string) error {
|
func (n *Trie) setEndpoint(method methodTyp, handler interface{}, pattern string) error {
|
||||||
// Set the handler for the method type on the node
|
// Set the handler for the method type on the node
|
||||||
if n.endpoints == nil {
|
if n.endpoints == nil {
|
||||||
n.endpoints = make(endpoints)
|
n.endpoints = make(endpoints)
|
||||||
@@ -398,7 +398,8 @@ func (n *Node) setEndpoint(method methodTyp, handler interface{}, pattern string
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n *Node) Search(method string, path string) (interface{}, map[string]string, bool) {
|
// Search try to find element in tree with path and method
|
||||||
|
func (n *Trie) Search(method string, path string) (interface{}, map[string]string, bool) {
|
||||||
params := &routeParams{}
|
params := &routeParams{}
|
||||||
// Find the routing handlers for the path
|
// Find the routing handlers for the path
|
||||||
rn := n.findRoute(params, methodMap[method], path)
|
rn := n.findRoute(params, methodMap[method], path)
|
||||||
@@ -425,7 +426,7 @@ type routeParams struct {
|
|||||||
|
|
||||||
// Recursive edge traversal by checking all nodeTyp groups along the way.
|
// Recursive edge traversal by checking all nodeTyp groups along the way.
|
||||||
// It's like searching through a multi-dimensional radix trie.
|
// It's like searching through a multi-dimensional radix trie.
|
||||||
func (n *Node) findRoute(params *routeParams, method methodTyp, path string) *Node {
|
func (n *Trie) findRoute(params *routeParams, method methodTyp, path string) *Trie {
|
||||||
nn := n
|
nn := n
|
||||||
search := path
|
search := path
|
||||||
|
|
||||||
@@ -435,7 +436,7 @@ func (n *Node) findRoute(params *routeParams, method methodTyp, path string) *No
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
var xn *Node
|
var xn *Trie
|
||||||
xsearch := search
|
xsearch := search
|
||||||
|
|
||||||
var label byte
|
var label byte
|
||||||
@@ -550,7 +551,7 @@ func (n *Node) findRoute(params *routeParams, method methodTyp, path string) *No
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n *Node) isLeaf() bool {
|
func (n *Trie) isLeaf() bool {
|
||||||
return n.endpoints != nil
|
return n.endpoints != nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -665,7 +666,7 @@ func longestPrefix(k1, k2 string) int {
|
|||||||
return i
|
return i
|
||||||
}
|
}
|
||||||
|
|
||||||
type nodes []*Node
|
type nodes []*Trie
|
||||||
|
|
||||||
// Sort the list of nodes by label
|
// Sort the list of nodes by label
|
||||||
func (ns nodes) Sort() { sort.Sort(ns); ns.tailSort() }
|
func (ns nodes) Sort() { sort.Sort(ns); ns.tailSort() }
|
||||||
@@ -684,7 +685,7 @@ func (ns nodes) tailSort() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ns nodes) findEdge(label byte) *Node {
|
func (ns nodes) findEdge(label byte) *Trie {
|
||||||
num := len(ns)
|
num := len(ns)
|
||||||
idx := 0
|
idx := 0
|
||||||
i, j := 0, num-1
|
i, j := 0, num-1
|
||||||
|
@@ -1,3 +1,4 @@
|
|||||||
|
//go:build ignore
|
||||||
// +build ignore
|
// +build ignore
|
||||||
|
|
||||||
package pool
|
package pool
|
||||||
|
@@ -10,16 +10,19 @@ type Rand struct {
|
|||||||
buf [8]byte
|
buf [8]byte
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Int31 function implementation
|
||||||
func (r *Rand) Int31() int32 {
|
func (r *Rand) Int31() int32 {
|
||||||
_, _ = crand.Read(r.buf[:4])
|
_, _ = crand.Read(r.buf[:4])
|
||||||
return int32(binary.BigEndian.Uint32(r.buf[:4]) & ^uint32(1<<31))
|
return int32(binary.BigEndian.Uint32(r.buf[:4]) & ^uint32(1<<31))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Int function implementation
|
||||||
func (r *Rand) Int() int {
|
func (r *Rand) Int() int {
|
||||||
u := uint(r.Int63())
|
u := uint(r.Int63())
|
||||||
return int(u << 1 >> 1) // clear sign bit if int == int32
|
return int(u << 1 >> 1) // clear sign bit if int == int32
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Float64 function implementation
|
||||||
func (r *Rand) Float64() float64 {
|
func (r *Rand) Float64() float64 {
|
||||||
again:
|
again:
|
||||||
f := float64(r.Int63()) / (1 << 63)
|
f := float64(r.Int63()) / (1 << 63)
|
||||||
@@ -29,6 +32,7 @@ again:
|
|||||||
return f
|
return f
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Float32 function implementation
|
||||||
func (r *Rand) Float32() float32 {
|
func (r *Rand) Float32() float32 {
|
||||||
again:
|
again:
|
||||||
f := float32(r.Float64())
|
f := float32(r.Float64())
|
||||||
@@ -38,14 +42,17 @@ again:
|
|||||||
return f
|
return f
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Uint32 function implementation
|
||||||
func (r *Rand) Uint32() uint32 {
|
func (r *Rand) Uint32() uint32 {
|
||||||
return uint32(r.Int63() >> 31)
|
return uint32(r.Int63() >> 31)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Uint64 function implementation
|
||||||
func (r *Rand) Uint64() uint64 {
|
func (r *Rand) Uint64() uint64 {
|
||||||
return uint64(r.Int63())>>31 | uint64(r.Int63())<<32
|
return uint64(r.Int63())>>31 | uint64(r.Int63())<<32
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Intn function implementation
|
||||||
func (r *Rand) Intn(n int) int {
|
func (r *Rand) Intn(n int) int {
|
||||||
if n <= 1<<31-1 {
|
if n <= 1<<31-1 {
|
||||||
return int(r.Int31n(int32(n)))
|
return int(r.Int31n(int32(n)))
|
||||||
@@ -53,12 +60,13 @@ func (r *Rand) Intn(n int) int {
|
|||||||
return int(r.Int63n(int64(n)))
|
return int(r.Int63n(int64(n)))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Int63 function implementation
|
||||||
func (r *Rand) Int63() int64 {
|
func (r *Rand) Int63() int64 {
|
||||||
_, _ = crand.Read(r.buf[:])
|
_, _ = crand.Read(r.buf[:])
|
||||||
return int64(binary.BigEndian.Uint64(r.buf[:]) & ^uint64(1<<63))
|
return int64(binary.BigEndian.Uint64(r.buf[:]) & ^uint64(1<<63))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Int31n copied from the standard library math/rand implementation of Int31n
|
// Int31n function implementation copied from the standard library math/rand implementation of Int31n
|
||||||
func (r *Rand) Int31n(n int32) int32 {
|
func (r *Rand) Int31n(n int32) int32 {
|
||||||
if n&(n-1) == 0 { // n is power of two, can mask
|
if n&(n-1) == 0 { // n is power of two, can mask
|
||||||
return r.Int31() & (n - 1)
|
return r.Int31() & (n - 1)
|
||||||
@@ -71,7 +79,7 @@ func (r *Rand) Int31n(n int32) int32 {
|
|||||||
return v % n
|
return v % n
|
||||||
}
|
}
|
||||||
|
|
||||||
// Int63n copied from the standard library math/rand implementation of Int63n
|
// Int63n function implementation copied from the standard library math/rand implementation of Int63n
|
||||||
func (r *Rand) Int63n(n int64) int64 {
|
func (r *Rand) Int63n(n int64) int64 {
|
||||||
if n&(n-1) == 0 { // n is power of two, can mask
|
if n&(n-1) == 0 { // n is power of two, can mask
|
||||||
return r.Int63() & (n - 1)
|
return r.Int63() & (n - 1)
|
||||||
@@ -84,7 +92,7 @@ func (r *Rand) Int63n(n int64) int64 {
|
|||||||
return v % n
|
return v % n
|
||||||
}
|
}
|
||||||
|
|
||||||
// Shuffle copied from the standard library math/rand implementation of Shuffle
|
// Shuffle function implementation copied from the standard library math/rand implementation of Shuffle
|
||||||
func (r *Rand) Shuffle(n int, swap func(i, j int)) {
|
func (r *Rand) Shuffle(n int, swap func(i, j int)) {
|
||||||
if n < 0 {
|
if n < 0 {
|
||||||
panic("invalid argument to Shuffle")
|
panic("invalid argument to Shuffle")
|
||||||
|
@@ -10,30 +10,40 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
// ErrInvalidStruct happens when passed not struct and not struct pointer
|
||||||
ErrInvalidStruct = errors.New("invalid struct specified")
|
ErrInvalidStruct = errors.New("invalid struct specified")
|
||||||
ErrInvalidValue = errors.New("invalid value specified")
|
// ErrInvalidValue happens when passed invalid value for field
|
||||||
ErrNotFound = errors.New("struct field not found")
|
ErrInvalidValue = errors.New("invalid value specified")
|
||||||
|
// ErrNotFound happens when struct field not found
|
||||||
|
ErrNotFound = errors.New("struct field not found")
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Option func signature
|
||||||
type Option func(*Options)
|
type Option func(*Options)
|
||||||
|
|
||||||
|
// Options for merge
|
||||||
type Options struct {
|
type Options struct {
|
||||||
Tags []string
|
// Tags specifies tags to lookup
|
||||||
|
Tags []string
|
||||||
|
// SliceAppend controls slice appending
|
||||||
SliceAppend bool
|
SliceAppend bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Tags sets the merge tags for lookup
|
||||||
func Tags(t []string) Option {
|
func Tags(t []string) Option {
|
||||||
return func(o *Options) {
|
return func(o *Options) {
|
||||||
o.Tags = t
|
o.Tags = t
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SliceAppend sets the option
|
||||||
func SliceAppend(b bool) Option {
|
func SliceAppend(b bool) Option {
|
||||||
return func(o *Options) {
|
return func(o *Options) {
|
||||||
o.SliceAppend = b
|
o.SliceAppend = b
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Merge merges map[string]interface{} to destination struct
|
||||||
func Merge(dst interface{}, mp map[string]interface{}, opts ...Option) error {
|
func Merge(dst interface{}, mp map[string]interface{}, opts ...Option) error {
|
||||||
var err error
|
var err error
|
||||||
var sval reflect.Value
|
var sval reflect.Value
|
||||||
@@ -91,7 +101,10 @@ func Merge(dst interface{}, mp map[string]interface{}, opts ...Option) error {
|
|||||||
|
|
||||||
val, ok := mp[fname]
|
val, ok := mp[fname]
|
||||||
if !ok {
|
if !ok {
|
||||||
continue
|
val, ok = mp[dfld.Name]
|
||||||
|
if !ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
sval = reflect.ValueOf(val)
|
sval = reflect.ValueOf(val)
|
||||||
@@ -478,6 +491,7 @@ func IsEmpty(v reflect.Value) bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FieldName returns map field name that can be looked up in struct field
|
||||||
func FieldName(name string) string {
|
func FieldName(name string) string {
|
||||||
newstr := make([]rune, 0, len(name))
|
newstr := make([]rune, 0, len(name))
|
||||||
for idx, chr := range name {
|
for idx, chr := range name {
|
||||||
|
@@ -1,3 +1,4 @@
|
|||||||
|
//go:build ignore
|
||||||
// +build ignore
|
// +build ignore
|
||||||
|
|
||||||
package basic
|
package basic
|
||||||
|
Reference in New Issue
Block a user