Compare commits
	
		
			30 Commits
		
	
	
		
			dee7bc9c38
			...
			v4.1.9
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
|  | 7b1c42e50b | ||
| f3b9493ac3 | |||
| e4ee705eb2 | |||
| 7ff7a3dbe0 | |||
| 7af5147f4b | |||
| 394fd16243 | |||
| 2b08c8f682 | |||
| f9a7f62d02 | |||
|  | f5aedf5951 | ||
| a5ef231171 | |||
| 23f2ee9bb7 | |||
| 88606e89ca | |||
|  | 24efbb68bf | ||
|  | cecdaa0fed | ||
|  | 9627995cee | ||
|  | 0f3539dc7b | ||
| ff414eff2e | |||
|  | fbf6832738 | ||
|  | 59ff1f931b | ||
| 2030bd2803 | |||
| bb87a87ae5 | |||
| 0bd5aed7cc | |||
| 434798a574 | |||
| 459a951115 | |||
| 770c2715d4 | |||
| c93286afd5 | |||
|  | 6bf118d978 | ||
| 7493de1168 | |||
|  | 212a685b50 | ||
| 3f21bafc2f | 
| @@ -14,6 +14,7 @@ on: | |||||||
| jobs: | jobs: | ||||||
| 
 | 
 | ||||||
|   build: |   build: | ||||||
|  |     if: github.server_url != 'https://github.com' | ||||||
|     runs-on: ubuntu-latest |     runs-on: ubuntu-latest | ||||||
|     steps: |     steps: | ||||||
|     - name: checkout code |     - name: checkout code | ||||||
| @@ -25,7 +26,7 @@ jobs: | |||||||
|       uses: actions/setup-go@v5 |       uses: actions/setup-go@v5 | ||||||
|       with: |       with: | ||||||
|         cache-dependency-path: "**/*.sum" |         cache-dependency-path: "**/*.sum" | ||||||
|         go-version: 'stable'  |         go-version: 'stable' | ||||||
| 
 | 
 | ||||||
|     - name: test coverage |     - name: test coverage | ||||||
|       run: | |       run: | | ||||||
| @@ -42,8 +43,8 @@ jobs: | |||||||
|       name: autocommit |       name: autocommit | ||||||
|       with: |       with: | ||||||
|         commit_message: Apply Code Coverage Badge |         commit_message: Apply Code Coverage Badge | ||||||
|         skip_fetch: true |         skip_fetch: false | ||||||
|         skip_checkout: true |         skip_checkout: false | ||||||
|         file_pattern: ./README.md |         file_pattern: ./README.md | ||||||
| 
 | 
 | ||||||
|     - name: push |     - name: push | ||||||
| @@ -51,4 +52,4 @@ jobs: | |||||||
|       uses: ad-m/github-push-action@master |       uses: ad-m/github-push-action@master | ||||||
|       with: |       with: | ||||||
|         github_token: ${{ github.token }} |         github_token: ${{ github.token }} | ||||||
|         branch: ${{ github.ref }} |         branch: ${{ github.ref }} | ||||||
| @@ -24,6 +24,6 @@ jobs: | |||||||
|     - name: setup deps |     - name: setup deps | ||||||
|       run: go get -v ./... |       run: go get -v ./... | ||||||
|     - name: run lint |     - name: run lint | ||||||
|       uses: https://github.com/golangci/golangci-lint-action@v6 |       uses: golangci/golangci-lint-action@v6 | ||||||
|       with: |       with: | ||||||
|         version: 'latest' |         version: 'latest' | ||||||
| @@ -1,54 +1,57 @@ | |||||||
| name: syncpull | name: sync | ||||||
| 
 | 
 | ||||||
| on: | on: | ||||||
|   schedule: |   schedule: | ||||||
|     - cron: '* * * * *' |     - cron: '*/5 * * * *' | ||||||
|  | #  push: | ||||||
|  | #    branches: [ master, v3, v4 ] | ||||||
|  | #    paths-ignore: | ||||||
|  | #      - '.github/**' | ||||||
|  | #      - '.gitea/**' | ||||||
|   # Allows you to run this workflow manually from the Actions tab |   # Allows you to run this workflow manually from the Actions tab | ||||||
|   workflow_dispatch: |   workflow_dispatch: | ||||||
| 
 | 
 | ||||||
| jobs: | jobs: | ||||||
|   pull: |   sync: | ||||||
|  |     if: github.server_url != 'https://github.com' | ||||||
|     runs-on: ubuntu-latest |     runs-on: ubuntu-latest | ||||||
|     steps: |     steps: | ||||||
|     - name: init |     - name: init | ||||||
|       run: | |       run: | | ||||||
|         git config --global user.email "vtolstov <vtolstov@users.noreply.github.com>" |         git config --global user.email "vtolstov <vtolstov@users.noreply.github.com>" | ||||||
|         git config --global user.name "github-actions[bot]" |         git config --global user.name "github-actions[bot]" | ||||||
|         echo "machine git.unistack.org login vtolstov password ${{ secrets.TOKEN_GITEA }}" | tee -a /root/.netrc |         echo "machine git.unistack.org login vtolstov password ${{ secrets.TOKEN_GITEA }}" >> /root/.netrc | ||||||
|         echo "machine github.com login vtolstov password ${{ secrets.TOKEN_GITHUB }}" | tee -a /root/.netrc |         echo "machine github.com login vtolstov password ${{ secrets.TOKEN_GITHUB }}" >> /root/.netrc | ||||||
| 
 | 
 | ||||||
|     - name: track master |     - name: sync master | ||||||
|       run: | |       run: | | ||||||
|         git clone --depth=10 --branch master --single-branch ${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY} repo |         git clone --filter=blob:none --filter=tree:0 --branch master --single-branch ${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY} repo | ||||||
|         cd repo |         cd repo | ||||||
|         git remote add --no-tags --fetch --track master upstream https://github.com/${GITHUB_REPOSITORY} |         git remote add --no-tags --fetch --track master upstream https://github.com/${GITHUB_REPOSITORY} | ||||||
|         git pull --rebase upstream master |         git merge upstream/master | ||||||
|         git push upstream master --progress |         git push upstream master --progress | ||||||
|         git merge --allow-unrelated-histories "upstream/master" |  | ||||||
|         git push origin master --progress |         git push origin master --progress | ||||||
|         cd ../ |         cd ../ | ||||||
|         rm -rf repo |         rm -rf repo | ||||||
| 
 | 
 | ||||||
|     - name: track v3 |     - name: sync v3 | ||||||
|       run: | |       run: | | ||||||
|         git clone --depth=10 --branch v3 --single-branch ${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY} repo |         git clone --filter=blob:none --filter=tree:0 --branch v3 --single-branch ${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY} repo | ||||||
|         cd repo |         cd repo | ||||||
|         git remote add --no-tags --fetch --track v3 upstream https://github.com/${GITHUB_REPOSITORY} |         git remote add --no-tags --fetch --track v3 upstream https://github.com/${GITHUB_REPOSITORY} | ||||||
|         git pull --rebase upstream v3 |         git merge upstream/v3 | ||||||
|         git push upstream v3 |         git push upstream v3 --progress | ||||||
|         git merge --allow-unrelated-histories "upstream/v3" |  | ||||||
|         git push origin v3 --progress |         git push origin v3 --progress | ||||||
|         cd ../ |         cd ../ | ||||||
|         rm -rf repo |         rm -rf repo | ||||||
| 
 | 
 | ||||||
