From 8028cf8339892fbe5216750165d05ba4215bc42c Mon Sep 17 00:00:00 2001 From: tadelesh Date: Wed, 7 Jun 2023 13:12:27 +0800 Subject: [PATCH] refine some logics and comments according to ACR service team's review --- .../azcontainerregistry/CHANGELOG.md | 1 + .../azcontainerregistry/digest_helper.go | 23 +++++++++++-------- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/sdk/containers/azcontainerregistry/CHANGELOG.md b/sdk/containers/azcontainerregistry/CHANGELOG.md index b9c4ceafd38a..203e9b75cf8f 100644 --- a/sdk/containers/azcontainerregistry/CHANGELOG.md +++ b/sdk/containers/azcontainerregistry/CHANGELOG.md @@ -9,6 +9,7 @@ ### Bugs Fixed ### Other Changes +* Refine some logics and comments ## 0.2.0 (2023-06-06) diff --git a/sdk/containers/azcontainerregistry/digest_helper.go b/sdk/containers/azcontainerregistry/digest_helper.go index b7c01d3fe428..b7ff1bf9861b 100644 --- a/sdk/containers/azcontainerregistry/digest_helper.go +++ b/sdk/containers/azcontainerregistry/digest_helper.go @@ -28,12 +28,15 @@ type digestValidator interface { } func parseDigestValidator(digest string) (digestValidator, error) { - alg := digest[:strings.Index(digest, ":")] + i := strings.Index(digest, ":") + if i < 0 { + return nil, ErrDigestAlgNotSupported + } + alg := digest[:i] if v, ok := validatorCtors[alg]; ok { return v(), nil - } else { - return nil, ErrDigestAlgNotSupported } + return nil, ErrDigestAlgNotSupported } type sha256Validator struct { @@ -62,15 +65,14 @@ type DigestValidationReader struct { // NewDigestValidationReader creates a new reader that help you to validate digest when you read manifest or blob data. func NewDigestValidationReader(digest string, reader io.Reader) (*DigestValidationReader, error) { validator, err := parseDigestValidator(digest) - if err == nil { - return &DigestValidationReader{ - digest: digest, - digestValidator: validator, - reader: reader, - }, nil - } else { + if err != nil { return nil, err } + return &DigestValidationReader{ + digest: digest, + digestValidator: validator, + reader: reader, + }, nil } // Read write to digest validator while read and validate digest when reach EOF. @@ -98,6 +100,7 @@ type BlobDigestCalculator struct { } // NewBlobDigestCalculator creates a new calculator to help to calculate blob digest when uploading blob. +// You should use a new BlobDigestCalculator each time you upload a blob. func NewBlobDigestCalculator() *BlobDigestCalculator { return &BlobDigestCalculator{ h: sha256.New(),