【发布时间】:2020-08-20 09:46:34
【问题描述】:
我正在尝试为 AWS 服务 (ECR) 构建抽象。代码如下:
type ECR struct {
Client ecriface.ECRAPI
Ctx context.Context
}
// ECRCreate establishes aws session and creates a repo with provided input
func (e *ECR) ECRCreate(ecrInput *ecr.CreateRepositoryInput) {
result, err := e.Client.CreateRepositoryWithContext(e.Ctx, ecrInput)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case ecr.ErrCodeServerException:
log.Errorln(ecr.ErrCodeServerException, aerr.Error())
case ecr.ErrCodeInvalidParameterException:
log.Errorln(ecr.ErrCodeInvalidParameterException, aerr.Error())
case ecr.ErrCodeInvalidTagParameterException:
log.Errorln(ecr.ErrCodeInvalidTagParameterException, aerr.Error())
case ecr.ErrCodeTooManyTagsException:
log.Errorln(ecr.ErrCodeTooManyTagsException, aerr.Error())
case ecr.ErrCodeRepositoryAlreadyExistsException:
log.Errorln(ecr.ErrCodeRepositoryAlreadyExistsException, aerr.Error())
case ecr.ErrCodeLimitExceededException:
log.Errorln(ecr.ErrCodeLimitExceededException, aerr.Error())
case ecr.ErrCodeKmsException:
log.Errorln(ecr.ErrCodeKmsException, aerr.Error())
default:
log.Errorln(aerr.Error())
}
} else {
// Print the error, cast err to awserr.Error to get the Code and
// Message from an error.
log.Errorln(err.Error())
}
return
}
log.Infof("Result: %v", result)
}
模拟 aws sdk 创建存储库调用:
type mockECRClient struct {
ecriface.ECRAPI
}
func (m *mockECRClient) CreateRepositoryWithContext(ctx aws.Context, input *ecr.CreateRepositoryInput, opts ...request.Option) (*ecr.CreateRepositoryOutput, error) {
createdAt := time.Now()
encryptionType := "AES256"
//awsMockAccount := "974589621236"
encryptConfig := ecr.EncryptionConfiguration{EncryptionType: &encryptionType}
imageScanConfig := input.ImageScanningConfiguration
mockRepo := ecr.Repository{
CreatedAt: &createdAt,
EncryptionConfiguration: &encryptConfig,
ImageScanningConfiguration: imageScanConfig,
}
mockRepoOuput := ecr.CreateRepositoryOutput{Repository: &mockRepo}
return &mockRepoOuput, nil
}
func TestECR_ECRCreate(t *testing.T) {
ctx := context.TODO()
mockSvc := &mockECRClient{}
scan := true
name := "Test1"
inputTest1 := ecr.CreateRepositoryInput{
RepositoryName: &name,
ImageScanningConfiguration: &ecr.ImageScanningConfiguration{ScanOnPush: &scan},
}
ecrTest := ECR{
mockSvc,
ctx,
}
ecrTest.ECRCreate(&inputTest1)
}
这很有效。但是,我对这里的界面和组合的使用有点困惑。 ECRAPI 由 ecriface 包定义,我实现了接口的签名之一,并且仍然能够使用模拟客户端 mockSvc。
问题:
- 这是如何工作的?这是模拟接口的惯用方式吗?
-
ECRAPI接口的其他方法呢?那些是如何照顾的?
那么我的理解是否正确,我们可以定义具有任意数量的方法签名的接口,将该接口嵌入到结构中,然后将结构传递到预期接口的任何位置。但这意味着,我跳过实现我的接口的其他签名?
我想我在这里遗漏了一些重要的概念,请指教!
【问题讨论】: