Repository: milvus-io/milvus-sdk-go
Pull request #557 (2023-08-17 04:00:36)

Support append struct/map to json column
/kind improvement

sre-ci-robot: [APPROVALNOTIFIER] This PR is **APPROVED**

This pull-request has been approved by: *<a href="https://github.com/milvus-io/milvus-sdk-go/pull/557#" title="Author self-approved">congqixia</a>*

The full list of commands accepted by this bot can be found [here](https://go.k8s.io/bot-commands?repo=milvus-io%2Fmilvus-sdk-go).

The pull request process is described [here](https://git.k8s.io/community/contributors/guide/owners.md#the-code-review-process)

<details >
Needs approval from an approver in each of these files:

- ~~[OWNERS](https://github.com/milvus-io/milvus-sdk-go/blob/master/OWNERS)~~ [congqixia]

Approvers can indicate their approval by writing `/approve` in a comment
Approvers can cancel approval by writing `/approve cancel` in a comment
</details>
<!-- META={"approvers":[]} -->
codecov[bot]: ## [Codecov](https://app.codecov.io/gh/milvus-io/milvus-sdk-go/pull/557?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=milvus-io) Report
> Merging [#557](https://app.codecov.io/gh/milvus-io/milvus-sdk-go/pull/557?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=milvus-io) (5f3dc78) into [master](https://app.codecov.io/gh/milvus-io/milvus-sdk-go/commit/dd5a31f64225fd0b2eb696c6792e3192a685f9d0?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=milvus-io) (dd5a31f) will **increase** coverage by `0.01%`.
> The diff coverage is `88.88%`.

[![Impacted file tree graph](https://app.codecov.io/gh/milvus-io/milvus-sdk-go/pull/557/graphs/tree.svg?width=650&height=150&src=pr&token=WJK43PV390&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=milvus-io)](https://app.codecov.io/gh/milvus-io/milvus-sdk-go/pull/557?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=milvus-io)

```diff
@@            Coverage Diff             @@
##           master     #557      +/-   ##
==========================================
+ Coverage   82.90%   82.91%   +0.01%     
==========================================
  Files          40       40              
  Lines        3638     3653      +15     
==========================================
+ Hits         3016     3029      +13     
- Misses        509      510       +1     
- Partials      113      114       +1     
```


| [Files Changed](https://app.codecov.io/gh/milvus-io/milvus-sdk-go/pull/557?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=milvus-io) | Coverage Δ | |
|---|---|---|
| [entity/columns\_json.go](https://app.codecov.io/gh/milvus-io/milvus-sdk-go/pull/557?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=milvus-io#diff-ZW50aXR5L2NvbHVtbnNfanNvbi5nbw==) | `82.00% <88.88%> (+2.00%)` | :arrow_up: |

jiaoew1991: /lgtm

Patch:
diff --git a/entity/columns_json.go b/entity/columns_json.go
index 8b03518..7d4a794 100644
--- a/entity/columns_json.go
+++ b/entity/columns_json.go
@@ -1,7 +1,9 @@
 package entity
 
 import (
+	"encoding/json"
 	"fmt"
+	"reflect"
 
 	"github.com/cockroachdb/errors"
 	schema "github.com/milvus-io/milvus-proto/go-api/v2/schemapb"
@@ -80,9 +82,27 @@ func (c *ColumnJSONBytes) ValueByIdx(idx int) ([]byte, error) {
 
 // AppendValue append value into column.
 func (c *ColumnJSONBytes) AppendValue(i interface{}) error {
-	v, ok := i.([]byte)
-	if !ok {
-		return fmt.Errorf("invalid type, expected []byte, got %T", i)
+	var v []byte
+	switch raw := i.(type) {
+	case []byte:
+		v = raw
+	default:
+		k := reflect.TypeOf(i).Kind()
+		if k == reflect.Ptr {
+			k = reflect.TypeOf(i).Elem().Kind()
+		}
+		switch k {
+		case reflect.Struct:
+			fallthrough
+		case reflect.Map:
+			bs, err := json.Marshal(raw)
+			if err != nil {
+				return err
+			}
+			v = bs
+		default:
+			return fmt.Errorf("expect json compatible type([]byte, struct[}, map], got %T)", i)
+		}
 	}
 	c.values = append(c.values, v)
 


Test patch:
diff --git a/entity/columns_json_test.go b/entity/columns_json_test.go
index 5595dbd..503cce1 100644
--- a/entity/columns_json_test.go
+++ b/entity/columns_json_test.go
@@ -67,6 +67,12 @@ func (s *ColumnJSONBytesSuite) TestAttrMethods() {
 		s.NoError(err)
 		s.Equal(item, val)
 
+		err = column.AppendValue(&struct{ Tag string }{Tag: "abc"})
+		s.NoError(err)
+
+		err = column.AppendValue(map[string]interface{}{"Value": 123})
+		s.NoError(err)
+
 		err = column.AppendValue(1)
 		s.Error(err)
 	})