diff --git a/spi/spi.go b/spi/spi.go index 85b4055..6579600 100644 --- a/spi/spi.go +++ b/spi/spi.go @@ -66,7 +66,7 @@ func LoadService[T any](dir string, symName string) ([]T, error) { // 尝试将符号断言为接口类型 service, ok := sym.(T) if !ok { - return errors.New("插件非该接口类型") + return ErrInvalidSo } // 收集服务 services = append(services, service) diff --git a/spi/spi_test.go b/spi/spi_test.go index 93f2801..ae83e18 100644 --- a/spi/spi_test.go +++ b/spi/spi_test.go @@ -87,6 +87,14 @@ func (l *LoadServiceSuite) Test_LoadService() { return assert.ErrorIs(t, err, ErrSymbolNameNotFound) }, }, + { + name: "加载的对象未实现对应的抽象", + dir: "./testdata/user_service3", + svcName: "UserSvc", + assertFunc: func(t assert.TestingT, err error, i ...interface{}) bool { + return assert.ErrorIs(t, err, ErrInvalidSo) + }, + }, } for _, tc := range testcases { t.Run(tc.name, func(t *testing.T) { diff --git a/spi/testdata/user_service3/a.go b/spi/testdata/user_service3/a.go new file mode 100644 index 0000000..bda4404 --- /dev/null +++ b/spi/testdata/user_service3/a.go @@ -0,0 +1,27 @@ +// Copyright 2021 ecodeclub +// +// 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. + +package main + +// 测试用 + +//go:generate go build -race --buildmode=plugin -o a.so ./a.go + +type UserService struct{} + +func (u UserService) GetV1() string { + return "Get" +} + +var UserSvc UserService