Repository: mosn/layotto
Pull request #980 (2023-08-09 04:22:35)

feat: fix huaweiyun obs convert
<!--  Thanks for sending a pull request!
Read contributing.md before commit pull request.
-->

**What this PR does**:

fix huaweiyun obs convert

**Which issue(s) this PR fixes**:
<!--
*Automatically closes linked issue when PR is merged.
Usage: `Fixes #<issue number>`, or `Fixes (paste link of issue)`.
_If PR is about `failing-tests or flakes`, please post the related issues/tests in a comment and do not use `Fixes`_*
-->
Fixes #

**Special notes for your reviewer**:

**Does this PR introduce a user-facing change?**:
<!--
If no, just write "NONE" in the release-note block below.
If yes, a release note is required:
Enter your extended release note in the block below. If the PR requires additional action from users switching to the new release, include the string "action required".
-->
```release-note

```

github-actions[bot]: Hi @wenxuwan. Thanks for your PR! 🎉
If the PR is ready, use the `/cc` command to assign reviewer to review. 

<details>
  <summary>Details</summary>

The full list of commands accepted by this bot can be found [here](https://github.com/Xunzhuo/prowox/blob/main/COMMAND.md).

The pull request process is described [here](https://github.com/Xunzhuo/prowox/blob/main/PROCESS.md).

</details>
codecov[bot]: ## [Codecov](https://app.codecov.io/gh/mosn/layotto/pull/980?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=mosn) Report
Patch coverage has no change and project coverage change: **`-0.03%`** :warning:
> Comparison is base [(`77d045b`)](https://app.codecov.io/gh/mosn/layotto/commit/77d045b4e70442c09592d38432b8faca2a1e92bb?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=mosn) 60.20% compared to head [(`b409b66`)](https://app.codecov.io/gh/mosn/layotto/pull/980?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=mosn) 60.18%.

> :exclamation: Current head b409b66 differs from pull request most recent head 9045d2f. Consider uploading reports for the commit 9045d2f to get more accurate results

<details><summary>Additional details and impacted files</summary>


```diff
@@            Coverage Diff             @@
##             main     #980      +/-   ##
==========================================
- Coverage   60.20%   60.18%   -0.03%     
==========================================
  Files         143      143              
  Lines       10005    10009       +4     
==========================================
  Hits         6024     6024              
- Misses       3201     3205       +4     
  Partials      780      780              
```


| [Files Changed](https://app.codecov.io/gh/mosn/layotto/pull/980?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=mosn) | Coverage Δ | |
|---|---|---|
| [components/oss/huaweicloud/oss.go](https://app.codecov.io/gh/mosn/layotto/pull/980?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=mosn#diff-Y29tcG9uZW50cy9vc3MvaHVhd2VpY2xvdWQvb3NzLmdv) | `34.01% <0.00%> (ø)` | |
| [components/oss/option.go](https://app.codecov.io/gh/mosn/layotto/pull/980?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=mosn#diff-Y29tcG9uZW50cy9vc3Mvb3B0aW9uLmdv) | `56.75% <0.00%> (-2.13%)` | :arrow_down: |


</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/mosn/layotto/pull/980?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=mosn).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=mosn).


Patch:
diff --git a/components/oss/huaweicloud/oss.go b/components/oss/huaweicloud/oss.go
index ca25374b..fa941f39 100644
--- a/components/oss/huaweicloud/oss.go
+++ b/components/oss/huaweicloud/oss.go
@@ -81,7 +81,7 @@ func (h *HuaweicloudOSS) GetObject(ctx context.Context, input *oss.GetObjectInpu
 	}
 
 	output := &oss.GetObjectOutput{}
-	if err = copier.CopyWithOption(output, obsOutput, copier.Option{IgnoreEmpty: true, DeepCopy: true, Converters: []copier.TypeConverter{oss.TimeToInt64}}); err != nil {
+	if err = copier.CopyWithOption(output, obsOutput, copier.Option{IgnoreEmpty: true, DeepCopy: true, Converters: []copier.TypeConverter{oss.TimeValueToInt64}}); err != nil {
 		return nil, err
 	}
 	output.DataStream = obsOutput.Body
@@ -179,7 +179,7 @@ func (h *HuaweicloudOSS) CopyObject(ctx context.Context, input *oss.CopyObjectIn
 	}
 
 	output := &oss.CopyObjectOutput{}
-	if err = copier.CopyWithOption(output, obsOutput, copier.Option{IgnoreEmpty: true, DeepCopy: true, Converters: []copier.TypeConverter{oss.TimeToInt64}}); err != nil {
+	if err = copier.CopyWithOption(output, obsOutput, copier.Option{IgnoreEmpty: true, DeepCopy: true, Converters: []copier.TypeConverter{oss.TimeValueToInt64}}); err != nil {
 		return nil, err
 	}
 
@@ -250,7 +250,7 @@ func (h *HuaweicloudOSS) ListObjects(ctx context.Context, input *oss.ListObjects
 	contents := make([]*oss.Object, 0, len(obsOutput.Contents))
 	for _, v := range obsOutput.Contents {
 		content := &oss.Object{}
-		if err = copier.CopyWithOption(content, v, copier.Option{IgnoreEmpty: true, DeepCopy: true, Converters: []copier.TypeConverter{oss.TimeToInt64}}); err != nil {
+		if err = copier.CopyWithOption(content, v, copier.Option{IgnoreEmpty: true, DeepCopy: true, Converters: []copier.TypeConverter{oss.TimeValueToInt64}}); err != nil {
 			return nil, err
 		}
 		owner := &oss.Owner{}
@@ -386,7 +386,7 @@ func (h *HuaweicloudOSS) UploadPartCopy(ctx context.Context, input *oss.UploadPa
 
 	output := &oss.UploadPartCopyOutput{}
 	partResult := &oss.CopyPartResult{}
-	if err = copier.CopyWithOption(partResult, obsOutput, copier.Option{IgnoreEmpty: true, DeepCopy: true, Converters: []copier.TypeConverter{oss.TimeToInt64}}); err != nil {
+	if err = copier.CopyWithOption(partResult, obsOutput, copier.Option{IgnoreEmpty: true, DeepCopy: true, Converters: []copier.TypeConverter{oss.TimeValueToInt64}}); err != nil {
 		return nil, err
 	}
 	output.CopyPartResult = partResult
@@ -455,7 +455,7 @@ func (h *HuaweicloudOSS) ListMultipartUploads(ctx context.Context, input *oss.Li
 	uploads := make([]*oss.MultipartUpload, 0, len(obsOutput.Uploads))
 	for _, v := range obsOutput.Uploads {
 		upload := &oss.MultipartUpload{}
-		if err = copier.CopyWithOption(upload, v, copier.Option{IgnoreEmpty: true, DeepCopy: true, Converters: []copier.TypeConverter{oss.TimeToInt64}}); err != nil {
+		if err = copier.CopyWithOption(upload, v, copier.Option{IgnoreEmpty: true, DeepCopy: true, Converters: []copier.TypeConverter{oss.TimeValueToInt64}}); err != nil {
 			return nil, err
 		}
 		initiator := &oss.Initiator{}
@@ -501,7 +501,7 @@ func (h *HuaweicloudOSS) ListObjectVersions(ctx context.Context, input *oss.List
 	deleteMarkers := make([]*oss.DeleteMarkerEntry, 0, len(obsOutput.DeleteMarkers))
 	for _, v := range obsOutput.DeleteMarkers {
 		entry := &oss.DeleteMarkerEntry{}
-		if err = copier.CopyWithOption(entry, v, copier.Option{IgnoreEmpty: true, DeepCopy: true, Converters: []copier.TypeConverter{oss.TimeToInt64}}); err != nil {
+		if err = copier.CopyWithOption(entry, v, copier.Option{IgnoreEmpty: true, DeepCopy: true, Converters: []copier.TypeConverter{oss.TimeValueToInt64}}); err != nil {
 			return nil, err
 		}
 		deleteMarkers = append(deleteMarkers, entry)
@@ -510,7 +510,7 @@ func (h *HuaweicloudOSS) ListObjectVersions(ctx context.Context, input *oss.List
 	versions := make([]*oss.ObjectVersion, 0, len(obsOutput.Versions))
 	for _, v := range obsOutput.Versions {
 		version := &oss.ObjectVersion{}
-		if err = copier.CopyWithOption(version, v, copier.Option{IgnoreEmpty: true, DeepCopy: true, Converters: []copier.TypeConverter{oss.TimeToInt64}}); err != nil {
+		if err = copier.CopyWithOption(version, v, copier.Option{IgnoreEmpty: true, DeepCopy: true, Converters: []copier.TypeConverter{oss.TimeValueToInt64}}); err != nil {
 			return nil, err
 		}
 		owner := &oss.Owner{}
diff --git a/components/oss/option.go b/components/oss/option.go
index dadafde6..a54c1f49 100644
--- a/components/oss/option.go
+++ b/components/oss/option.go
@@ -46,6 +46,14 @@ var (
 			return s.Unix(), nil
 		},
 	}
+	TimeValueToInt64 = copier.TypeConverter{
+		SrcType: time.Time{},
+		DstType: int64(0),
+		Fn: func(src interface{}) (interface{}, error) {
+			s, _ := src.(time.Time)
+			return s.Unix(), nil
+		},
+	}
 )
 
 func GetGetObjectOutput(ob *s3.GetObjectOutput) (*GetObjectOutput, error) {
diff --git a/components/oss/oss.go b/components/oss/oss.go
index 06394987..4f4f4e11 100644
--- a/components/oss/oss.go
+++ b/components/oss/oss.go
@@ -93,7 +93,7 @@ type GetObjectOutput struct {
 	ContentRange       string            `json:"content_range,omitempty"`
 	ContentType        string            `json:"content_type,omitempty"`
 	DeleteMarker       bool              `json:"delete_marker,omitempty"`
-	Etag               string            `json:"etag,omitempty"`
+	ETag               string            `json:"etag,omitempty"`
 	Expiration         string            `json:"expiration,omitempty"`
 	Expires            string            `json:"expires,omitempty"`
 	LastModified       int64             `json:"last_modified,omitempty"`


Test patch:
diff --git a/components/oss/huaweicloud/oss_test.go b/components/oss/huaweicloud/oss_test.go
index 17d002da..2f1513a6 100644
--- a/components/oss/huaweicloud/oss_test.go
+++ b/components/oss/huaweicloud/oss_test.go
@@ -23,6 +23,11 @@ import (
 	"os"
 	"strings"
 	"testing"
+	"time"
+
+	"github.com/huaweicloud/huaweicloud-sdk-go-obs/obs"
+
+	"github.com/jinzhu/copier"
 
 	"github.com/stretchr/testify/assert"
 
@@ -121,6 +126,14 @@ func TestGetObject(t *testing.T) {
 	Nil(t, err)
 	content := string(body)
 	println(content)
+
+	out := &obs.GetObjectOutput{}
+	out.GetObjectMetadataOutput = obs.GetObjectMetadataOutput{ETag: "a213asdasdasdasd123"}
+
+	wanted := &oss.GetObjectOutput{}
+	err = copier.CopyWithOption(wanted, out, copier.Option{IgnoreEmpty: true, DeepCopy: true, Converters: []copier.TypeConverter{oss.TimeToInt64}})
+	assert.Nil(t, err)
+	assert.Equal(t, out.ETag, wanted.ETag)
 }
 
 func TestPutObject(t *testing.T) {
@@ -195,6 +208,11 @@ func TestListObjects(t *testing.T) {
 	output, err := h.ListObjects(context.TODO(), input)
 	Nil(t, err)
 	NotNil(t, output)
+	content := &oss.Object{}
+	v := obs.Content{LastModified: time.Now()}
+	err = copier.CopyWithOption(content, v, copier.Option{IgnoreEmpty: true, DeepCopy: true, Converters: []copier.TypeConverter{oss.TimeValueToInt64}})
+	assert.Nil(t, err)
+	assert.NotEqual(t, 0, content.LastModified)
 }
 
 func TestPutObjectCannedAcl(t *testing.T) {
diff --git a/pkg/grpc/extension/s3/s3_test.go b/pkg/grpc/extension/s3/s3_test.go
index cfa25da8..024091a9 100644
--- a/pkg/grpc/extension/s3/s3_test.go
+++ b/pkg/grpc/extension/s3/s3_test.go
@@ -71,7 +71,7 @@ func TestGetObject(t *testing.T) {
 	assert.Equal(t, status.Errorf(codes.InvalidArgument, NotSupportStoreName, "NoStore"), err)
 	iobuf := buffer.NewIoBufferBytes([]byte("hello"))
 	dataStream := &MockDataStream{iobuf}
-	output := &l8s3.GetObjectOutput{Etag: "tag"}
+	output := &l8s3.GetObjectOutput{ETag: "tag"}
 	output.DataStream = dataStream
 	mockServer.EXPECT().Context().Return(ctx)
 	mockossServer.EXPECT().GetObject(ctx, &l8s3.GetObjectInput{Bucket: "layotto", Key: "object"}).Return(output, nil)