diff --git a/.github/workflows/pr-check.yml b/.github/workflows/pr-check.yml index fc50ac3..6c4bb47 100644 --- a/.github/workflows/pr-check.yml +++ b/.github/workflows/pr-check.yml @@ -6,12 +6,12 @@ jobs: build: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - name: Set up Go - uses: actions/setup-go@v2 + uses: actions/setup-go@v3 with: - go-version: 1.16 + go-version: 1.18 - uses: actions/cache@v2 with: @@ -19,3 +19,31 @@ jobs: key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} restore-keys: | ${{ runner.os }}-go- + + resolve-modules: + name: resolve module + runs-on: ubuntu-latest + outputs: + matrix: ${{ steps.set-matrix.outputs.matrix }} + steps: + - name: Checkout Repo + uses: actions/checkout@v3 + + - id: set-matrix + run: ./hack/resolve-modules.sh + + lint: + name: lint module + runs-on: ubuntu-latest + needs: resolve-modules + strategy: + matrix: ${{ fromJson(needs.resolve-modules.outputs.matrix) }} + steps: + - uses: actions/checkout@v3 + - name: Lint + uses: golangci/golangci-lint-action@v3 + with: + version: latest + working-directory: ${{ matrix.workdir }} + args: -E gofumpt --timeout 10m + skip-pkg-cache: true diff --git a/.github/workflows/push-check.yml b/.github/workflows/push-check.yml index 42b9562..556eaad 100644 --- a/.github/workflows/push-check.yml +++ b/.github/workflows/push-check.yml @@ -5,25 +5,31 @@ on: [ push, pull_request ] jobs: build: runs-on: ubuntu-latest + + services: + nacos: + image: 'nacos/nacos-server:2.0.3' + ports: + - '8848:8848' + - '9848:9848' + env: + MODE: standalone + steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - name: Set up Go - uses: actions/setup-go@v2 + uses: actions/setup-go@v3 with: - go-version: 1.16 + go-version: 1.18 - - uses: actions/cache@v2 + - uses: actions/cache@v3 with: path: ~/go/pkg/mod key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} restore-keys: | ${{ runner.os }}-go- - - name: Prepare Nacos - run: | - make prepare - - name: Check License Header uses: apache/skywalking-eyes/header@v0.4.0 env: @@ -32,18 +38,14 @@ jobs: - name: typos-action uses: crate-ci/typos@v1.2.1 - - name: Lint - run: | - go vet -stdmethods=false $(go list ./...) - go install mvdan.cc/gofumpt@v0.2.0 - test -z "$(gofumpt -l -extra .)" + + - name: Benchmark + run: go test -bench=. -benchmem -run=none ./... - name: Unit Test - run: go test -v -race -covermode=atomic -coverprofile=coverage.out ./... + run: make test env: serverAddr: 127.0.0.1 serverPort: 8848 namespace: public - - name: Benchmark - run: go test -bench=. -benchmem -run=none ./... diff --git a/.licenserc.yaml b/.licenserc.yaml index a1ba77d..5af9bfa 100644 --- a/.licenserc.yaml +++ b/.licenserc.yaml @@ -21,4 +21,5 @@ header: - '**/*.go' paths-ignore: - example/hello/** + - v2/example/hello/** comment: on-failure diff --git a/Makefile b/Makefile index 5077e79..195346e 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,5 @@ NACOS_VERSION ?= 2.0.3 +TOOLS_SHELL="./hack/tools.sh" prepare: docker pull nacos/nacos-server:$(NACOS_VERSION) @@ -6,3 +7,16 @@ prepare: stop: docker stop nacos-quick + + +.PHONY: test +test: + @${TOOLS_SHELL} test + @echo "go test finished" + + + +.PHONY: vet +vet: + @${TOOLS_SHELL} vet + @echo "vet check finished" diff --git a/hack/resolve-modules.sh b/hack/resolve-modules.sh new file mode 100755 index 0000000..acef142 --- /dev/null +++ b/hack/resolve-modules.sh @@ -0,0 +1,22 @@ +#!/usr/bin/env bash + +# This is used by the linter action. +# Recursively finds all directories with a go.mod file and creates +# a GitHub Actions JSON output option. + +set -o errexit + +HOME=$( + cd "$(dirname "${BASH_SOURCE[0]}")" && + cd .. && + pwd +) + +source "${HOME}/hack/util.sh" +all_modules=$(util::find_modules) +PATHS="" +for mod in $all_modules; do + PATHS+=$(printf '{"workdir":"%s"},' ${mod}) +done + +echo "::set-output name=matrix::{\"include\":[${PATHS%?}]}" \ No newline at end of file diff --git a/hack/tools.sh b/hack/tools.sh new file mode 100755 index 0000000..d11c8b6 --- /dev/null +++ b/hack/tools.sh @@ -0,0 +1,51 @@ +#!/usr/bin/env bash + +set -o errexit +set -o nounset +set -o pipefail + +HOME=$( + cd "$(dirname "${BASH_SOURCE[0]}")" && + cd .. && + pwd +) + +source "${HOME}/hack/util.sh" + +all_modules=$(util::find_modules) + +# test all mod +function test() { + for mod in $all_modules; do + pushd "$mod" >/dev/null && + echo "go test $(sed -n 1p go.mod | cut -d ' ' -f2)" && + go test -race -covermode=atomic -coverprofile=coverage.out ./... + popd >/dev/null || exit + done +} + +# vet all mod +function vet() { + for mod in $all_modules; do + pushd "$mod" >/dev/null && + echo "go vet $(sed -n 1p go.mod | cut -d ' ' -f2)" && + go vet -stdmethods=false ./... + popd >/dev/null || exit + done +} + +function help() { + echo "use: test,vet" +} + +case $1 in +vet) + vet + ;; +test) + test + ;; +*) + help + ;; +esac diff --git a/hack/util.sh b/hack/util.sh new file mode 100644 index 0000000..05f47e0 --- /dev/null +++ b/hack/util.sh @@ -0,0 +1,15 @@ +#!/usr/bin/env bash + +# find all go mod path +# returns an array contains mod path +function util::find_modules() { + find . -not \( \ + \( \ + -path './output' \ + -o -path './.git' \ + -o -path '*/third_party/*' \ + -o -path '*/vendor/*' \ + -o -path '*/example/*' \ + \) -prune \ + \) -name 'go.mod' -print0 | xargs -0 -I {} dirname {} +} \ No newline at end of file diff --git a/v2/Makefile b/v2/Makefile new file mode 100644 index 0000000..bd52b74 --- /dev/null +++ b/v2/Makefile @@ -0,0 +1,9 @@ +NACOS_VERSION ?= 2.0.3 +TOOLS_SHELL="./hack/tools.sh" + +prepare: + docker pull nacos/nacos-server:$(NACOS_VERSION) + docker run --rm --name nacos-quick -e MODE=standalone -p 8848:8848 -p 9848:9848 -d nacos/nacos-server:$(NACOS_VERSION) + +stop: + docker stop nacos-quick diff --git a/v2/example/basic/client/client.go b/v2/example/basic/client/client.go index 617ccdf..5947787 100644 --- a/v2/example/basic/client/client.go +++ b/v2/example/basic/client/client.go @@ -19,9 +19,10 @@ import ( "log" "time" + "github.com/kitex-contrib/registry-nacos/v2/example/hello/kitex_gen/api" + "github.com/kitex-contrib/registry-nacos/v2/example/hello/kitex_gen/api/hello" + "github.com/cloudwego/kitex/client" - "github.com/kitex-contrib/registry-nacos/example/hello/kitex_gen/api" - "github.com/kitex-contrib/registry-nacos/example/hello/kitex_gen/api/hello" "github.com/kitex-contrib/registry-nacos/v2/resolver" ) diff --git a/v2/example/basic/server/server.go b/v2/example/basic/server/server.go index 760fe5b..df879cf 100644 --- a/v2/example/basic/server/server.go +++ b/v2/example/basic/server/server.go @@ -21,8 +21,8 @@ import ( "github.com/cloudwego/kitex/pkg/rpcinfo" "github.com/cloudwego/kitex/server" - "github.com/kitex-contrib/registry-nacos/example/hello/kitex_gen/api" - "github.com/kitex-contrib/registry-nacos/example/hello/kitex_gen/api/hello" + "github.com/kitex-contrib/registry-nacos/v2/example/hello/kitex_gen/api" + "github.com/kitex-contrib/registry-nacos/v2/example/hello/kitex_gen/api/hello" "github.com/kitex-contrib/registry-nacos/v2/registry" ) diff --git a/v2/example/custom-config/client/main.go b/v2/example/custom-config/client/main.go index d2ecae3..1e372f3 100644 --- a/v2/example/custom-config/client/main.go +++ b/v2/example/custom-config/client/main.go @@ -20,8 +20,8 @@ import ( "time" "github.com/cloudwego/kitex/client" - "github.com/kitex-contrib/registry-nacos/example/hello/kitex_gen/api" - "github.com/kitex-contrib/registry-nacos/example/hello/kitex_gen/api/hello" + "github.com/kitex-contrib/registry-nacos/v2/example/hello/kitex_gen/api" + "github.com/kitex-contrib/registry-nacos/v2/example/hello/kitex_gen/api/hello" "github.com/kitex-contrib/registry-nacos/v2/resolver" "github.com/nacos-group/nacos-sdk-go/v2/clients" "github.com/nacos-group/nacos-sdk-go/v2/common/constant" diff --git a/v2/example/custom-config/server/main.go b/v2/example/custom-config/server/main.go index aaf7b03..3b3d381 100644 --- a/v2/example/custom-config/server/main.go +++ b/v2/example/custom-config/server/main.go @@ -20,8 +20,8 @@ import ( "github.com/cloudwego/kitex/pkg/rpcinfo" "github.com/cloudwego/kitex/server" - "github.com/kitex-contrib/registry-nacos/example/hello/kitex_gen/api" - "github.com/kitex-contrib/registry-nacos/example/hello/kitex_gen/api/hello" + "github.com/kitex-contrib/registry-nacos/v2/example/hello/kitex_gen/api" + "github.com/kitex-contrib/registry-nacos/v2/example/hello/kitex_gen/api/hello" "github.com/kitex-contrib/registry-nacos/v2/registry" "github.com/nacos-group/nacos-sdk-go/v2/clients" "github.com/nacos-group/nacos-sdk-go/v2/common/constant" diff --git a/v2/example/hello/hello.thrift b/v2/example/hello/hello.thrift new file mode 100644 index 0000000..8a23ca9 --- /dev/null +++ b/v2/example/hello/hello.thrift @@ -0,0 +1,28 @@ +// Copyright 2021 CloudWeGo Authors +// +// 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. +// + +namespace go api + +struct Request { + 1: string message +} + +struct Response { + 1: string message +} + +service Hello { + Response echo(1: Request req) +} diff --git a/v2/example/hello/kitex_gen/api/hello.go b/v2/example/hello/kitex_gen/api/hello.go new file mode 100644 index 0000000..b407499 --- /dev/null +++ b/v2/example/hello/kitex_gen/api/hello.go @@ -0,0 +1,814 @@ +// Code generated by thriftgo (0.2.8). DO NOT EDIT. + +package api + +import ( + "context" + "fmt" + "strings" + + "github.com/apache/thrift/lib/go/thrift" +) + +type Request struct { + Message string `thrift:"message,1" frugal:"1,default,string" json:"message"` +} + +func NewRequest() *Request { + return &Request{} +} + +func (p *Request) InitDefault() { + *p = Request{} +} + +func (p *Request) GetMessage() (v string) { + return p.Message +} +func (p *Request) SetMessage(val string) { + p.Message = val +} + +var fieldIDToName_Request = map[int16]string{ + 1: "message", +} + +func (p *Request) Read(iprot thrift.TProtocol) (err error) { + + var fieldTypeId thrift.TType + var fieldId int16 + + if _, err = iprot.ReadStructBegin(); err != nil { + goto ReadStructBeginError + } + + for { + _, fieldTypeId, fieldId, err = iprot.ReadFieldBegin() + if err != nil { + goto ReadFieldBeginError + } + if fieldTypeId == thrift.STOP { + break + } + + switch fieldId { + case 1: + if fieldTypeId == thrift.STRING { + if err = p.ReadField1(iprot); err != nil { + goto ReadFieldError + } + } else { + if err = iprot.Skip(fieldTypeId); err != nil { + goto SkipFieldError + } + } + default: + if err = iprot.Skip(fieldTypeId); err != nil { + goto SkipFieldError + } + } + + if err = iprot.ReadFieldEnd(); err != nil { + goto ReadFieldEndError + } + } + if err = iprot.ReadStructEnd(); err != nil { + goto ReadStructEndError + } + + return nil +ReadStructBeginError: + return thrift.PrependError(fmt.Sprintf("%T read struct begin error: ", p), err) +ReadFieldBeginError: + return thrift.PrependError(fmt.Sprintf("%T read field %d begin error: ", p, fieldId), err) +ReadFieldError: + return thrift.PrependError(fmt.Sprintf("%T read field %d '%s' error: ", p, fieldId, fieldIDToName_Request[fieldId]), err) +SkipFieldError: + return thrift.PrependError(fmt.Sprintf("%T field %d skip type %d error: ", p, fieldId, fieldTypeId), err) + +ReadFieldEndError: + return thrift.PrependError(fmt.Sprintf("%T read field end error", p), err) +ReadStructEndError: + return thrift.PrependError(fmt.Sprintf("%T read struct end error: ", p), err) +} + +func (p *Request) ReadField1(iprot thrift.TProtocol) error { + if v, err := iprot.ReadString(); err != nil { + return err + } else { + p.Message = v + } + return nil +} + +func (p *Request) Write(oprot thrift.TProtocol) (err error) { + var fieldId int16 + if err = oprot.WriteStructBegin("Request"); err != nil { + goto WriteStructBeginError + } + if p != nil { + if err = p.writeField1(oprot); err != nil { + fieldId = 1 + goto WriteFieldError + } + + } + if err = oprot.WriteFieldStop(); err != nil { + goto WriteFieldStopError + } + if err = oprot.WriteStructEnd(); err != nil { + goto WriteStructEndError + } + return nil +WriteStructBeginError: + return thrift.PrependError(fmt.Sprintf("%T write struct begin error: ", p), err) +WriteFieldError: + return thrift.PrependError(fmt.Sprintf("%T write field %d error: ", p, fieldId), err) +WriteFieldStopError: + return thrift.PrependError(fmt.Sprintf("%T write field stop error: ", p), err) +WriteStructEndError: + return thrift.PrependError(fmt.Sprintf("%T write struct end error: ", p), err) +} + +func (p *Request) writeField1(oprot thrift.TProtocol) (err error) { + if err = oprot.WriteFieldBegin("message", thrift.STRING, 1); err != nil { + goto WriteFieldBeginError + } + if err := oprot.WriteString(p.Message); err != nil { + return err + } + if err = oprot.WriteFieldEnd(); err != nil { + goto WriteFieldEndError + } + return nil +WriteFieldBeginError: + return thrift.PrependError(fmt.Sprintf("%T write field 1 begin error: ", p), err) +WriteFieldEndError: + return thrift.PrependError(fmt.Sprintf("%T write field 1 end error: ", p), err) +} + +func (p *Request) String() string { + if p == nil { + return "" + } + return fmt.Sprintf("Request(%+v)", *p) +} + +func (p *Request) DeepEqual(ano *Request) bool { + if p == ano { + return true + } else if p == nil || ano == nil { + return false + } + if !p.Field1DeepEqual(ano.Message) { + return false + } + return true +} + +func (p *Request) Field1DeepEqual(src string) bool { + + if strings.Compare(p.Message, src) != 0 { + return false + } + return true +} + +type Response struct { + Message string `thrift:"message,1" frugal:"1,default,string" json:"message"` +} + +func NewResponse() *Response { + return &Response{} +} + +func (p *Response) InitDefault() { + *p = Response{} +} + +func (p *Response) GetMessage() (v string) { + return p.Message +} +func (p *Response) SetMessage(val string) { + p.Message = val +} + +var fieldIDToName_Response = map[int16]string{ + 1: "message", +} + +func (p *Response) Read(iprot thrift.TProtocol) (err error) { + + var fieldTypeId thrift.TType + var fieldId int16 + + if _, err = iprot.ReadStructBegin(); err != nil { + goto ReadStructBeginError + } + + for { + _, fieldTypeId, fieldId, err = iprot.ReadFieldBegin() + if err != nil { + goto ReadFieldBeginError + } + if fieldTypeId == thrift.STOP { + break + } + + switch fieldId { + case 1: + if fieldTypeId == thrift.STRING { + if err = p.ReadField1(iprot); err != nil { + goto ReadFieldError + } + } else { + if err = iprot.Skip(fieldTypeId); err != nil { + goto SkipFieldError + } + } + default: + if err = iprot.Skip(fieldTypeId); err != nil { + goto SkipFieldError + } + } + + if err = iprot.ReadFieldEnd(); err != nil { + goto ReadFieldEndError + } + } + if err = iprot.ReadStructEnd(); err != nil { + goto ReadStructEndError + } + + return nil +ReadStructBeginError: + return thrift.PrependError(fmt.Sprintf("%T read struct begin error: ", p), err) +ReadFieldBeginError: + return thrift.PrependError(fmt.Sprintf("%T read field %d begin error: ", p, fieldId), err) +ReadFieldError: + return thrift.PrependError(fmt.Sprintf("%T read field %d '%s' error: ", p, fieldId, fieldIDToName_Response[fieldId]), err) +SkipFieldError: + return thrift.PrependError(fmt.Sprintf("%T field %d skip type %d error: ", p, fieldId, fieldTypeId), err) + +ReadFieldEndError: + return thrift.PrependError(fmt.Sprintf("%T read field end error", p), err) +ReadStructEndError: + return thrift.PrependError(fmt.Sprintf("%T read struct end error: ", p), err) +} + +func (p *Response) ReadField1(iprot thrift.TProtocol) error { + if v, err := iprot.ReadString(); err != nil { + return err + } else { + p.Message = v + } + return nil +} + +func (p *Response) Write(oprot thrift.TProtocol) (err error) { + var fieldId int16 + if err = oprot.WriteStructBegin("Response"); err != nil { + goto WriteStructBeginError + } + if p != nil { + if err = p.writeField1(oprot); err != nil { + fieldId = 1 + goto WriteFieldError + } + + } + if err = oprot.WriteFieldStop(); err != nil { + goto WriteFieldStopError + } + if err = oprot.WriteStructEnd(); err != nil { + goto WriteStructEndError + } + return nil +WriteStructBeginError: + return thrift.PrependError(fmt.Sprintf("%T write struct begin error: ", p), err) +WriteFieldError: + return thrift.PrependError(fmt.Sprintf("%T write field %d error: ", p, fieldId), err) +WriteFieldStopError: + return thrift.PrependError(fmt.Sprintf("%T write field stop error: ", p), err) +WriteStructEndError: + return thrift.PrependError(fmt.Sprintf("%T write struct end error: ", p), err) +} + +func (p *Response) writeField1(oprot thrift.TProtocol) (err error) { + if err = oprot.WriteFieldBegin("message", thrift.STRING, 1); err != nil { + goto WriteFieldBeginError + } + if err := oprot.WriteString(p.Message); err != nil { + return err + } + if err = oprot.WriteFieldEnd(); err != nil { + goto WriteFieldEndError + } + return nil +WriteFieldBeginError: + return thrift.PrependError(fmt.Sprintf("%T write field 1 begin error: ", p), err) +WriteFieldEndError: + return thrift.PrependError(fmt.Sprintf("%T write field 1 end error: ", p), err) +} + +func (p *Response) String() string { + if p == nil { + return "" + } + return fmt.Sprintf("Response(%+v)", *p) +} + +func (p *Response) DeepEqual(ano *Response) bool { + if p == ano { + return true + } else if p == nil || ano == nil { + return false + } + if !p.Field1DeepEqual(ano.Message) { + return false + } + return true +} + +func (p *Response) Field1DeepEqual(src string) bool { + + if strings.Compare(p.Message, src) != 0 { + return false + } + return true +} + +type Hello interface { + Echo(ctx context.Context, req *Request) (r *Response, err error) +} + +type HelloClient struct { + c thrift.TClient +} + +func NewHelloClientFactory(t thrift.TTransport, f thrift.TProtocolFactory) *HelloClient { + return &HelloClient{ + c: thrift.NewTStandardClient(f.GetProtocol(t), f.GetProtocol(t)), + } +} + +func NewHelloClientProtocol(t thrift.TTransport, iprot thrift.TProtocol, oprot thrift.TProtocol) *HelloClient { + return &HelloClient{ + c: thrift.NewTStandardClient(iprot, oprot), + } +} + +func NewHelloClient(c thrift.TClient) *HelloClient { + return &HelloClient{ + c: c, + } +} + +func (p *HelloClient) Client_() thrift.TClient { + return p.c +} + +func (p *HelloClient) Echo(ctx context.Context, req *Request) (r *Response, err error) { + var _args HelloEchoArgs + _args.Req = req + var _result HelloEchoResult + if err = p.Client_().Call(ctx, "echo", &_args, &_result); err != nil { + return + } + return _result.GetSuccess(), nil +} + +type HelloProcessor struct { + processorMap map[string]thrift.TProcessorFunction + handler Hello +} + +func (p *HelloProcessor) AddToProcessorMap(key string, processor thrift.TProcessorFunction) { + p.processorMap[key] = processor +} + +func (p *HelloProcessor) GetProcessorFunction(key string) (processor thrift.TProcessorFunction, ok bool) { + processor, ok = p.processorMap[key] + return processor, ok +} + +func (p *HelloProcessor) ProcessorMap() map[string]thrift.TProcessorFunction { + return p.processorMap +} + +func NewHelloProcessor(handler Hello) *HelloProcessor { + self := &HelloProcessor{handler: handler, processorMap: make(map[string]thrift.TProcessorFunction)} + self.AddToProcessorMap("echo", &helloProcessorEcho{handler: handler}) + return self +} +func (p *HelloProcessor) Process(ctx context.Context, iprot, oprot thrift.TProtocol) (success bool, err thrift.TException) { + name, _, seqId, err := iprot.ReadMessageBegin() + if err != nil { + return false, err + } + if processor, ok := p.GetProcessorFunction(name); ok { + return processor.Process(ctx, seqId, iprot, oprot) + } + iprot.Skip(thrift.STRUCT) + iprot.ReadMessageEnd() + x := thrift.NewTApplicationException(thrift.UNKNOWN_METHOD, "Unknown function "+name) + oprot.WriteMessageBegin(name, thrift.EXCEPTION, seqId) + x.Write(oprot) + oprot.WriteMessageEnd() + oprot.Flush(ctx) + return false, x +} + +type helloProcessorEcho struct { + handler Hello +} + +func (p *helloProcessorEcho) Process(ctx context.Context, seqId int32, iprot, oprot thrift.TProtocol) (success bool, err thrift.TException) { + args := HelloEchoArgs{} + if err = args.Read(iprot); err != nil { + iprot.ReadMessageEnd() + x := thrift.NewTApplicationException(thrift.PROTOCOL_ERROR, err.Error()) + oprot.WriteMessageBegin("echo", thrift.EXCEPTION, seqId) + x.Write(oprot) + oprot.WriteMessageEnd() + oprot.Flush(ctx) + return false, err + } + + iprot.ReadMessageEnd() + var err2 error + result := HelloEchoResult{} + var retval *Response + if retval, err2 = p.handler.Echo(ctx, args.Req); err2 != nil { + x := thrift.NewTApplicationException(thrift.INTERNAL_ERROR, "Internal error processing echo: "+err2.Error()) + oprot.WriteMessageBegin("echo", thrift.EXCEPTION, seqId) + x.Write(oprot) + oprot.WriteMessageEnd() + oprot.Flush(ctx) + return true, err2 + } else { + result.Success = retval + } + if err2 = oprot.WriteMessageBegin("echo", thrift.REPLY, seqId); err2 != nil { + err = err2 + } + if err2 = result.Write(oprot); err == nil && err2 != nil { + err = err2 + } + if err2 = oprot.WriteMessageEnd(); err == nil && err2 != nil { + err = err2 + } + if err2 = oprot.Flush(ctx); err == nil && err2 != nil { + err = err2 + } + if err != nil { + return + } + return true, err +} + +type HelloEchoArgs struct { + Req *Request `thrift:"req,1" frugal:"1,default,Request" json:"req"` +} + +func NewHelloEchoArgs() *HelloEchoArgs { + return &HelloEchoArgs{} +} + +func (p *HelloEchoArgs) InitDefault() { + *p = HelloEchoArgs{} +} + +var HelloEchoArgs_Req_DEFAULT *Request + +func (p *HelloEchoArgs) GetReq() (v *Request) { + if !p.IsSetReq() { + return HelloEchoArgs_Req_DEFAULT + } + return p.Req +} +func (p *HelloEchoArgs) SetReq(val *Request) { + p.Req = val +} + +var fieldIDToName_HelloEchoArgs = map[int16]string{ + 1: "req", +} + +func (p *HelloEchoArgs) IsSetReq() bool { + return p.Req != nil +} + +func (p *HelloEchoArgs) Read(iprot thrift.TProtocol) (err error) { + + var fieldTypeId thrift.TType + var fieldId int16 + + if _, err = iprot.ReadStructBegin(); err != nil { + goto ReadStructBeginError + } + + for { + _, fieldTypeId, fieldId, err = iprot.ReadFieldBegin() + if err != nil { + goto ReadFieldBeginError + } + if fieldTypeId == thrift.STOP { + break + } + + switch fieldId { + case 1: + if fieldTypeId == thrift.STRUCT { + if err = p.ReadField1(iprot); err != nil { + goto ReadFieldError + } + } else { + if err = iprot.Skip(fieldTypeId); err != nil { + goto SkipFieldError + } + } + default: + if err = iprot.Skip(fieldTypeId); err != nil { + goto SkipFieldError + } + } + + if err = iprot.ReadFieldEnd(); err != nil { + goto ReadFieldEndError + } + } + if err = iprot.ReadStructEnd(); err != nil { + goto ReadStructEndError + } + + return nil +ReadStructBeginError: + return thrift.PrependError(fmt.Sprintf("%T read struct begin error: ", p), err) +ReadFieldBeginError: + return thrift.PrependError(fmt.Sprintf("%T read field %d begin error: ", p, fieldId), err) +ReadFieldError: + return thrift.PrependError(fmt.Sprintf("%T read field %d '%s' error: ", p, fieldId, fieldIDToName_HelloEchoArgs[fieldId]), err) +SkipFieldError: + return thrift.PrependError(fmt.Sprintf("%T field %d skip type %d error: ", p, fieldId, fieldTypeId), err) + +ReadFieldEndError: + return thrift.PrependError(fmt.Sprintf("%T read field end error", p), err) +ReadStructEndError: + return thrift.PrependError(fmt.Sprintf("%T read struct end error: ", p), err) +} + +func (p *HelloEchoArgs) ReadField1(iprot thrift.TProtocol) error { + p.Req = NewRequest() + if err := p.Req.Read(iprot); err != nil { + return err + } + return nil +} + +func (p *HelloEchoArgs) Write(oprot thrift.TProtocol) (err error) { + var fieldId int16 + if err = oprot.WriteStructBegin("echo_args"); err != nil { + goto WriteStructBeginError + } + if p != nil { + if err = p.writeField1(oprot); err != nil { + fieldId = 1 + goto WriteFieldError + } + + } + if err = oprot.WriteFieldStop(); err != nil { + goto WriteFieldStopError + } + if err = oprot.WriteStructEnd(); err != nil { + goto WriteStructEndError + } + return nil +WriteStructBeginError: + return thrift.PrependError(fmt.Sprintf("%T write struct begin error: ", p), err) +WriteFieldError: + return thrift.PrependError(fmt.Sprintf("%T write field %d error: ", p, fieldId), err) +WriteFieldStopError: + return thrift.PrependError(fmt.Sprintf("%T write field stop error: ", p), err) +WriteStructEndError: + return thrift.PrependError(fmt.Sprintf("%T write struct end error: ", p), err) +} + +func (p *HelloEchoArgs) writeField1(oprot thrift.TProtocol) (err error) { + if err = oprot.WriteFieldBegin("req", thrift.STRUCT, 1); err != nil { + goto WriteFieldBeginError + } + if err := p.Req.Write(oprot); err != nil { + return err + } + if err = oprot.WriteFieldEnd(); err != nil { + goto WriteFieldEndError + } + return nil +WriteFieldBeginError: + return thrift.PrependError(fmt.Sprintf("%T write field 1 begin error: ", p), err) +WriteFieldEndError: + return thrift.PrependError(fmt.Sprintf("%T write field 1 end error: ", p), err) +} + +func (p *HelloEchoArgs) String() string { + if p == nil { + return "" + } + return fmt.Sprintf("HelloEchoArgs(%+v)", *p) +} + +func (p *HelloEchoArgs) DeepEqual(ano *HelloEchoArgs) bool { + if p == ano { + return true + } else if p == nil || ano == nil { + return false + } + if !p.Field1DeepEqual(ano.Req) { + return false + } + return true +} + +func (p *HelloEchoArgs) Field1DeepEqual(src *Request) bool { + + if !p.Req.DeepEqual(src) { + return false + } + return true +} + +type HelloEchoResult struct { + Success *Response `thrift:"success,0,optional" frugal:"0,optional,Response" json:"success,omitempty"` +} + +func NewHelloEchoResult() *HelloEchoResult { + return &HelloEchoResult{} +} + +func (p *HelloEchoResult) InitDefault() { + *p = HelloEchoResult{} +} + +var HelloEchoResult_Success_DEFAULT *Response + +func (p *HelloEchoResult) GetSuccess() (v *Response) { + if !p.IsSetSuccess() { + return HelloEchoResult_Success_DEFAULT + } + return p.Success +} +func (p *HelloEchoResult) SetSuccess(x interface{}) { + p.Success = x.(*Response) +} + +var fieldIDToName_HelloEchoResult = map[int16]string{ + 0: "success", +} + +func (p *HelloEchoResult) IsSetSuccess() bool { + return p.Success != nil +} + +func (p *HelloEchoResult) Read(iprot thrift.TProtocol) (err error) { + + var fieldTypeId thrift.TType + var fieldId int16 + + if _, err = iprot.ReadStructBegin(); err != nil { + goto ReadStructBeginError + } + + for { + _, fieldTypeId, fieldId, err = iprot.ReadFieldBegin() + if err != nil { + goto ReadFieldBeginError + } + if fieldTypeId == thrift.STOP { + break + } + + switch fieldId { + case 0: + if fieldTypeId == thrift.STRUCT { + if err = p.ReadField0(iprot); err != nil { + goto ReadFieldError + } + } else { + if err = iprot.Skip(fieldTypeId); err != nil { + goto SkipFieldError + } + } + default: + if err = iprot.Skip(fieldTypeId); err != nil { + goto SkipFieldError + } + } + + if err = iprot.ReadFieldEnd(); err != nil { + goto ReadFieldEndError + } + } + if err = iprot.ReadStructEnd(); err != nil { + goto ReadStructEndError + } + + return nil +ReadStructBeginError: + return thrift.PrependError(fmt.Sprintf("%T read struct begin error: ", p), err) +ReadFieldBeginError: + return thrift.PrependError(fmt.Sprintf("%T read field %d begin error: ", p, fieldId), err) +ReadFieldError: + return thrift.PrependError(fmt.Sprintf("%T read field %d '%s' error: ", p, fieldId, fieldIDToName_HelloEchoResult[fieldId]), err) +SkipFieldError: + return thrift.PrependError(fmt.Sprintf("%T field %d skip type %d error: ", p, fieldId, fieldTypeId), err) + +ReadFieldEndError: + return thrift.PrependError(fmt.Sprintf("%T read field end error", p), err) +ReadStructEndError: + return thrift.PrependError(fmt.Sprintf("%T read struct end error: ", p), err) +} + +func (p *HelloEchoResult) ReadField0(iprot thrift.TProtocol) error { + p.Success = NewResponse() + if err := p.Success.Read(iprot); err != nil { + return err + } + return nil +} + +func (p *HelloEchoResult) Write(oprot thrift.TProtocol) (err error) { + var fieldId int16 + if err = oprot.WriteStructBegin("echo_result"); err != nil { + goto WriteStructBeginError + } + if p != nil { + if err = p.writeField0(oprot); err != nil { + fieldId = 0 + goto WriteFieldError + } + + } + if err = oprot.WriteFieldStop(); err != nil { + goto WriteFieldStopError + } + if err = oprot.WriteStructEnd(); err != nil { + goto WriteStructEndError + } + return nil +WriteStructBeginError: + return thrift.PrependError(fmt.Sprintf("%T write struct begin error: ", p), err) +WriteFieldError: + return thrift.PrependError(fmt.Sprintf("%T write field %d error: ", p, fieldId), err) +WriteFieldStopError: + return thrift.PrependError(fmt.Sprintf("%T write field stop error: ", p), err) +WriteStructEndError: + return thrift.PrependError(fmt.Sprintf("%T write struct end error: ", p), err) +} + +func (p *HelloEchoResult) writeField0(oprot thrift.TProtocol) (err error) { + if p.IsSetSuccess() { + if err = oprot.WriteFieldBegin("success", thrift.STRUCT, 0); err != nil { + goto WriteFieldBeginError + } + if err := p.Success.Write(oprot); err != nil { + return err + } + if err = oprot.WriteFieldEnd(); err != nil { + goto WriteFieldEndError + } + } + return nil +WriteFieldBeginError: + return thrift.PrependError(fmt.Sprintf("%T write field 0 begin error: ", p), err) +WriteFieldEndError: + return thrift.PrependError(fmt.Sprintf("%T write field 0 end error: ", p), err) +} + +func (p *HelloEchoResult) String() string { + if p == nil { + return "" + } + return fmt.Sprintf("HelloEchoResult(%+v)", *p) +} + +func (p *HelloEchoResult) DeepEqual(ano *HelloEchoResult) bool { + if p == ano { + return true + } else if p == nil || ano == nil { + return false + } + if !p.Field0DeepEqual(ano.Success) { + return false + } + return true +} + +func (p *HelloEchoResult) Field0DeepEqual(src *Response) bool { + + if !p.Success.DeepEqual(src) { + return false + } + return true +} diff --git a/v2/example/hello/kitex_gen/api/hello/client.go b/v2/example/hello/kitex_gen/api/hello/client.go new file mode 100644 index 0000000..73b3c00 --- /dev/null +++ b/v2/example/hello/kitex_gen/api/hello/client.go @@ -0,0 +1,50 @@ +// Code generated by Kitex v0.5.1. DO NOT EDIT. + +package hello + +import ( + "context" + + client "github.com/cloudwego/kitex/client" + callopt "github.com/cloudwego/kitex/client/callopt" + api "github.com/kitex-contrib/registry-nacos/v2/example/hello/kitex_gen/api" +) + +// Client is designed to provide IDL-compatible methods with call-option parameter for kitex framework. +type Client interface { + Echo(ctx context.Context, req *api.Request, callOptions ...callopt.Option) (r *api.Response, err error) +} + +// NewClient creates a client for the service defined in IDL. +func NewClient(destService string, opts ...client.Option) (Client, error) { + var options []client.Option + options = append(options, client.WithDestService(destService)) + + options = append(options, opts...) + + kc, err := client.NewClient(serviceInfo(), options...) + if err != nil { + return nil, err + } + return &kHelloClient{ + kClient: newServiceClient(kc), + }, nil +} + +// MustNewClient creates a client for the service defined in IDL. It panics if any error occurs. +func MustNewClient(destService string, opts ...client.Option) Client { + kc, err := NewClient(destService, opts...) + if err != nil { + panic(err) + } + return kc +} + +type kHelloClient struct { + *kClient +} + +func (p *kHelloClient) Echo(ctx context.Context, req *api.Request, callOptions ...callopt.Option) (r *api.Response, err error) { + ctx = client.NewCtxWithCallOptions(ctx, callOptions) + return p.kClient.Echo(ctx, req) +} diff --git a/v2/example/hello/kitex_gen/api/hello/hello.go b/v2/example/hello/kitex_gen/api/hello/hello.go new file mode 100644 index 0000000..299b79c --- /dev/null +++ b/v2/example/hello/kitex_gen/api/hello/hello.go @@ -0,0 +1,75 @@ +// Code generated by Kitex v0.5.1. DO NOT EDIT. + +package hello + +import ( + "context" + + client "github.com/cloudwego/kitex/client" + kitex "github.com/cloudwego/kitex/pkg/serviceinfo" + api "github.com/kitex-contrib/registry-nacos/v2/example/hello/kitex_gen/api" +) + +func serviceInfo() *kitex.ServiceInfo { + return helloServiceInfo +} + +var helloServiceInfo = NewServiceInfo() + +func NewServiceInfo() *kitex.ServiceInfo { + serviceName := "Hello" + handlerType := (*api.Hello)(nil) + methods := map[string]kitex.MethodInfo{ + "echo": kitex.NewMethodInfo(echoHandler, newHelloEchoArgs, newHelloEchoResult, false), + } + extra := map[string]interface{}{ + "PackageName": "api", + } + svcInfo := &kitex.ServiceInfo{ + ServiceName: serviceName, + HandlerType: handlerType, + Methods: methods, + PayloadCodec: kitex.Thrift, + KiteXGenVersion: "v0.5.1", + Extra: extra, + } + return svcInfo +} + +func echoHandler(ctx context.Context, handler interface{}, arg, result interface{}) error { + realArg := arg.(*api.HelloEchoArgs) + realResult := result.(*api.HelloEchoResult) + success, err := handler.(api.Hello).Echo(ctx, realArg.Req) + if err != nil { + return err + } + realResult.Success = success + return nil +} +func newHelloEchoArgs() interface{} { + return api.NewHelloEchoArgs() +} + +func newHelloEchoResult() interface{} { + return api.NewHelloEchoResult() +} + +type kClient struct { + c client.Client +} + +func newServiceClient(c client.Client) *kClient { + return &kClient{ + c: c, + } +} + +func (p *kClient) Echo(ctx context.Context, req *api.Request) (r *api.Response, err error) { + var _args api.HelloEchoArgs + _args.Req = req + var _result api.HelloEchoResult + if err = p.c.Call(ctx, "echo", &_args, &_result); err != nil { + return + } + return _result.GetSuccess(), nil +} diff --git a/v2/example/hello/kitex_gen/api/hello/invoker.go b/v2/example/hello/kitex_gen/api/hello/invoker.go new file mode 100644 index 0000000..0a5fa46 --- /dev/null +++ b/v2/example/hello/kitex_gen/api/hello/invoker.go @@ -0,0 +1,24 @@ +// Code generated by Kitex v0.5.1. DO NOT EDIT. + +package hello + +import ( + server "github.com/cloudwego/kitex/server" + api "github.com/kitex-contrib/registry-nacos/v2/example/hello/kitex_gen/api" +) + +// NewInvoker creates a server.Invoker with the given handler and options. +func NewInvoker(handler api.Hello, opts ...server.Option) server.Invoker { + var options []server.Option + + options = append(options, opts...) + + s := server.NewInvoker(options...) + if err := s.RegisterService(serviceInfo(), handler); err != nil { + panic(err) + } + if err := s.Init(); err != nil { + panic(err) + } + return s +} diff --git a/v2/example/hello/kitex_gen/api/hello/server.go b/v2/example/hello/kitex_gen/api/hello/server.go new file mode 100644 index 0000000..7b319ba --- /dev/null +++ b/v2/example/hello/kitex_gen/api/hello/server.go @@ -0,0 +1,20 @@ +// Code generated by Kitex v0.5.1. DO NOT EDIT. +package hello + +import ( + server "github.com/cloudwego/kitex/server" + api "github.com/kitex-contrib/registry-nacos/v2/example/hello/kitex_gen/api" +) + +// NewServer creates a server.Server with the given handler and options. +func NewServer(handler api.Hello, opts ...server.Option) server.Server { + var options []server.Option + + options = append(options, opts...) + + svr := server.NewServer(options...) + if err := svr.RegisterService(serviceInfo(), handler); err != nil { + panic(err) + } + return svr +} diff --git a/v2/example/hello/kitex_gen/api/k-consts.go b/v2/example/hello/kitex_gen/api/k-consts.go new file mode 100644 index 0000000..d5cc5ba --- /dev/null +++ b/v2/example/hello/kitex_gen/api/k-consts.go @@ -0,0 +1,4 @@ +package api + +// KitexUnusedProtection is used to prevent 'imported and not used' error. +var KitexUnusedProtection = struct{}{} diff --git a/v2/example/hello/kitex_gen/api/k-hello.go b/v2/example/hello/kitex_gen/api/k-hello.go new file mode 100644 index 0000000..6c2e978 --- /dev/null +++ b/v2/example/hello/kitex_gen/api/k-hello.go @@ -0,0 +1,550 @@ +// Code generated by Kitex v0.5.1. DO NOT EDIT. + +package api + +import ( + "bytes" + "fmt" + "reflect" + "strings" + + "github.com/apache/thrift/lib/go/thrift" + + "github.com/cloudwego/kitex/pkg/protocol/bthrift" +) + +// unused protection +var ( + _ = fmt.Formatter(nil) + _ = (*bytes.Buffer)(nil) + _ = (*strings.Builder)(nil) + _ = reflect.Type(nil) + _ = thrift.TProtocol(nil) + _ = bthrift.BinaryWriter(nil) +) + +func (p *Request) FastRead(buf []byte) (int, error) { + var err error + var offset int + var l int + var fieldTypeId thrift.TType + var fieldId int16 + _, l, err = bthrift.Binary.ReadStructBegin(buf) + offset += l + if err != nil { + goto ReadStructBeginError + } + + for { + _, fieldTypeId, fieldId, l, err = bthrift.Binary.ReadFieldBegin(buf[offset:]) + offset += l + if err != nil { + goto ReadFieldBeginError + } + if fieldTypeId == thrift.STOP { + break + } + switch fieldId { + case 1: + if fieldTypeId == thrift.STRING { + l, err = p.FastReadField1(buf[offset:]) + offset += l + if err != nil { + goto ReadFieldError + } + } else { + l, err = bthrift.Binary.Skip(buf[offset:], fieldTypeId) + offset += l + if err != nil { + goto SkipFieldError + } + } + default: + l, err = bthrift.Binary.Skip(buf[offset:], fieldTypeId) + offset += l + if err != nil { + goto SkipFieldError + } + } + + l, err = bthrift.Binary.ReadFieldEnd(buf[offset:]) + offset += l + if err != nil { + goto ReadFieldEndError + } + } + l, err = bthrift.Binary.ReadStructEnd(buf[offset:]) + offset += l + if err != nil { + goto ReadStructEndError + } + + return offset, nil +ReadStructBeginError: + return offset, thrift.PrependError(fmt.Sprintf("%T read struct begin error: ", p), err) +ReadFieldBeginError: + return offset, thrift.PrependError(fmt.Sprintf("%T read field %d begin error: ", p, fieldId), err) +ReadFieldError: + return offset, thrift.PrependError(fmt.Sprintf("%T read field %d '%s' error: ", p, fieldId, fieldIDToName_Request[fieldId]), err) +SkipFieldError: + return offset, thrift.PrependError(fmt.Sprintf("%T field %d skip type %d error: ", p, fieldId, fieldTypeId), err) +ReadFieldEndError: + return offset, thrift.PrependError(fmt.Sprintf("%T read field end error", p), err) +ReadStructEndError: + return offset, thrift.PrependError(fmt.Sprintf("%T read struct end error: ", p), err) +} + +func (p *Request) FastReadField1(buf []byte) (int, error) { + offset := 0 + + if v, l, err := bthrift.Binary.ReadString(buf[offset:]); err != nil { + return offset, err + } else { + offset += l + + p.Message = v + + } + return offset, nil +} + +// for compatibility +func (p *Request) FastWrite(buf []byte) int { + return 0 +} + +func (p *Request) FastWriteNocopy(buf []byte, binaryWriter bthrift.BinaryWriter) int { + offset := 0 + offset += bthrift.Binary.WriteStructBegin(buf[offset:], "Request") + if p != nil { + offset += p.fastWriteField1(buf[offset:], binaryWriter) + } + offset += bthrift.Binary.WriteFieldStop(buf[offset:]) + offset += bthrift.Binary.WriteStructEnd(buf[offset:]) + return offset +} + +func (p *Request) BLength() int { + l := 0 + l += bthrift.Binary.StructBeginLength("Request") + if p != nil { + l += p.field1Length() + } + l += bthrift.Binary.FieldStopLength() + l += bthrift.Binary.StructEndLength() + return l +} + +func (p *Request) fastWriteField1(buf []byte, binaryWriter bthrift.BinaryWriter) int { + offset := 0 + offset += bthrift.Binary.WriteFieldBegin(buf[offset:], "message", thrift.STRING, 1) + offset += bthrift.Binary.WriteStringNocopy(buf[offset:], binaryWriter, p.Message) + + offset += bthrift.Binary.WriteFieldEnd(buf[offset:]) + return offset +} + +func (p *Request) field1Length() int { + l := 0 + l += bthrift.Binary.FieldBeginLength("message", thrift.STRING, 1) + l += bthrift.Binary.StringLengthNocopy(p.Message) + + l += bthrift.Binary.FieldEndLength() + return l +} + +func (p *Response) FastRead(buf []byte) (int, error) { + var err error + var offset int + var l int + var fieldTypeId thrift.TType + var fieldId int16 + _, l, err = bthrift.Binary.ReadStructBegin(buf) + offset += l + if err != nil { + goto ReadStructBeginError + } + + for { + _, fieldTypeId, fieldId, l, err = bthrift.Binary.ReadFieldBegin(buf[offset:]) + offset += l + if err != nil { + goto ReadFieldBeginError + } + if fieldTypeId == thrift.STOP { + break + } + switch fieldId { + case 1: + if fieldTypeId == thrift.STRING { + l, err = p.FastReadField1(buf[offset:]) + offset += l + if err != nil { + goto ReadFieldError + } + } else { + l, err = bthrift.Binary.Skip(buf[offset:], fieldTypeId) + offset += l + if err != nil { + goto SkipFieldError + } + } + default: + l, err = bthrift.Binary.Skip(buf[offset:], fieldTypeId) + offset += l + if err != nil { + goto SkipFieldError + } + } + + l, err = bthrift.Binary.ReadFieldEnd(buf[offset:]) + offset += l + if err != nil { + goto ReadFieldEndError + } + } + l, err = bthrift.Binary.ReadStructEnd(buf[offset:]) + offset += l + if err != nil { + goto ReadStructEndError + } + + return offset, nil +ReadStructBeginError: + return offset, thrift.PrependError(fmt.Sprintf("%T read struct begin error: ", p), err) +ReadFieldBeginError: + return offset, thrift.PrependError(fmt.Sprintf("%T read field %d begin error: ", p, fieldId), err) +ReadFieldError: + return offset, thrift.PrependError(fmt.Sprintf("%T read field %d '%s' error: ", p, fieldId, fieldIDToName_Response[fieldId]), err) +SkipFieldError: + return offset, thrift.PrependError(fmt.Sprintf("%T field %d skip type %d error: ", p, fieldId, fieldTypeId), err) +ReadFieldEndError: + return offset, thrift.PrependError(fmt.Sprintf("%T read field end error", p), err) +ReadStructEndError: + return offset, thrift.PrependError(fmt.Sprintf("%T read struct end error: ", p), err) +} + +func (p *Response) FastReadField1(buf []byte) (int, error) { + offset := 0 + + if v, l, err := bthrift.Binary.ReadString(buf[offset:]); err != nil { + return offset, err + } else { + offset += l + + p.Message = v + + } + return offset, nil +} + +// for compatibility +func (p *Response) FastWrite(buf []byte) int { + return 0 +} + +func (p *Response) FastWriteNocopy(buf []byte, binaryWriter bthrift.BinaryWriter) int { + offset := 0 + offset += bthrift.Binary.WriteStructBegin(buf[offset:], "Response") + if p != nil { + offset += p.fastWriteField1(buf[offset:], binaryWriter) + } + offset += bthrift.Binary.WriteFieldStop(buf[offset:]) + offset += bthrift.Binary.WriteStructEnd(buf[offset:]) + return offset +} + +func (p *Response) BLength() int { + l := 0 + l += bthrift.Binary.StructBeginLength("Response") + if p != nil { + l += p.field1Length() + } + l += bthrift.Binary.FieldStopLength() + l += bthrift.Binary.StructEndLength() + return l +} + +func (p *Response) fastWriteField1(buf []byte, binaryWriter bthrift.BinaryWriter) int { + offset := 0 + offset += bthrift.Binary.WriteFieldBegin(buf[offset:], "message", thrift.STRING, 1) + offset += bthrift.Binary.WriteStringNocopy(buf[offset:], binaryWriter, p.Message) + + offset += bthrift.Binary.WriteFieldEnd(buf[offset:]) + return offset +} + +func (p *Response) field1Length() int { + l := 0 + l += bthrift.Binary.FieldBeginLength("message", thrift.STRING, 1) + l += bthrift.Binary.StringLengthNocopy(p.Message) + + l += bthrift.Binary.FieldEndLength() + return l +} + +func (p *HelloEchoArgs) FastRead(buf []byte) (int, error) { + var err error + var offset int + var l int + var fieldTypeId thrift.TType + var fieldId int16 + _, l, err = bthrift.Binary.ReadStructBegin(buf) + offset += l + if err != nil { + goto ReadStructBeginError + } + + for { + _, fieldTypeId, fieldId, l, err = bthrift.Binary.ReadFieldBegin(buf[offset:]) + offset += l + if err != nil { + goto ReadFieldBeginError + } + if fieldTypeId == thrift.STOP { + break + } + switch fieldId { + case 1: + if fieldTypeId == thrift.STRUCT { + l, err = p.FastReadField1(buf[offset:]) + offset += l + if err != nil { + goto ReadFieldError + } + } else { + l, err = bthrift.Binary.Skip(buf[offset:], fieldTypeId) + offset += l + if err != nil { + goto SkipFieldError + } + } + default: + l, err = bthrift.Binary.Skip(buf[offset:], fieldTypeId) + offset += l + if err != nil { + goto SkipFieldError + } + } + + l, err = bthrift.Binary.ReadFieldEnd(buf[offset:]) + offset += l + if err != nil { + goto ReadFieldEndError + } + } + l, err = bthrift.Binary.ReadStructEnd(buf[offset:]) + offset += l + if err != nil { + goto ReadStructEndError + } + + return offset, nil +ReadStructBeginError: + return offset, thrift.PrependError(fmt.Sprintf("%T read struct begin error: ", p), err) +ReadFieldBeginError: + return offset, thrift.PrependError(fmt.Sprintf("%T read field %d begin error: ", p, fieldId), err) +ReadFieldError: + return offset, thrift.PrependError(fmt.Sprintf("%T read field %d '%s' error: ", p, fieldId, fieldIDToName_HelloEchoArgs[fieldId]), err) +SkipFieldError: + return offset, thrift.PrependError(fmt.Sprintf("%T field %d skip type %d error: ", p, fieldId, fieldTypeId), err) +ReadFieldEndError: + return offset, thrift.PrependError(fmt.Sprintf("%T read field end error", p), err) +ReadStructEndError: + return offset, thrift.PrependError(fmt.Sprintf("%T read struct end error: ", p), err) +} + +func (p *HelloEchoArgs) FastReadField1(buf []byte) (int, error) { + offset := 0 + + tmp := NewRequest() + if l, err := tmp.FastRead(buf[offset:]); err != nil { + return offset, err + } else { + offset += l + } + p.Req = tmp + return offset, nil +} + +// for compatibility +func (p *HelloEchoArgs) FastWrite(buf []byte) int { + return 0 +} + +func (p *HelloEchoArgs) FastWriteNocopy(buf []byte, binaryWriter bthrift.BinaryWriter) int { + offset := 0 + offset += bthrift.Binary.WriteStructBegin(buf[offset:], "echo_args") + if p != nil { + offset += p.fastWriteField1(buf[offset:], binaryWriter) + } + offset += bthrift.Binary.WriteFieldStop(buf[offset:]) + offset += bthrift.Binary.WriteStructEnd(buf[offset:]) + return offset +} + +func (p *HelloEchoArgs) BLength() int { + l := 0 + l += bthrift.Binary.StructBeginLength("echo_args") + if p != nil { + l += p.field1Length() + } + l += bthrift.Binary.FieldStopLength() + l += bthrift.Binary.StructEndLength() + return l +} + +func (p *HelloEchoArgs) fastWriteField1(buf []byte, binaryWriter bthrift.BinaryWriter) int { + offset := 0 + offset += bthrift.Binary.WriteFieldBegin(buf[offset:], "req", thrift.STRUCT, 1) + offset += p.Req.FastWriteNocopy(buf[offset:], binaryWriter) + offset += bthrift.Binary.WriteFieldEnd(buf[offset:]) + return offset +} + +func (p *HelloEchoArgs) field1Length() int { + l := 0 + l += bthrift.Binary.FieldBeginLength("req", thrift.STRUCT, 1) + l += p.Req.BLength() + l += bthrift.Binary.FieldEndLength() + return l +} + +func (p *HelloEchoResult) FastRead(buf []byte) (int, error) { + var err error + var offset int + var l int + var fieldTypeId thrift.TType + var fieldId int16 + _, l, err = bthrift.Binary.ReadStructBegin(buf) + offset += l + if err != nil { + goto ReadStructBeginError + } + + for { + _, fieldTypeId, fieldId, l, err = bthrift.Binary.ReadFieldBegin(buf[offset:]) + offset += l + if err != nil { + goto ReadFieldBeginError + } + if fieldTypeId == thrift.STOP { + break + } + switch fieldId { + case 0: + if fieldTypeId == thrift.STRUCT { + l, err = p.FastReadField0(buf[offset:]) + offset += l + if err != nil { + goto ReadFieldError + } + } else { + l, err = bthrift.Binary.Skip(buf[offset:], fieldTypeId) + offset += l + if err != nil { + goto SkipFieldError + } + } + default: + l, err = bthrift.Binary.Skip(buf[offset:], fieldTypeId) + offset += l + if err != nil { + goto SkipFieldError + } + } + + l, err = bthrift.Binary.ReadFieldEnd(buf[offset:]) + offset += l + if err != nil { + goto ReadFieldEndError + } + } + l, err = bthrift.Binary.ReadStructEnd(buf[offset:]) + offset += l + if err != nil { + goto ReadStructEndError + } + + return offset, nil +ReadStructBeginError: + return offset, thrift.PrependError(fmt.Sprintf("%T read struct begin error: ", p), err) +ReadFieldBeginError: + return offset, thrift.PrependError(fmt.Sprintf("%T read field %d begin error: ", p, fieldId), err) +ReadFieldError: + return offset, thrift.PrependError(fmt.Sprintf("%T read field %d '%s' error: ", p, fieldId, fieldIDToName_HelloEchoResult[fieldId]), err) +SkipFieldError: + return offset, thrift.PrependError(fmt.Sprintf("%T field %d skip type %d error: ", p, fieldId, fieldTypeId), err) +ReadFieldEndError: + return offset, thrift.PrependError(fmt.Sprintf("%T read field end error", p), err) +ReadStructEndError: + return offset, thrift.PrependError(fmt.Sprintf("%T read struct end error: ", p), err) +} + +func (p *HelloEchoResult) FastReadField0(buf []byte) (int, error) { + offset := 0 + + tmp := NewResponse() + if l, err := tmp.FastRead(buf[offset:]); err != nil { + return offset, err + } else { + offset += l + } + p.Success = tmp + return offset, nil +} + +// for compatibility +func (p *HelloEchoResult) FastWrite(buf []byte) int { + return 0 +} + +func (p *HelloEchoResult) FastWriteNocopy(buf []byte, binaryWriter bthrift.BinaryWriter) int { + offset := 0 + offset += bthrift.Binary.WriteStructBegin(buf[offset:], "echo_result") + if p != nil { + offset += p.fastWriteField0(buf[offset:], binaryWriter) + } + offset += bthrift.Binary.WriteFieldStop(buf[offset:]) + offset += bthrift.Binary.WriteStructEnd(buf[offset:]) + return offset +} + +func (p *HelloEchoResult) BLength() int { + l := 0 + l += bthrift.Binary.StructBeginLength("echo_result") + if p != nil { + l += p.field0Length() + } + l += bthrift.Binary.FieldStopLength() + l += bthrift.Binary.StructEndLength() + return l +} + +func (p *HelloEchoResult) fastWriteField0(buf []byte, binaryWriter bthrift.BinaryWriter) int { + offset := 0 + if p.IsSetSuccess() { + offset += bthrift.Binary.WriteFieldBegin(buf[offset:], "success", thrift.STRUCT, 0) + offset += p.Success.FastWriteNocopy(buf[offset:], binaryWriter) + offset += bthrift.Binary.WriteFieldEnd(buf[offset:]) + } + return offset +} + +func (p *HelloEchoResult) field0Length() int { + l := 0 + if p.IsSetSuccess() { + l += bthrift.Binary.FieldBeginLength("success", thrift.STRUCT, 0) + l += p.Success.BLength() + l += bthrift.Binary.FieldEndLength() + } + return l +} + +func (p *HelloEchoArgs) GetFirstArgument() interface{} { + return p.Req +} + +func (p *HelloEchoResult) GetResult() interface{} { + return p.Success +} diff --git a/v2/example/hello/kitex_info.yaml b/v2/example/hello/kitex_info.yaml new file mode 100644 index 0000000..903d79c --- /dev/null +++ b/v2/example/hello/kitex_info.yaml @@ -0,0 +1,3 @@ +kitexinfo: + ServiceName: 'hello' + ToolVersion: 'v0.5.1' diff --git a/v2/go.mod b/v2/go.mod index be80e60..cea9b67 100644 --- a/v2/go.mod +++ b/v2/go.mod @@ -3,15 +3,19 @@ module github.com/kitex-contrib/registry-nacos/v2 go 1.16 require ( - github.com/aliyun/alibaba-cloud-sdk-go v1.61.1800 // indirect + github.com/apache/thrift v0.13.0 github.com/cloudwego/kitex v0.8.0 + github.com/nacos-group/nacos-sdk-go/v2 v2.2.3 + github.com/stretchr/testify v1.8.4 + +) + +require ( + github.com/aliyun/alibaba-cloud-sdk-go v1.61.1800 // indirect github.com/golang/protobuf v1.5.3 // indirect - github.com/kitex-contrib/registry-nacos v0.1.1 github.com/kr/pretty v0.3.0 // indirect - github.com/nacos-group/nacos-sdk-go/v2 v2.2.3 github.com/prometheus/client_model v0.3.0 // indirect github.com/rogpeppe/go-internal v1.9.0 // indirect - github.com/stretchr/testify v1.8.3 golang.org/x/net v0.17.0 // indirect golang.org/x/sys v0.15.0 // indirect golang.org/x/text v0.14.0 // indirect diff --git a/v2/go.sum b/v2/go.sum index bfb3a5c..5583501 100644 --- a/v2/go.sum +++ b/v2/go.sum @@ -405,7 +405,6 @@ github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuy github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d/go.mod h1:rBZYJk541a8SKzHPHnH3zbiI+7dagKZ0cgpgrD7Fyho= -github.com/aliyun/alibaba-cloud-sdk-go v1.61.18/go.mod h1:v8ESoHo4SyHmuB4b1tJqDHxfTGEciD+yhvOU/5s1Rfk= github.com/aliyun/alibaba-cloud-sdk-go v1.61.1704/go.mod h1:RcDobYh8k5VP6TNybz9m++gL3ijVI5wueVr0EM10VsU= github.com/aliyun/alibaba-cloud-sdk-go v1.61.1800 h1:ie/8RxBOfKZWcrbYSJi2Z8uX8TcOlSMwPlEJh83OeOw= github.com/aliyun/alibaba-cloud-sdk-go v1.61.1800/go.mod h1:RcDobYh8k5VP6TNybz9m++gL3ijVI5wueVr0EM10VsU= @@ -426,7 +425,6 @@ github.com/buger/jsonparser v1.1.1/go.mod h1:6RYKKt7H4d4+iWqouImQ9R2FZql3VbhNgx2 github.com/bytedance/gopkg v0.0.0-20220413063733-65bf48ffb3a7/go.mod h1:2ZlV9BaUH4+NXIBF0aMdKKAnHTzqH+iMU4KUjAbL23Q= github.com/bytedance/gopkg v0.0.0-20220509134931-d1878f638986/go.mod h1:2ZlV9BaUH4+NXIBF0aMdKKAnHTzqH+iMU4KUjAbL23Q= github.com/bytedance/gopkg v0.0.0-20220531084716-665b4f21126f/go.mod h1:2ZlV9BaUH4+NXIBF0aMdKKAnHTzqH+iMU4KUjAbL23Q= -github.com/bytedance/gopkg v0.0.0-20220817015305-b879a72dc90f/go.mod h1:2ZlV9BaUH4+NXIBF0aMdKKAnHTzqH+iMU4KUjAbL23Q= github.com/bytedance/gopkg v0.0.0-20230531144706-a12972768317/go.mod h1:FtQG3YbQG9L/91pbKSw787yBQPutC+457AvDW77fgUQ= github.com/bytedance/gopkg v0.0.0-20230728082804-614d0af6619b h1:R6PWoQtxEMpWJPHnpci+9LgFxCS7iJCfOGBvCgZeTKI= github.com/bytedance/gopkg v0.0.0-20230728082804-614d0af6619b/go.mod h1:FtQG3YbQG9L/91pbKSw787yBQPutC+457AvDW77fgUQ= @@ -482,7 +480,6 @@ github.com/cloudwego/frugal v0.1.12 h1:/AFrjnMVOBBd6d5Vn59bU2IhlOP2vs/jQCsJ2seJB github.com/cloudwego/frugal v0.1.12/go.mod h1:zFBA63ne4+Tz4qayRZFZf+ZVwGqTzb+1Xe3ZDCq+Wfc= github.com/cloudwego/kitex v0.3.2/go.mod h1:/XD07VpUD9VQWmmoepASgZ6iw//vgWikVA9MpzLC5i0= github.com/cloudwego/kitex v0.4.4/go.mod h1:3FcH5h9Qw+dhRljSzuGSpWuThttA8DvK0BsL7HUYydo= -github.com/cloudwego/kitex v0.5.1/go.mod h1:B3oH+MTQ7/wBL3BrCAMlyeyjAqOpi4pRzCvQcXN7RgM= github.com/cloudwego/kitex v0.6.1/go.mod h1:zI1GBrjT0qloTikcCfQTgxg3Ws+yQMyaChEEOcGNUvA= github.com/cloudwego/kitex v0.8.0 h1:eL6Xb2vnHfOjvDqmPsvCuheDo513lOc1HG6hSHGiFyM= github.com/cloudwego/kitex v0.8.0/go.mod h1:5o98nYKp8GwauvA1hhJwTA3YQcPa8Nu5tx+2j+JjwoM= @@ -490,14 +487,12 @@ github.com/cloudwego/localsession v0.0.2 h1:N9/IDtCPj1fCL9bCTP+DbXx3f40YjVYWcwkJ github.com/cloudwego/localsession v0.0.2/go.mod h1:kiJxmvAcy4PLgKtEnPS5AXed3xCiXcs7Z+KBHP72Wv8= github.com/cloudwego/netpoll v0.2.4/go.mod h1:1T2WVuQ+MQw6h6DpE45MohSvDTKdy2DlzCx2KsnPI4E= github.com/cloudwego/netpoll v0.3.1/go.mod h1:1T2WVuQ+MQw6h6DpE45MohSvDTKdy2DlzCx2KsnPI4E= -github.com/cloudwego/netpoll v0.3.2/go.mod h1:xVefXptcyheopwNDZjDPcfU6kIjZXZ4nY550k1yH9eQ= github.com/cloudwego/netpoll v0.4.0/go.mod h1:xVefXptcyheopwNDZjDPcfU6kIjZXZ4nY550k1yH9eQ= github.com/cloudwego/netpoll v0.5.1 h1:zDUF7xF0C97I10fGlQFJ4jg65khZZMUvSu/TWX44Ohc= github.com/cloudwego/netpoll v0.5.1/go.mod h1:xVefXptcyheopwNDZjDPcfU6kIjZXZ4nY550k1yH9eQ= github.com/cloudwego/thriftgo v0.1.2/go.mod h1:LzeafuLSiHA9JTiWC8TIMIq64iadeObgRUhmVG1OC/w= github.com/cloudwego/thriftgo v0.2.4/go.mod h1:8i9AF5uDdWHGqzUhXDlubCjx4MEfKvWXGQlMWyR0tM4= github.com/cloudwego/thriftgo v0.2.7/go.mod h1:8i9AF5uDdWHGqzUhXDlubCjx4MEfKvWXGQlMWyR0tM4= -github.com/cloudwego/thriftgo v0.2.8/go.mod h1:dAyXHEmKXo0LfMCrblVEY3mUZsdeuA5+i0vF5f09j7E= github.com/cloudwego/thriftgo v0.2.11/go.mod h1:dAyXHEmKXo0LfMCrblVEY3mUZsdeuA5+i0vF5f09j7E= github.com/cloudwego/thriftgo v0.3.3 h1:dqdB5wXnw9icTXdb+/13dVZhSEf83UVsa75Yr05vTVE= github.com/cloudwego/thriftgo v0.3.3/go.mod h1:29ukiySoAMd0vXMYIduAY9dph/7dmChvOS11YLotFb8= @@ -537,7 +532,6 @@ github.com/fatih/structtag v1.2.0/go.mod h1:mBJUNpUnHmRKrKlQQlmCrh5PuhftFbNv8Ys4 github.com/fogleman/gg v1.2.1-0.20190220221249-0403632d5b90/go.mod h1:R/bRT+9gY/C5z7JzPU0zXsXHKM4/ayA+zqcVNZzPa1k= github.com/fogleman/gg v1.3.0/go.mod h1:R/bRT+9gY/C5z7JzPU0zXsXHKM4/ayA+zqcVNZzPa1k= github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= -github.com/go-errors/errors v1.0.1/go.mod h1:f4zRHt4oKfwPJE5k8C9vpYG+aDHdBFUsgrm6/TyX73Q= github.com/go-fonts/dejavu v0.1.0/go.mod h1:4Wt4I4OU2Nq9asgDCteaAaWZOV24E+0/Pwo0gppep4g= github.com/go-fonts/latin-modern v0.2.0/go.mod h1:rQVLdDMK+mK1xscDwsqM5J8U2jrRa3T0ecnM9pNujks= github.com/go-fonts/liberation v0.1.1/go.mod h1:K6qoJYypsmfVjWg8KOVDQhLc8UDgIK2HYqyqAO9z7GY= @@ -690,8 +684,6 @@ github.com/jung-kurt/gofpdf v1.0.0/go.mod h1:7Id9E/uU8ce6rXgefFLlgrJj/GYY22cpxn+ github.com/jung-kurt/gofpdf v1.0.3-0.20190309125859-24315acbbda5/go.mod h1:7Id9E/uU8ce6rXgefFLlgrJj/GYY22cpxn+r32jIOes= github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8= github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= -github.com/kitex-contrib/registry-nacos v0.1.1 h1:foJ78wn8KLiW0LXNlQUhQv2kFKt9nYPsVcZlq6qJ/48= -github.com/kitex-contrib/registry-nacos v0.1.1/go.mod h1:XnDe4b1xMezdke2n53naF7oikLNjwKQZdh3WrM5VHnQ= github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= github.com/klauspost/cpuid/v2 v2.1.0/go.mod h1:RVVoqg1df56z8g3pUjL/3lE5UfnlrJX8tyFgg4nqhuY= github.com/klauspost/cpuid/v2 v2.2.4 h1:acbojRNwl3o09bUq+yDCtZFc1aiwaAAxtcn8YkZXnvk= @@ -725,8 +717,6 @@ github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9G github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= -github.com/nacos-group/nacos-sdk-go v1.1.4 h1:qyrZ7HTWM4aeymFfqnbgNRERh7TWuER10pCB7ddRcTY= -github.com/nacos-group/nacos-sdk-go v1.1.4/go.mod h1:cBv9wy5iObs7khOqov1ERFQrCuTR4ILpgaiaVMxEmGI= github.com/nacos-group/nacos-sdk-go/v2 v2.2.3 h1:sUQx4f1bXDeeOOEQZjGAitzxYApbYY9fVDbxVCaBW+I= github.com/nacos-group/nacos-sdk-go/v2 v2.2.3/go.mod h1:UL4U89WYdnyajgKJUMpuT1Rr6iNmbjrxOO40JRgtA00= github.com/nishanths/predeclared v0.0.0-20200524104333-86fad755b4d3/go.mod h1:nt3d53pc1VYcphSCIaYAJtnPYnr3Zyn8fMq2wvPGPso= @@ -780,7 +770,6 @@ github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6Mwd github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88= github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykElWQ6/NYmHa3jpm/yHnI4xSofP+UP6SpjHcSeM= github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc= -github.com/smartystreets/goconvey v0.0.0-20190330032615-68dc04aab96a/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA= github.com/smartystreets/goconvey v1.6.4 h1:fv0U8FUIMPNf1L9lnHLvLhgicrIVChEkdzIKYqbNC9s= github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA= github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= @@ -801,8 +790,8 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= -github.com/stretchr/testify v1.8.3 h1:RP3t2pwF7cMEbC1dqtB6poj3niw/9gnV4Cjg5oW5gtY= -github.com/stretchr/testify v1.8.3/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/thrift-iterator/go v0.0.0-20190402154806-9b5a67519118/go.mod h1:60PRwE/TCI1UqLvn8v2pwAf6+yzTPLP/Ji5xaesWDqk= github.com/tidwall/gjson v1.9.3 h1:hqzS9wAHMO+KVBBkLxYdkEeeFHuqr95GfClRLKlgK0E= github.com/tidwall/gjson v1.9.3/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= @@ -832,16 +821,12 @@ go.opencensus.io v0.23.0/go.mod h1:XItmlyltB5F7CS4xOC1DcqMoFqwtC6OG2xF7mCv7P7E= go.opencensus.io v0.24.0/go.mod h1:vNK8G9p7aAivkbmorf4v+7Hgx+Zs0yY+0fOtgBfjQKo= go.opentelemetry.io/proto/otlp v0.7.0/go.mod h1:PqfVotwruBrMGOCsRd/89rSnXhoiJIqeYNgFYFoEGnI= go.opentelemetry.io/proto/otlp v0.15.0/go.mod h1:H7XAot3MsfNsj7EXtrA2q5xSNQ10UqI405h3+duxN4U= -go.uber.org/atomic v1.6.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ= go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw= go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= go.uber.org/goleak v1.1.11 h1:wy28qYRKZgnJTxGxvye5/wgWr1EKjmUDGYox5mGlRlI= go.uber.org/goleak v1.1.11/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ= -go.uber.org/multierr v1.5.0/go.mod h1:FeouvMocqHpRaaGuG9EjoKcStLC43Zu/fmqdUMPcKYU= go.uber.org/multierr v1.6.0 h1:y6IPFStTAIT5Ytl7/XYmHvzXQ7S3g/IeZW9hyZ5thw4= go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU= -go.uber.org/tools v0.0.0-20190618225709-2cfd321de3ee/go.mod h1:vJERXedbb3MVM5f9Ejo0C68/HhF8uaILCdgjnY+goOA= -go.uber.org/zap v1.15.0/go.mod h1:Mb2vm2krFEG5DV0W9qcHBYFtp/Wku1cvYaqPsS/WYfc= go.uber.org/zap v1.21.0 h1:WefMeulhovoZ2sYXz7st6K0sLj7bBhpiFaud4r4zST8= go.uber.org/zap v1.21.0/go.mod h1:wjWOCqI0f2ZZrJF/UufIOkiC8ii6tm1iqIsLo76RfJw= golang.org/x/arch v0.0.0-20201008161808-52c3e6f60cff/go.mod h1:flIaEI6LNU6xOCD5PaJvn9wGP0agmIOqjrtsKGRguv4= @@ -1163,8 +1148,6 @@ golang.org/x/tools v0.0.0-20190816200558-6889da9d5479/go.mod h1:b+2E5dAYhXwXZwtn golang.org/x/tools v0.0.0-20190911174233-4f2ddba30aff/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20190927191325-030b2cf1153e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20191012152004-8de300cfc20a/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= -golang.org/x/tools v0.0.0-20191029041327-9cc4af7d6b2c/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= -golang.org/x/tools v0.0.0-20191029190741-b9c20aec41a5/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20191113191852-77e3bb0ad9e7/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20191115202509-3a792d9c32b2/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= @@ -1469,7 +1452,6 @@ gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8 gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= -gopkg.in/ini.v1 v1.42.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= gopkg.in/ini.v1 v1.66.2 h1:XfR1dOYubytKy4Shzc2LHrrGhU0lDCfDGG1yLPmpgsI= gopkg.in/ini.v1 v1.66.2/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= gopkg.in/natefinch/lumberjack.v2 v2.0.0 h1:1Lc07Kr7qY4U2YPouBjpCLxpiyxIVoxqXgkXLknAOE8= diff --git a/v2/nacos/env_test.go b/v2/nacos/env_test.go index 3d2fbb1..d44b140 100644 --- a/v2/nacos/env_test.go +++ b/v2/nacos/env_test.go @@ -20,13 +20,6 @@ import ( "github.com/stretchr/testify/assert" ) -// TestEnvFunc test env func -func TestEnvFunc(t *testing.T) { - assert.Equal(t, int64(8848), NacosPort()) - assert.Equal(t, "127.0.0.1", NacosAddr()) - assert.Equal(t, "public", NacosNameSpaceId()) -} - func TestParseTags(t *testing.T) { assert.Equal(t, parseTags("k1=v1,k2=v2"), map[string]string{ "k1": "v1", diff --git a/v2/registry/registry_test.go b/v2/registry/registry_test.go index 76e49fc..d02643a 100644 --- a/v2/registry/registry_test.go +++ b/v2/registry/registry_test.go @@ -15,47 +15,48 @@ package registry import ( + "context" "net" "testing" "time" + "github.com/cloudwego/kitex/client" + "github.com/cloudwego/kitex/pkg/rpcinfo" + "github.com/cloudwego/kitex/pkg/utils" + "github.com/cloudwego/kitex/server" + "github.com/kitex-contrib/registry-nacos/v2/example/hello/kitex_gen/api" + "github.com/kitex-contrib/registry-nacos/v2/example/hello/kitex_gen/api/hello" + "github.com/kitex-contrib/registry-nacos/v2/resolver" + "github.com/stretchr/testify/assert" + "github.com/cloudwego/kitex/pkg/registry" "github.com/nacos-group/nacos-sdk-go/v2/clients" "github.com/nacos-group/nacos-sdk-go/v2/clients/naming_client" "github.com/nacos-group/nacos-sdk-go/v2/common/constant" "github.com/nacos-group/nacos-sdk-go/v2/vo" - "github.com/stretchr/testify/assert" ) func getNacosClient() (naming_client.INamingClient, error) { + // create ServerConfig sc := []constant.ServerConfig{ - *constant.NewServerConfig("127.0.0.1", 8848), + *constant.NewServerConfig("127.0.0.1", 8848, constant.WithContextPath("/nacos"), constant.WithScheme("http")), } - cc := constant.ClientConfig{ - NamespaceId: "public", - TimeoutMs: 5000, - NotLoadCacheAtStart: true, - CacheDir: "/tmp/nacos/cache", - } + // create ClientConfig + cc := *constant.NewClientConfig( + constant.WithTimeoutMs(50000), + constant.WithUpdateCacheWhenEmpty(true), + constant.WithNotLoadCacheAtStart(true), + ) - return clients.NewNamingClient( + // create naming client + newClient, err := clients.NewNamingClient( vo.NacosClientParam{ ClientConfig: &cc, ServerConfigs: sc, }, ) -} - -// TestNewNacosRegistry test new a nacos registry -func TestNewNacosRegistry(t *testing.T) { - client, err := getNacosClient() - if err != nil { - t.Errorf("err:%v", err) - return - } - got := NewNacosRegistry(client, WithCluster("DEFAULT"), WithGroup("DEFAULT_GROUP")) - assert.NotNil(t, got) + return newClient, err } // TestNewNacosRegistry test registry a service @@ -142,89 +143,6 @@ func TestNacosRegistryDeregister(t *testing.T) { } } -// TestNacosMultipleInstances test registry multiple service,then deregister one -func TestNacosMultipleInstances(t *testing.T) { - var ( - svcName = "MultipleInstances" - clusterName = "TheCluster" - groupName = "TheGroup" - ) - client, err := getNacosClient() - if err != nil { - t.Errorf("err:%v", err) - return - } - time.Sleep(time.Second) - got := NewNacosRegistry(client, WithCluster(clusterName), WithGroup(groupName)) - if !assert.NotNil(t, got) { - t.Errorf("err: new registry fail") - return - } - time.Sleep(time.Second) - err = got.Register(®istry.Info{ - ServiceName: svcName, - Addr: &net.TCPAddr{IP: net.IPv4(127, 0, 0, 1), Port: 8081}, - }) - assert.Nil(t, err) - if err != nil { - t.Errorf("err:%v", err) - return - } - - err = got.Register(®istry.Info{ - ServiceName: svcName, - Addr: &net.TCPAddr{IP: net.IPv4(127, 0, 0, 1), Port: 8082}, - }) - assert.Nil(t, err) - if err != nil { - t.Errorf("err:%v", err) - return - } - - err = got.Register(®istry.Info{ - ServiceName: svcName, - Addr: &net.TCPAddr{IP: net.IPv4(127, 0, 0, 1), Port: 8083}, - }) - assert.Nil(t, err) - - time.Sleep(time.Second) - res, err := client.SelectAllInstances(vo.SelectAllInstancesParam{ - ServiceName: svcName, - GroupName: groupName, - Clusters: []string{clusterName}, - }) - assert.Nil(t, err) - assert.Equal(t, 3, len(res), "successful register not three") - - time.Sleep(time.Second) - err = got.Deregister(®istry.Info{ - ServiceName: svcName, - Addr: &net.TCPAddr{IP: net.IPv4(127, 0, 0, 1), Port: 8083}, - }) - assert.Nil(t, err) - - time.Sleep(time.Second * 3) - res, err = client.SelectAllInstances(vo.SelectAllInstancesParam{ - ServiceName: svcName, - GroupName: groupName, - Clusters: []string{clusterName}, - }) - assert.Nil(t, err) - if assert.Equal(t, 2, len(res), "deregister one, instances num should be two") { - for _, i := range res { - assert.Equal(t, "127.0.0.1", i.Ip) - assert.Contains(t, []uint64{8081, 8082}, i.Port) - } - } -} - -// TestNewDefaultNacosRegistry test new a default nacos registry -func TestNewDefaultNacosRegistry(t *testing.T) { - r, err := NewDefaultNacosRegistry() - assert.Nil(t, err) - assert.NotNil(t, r) -} - // TestNacosMultipleInstancesWithDefaultNacosRegistry use DefaultNacosRegistry to test registry multiple service,then deregister one func TestNacosMultipleInstancesWithDefaultNacosRegistry(t *testing.T) { var ( @@ -244,61 +162,34 @@ func TestNacosMultipleInstancesWithDefaultNacosRegistry(t *testing.T) { Addr: &net.TCPAddr{IP: net.IPv4(127, 0, 0, 1), Port: 8081}, }) assert.Nil(t, err) - if err != nil { - t.Errorf("err:%v", err) - return - } - - err = got.Register(®istry.Info{ - ServiceName: svcName, - Addr: &net.TCPAddr{IP: net.IPv4(127, 0, 0, 1), Port: 8082}, - }) - assert.Nil(t, err) - if err != nil { - t.Errorf("err:%v", err) - return - } - - err = got.Register(®istry.Info{ - ServiceName: svcName, - Addr: &net.TCPAddr{IP: net.IPv4(127, 0, 0, 1), Port: 8083}, - }) - assert.Nil(t, err) time.Sleep(time.Second * 1) client, err := getNacosClient() - if err != nil { - t.Errorf("err:%v", err) - return - } + assert.Nil(t, err) res, err := client.SelectAllInstances(vo.SelectAllInstancesParam{ ServiceName: svcName, GroupName: groupName, Clusters: []string{clusterName}, }) assert.Nil(t, err) - assert.Equal(t, 3, len(res), "successful register not three") + assert.Equal(t, 1, len(res)) time.Sleep(time.Second) err = got.Deregister(®istry.Info{ ServiceName: svcName, - Addr: &net.TCPAddr{IP: net.IPv4(127, 0, 0, 1), Port: 8083}, + Addr: &net.TCPAddr{IP: net.IPv4(127, 0, 0, 1), Port: 8081}, }) assert.Nil(t, err) time.Sleep(time.Second * 3) - res, err = client.SelectAllInstances(vo.SelectAllInstancesParam{ + res, err = client.SelectInstances(vo.SelectInstancesParam{ ServiceName: svcName, GroupName: groupName, Clusters: []string{clusterName}, + HealthyOnly: true, }) assert.Nil(t, err) - if assert.Equal(t, 2, len(res), "deregister one, instances num should be two") { - for _, i := range res { - assert.Equal(t, "127.0.0.1", i.Ip) - assert.Contains(t, []uint64{8081, 8082}, i.Port) - } - } + assert.Nil(t, res) } func TestMergeTags(t *testing.T) { @@ -326,3 +217,77 @@ func TestMergeTags(t *testing.T) { "k4": "v4", }) } + +type HelloImpl struct{} + +func (h *HelloImpl) Echo(_ context.Context, req *api.Request) (resp *api.Response, err error) { + resp = &api.Response{ + Message: req.Message, + } + return +} + +func TestResolverDifferentGroup(t *testing.T) { + r, err := NewDefaultNacosRegistry() + assert.Nil(t, err) + r2, err := NewDefaultNacosRegistry(WithGroup("OTHER")) + assert.Nil(t, err) + + svr := hello.NewServer( + new(HelloImpl), + server.WithRegistry(r), + server.WithRegistryInfo(®istry.Info{ + ServiceName: "demo1", + Addr: utils.NewNetAddr("tcp", "127.0.0.1:8080"), + Weight: 10, + Tags: nil, + }), + server.WithServerBasicInfo(&rpcinfo.EndpointBasicInfo{ServiceName: "demo1"}), + server.WithServiceAddr(&net.TCPAddr{IP: net.IPv4(127, 0, 0, 1), Port: 8080}), + ) + + svr2 := hello.NewServer( + new(HelloImpl), + server.WithRegistry(r2), + server.WithRegistryInfo(®istry.Info{ + ServiceName: "demo2", + Addr: utils.NewNetAddr("tcp", "127.0.0.1:8082"), + Weight: 10, + Tags: nil, + }), + server.WithServerBasicInfo(&rpcinfo.EndpointBasicInfo{ServiceName: "demo2"}), + server.WithServiceAddr(&net.TCPAddr{IP: net.IPv4(127, 0, 0, 1), Port: 8082}), + ) + + go svr.Run() //nolint:errcheck + go svr2.Run() //nolint:errcheck + time.Sleep(2 * time.Second) + + resolver1, err := resolver.NewDefaultNacosResolver() + assert.Nil(t, err) + resolver2, err := resolver.NewDefaultNacosResolver(resolver.WithGroup("OTHER")) + assert.Nil(t, err) + + client1 := hello.MustNewClient( + "demo1", + client.WithResolver(resolver1), + client.WithRPCTimeout(time.Second*3), + ) + client2 := hello.MustNewClient( + "demo2", + client.WithResolver(resolver2), + client.WithRPCTimeout(time.Second*3), + ) + resp, err := client1.Echo(context.Background(), &api.Request{Message: "Hello"}) + assert.Nil(t, err) + assert.Equal(t, resp.Message, "Hello") + + resp, err = client2.Echo(context.Background(), &api.Request{Message: "Hello1"}) + assert.Nil(t, err) + assert.Equal(t, resp.Message, "Hello1") + + defer func() { + svr.Stop() //nolint:errcheck + svr2.Stop() //nolint:errcheck + }() +} diff --git a/v2/resolver/resolver.go b/v2/resolver/resolver.go index a7f4518..491c2f3 100644 --- a/v2/resolver/resolver.go +++ b/v2/resolver/resolver.go @@ -87,6 +87,7 @@ func (n *nacosResolver) Resolve(_ context.Context, desc string) (discovery.Resul if err != nil { return discovery.Result{}, err } + if len(res) == 0 { return discovery.Result{}, fmt.Errorf("no instance remains for %v", desc) } diff --git a/v2/resolver/resolver_test.go b/v2/resolver/resolver_test.go index b0a6a86..addaaa6 100644 --- a/v2/resolver/resolver_test.go +++ b/v2/resolver/resolver_test.go @@ -59,25 +59,26 @@ func init() { } func getNacosClient() (naming_client.INamingClient, error) { + // create ServerConfig sc := []constant.ServerConfig{ - *constant.NewServerConfig("127.0.0.1", 8848), + *constant.NewServerConfig("127.0.0.1", 8848, constant.WithContextPath("/nacos"), constant.WithScheme("http")), } - cc := constant.ClientConfig{ - NamespaceId: "public", - TimeoutMs: 5000, - NotLoadCacheAtStart: true, - LogDir: "/tmp/nacos/log", - CacheDir: "/tmp/nacos/cache", - LogLevel: "debug", - } + // create ClientConfig + cc := *constant.NewClientConfig( + constant.WithTimeoutMs(50000), + constant.WithUpdateCacheWhenEmpty(true), + constant.WithNotLoadCacheAtStart(true), + ) - return clients.NewNamingClient( + // create naming client + newClient, err := clients.NewNamingClient( vo.NacosClientParam{ ClientConfig: &cc, ServerConfigs: sc, }, ) + return newClient, err } // TestNacosResolverResolve test Resolve a service @@ -131,10 +132,7 @@ func TestNacosResolverResolve(t *testing.T) { } err := nacosregistry.NewNacosRegistry(nacosCli).Deregister(svcInfo) - if err != nil { - t.Errorf("Deregister Fail") - return - } + assert.Nil(t, err) } // TestNacosResolverDifferentCluster test NewNacosResolver WithCluster option