|     - name: track v4 |     - name: sync v4 | ||||||
|       run: | |       run: | | ||||||
|         git clone --depth=10 --branch v4 --single-branch ${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY} repo |         git clone --filter=blob:none --filter=tree:0 --branch v4 --single-branch ${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY} repo | ||||||
|         cd repo |         cd repo | ||||||
|         git remote add --no-tags --fetch --track v4 upstream https://github.com/${GITHUB_REPOSITORY} |         git remote add --no-tags --fetch --track v4 upstream https://github.com/${GITHUB_REPOSITORY} | ||||||
|         git pull --rebase upstream v4 |         git merge upstream/v4 | ||||||
|         git push upstream v4 |         git push upstream v4 --progress | ||||||
|         git merge --allow-unrelated-histories "upstream/v4" |  | ||||||
|         git push origin v4 --progress |         git push origin v4 --progress | ||||||
|         cd ../ |         cd ../ | ||||||
|         rm -rf repo |         rm -rf repo | ||||||
| @@ -32,19 +32,19 @@ jobs: | |||||||
|         go-version: 'stable' |         go-version: 'stable' | ||||||
|     - name: setup go work |     - name: setup go work | ||||||
|       env: |       env: | ||||||
|         GOWORK: /workspace/${{ github.repository_owner }}/go.work |         GOWORK: ${{ github.workspace }}/go.work | ||||||
|       run: | |       run: | | ||||||
|         go work init |         go work init | ||||||
|         go work use . |         go work use . | ||||||
|         go work use micro-tests |         go work use micro-tests | ||||||
|     - name: setup deps |     - name: setup deps | ||||||
|       env: |       env: | ||||||
|         GOWORK: /workspace/${{ github.repository_owner }}/go.work |         GOWORK: ${{ github.workspace }}/go.work | ||||||
|       run: go get -v ./... |       run: go get -v ./... | ||||||
|     - name: run tests |     - name: run tests | ||||||
|       env: |       env: | ||||||
|         INTEGRATION_TESTS: yes |         INTEGRATION_TESTS: yes | ||||||
|         GOWORK: /workspace/${{ github.repository_owner }}/go.work |         GOWORK: ${{ github.workspace }}/go.work | ||||||
|       run: | |       run: | | ||||||
|         cd micro-tests |         cd micro-tests | ||||||
|         go test -mod readonly -v ./... || true |         go test -mod readonly -v ./... || true | ||||||
| @@ -1,5 +1,5 @@ | |||||||
| run: | run: | ||||||
|   concurrency: 8 |   concurrency: 8 | ||||||
|   deadline: 5m |   timeout: 5m | ||||||
|   issues-exit-code: 1 |   issues-exit-code: 1 | ||||||
|   tests: true |   tests: true | ||||||
|   | |||||||
| @@ -1,5 +1,5 @@ | |||||||
| # Micro | # Micro | ||||||
|  |  | ||||||
| [](https://opensource.org/licenses/Apache-2.0) | [](https://opensource.org/licenses/Apache-2.0) | ||||||
| [](https://pkg.go.dev/go.unistack.org/micro/v4?tab=overview) | [](https://pkg.go.dev/go.unistack.org/micro/v4?tab=overview) | ||||||
| [](https://git.unistack.org/unistack-org/micro/actions?query=workflow%3Abuild+branch%3Av4+event%3Apush) | [](https://git.unistack.org/unistack-org/micro/actions?query=workflow%3Abuild+branch%3Av4+event%3Apush) | ||||||
|   | |||||||
							
								
								
									
										2
									
								
								go.mod
									
									
									
									
									
								
							
							
						
						
									
										2
									
								
								go.mod
									
									
									
									
									
								
							| @@ -12,6 +12,7 @@ require ( | |||||||
| 	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-20220518035006-a7e85ada93c5 | 	github.com/silas/dag v0.0.0-20220518035006-a7e85ada93c5 | ||||||
| 	github.com/spf13/cast v1.7.1 | 	github.com/spf13/cast v1.7.1 | ||||||
|  | 	github.com/stretchr/testify v1.10.0 | ||||||
| 	go.uber.org/atomic v1.11.0 | 	go.uber.org/atomic v1.11.0 | ||||||
| 	go.uber.org/automaxprocs v1.6.0 | 	go.uber.org/automaxprocs v1.6.0 | ||||||
| 	go.unistack.org/micro-proto/v4 v4.1.0 | 	go.unistack.org/micro-proto/v4 v4.1.0 | ||||||
| @@ -26,7 +27,6 @@ require ( | |||||||
| 	github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58 // indirect | 	github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58 // indirect | ||||||
| 	github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect | 	github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect | ||||||
| 	github.com/rogpeppe/go-internal v1.13.1 // indirect | 	github.com/rogpeppe/go-internal v1.13.1 // indirect | ||||||
| 	github.com/stretchr/testify v1.10.0 // indirect |  | ||||||
| 	golang.org/x/net v0.34.0 // indirect | 	golang.org/x/net v0.34.0 // indirect | ||||||
| 	golang.org/x/sys v0.29.0 // indirect | 	golang.org/x/sys v0.29.0 // indirect | ||||||
| 	google.golang.org/genproto/googleapis/rpc v0.0.0-20241216192217-9240e9c98484 // indirect | 	google.golang.org/genproto/googleapis/rpc v0.0.0-20241216192217-9240e9c98484 // indirect | ||||||
|   | |||||||
							
								
								
									
										117
									
								
								hooks/metadata/metadata.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										117
									
								
								hooks/metadata/metadata.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,117 @@ | |||||||
|  | package metadata | ||||||
|  |  | ||||||
|  | import ( | ||||||
|  | 	"context" | ||||||
|  |  | ||||||
|  | 	"go.unistack.org/micro/v4/client" | ||||||
|  | 	"go.unistack.org/micro/v4/metadata" | ||||||
|  | 	"go.unistack.org/micro/v4/server" | ||||||
|  | ) | ||||||
|  |  | ||||||
|  | type wrapper struct { | ||||||
|  | 	keys []string | ||||||
|  |  | ||||||
|  | 	client.Client | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func NewClientWrapper(keys ...string) client.Wrapper { | ||||||
|  | 	return func(c client.Client) client.Client { | ||||||
|  | 		handler := &wrapper{ | ||||||
|  | 			Client: c, | ||||||
|  | 			keys:   keys, | ||||||
|  | 		} | ||||||
|  | 		return handler | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func NewClientCallWrapper(keys ...string) client.CallWrapper { | ||||||
|  | 	return func(fn client.CallFunc) client.CallFunc { | ||||||
|  | 		return func(ctx context.Context, addr string, req client.Request, rsp interface{}, opts client.CallOptions) error { | ||||||
|  | 			if keys == nil { | ||||||
|  | 				return fn(ctx, addr, req, rsp, opts) | ||||||
|  | 			} | ||||||
|  | 			if imd, iok := metadata.FromIncomingContext(ctx); iok && imd != nil { | ||||||
|  | 				omd, ook := metadata.FromOutgoingContext(ctx) | ||||||
|  | 				if !ook || omd == nil { | ||||||
|  | 					omd = metadata.New(len(imd)) | ||||||
|  | 				} | ||||||
|  | 				for _, k := range keys { | ||||||
|  | 					if v := imd.Get(k); v != nil { | ||||||
|  | 						omd.Set(k, v...) | ||||||
|  | 					} | ||||||
|  | 				} | ||||||
|  | 				if !ook { | ||||||
|  | 					ctx = metadata.NewOutgoingContext(ctx, omd) | ||||||
|  | 				} | ||||||
|  | 			} | ||||||
|  | 			return fn(ctx, addr, req, rsp, opts) | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func (w *wrapper) Call(ctx context.Context, req client.Request, rsp interface{}, opts ...client.CallOption) error { | ||||||
|  | 	if w.keys == nil { | ||||||
|  | 		return w.Client.Call(ctx, req, rsp, opts...) | ||||||
|  | 	} | ||||||
|  | 	if imd, iok := metadata.FromIncomingContext(ctx); iok && imd != nil { | ||||||
|  | 		omd, ook := metadata.FromOutgoingContext(ctx) | ||||||
|  | 		if !ook || omd == nil { | ||||||
|  | 			omd = metadata.New(len(imd)) | ||||||
|  | 		} | ||||||
|  | 		for _, k := range w.keys { | ||||||
|  | 			if v := imd.Get(k); v != nil { | ||||||
|  | 				omd.Set(k, v...) | ||||||
|  | 			} | ||||||
|  | 		} | ||||||
|  | 		if !ook { | ||||||
|  | 			ctx = metadata.NewOutgoingContext(ctx, omd) | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | 	return w.Client.Call(ctx, req, rsp, opts...) | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func (w *wrapper) Stream(ctx context.Context, req client.Request, opts ...client.CallOption) (client.Stream, error) { | ||||||
|  | 	if w.keys == nil { | ||||||
|  | 		return w.Client.Stream(ctx, req, opts...) | ||||||
|  | 	} | ||||||
|  | 	if imd, iok := metadata.FromIncomingContext(ctx); iok && imd != nil { | ||||||
|  | 		omd, ook := metadata.FromOutgoingContext(ctx) | ||||||
|  | 		if !ook || omd == nil { | ||||||
|  | 			omd = metadata.New(len(imd)) | ||||||
|  | 		} | ||||||
|  | 		for _, k := range w.keys { | ||||||
|  | 			if v := imd.Get(k); v != nil { | ||||||
|  | 				omd.Set(k, v...) | ||||||
|  | 			} | ||||||
|  | 		} | ||||||
|  | 		if !ook { | ||||||
|  | 			ctx = metadata.NewOutgoingContext(ctx, omd) | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | 	return w.Client.Stream(ctx, req, opts...) | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func NewServerHandlerWrapper(keys ...string) server.HandlerWrapper { | ||||||
|  | 	return func(fn server.HandlerFunc) server.HandlerFunc { | ||||||
|  | 		return func(ctx context.Context, req server.Request, rsp interface{}) error { | ||||||
|  | 			if keys == nil { | ||||||
|  | 				return fn(ctx, req, rsp) | ||||||
|  | 			} | ||||||
|  | 			if imd, iok := metadata.FromIncomingContext(ctx); iok && imd != nil { | ||||||
|  | 				omd, ook := metadata.FromOutgoingContext(ctx) | ||||||
|  | 				if !ook || omd == nil { | ||||||
|  | 					omd = metadata.New(len(imd)) | ||||||
|  | 				} | ||||||
|  | 				for _, k := range keys { | ||||||
|  | 					if v := imd.Get(k); v != nil { | ||||||
|  | 						omd.Set(k, v...) | ||||||
|  | 					} | ||||||
|  | 				} | ||||||
|  | 				if !ook { | ||||||
|  | 					ctx = metadata.NewOutgoingContext(ctx, omd) | ||||||
|  | 				} | ||||||
|  | 			} | ||||||
|  | 			return fn(ctx, req, rsp) | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | } | ||||||
							
								
								
									
										63
									
								
								hooks/recovery/recovery.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										63
									
								
								hooks/recovery/recovery.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,63 @@ | |||||||
|  | package recovery | ||||||
|  |  | ||||||
|  | import ( | ||||||
|  | 	"context" | ||||||
|  | 	"fmt" | ||||||
|  |  | ||||||
|  | 	"go.unistack.org/micro/v4/errors" | ||||||
|  | 	"go.unistack.org/micro/v4/server" | ||||||
|  | ) | ||||||
|  |  | ||||||
|  | func NewOptions(opts ...Option) Options { | ||||||
|  | 	options := Options{ | ||||||
|  | 		ServerHandlerFn: DefaultServerHandlerFn, | ||||||
|  | 	} | ||||||
|  | 	for _, o := range opts { | ||||||
|  | 		o(&options) | ||||||
|  | 	} | ||||||
|  | 	return options | ||||||
|  | } | ||||||
|  |  | ||||||
|  | type Options struct { | ||||||
|  | 	ServerHandlerFn func(context.Context, server.Request, interface{}, error) error | ||||||
|  | } | ||||||
|  |  | ||||||
|  | type Option func(*Options) | ||||||
|  |  | ||||||
|  | func ServerHandlerFunc(fn func(context.Context, server.Request, interface{}, error) error) Option { | ||||||
|  | 	return func(o *Options) { | ||||||
|  | 		o.ServerHandlerFn = fn | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  |  | ||||||
|  | var DefaultServerHandlerFn = func(ctx context.Context, req server.Request, rsp interface{}, err error) error { | ||||||
|  | 	return errors.BadRequest("", "%v", err) | ||||||
|  | } | ||||||
|  |  | ||||||
|  | var Hook = NewHook() | ||||||
|  |  | ||||||
|  | type hook struct { | ||||||
|  | 	opts Options | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func NewHook(opts ...Option) *hook { | ||||||
|  | 	return &hook{opts: NewOptions(opts...)} | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func (w *hook) ServerHandler(next server.FuncHandler) server.FuncHandler { | ||||||
|  | 	return func(ctx context.Context, req server.Request, rsp interface{}) (err error) { | ||||||
|  | 		defer func() { | ||||||
|  | 			r := recover() | ||||||
|  | 			switch verr := r.(type) { | ||||||
|  | 			case nil: | ||||||
|  | 				return | ||||||
|  | 			case error: | ||||||
|  | 				err = w.opts.ServerHandlerFn(ctx, req, rsp, verr) | ||||||
|  | 			default: | ||||||
|  | 				err = w.opts.ServerHandlerFn(ctx, req, rsp, fmt.Errorf("%v", r)) | ||||||
|  | 			} | ||||||
|  | 		}() | ||||||
|  | 		err = next(ctx, req, rsp) | ||||||
|  | 		return err | ||||||
|  | 	} | ||||||
|  | } | ||||||
							
								
								
									
										112
									
								
								hooks/requestid/requestid.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										112
									
								
								hooks/requestid/requestid.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,112 @@ | |||||||
|  | package requestid | ||||||
|  |  | ||||||
|  | import ( | ||||||
|  | 	"context" | ||||||
|  | 	"net/textproto" | ||||||
|  |  | ||||||
|  | 	"go.unistack.org/micro/v4/client" | ||||||
|  | 	"go.unistack.org/micro/v4/metadata" | ||||||
|  | 	"go.unistack.org/micro/v4/server" | ||||||
|  | 	"go.unistack.org/micro/v4/util/id" | ||||||
|  | ) | ||||||
|  |  | ||||||
|  | type XRequestIDKey struct{} | ||||||
|  |  | ||||||
|  | // DefaultMetadataKey contains metadata key | ||||||
|  | var DefaultMetadataKey = textproto.CanonicalMIMEHeaderKey("x-request-id") | ||||||
|  |  | ||||||
|  | // DefaultMetadataFunc wil be used if user not provide own func to fill metadata | ||||||
|  | var DefaultMetadataFunc = func(ctx context.Context) (context.Context, error) { | ||||||
|  | 	var xid string | ||||||
|  |  | ||||||
|  | 	cid, cok := ctx.Value(XRequestIDKey{}).(string) | ||||||
|  | 	if cok && cid != "" { | ||||||
|  | 		xid = cid | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	imd, iok := metadata.FromIncomingContext(ctx) | ||||||
|  | 	if !iok || imd == nil { | ||||||
|  | 		imd = metadata.New(1) | ||||||
|  | 		ctx = metadata.NewIncomingContext(ctx, imd) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	omd, ook := metadata.FromOutgoingContext(ctx) | ||||||
|  | 	if !ook || omd == nil { | ||||||
|  | 		omd = metadata.New(1) | ||||||
|  | 		ctx = metadata.NewOutgoingContext(ctx, omd) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	if xid == "" { | ||||||
|  | 		var ids []string | ||||||
|  |  | ||||||
|  | 		for i := range imd.Get(DefaultMetadataKey) { | ||||||
|  | 			if ids[i] != "" { | ||||||
|  | 				xid = ids[i] | ||||||
|  | 			} | ||||||
|  | 		} | ||||||
|  |  | ||||||
|  | 		for i := range omd.Get(DefaultMetadataKey) { | ||||||
|  | 			if ids[i] != "" { | ||||||
|  | 				xid = ids[i] | ||||||
|  | 			} | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	if xid == "" { | ||||||
|  | 		var err error | ||||||
|  | 		xid, err = id.New() | ||||||
|  | 		if err != nil { | ||||||
|  | 			return ctx, err | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	if !cok { | ||||||
|  | 		ctx = context.WithValue(ctx, XRequestIDKey{}, xid) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	if !iok { | ||||||
|  | 		imd.Set(DefaultMetadataKey, xid) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	if !ook { | ||||||
|  | 		omd.Set(DefaultMetadataKey, xid) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	return ctx, nil | ||||||
|  | } | ||||||
|  |  | ||||||
|  | type hook struct{} | ||||||
|  |  | ||||||
|  | func NewHook() *hook { | ||||||
|  | 	return &hook{} | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func (w *hook) ServerHandler(next server.FuncHandler) server.FuncHandler { | ||||||
|  | 	return func(ctx context.Context, req server.Request, rsp interface{}) error { | ||||||
|  | 		var err error | ||||||
|  | 		if ctx, err = DefaultMetadataFunc(ctx); err != nil { | ||||||
|  | 			return err | ||||||
|  | 		} | ||||||
|  | 		return next(ctx, req, rsp) | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func (w *hook) ClientCall(next client.FuncCall) client.FuncCall { | ||||||
|  | 	return func(ctx context.Context, req client.Request, rsp interface{}, opts ...client.CallOption) error { | ||||||
|  | 		var err error | ||||||
|  | 		if ctx, err = DefaultMetadataFunc(ctx); err != nil { | ||||||
|  | 			return err | ||||||
|  | 		} | ||||||
|  | 		return next(ctx, req, rsp, opts...) | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func (w *hook) ClientStream(next client.FuncStream) client.FuncStream { | ||||||
|  | 	return func(ctx context.Context, req client.Request, opts ...client.CallOption) (client.Stream, error) { | ||||||
|  | 		var err error | ||||||
|  | 		if ctx, err = DefaultMetadataFunc(ctx); err != nil { | ||||||
|  | 			return nil, err | ||||||
|  | 		} | ||||||
|  | 		return next(ctx, req, opts...) | ||||||
|  | 	} | ||||||
|  | } | ||||||
							
								
								
									
										34
									
								
								hooks/requestid/requestid_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										34
									
								
								hooks/requestid/requestid_test.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,34 @@ | |||||||
|  | package requestid | ||||||
|  |  | ||||||
|  | import ( | ||||||
|  | 	"context" | ||||||
|  | 	"slices" | ||||||
|  | 	"testing" | ||||||
|  |  | ||||||
|  | 	"go.unistack.org/micro/v4/metadata" | ||||||
|  | ) | ||||||
|  |  | ||||||
|  | func TestDefaultMetadataFunc(t *testing.T) { | ||||||
|  | 	ctx := context.TODO() | ||||||
|  |  | ||||||
|  | 	nctx, err := DefaultMetadataFunc(ctx) | ||||||
|  | 	if err != nil { | ||||||
|  | 		t.Fatalf("%v", err) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	imd, ok := metadata.FromIncomingContext(nctx) | ||||||
|  | 	if !ok { | ||||||
|  | 		t.Fatalf("md missing in incoming context") | ||||||
|  | 	} | ||||||
|  | 	omd, ok := metadata.FromOutgoingContext(nctx) | ||||||
|  | 	if !ok { | ||||||
|  | 		t.Fatalf("md missing in outgoing context") | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	iv := imd.Get(DefaultMetadataKey) | ||||||
|  | 	ov := omd.Get(DefaultMetadataKey) | ||||||
|  |  | ||||||
|  | 	if !slices.Equal(iv, ov) { | ||||||
|  | 		t.Fatalf("missing metadata key value %v != %v", iv, ov) | ||||||
|  | 	} | ||||||
|  | } | ||||||
							
								
								
									
										133
									
								
								hooks/validator/validator.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										133
									
								
								hooks/validator/validator.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,133 @@ | |||||||
|  | package validator | ||||||
|  |  | ||||||
|  | import ( | ||||||
|  | 	"context" | ||||||
|  |  | ||||||
|  | 	"go.unistack.org/micro/v4/client" | ||||||
|  | 	"go.unistack.org/micro/v4/errors" | ||||||
|  | 	"go.unistack.org/micro/v4/server" | ||||||
|  | ) | ||||||
|  |  | ||||||
|  | var ( | ||||||
|  | 	DefaultClientErrorFunc = func(req client.Request, rsp interface{}, err error) error { | ||||||
|  | 		if rsp != nil { | ||||||
|  | 			return errors.BadGateway(req.Service(), "%v", err) | ||||||
|  | 		} | ||||||
|  | 		return errors.BadRequest(req.Service(), "%v", err) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	DefaultServerErrorFunc = func(req server.Request, rsp interface{}, err error) error { | ||||||
|  | 		if rsp != nil { | ||||||
|  | 			return errors.BadGateway(req.Service(), "%v", err) | ||||||
|  | 		} | ||||||
|  | 		return errors.BadRequest(req.Service(), "%v", err) | ||||||
|  | 	} | ||||||
|  | ) | ||||||
|  |  | ||||||
|  | type ( | ||||||
|  | 	ClientErrorFunc func(client.Request, interface{}, error) error | ||||||
|  | 	ServerErrorFunc func(server.Request, interface{}, error) error | ||||||
|  | ) | ||||||
|  |  | ||||||
|  | // Options struct holds wrapper options | ||||||
|  | type Options struct { | ||||||
|  | 	ClientErrorFn          ClientErrorFunc | ||||||
|  | 	ServerErrorFn          ServerErrorFunc | ||||||
|  | 	ClientValidateResponse bool | ||||||
|  | 	ServerValidateResponse bool | ||||||
|  | } | ||||||
|  |  | ||||||
|  | // Option func signature | ||||||
|  | type Option func(*Options) | ||||||
|  |  | ||||||
|  | func ClientValidateResponse(b bool) Option { | ||||||
|  | 	return func(o *Options) { | ||||||
|  | 		o.ClientValidateResponse = b | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func ServerValidateResponse(b bool) Option { | ||||||
|  | 	return func(o *Options) { | ||||||
|  | 		o.ClientValidateResponse = b | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func ClientReqErrorFn(fn ClientErrorFunc) Option { | ||||||
|  | 	return func(o *Options) { | ||||||
|  | 		o.ClientErrorFn = fn | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func ServerErrorFn(fn ServerErrorFunc) Option { | ||||||
|  | 	return func(o *Options) { | ||||||
|  | 		o.ServerErrorFn = fn | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func NewOptions(opts ...Option) Options { | ||||||
|  | 	options := Options{ | ||||||
|  | 		ClientErrorFn: DefaultClientErrorFunc, | ||||||
|  | 		ServerErrorFn: DefaultServerErrorFunc, | ||||||
|  | 	} | ||||||
|  | 	for _, o := range opts { | ||||||
|  | 		o(&options) | ||||||
|  | 	} | ||||||
|  | 	return options | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func NewHook(opts ...Option) *hook { | ||||||
|  | 	return &hook{opts: NewOptions(opts...)} | ||||||
|  | } | ||||||
|  |  | ||||||
|  | type validator interface { | ||||||
|  | 	Validate() error | ||||||
|  | } | ||||||
|  |  | ||||||
|  | type hook struct { | ||||||
|  | 	opts Options | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func (w *hook) ClientCall(next client.FuncCall) client.FuncCall { | ||||||
|  | 	return func(ctx context.Context, req client.Request, rsp interface{}, opts ...client.CallOption) error { | ||||||
|  | 		if v, ok := req.Body().(validator); ok { | ||||||
|  | 			if err := v.Validate(); err != nil { | ||||||
|  | 				return w.opts.ClientErrorFn(req, nil, err) | ||||||
|  | 			} | ||||||
|  | 		} | ||||||
|  | 		err := next(ctx, req, rsp, opts...) | ||||||
|  | 		if v, ok := rsp.(validator); ok && w.opts.ClientValidateResponse { | ||||||
|  | 			if verr := v.Validate(); verr != nil { | ||||||
|  | 				return w.opts.ClientErrorFn(req, rsp, verr) | ||||||
|  | 			} | ||||||
|  | 		} | ||||||
|  | 		return err | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func (w *hook) ClientStream(next client.FuncStream) client.FuncStream { | ||||||
|  | 	return func(ctx context.Context, req client.Request, opts ...client.CallOption) (client.Stream, error) { | ||||||
|  | 		if v, ok := req.Body().(validator); ok { | ||||||
|  | 			if err := v.Validate(); err != nil { | ||||||
|  | 				return nil, w.opts.ClientErrorFn(req, nil, err) | ||||||
|  | 			} | ||||||
|  | 		} | ||||||
|  | 		return next(ctx, req, opts...) | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func (w *hook) ServerHandler(next server.FuncHandler) server.FuncHandler { | ||||||
|  | 	return func(ctx context.Context, req server.Request, rsp interface{}) error { | ||||||
|  | 		if v, ok := req.Body().(validator); ok { | ||||||
|  | 			if err := v.Validate(); err != nil { | ||||||
|  | 				return w.opts.ServerErrorFn(req, nil, err) | ||||||
|  | 			} | ||||||
|  | 		} | ||||||
|  | 		err := next(ctx, req, rsp) | ||||||
|  | 		if v, ok := rsp.(validator); ok && w.opts.ServerValidateResponse { | ||||||
|  | 			if verr := v.Validate(); verr != nil { | ||||||
|  | 				return w.opts.ServerErrorFn(req, rsp, verr) | ||||||
|  | 			} | ||||||
|  | 		} | ||||||
|  | 		return err | ||||||
|  | 	} | ||||||
|  | } | ||||||
| @@ -406,7 +406,7 @@ func TestLogger(t *testing.T) { | |||||||
| func Test_WithContextAttrFunc(t *testing.T) { | func Test_WithContextAttrFunc(t *testing.T) { | ||||||
| 	loggerContextAttrFuncs := []logger.ContextAttrFunc{ | 	loggerContextAttrFuncs := []logger.ContextAttrFunc{ | ||||||
| 		func(ctx context.Context) []interface{} { | 		func(ctx context.Context) []interface{} { | ||||||
| 			md, ok := metadata.FromIncomingContext(ctx) | 			md, ok := metadata.FromOutgoingContext(ctx) | ||||||
| 			if !ok { | 			if !ok { | ||||||
| 				return nil | 				return nil | ||||||
| 			} | 			} | ||||||
| @@ -425,7 +425,7 @@ func Test_WithContextAttrFunc(t *testing.T) { | |||||||
| 	logger.DefaultContextAttrFuncs = append(logger.DefaultContextAttrFuncs, loggerContextAttrFuncs...) | 	logger.DefaultContextAttrFuncs = append(logger.DefaultContextAttrFuncs, loggerContextAttrFuncs...) | ||||||
|  |  | ||||||
| 	ctx := context.TODO() | 	ctx := context.TODO() | ||||||
| 	ctx = metadata.AppendIncomingContext(ctx, "X-Request-Id", uuid.New().String(), | 	ctx = metadata.AppendOutgoingContext(ctx, "X-Request-Id", uuid.New().String(), | ||||||
| 		"Source-Service", "Test-System") | 		"Source-Service", "Test-System") | ||||||
|  |  | ||||||
| 	buf := bytes.NewBuffer(nil) | 	buf := bytes.NewBuffer(nil) | ||||||
| @@ -445,9 +445,9 @@ func Test_WithContextAttrFunc(t *testing.T) { | |||||||
| 		t.Fatalf("logger info, buf %s", buf.Bytes()) | 		t.Fatalf("logger info, buf %s", buf.Bytes()) | ||||||
| 	} | 	} | ||||||
| 	buf.Reset() | 	buf.Reset() | ||||||
| 	imd, _ := metadata.FromIncomingContext(ctx) | 	omd, _ := metadata.FromOutgoingContext(ctx) | ||||||
| 	l.Info(ctx, "test message1") | 	l.Info(ctx, "test message1") | ||||||
| 	imd.Set("Source-Service", "Test-System2") | 	omd.Set("Source-Service", "Test-System2") | ||||||
| 	l.Info(ctx, "test message2") | 	l.Info(ctx, "test message2") | ||||||
|  |  | ||||||
| 	// t.Logf("xxx %s", buf.Bytes()) | 	// t.Logf("xxx %s", buf.Bytes()) | ||||||
|   | |||||||
| @@ -106,65 +106,31 @@ func (md Metadata) CopyTo(out Metadata) { | |||||||
| } | } | ||||||
|  |  | ||||||
| // Get obtains the values for a given key. | // Get obtains the values for a given key. | ||||||
| func (md Metadata) MustGet(k string) []string { | func (md Metadata) Get(k string) []string { | ||||||
| 	v, ok := md.Get(k) |  | ||||||
| 	if !ok { |  | ||||||
| 		panic("missing metadata key") |  | ||||||
| 	} |  | ||||||
| 	return v |  | ||||||
| } |  | ||||||
|  |  | ||||||
| // Get obtains the values for a given key. |  | ||||||
| func (md Metadata) Get(k string) ([]string, bool) { |  | ||||||
| 	v, ok := md[k] | 	v, ok := md[k] | ||||||
| 	if !ok { | 	if !ok { | ||||||
| 		v, ok = md[strings.ToLower(k)] | 		v, ok = md[strings.ToLower(k)] | ||||||
| 	} | 	} | ||||||
| 	if !ok { | 	if !ok { | ||||||
| 		v, ok = md[textproto.CanonicalMIMEHeaderKey(k)] | 		v = md[textproto.CanonicalMIMEHeaderKey(k)] | ||||||
| 	} |  | ||||||
| 	return v, ok |  | ||||||
| } |  | ||||||
|  |  | ||||||
| // MustGetJoined obtains the values for a given key |  | ||||||
| // with joined values with "," symbol |  | ||||||
| func (md Metadata) MustGetJoined(k string) string { |  | ||||||
| 	v, ok := md.GetJoined(k) |  | ||||||
| 	if !ok { |  | ||||||
| 		panic("missing metadata key") |  | ||||||
| 	} | 	} | ||||||
| 	return v | 	return v | ||||||
| } | } | ||||||
|  |  | ||||||
| // GetJoined obtains the values for a given key | // GetJoined obtains the values for a given key | ||||||
| // with joined values with "," symbol | // with joined values with "," symbol | ||||||
| func (md Metadata) GetJoined(k string) (string, bool) { | func (md Metadata) GetJoined(k string) string { | ||||||
| 	v, ok := md.Get(k) | 	return strings.Join(md.Get(k), ",") | ||||||
| 	if !ok { |  | ||||||
| 		return "", ok |  | ||||||
| 	} |  | ||||||
| 	return strings.Join(v, ","), true |  | ||||||
| } | } | ||||||
|  |  | ||||||
| // Set sets the value of a given key with a slice of values. | // Set sets the value of a given key with a slice of values. | ||||||
| func (md Metadata) Add(key string, vals ...string) { | func (md Metadata) Set(key string, vals ...string) { | ||||||
| 	if len(vals) == 0 { | 	if len(vals) == 0 { | ||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
| 	md[key] = vals | 	md[key] = vals | ||||||
| } | } | ||||||
|  |  | ||||||
| // Set sets the value of a given key with a slice of values. |  | ||||||
| func (md Metadata) Set(kvs ...string) { |  | ||||||
| 	if len(kvs)%2 == 1 { |  | ||||||
| 		panic(fmt.Sprintf("metadata: Set got an odd number of input pairs for metadata: %d", len(kvs))) |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	for i := 0; i < len(kvs); i += 2 { |  | ||||||
| 		md[kvs[i]] = append(md[kvs[i]], kvs[i+1]) |  | ||||||
| 	} |  | ||||||
| } |  | ||||||
|  |  | ||||||
| // Append adds the values to key k, not overwriting what was already stored at | // Append adds the values to key k, not overwriting what was already stored at | ||||||
| // that key. | // that key. | ||||||
| func (md Metadata) Append(key string, vals ...string) { | func (md Metadata) Append(key string, vals ...string) { | ||||||
| @@ -241,24 +207,6 @@ func AppendContext(ctx context.Context, kv ...string) context.Context { | |||||||
| 	return context.WithValue(ctx, metadataCurrentKey{}, rawMetadata{md: md.md, added: added}) | 	return context.WithValue(ctx, metadataCurrentKey{}, rawMetadata{md: md.md, added: added}) | ||||||
| } | } | ||||||
|  |  | ||||||
| // AppendIncomingContext returns a new context with the provided kv merged |  | ||||||
| // with any existing metadata in the context. Please refer to the documentation |  | ||||||
| // of Pairs for a description of kv. |  | ||||||
| func AppendIncomingContext(ctx context.Context, kv ...string) context.Context { |  | ||||||
| 	if len(kv)%2 == 1 { |  | ||||||
| 		panic(fmt.Sprintf("metadata: AppendIncomingContext got an odd number of input pairs for metadata: %d", len(kv))) |  | ||||||
| 	} |  | ||||||
| 	md, _ := ctx.Value(metadataIncomingKey{}).(rawMetadata) |  | ||||||
| 	added := make([][]string, len(md.added)+1) |  | ||||||
| 	copy(added, md.added) |  | ||||||
| 	kvCopy := make([]string, 0, len(kv)) |  | ||||||
| 	for i := 0; i < len(kv); i += 2 { |  | ||||||
| 		kvCopy = append(kvCopy, strings.ToLower(kv[i]), kv[i+1]) |  | ||||||
| 	} |  | ||||||
| 	added[len(added)-1] = kvCopy |  | ||||||
| 	return context.WithValue(ctx, metadataIncomingKey{}, rawMetadata{md: md.md, added: added}) |  | ||||||
| } |  | ||||||
|  |  | ||||||
| // AppendOutgoingContext returns a new context with the provided kv merged | // AppendOutgoingContext returns a new context with the provided kv merged | ||||||
| // with any existing metadata in the context. Please refer to the documentation | // with any existing metadata in the context. Please refer to the documentation | ||||||
| // of Pairs for a description of kv. | // of Pairs for a description of kv. | ||||||
|   | |||||||
| @@ -5,6 +5,15 @@ import ( | |||||||
| 	"testing" | 	"testing" | ||||||
| ) | ) | ||||||
|  |  | ||||||
|  | func TesSet(t *testing.T) { | ||||||
|  | 	md := Pairs("key1", "val1", "key2", "val2") | ||||||
|  | 	md.Set("key1", "val2", "val3") | ||||||
|  | 	v := md.GetJoined("X-Request-Id") | ||||||
|  | 	if v != "val2, val3" { | ||||||
|  | 		t.Fatal("set not works") | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  |  | ||||||
| /* | /* | ||||||
| func TestAppendOutgoingContextModify(t *testing.T) { | func TestAppendOutgoingContextModify(t *testing.T) { | ||||||
| 	md := Pairs("key1", "val1") | 	md := Pairs("key1", "val1") | ||||||
| @@ -19,8 +28,8 @@ func TestAppendOutgoingContextModify(t *testing.T) { | |||||||
| func TestLowercase(t *testing.T) { | func TestLowercase(t *testing.T) { | ||||||
| 	md := New(1) | 	md := New(1) | ||||||
| 	md["x-request-id"] = []string{"12345"} | 	md["x-request-id"] = []string{"12345"} | ||||||
| 	v, ok := md.GetJoined("X-Request-Id") | 	v := md.GetJoined("X-Request-Id") | ||||||
| 	if !ok || v == "" { | 	if v == "" { | ||||||
| 		t.Fatalf("metadata invalid %#+v", md) | 		t.Fatalf("metadata invalid %#+v", md) | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
| @@ -47,33 +56,9 @@ func TestMultipleUsage(t *testing.T) { | |||||||
| 	_ = omd | 	_ = omd | ||||||
| } | } | ||||||
|  |  | ||||||
| func TestMetadataSetMultiple(t *testing.T) { |  | ||||||
| 	md := New(4) |  | ||||||
| 	md.Set("key1", "val1", "key2", "val2") |  | ||||||
|  |  | ||||||
| 	if v, ok := md.GetJoined("key1"); !ok || v != "val1" { |  | ||||||
| 		t.Fatalf("invalid kv %#+v", md) |  | ||||||
| 	} |  | ||||||
| 	if v, ok := md.GetJoined("key2"); !ok || v != "val2" { |  | ||||||
| 		t.Fatalf("invalid kv %#+v", md) |  | ||||||
| 	} |  | ||||||
| } |  | ||||||
|  |  | ||||||
| func TestAppend(t *testing.T) { |  | ||||||
| 	ctx := context.Background() |  | ||||||
| 	ctx = AppendIncomingContext(ctx, "key1", "val1", "key2", "val2") |  | ||||||
| 	md, ok := FromIncomingContext(ctx) |  | ||||||
| 	if !ok { |  | ||||||
| 		t.Fatal("metadata empty") |  | ||||||
| 	} |  | ||||||
| 	if _, ok := md.Get("key1"); !ok { |  | ||||||
| 		t.Fatal("key1 not found") |  | ||||||
| 	} |  | ||||||
| } |  | ||||||
|  |  | ||||||
| func TestPairs(t *testing.T) { | func TestPairs(t *testing.T) { | ||||||
| 	md := Pairs("key1", "val1", "key2", "val2") | 	md := Pairs("key1", "val1", "key2", "val2") | ||||||
| 	if _, ok := md.Get("key1"); !ok { | 	if v := md.Get("key1"); v == nil { | ||||||
| 		t.Fatal("key1 not found") | 		t.Fatal("key1 not found") | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
| @@ -97,7 +82,7 @@ func TestPassing(t *testing.T) { | |||||||
| 	if !ok { | 	if !ok { | ||||||
| 		t.Fatalf("missing metadata from outgoing context") | 		t.Fatalf("missing metadata from outgoing context") | ||||||
| 	} | 	} | ||||||
| 	if v, ok := md.Get("Key1"); !ok || v[0] != "Val1" { | 	if v := md.Get("Key1"); v == nil || v[0] != "Val1" { | ||||||
| 		t.Fatalf("invalid metadata value %#+v", md) | 		t.Fatalf("invalid metadata value %#+v", md) | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
| @@ -127,21 +112,21 @@ func TestIterator(t *testing.T) { | |||||||
| func TestMedataCanonicalKey(t *testing.T) { | func TestMedataCanonicalKey(t *testing.T) { | ||||||
| 	md := New(1) | 	md := New(1) | ||||||
| 	md.Set("x-request-id", "12345") | 	md.Set("x-request-id", "12345") | ||||||
| 	v, ok := md.GetJoined("x-request-id") | 	v := md.GetJoined("x-request-id") | ||||||
| 	if !ok { | 	if v == "" { | ||||||
| 		t.Fatalf("failed to get x-request-id") | 		t.Fatalf("failed to get x-request-id") | ||||||
| 	} else if v != "12345" { | 	} else if v != "12345" { | ||||||
| 		t.Fatalf("invalid metadata value: %s != %s", "12345", v) | 		t.Fatalf("invalid metadata value: %s != %s", "12345", v) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	v, ok = md.GetJoined("X-Request-Id") | 	v = md.GetJoined("X-Request-Id") | ||||||
| 	if !ok { | 	if v == "" { | ||||||
| 		t.Fatalf("failed to get x-request-id") | 		t.Fatalf("failed to get x-request-id") | ||||||
| 	} else if v != "12345" { | 	} else if v != "12345" { | ||||||
| 		t.Fatalf("invalid metadata value: %s != %s", "12345", v) | 		t.Fatalf("invalid metadata value: %s != %s", "12345", v) | ||||||
| 	} | 	} | ||||||
| 	v, ok = md.GetJoined("X-Request-ID") | 	v = md.GetJoined("X-Request-ID") | ||||||
| 	if !ok { | 	if v == "" { | ||||||
| 		t.Fatalf("failed to get x-request-id") | 		t.Fatalf("failed to get x-request-id") | ||||||
| 	} else if v != "12345" { | 	} else if v != "12345" { | ||||||
| 		t.Fatalf("invalid metadata value: %s != %s", "12345", v) | 		t.Fatalf("invalid metadata value: %s != %s", "12345", v) | ||||||
| @@ -153,8 +138,8 @@ func TestMetadataSet(t *testing.T) { | |||||||
|  |  | ||||||
| 	md.Set("Key", "val") | 	md.Set("Key", "val") | ||||||
|  |  | ||||||
| 	val, ok := md.GetJoined("Key") | 	val := md.GetJoined("Key") | ||||||
| 	if !ok { | 	if val == "" { | ||||||
| 		t.Fatal("key Key not found") | 		t.Fatal("key Key not found") | ||||||
| 	} | 	} | ||||||
| 	if val != "val" { | 	if val != "val" { | ||||||
| @@ -169,8 +154,8 @@ func TestMetadataDelete(t *testing.T) { | |||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	md.Del("Baz") | 	md.Del("Baz") | ||||||
| 	_, ok := md.Get("Baz") | 	v := md.Get("Baz") | ||||||
| 	if ok { | 	if v != nil { | ||||||
| 		t.Fatal("key Baz not deleted") | 		t.Fatal("key Baz not deleted") | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
| @@ -269,20 +254,6 @@ func TestNewOutgoingContext(t *testing.T) { | |||||||
| 	} | 	} | ||||||
| } | } | ||||||
|  |  | ||||||
| func TestAppendIncomingContext(t *testing.T) { |  | ||||||
| 	md := New(1) |  | ||||||
| 	md.Set("key1", "val1") |  | ||||||
| 	ctx := AppendIncomingContext(context.TODO(), "key2", "val2") |  | ||||||
|  |  | ||||||
| 	nmd, ok := FromIncomingContext(ctx) |  | ||||||
| 	if nmd == nil || !ok { |  | ||||||
| 		t.Fatal("AppendIncomingContext not works") |  | ||||||
| 	} |  | ||||||
| 	if v, ok := nmd.GetJoined("key2"); !ok || v != "val2" { |  | ||||||
| 		t.Fatal("AppendIncomingContext not works") |  | ||||||
| 	} |  | ||||||
| } |  | ||||||
|  |  | ||||||
| func TestAppendOutgoingContext(t *testing.T) { | func TestAppendOutgoingContext(t *testing.T) { | ||||||
| 	md := New(1) | 	md := New(1) | ||||||
| 	md.Set("key1", "val1") | 	md.Set("key1", "val1") | ||||||
| @@ -292,7 +263,7 @@ func TestAppendOutgoingContext(t *testing.T) { | |||||||
| 	if nmd == nil || !ok { | 	if nmd == nil || !ok { | ||||||
| 		t.Fatal("AppendOutgoingContext not works") | 		t.Fatal("AppendOutgoingContext not works") | ||||||
| 	} | 	} | ||||||
| 	if v, ok := nmd.GetJoined("key2"); !ok || v != "val2" { | 	if v := nmd.GetJoined("key2"); v != "val2" { | ||||||
| 		t.Fatal("AppendOutgoingContext not works") | 		t.Fatal("AppendOutgoingContext not works") | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
|   | |||||||
| @@ -489,35 +489,74 @@ func URLMap(query string) (map[string]interface{}, error) { | |||||||
| 	return mp.(map[string]interface{}), nil | 	return mp.(map[string]interface{}), nil | ||||||
| } | } | ||||||
|  |  | ||||||
| // FlattenMap expand key.subkey to nested map | // FlattenMap flattens a nested map into a single-level map using dot notation for nested keys. | ||||||
| func FlattenMap(a map[string]interface{}) map[string]interface{} { | // In case of key conflicts, all nested levels will be discarded in favor of the first-level key. | ||||||
| 	// preprocess map | // | ||||||
| 	nb := make(map[string]interface{}, len(a)) | // Example #1: | ||||||
| 	for k, v := range a { | // | ||||||
| 		ps := strings.Split(k, ".") | //	Input: | ||||||
| 		if len(ps) == 1 { | //	  { | ||||||
| 			nb[k] = v | //	    "user.name": "alex", | ||||||
|  | //	    "user.document.id": "document_id" | ||||||
|  | //	    "user.document.number": "document_number" | ||||||
|  | //	  } | ||||||
|  | //	Output: | ||||||
|  | //	  { | ||||||
|  | //	    "user": { | ||||||
|  | //	      "name": "alex", | ||||||
|  | //	      "document": { | ||||||
|  | //	        "id": "document_id" | ||||||
|  | //	        "number": "document_number" | ||||||
|  | //	      } | ||||||
|  | //	    } | ||||||
|  | //	  } | ||||||
|  | // | ||||||
|  | // Example #2 (with conflicts): | ||||||
|  | // | ||||||
|  | //	Input: | ||||||
|  | //	  { | ||||||
|  | //	    "user": "alex", | ||||||
|  | //	    "user.document.id": "document_id" | ||||||
|  | //	    "user.document.number": "document_number" | ||||||
|  | //	  } | ||||||
|  | //	Output: | ||||||
|  | //	  { | ||||||
|  | //	    "user": "alex" | ||||||
|  | //	  } | ||||||
|  | func FlattenMap(input map[string]interface{}) map[string]interface{} { | ||||||
|  | 	result := make(map[string]interface{}) | ||||||
|  |  | ||||||
|  | 	for k, v := range input { | ||||||
|  | 		parts := strings.Split(k, ".") | ||||||
|  |  | ||||||
|  | 		if len(parts) == 1 { | ||||||
|  | 			result[k] = v | ||||||
| 			continue | 			continue | ||||||
| 		} | 		} | ||||||
| 		em := make(map[string]interface{}) |  | ||||||
| 		em[ps[len(ps)-1]] = v | 		current := result | ||||||
| 		for i := len(ps) - 2; i > 0; i-- { |  | ||||||
| 			nm := make(map[string]interface{}) | 		for i, part := range parts { | ||||||
| 			nm[ps[i]] = em | 			// last element in the path | ||||||
| 			em = nm | 			if i == len(parts)-1 { | ||||||
| 		} | 				current[part] = v | ||||||
| 		if vm, ok := nb[ps[0]]; ok { | 				break | ||||||
| 			// nested map | 			} | ||||||
| 			nm := vm.(map[string]interface{}) |  | ||||||
| 			for vk, vv := range em { | 			// initialize map for current level if not exist | ||||||
| 				nm[vk] = vv | 			if _, ok := current[part]; !ok { | ||||||
|  | 				current[part] = make(map[string]interface{}) | ||||||
|  | 			} | ||||||
|  |  | ||||||
|  | 			if nested, ok := current[part].(map[string]interface{}); ok { | ||||||
|  | 				current = nested // continue to the nested map | ||||||
|  | 			} else { | ||||||
|  | 				break // if current element is not a map, ignore it | ||||||
| 			} | 			} | ||||||
| 			nb[ps[0]] = nm |  | ||||||
| 		} else { |  | ||||||
| 			nb[ps[0]] = em |  | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| 	return nb |  | ||||||
|  | 	return result | ||||||
| } | } | ||||||
|  |  | ||||||
| /* | /* | ||||||
|   | |||||||
| @@ -6,6 +6,7 @@ import ( | |||||||
| 	"testing" | 	"testing" | ||||||
| 	"time" | 	"time" | ||||||
|  |  | ||||||
|  | 	"github.com/stretchr/testify/require" | ||||||
| 	rutil "go.unistack.org/micro/v4/util/reflect" | 	rutil "go.unistack.org/micro/v4/util/reflect" | ||||||
| ) | ) | ||||||
|  |  | ||||||
| @@ -319,3 +320,140 @@ func TestIsZero(t *testing.T) { | |||||||
|  |  | ||||||
| 	// t.Logf("XX %#+v\n", ok) | 	// t.Logf("XX %#+v\n", ok) | ||||||
| } | } | ||||||
|  |  | ||||||
|  | func TestFlattenMap(t *testing.T) { | ||||||
|  | 	tests := []struct { | ||||||
|  | 		name     string | ||||||
|  | 		input    map[string]interface{} | ||||||
|  | 		expected map[string]interface{} | ||||||
|  | 	}{ | ||||||
|  | 		{ | ||||||
|  | 			name:     "empty map", | ||||||
|  | 			input:    map[string]interface{}{}, | ||||||
|  | 			expected: map[string]interface{}{}, | ||||||
|  | 		}, | ||||||
|  | 		{ | ||||||
|  | 			name:     "nil map", | ||||||
|  | 			input:    nil, | ||||||
|  | 			expected: map[string]interface{}{}, | ||||||
|  | 		}, | ||||||
|  | 		{ | ||||||
|  | 			name: "single level", | ||||||
|  | 			input: map[string]interface{}{ | ||||||
|  | 				"username": "username", | ||||||
|  | 				"password": "password", | ||||||
|  | 			}, | ||||||
|  | 			expected: map[string]interface{}{ | ||||||
|  | 				"username": "username", | ||||||
|  | 				"password": "password", | ||||||
|  | 			}, | ||||||
|  | 		}, | ||||||
|  | 		{ | ||||||
|  | 			name: "two level", | ||||||
|  | 			input: map[string]interface{}{ | ||||||
|  | 				"order_id":      "order_id", | ||||||
|  | 				"user.name":     "username", | ||||||
|  | 				"user.password": "password", | ||||||
|  | 			}, | ||||||
|  | 			expected: map[string]interface{}{ | ||||||
|  | 				"order_id": "order_id", | ||||||
|  | 				"user": map[string]interface{}{ | ||||||
|  | 					"name":     "username", | ||||||
|  | 					"password": "password", | ||||||
|  | 				}, | ||||||
|  | 			}, | ||||||
|  | 		}, | ||||||
|  | 		{ | ||||||
|  | 			name: "three level", | ||||||
|  | 			input: map[string]interface{}{ | ||||||
|  | 				"order_id":             "order_id", | ||||||
|  | 				"user.name":            "username", | ||||||
|  | 				"user.password":        "password", | ||||||
|  | 				"user.document.id":     "document_id", | ||||||
|  | 				"user.document.number": "document_number", | ||||||
|  | 			}, | ||||||
|  | 			expected: map[string]interface{}{ | ||||||
|  | 				"order_id": "order_id", | ||||||
|  | 				"user": map[string]interface{}{ | ||||||
|  | 					"name":     "username", | ||||||
|  | 					"password": "password", | ||||||
|  | 					"document": map[string]interface{}{ | ||||||
|  | 						"id":     "document_id", | ||||||
|  | 						"number": "document_number", | ||||||
|  | 					}, | ||||||
|  | 				}, | ||||||
|  | 			}, | ||||||
|  | 		}, | ||||||
|  | 		{ | ||||||
|  | 			name: "four level", | ||||||
|  | 			input: map[string]interface{}{ | ||||||
|  | 				"order_id":                    "order_id", | ||||||
|  | 				"user.name":                   "username", | ||||||
|  | 				"user.password":               "password", | ||||||
|  | 				"user.document.id":            "document_id", | ||||||
|  | 				"user.document.number":        "document_number", | ||||||
|  | 				"user.info.permissions.read":  "available", | ||||||
|  | 				"user.info.permissions.write": "available", | ||||||
|  | 			}, | ||||||
|  | 			expected: map[string]interface{}{ | ||||||
|  | 				"order_id": "order_id", | ||||||
|  | 				"user": map[string]interface{}{ | ||||||
|  | 					"name":     "username", | ||||||
|  | 					"password": "password", | ||||||
|  | 					"document": map[string]interface{}{ | ||||||
|  | 						"id":     "document_id", | ||||||
|  | 						"number": "document_number", | ||||||
|  | 					}, | ||||||
|  | 					"info": map[string]interface{}{ | ||||||
|  | 						"permissions": map[string]interface{}{ | ||||||
|  | 							"read":  "available", | ||||||
|  | 							"write": "available", | ||||||
|  | 						}, | ||||||
|  | 					}, | ||||||
|  | 				}, | ||||||
|  | 			}, | ||||||
|  | 		}, | ||||||
|  | 		{ | ||||||
|  | 			name: "key conflicts", | ||||||
|  | 			input: map[string]interface{}{ | ||||||
|  | 				"user":          "user", | ||||||
|  | 				"user.name":     "username", | ||||||
|  | 				"user.password": "password", | ||||||
|  | 			}, | ||||||
|  | 			expected: map[string]interface{}{ | ||||||
|  | 				"user": "user", | ||||||
|  | 			}, | ||||||
|  | 		}, | ||||||
|  | 		{ | ||||||
|  | 			name: "overwriting conflicts", | ||||||
|  | 			input: map[string]interface{}{ | ||||||
|  | 				"order_id":             "order_id", | ||||||
|  | 				"user.document.id":     "document_id", | ||||||
|  | 				"user.document.number": "document_number", | ||||||
|  | 				"user.info.address":    "address", | ||||||
|  | 				"user.info.phone":      "phone", | ||||||
|  | 			}, | ||||||
|  | 			expected: map[string]interface{}{ | ||||||
|  | 				"order_id": "order_id", | ||||||
|  | 				"user": map[string]interface{}{ | ||||||
|  | 					"document": map[string]interface{}{ | ||||||
|  | 						"id":     "document_id", | ||||||
|  | 						"number": "document_number", | ||||||
|  | 					}, | ||||||
|  | 					"info": map[string]interface{}{ | ||||||
|  | 						"address": "address", | ||||||
|  | 						"phone":   "phone", | ||||||
|  | 					}, | ||||||
|  | 				}, | ||||||
|  | 			}, | ||||||
|  | 		}, | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	for _, tt := range tests { | ||||||
|  | 		t.Run(tt.name, func(t *testing.T) { | ||||||
|  | 			for range 100 { // need to exclude the impact of key order in the map on the test. | ||||||
|  | 				require.Equal(t, tt.expected, rutil.FlattenMap(tt.input)) | ||||||
|  | 			} | ||||||
|  | 		}) | ||||||
|  | 	} | ||||||
|  | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user