Repository: milvus-io/milvus-sdk-go
Pull request #253 (2022-01-26 02:39:41)

Use const value for vector field dimension key
Set "dim" to const value for reference

Related #252

Signed-off-by: Congqi Xia <congqi.xia@zilliz.com>

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/253#" 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://codecov.io/gh/milvus-io/milvus-sdk-go/pull/253?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=milvus-io) Report
> Merging [#253](https://codecov.io/gh/milvus-io/milvus-sdk-go/pull/253?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=milvus-io) (4ea1d73) into [master](https://codecov.io/gh/milvus-io/milvus-sdk-go/commit/151dd10d1bd31f31f34dc1120595e261b0d2c2b3?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=milvus-io) (151dd10) will **not change** coverage.
> The diff coverage is `100.00%`.

[![Impacted file tree graph](https://codecov.io/gh/milvus-io/milvus-sdk-go/pull/253/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://codecov.io/gh/milvus-io/milvus-sdk-go/pull/253?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     #253   +/-   ##
=======================================
  Coverage   89.83%   89.83%           
=======================================
  Files          19       19           
  Lines        2429     2429           
=======================================
  Hits         2182     2182           
  Misses        170      170           
  Partials       77       77           
```


| [Impacted Files](https://codecov.io/gh/milvus-io/milvus-sdk-go/pull/253?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=milvus-io) | Coverage Δ | |
|---|---|---|
| [entity/schema.go](https://codecov.io/gh/milvus-io/milvus-sdk-go/pull/253/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=milvus-io#diff-ZW50aXR5L3NjaGVtYS5nbw==) | `92.66% <ø> (ø)` | |
| [client/client\_grpc\_data.go](https://codecov.io/gh/milvus-io/milvus-sdk-go/pull/253/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=milvus-io#diff-Y2xpZW50L2NsaWVudF9ncnBjX2RhdGEuZ28=) | `78.03% <100.00%> (ø)` | |
| [entity/rows.go](https://codecov.io/gh/milvus-io/milvus-sdk-go/pull/253/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=milvus-io#diff-ZW50aXR5L3Jvd3MuZ28=) | `91.97% <100.00%> (ø)` | |

sunby: /lgtm

Patch:
diff --git a/client/client_grpc_data.go b/client/client_grpc_data.go
index 5066a1a..0d8856a 100644
--- a/client/client_grpc_data.go
+++ b/client/client_grpc_data.go
@@ -84,8 +84,8 @@ func (c *grpcClient) Insert(ctx context.Context, collName string, partitionName
 			case *entity.ColumnBinaryVector:
 				dim = column.Dim()
 			}
-			if fmt.Sprintf("%d", dim) != field.TypeParams["dim"] {
-				return nil, fmt.Errorf("params column %s vector dim %d not match collection definition, which has dim of %s", field.Name, dim, field.TypeParams["dim"])
+			if fmt.Sprintf("%d", dim) != field.TypeParams[entity.TYPE_PARAM_DIM] {
+				return nil, fmt.Errorf("params column %s vector dim %d not match collection definition, which has dim of %s", field.Name, dim, field.TypeParams[entity.TYPE_PARAM_DIM])
 			}
 		}
 	}
@@ -259,7 +259,7 @@ func (c *grpcClient) Search(ctx context.Context, collName string, partitions []s
 	if !has {
 		return nil, fmt.Errorf("vector field %s does not exist in collection %s", vectorField, collName)
 	}
-	dimStr := vfDef.TypeParams["dim"]
+	dimStr := vfDef.TypeParams[entity.TYPE_PARAM_DIM]
 	for _, vector := range vectors {
 		if fmt.Sprintf("%d", vector.Dim()) != dimStr {
 			return nil, fmt.Errorf("vector %s has dim of %s while found search vector with dim %d", vectorField,
@@ -716,11 +716,11 @@ func estRowSize(sch *entity.Schema, selected []string) int64 {
 		case entity.FieldTypeString:
 			//TODO string need varchar[max] syntax like limitation
 		case entity.FieldTypeFloatVector:
-			dimStr := field.TypeParams["dim"]
+			dimStr := field.TypeParams[entity.TYPE_PARAM_DIM]
 			dim, _ := strconv.ParseInt(dimStr, 10, 64)
 			total += 4 * dim
 		case entity.FieldTypeBinaryVector:
-			dimStr := field.TypeParams["dim"]
+			dimStr := field.TypeParams[entity.TYPE_PARAM_DIM]
 			dim, _ := strconv.ParseInt(dimStr, 10, 64)
 			total += 4 * dim / 8
 		}
diff --git a/entity/rows.go b/entity/rows.go
index 57f29e5..c07e33b 100644
--- a/entity/rows.go
+++ b/entity/rows.go
@@ -142,12 +142,12 @@ func ParseSchema(r Row) (*Schema, error) {
 				field.DataType = FieldTypeBinaryVector
 				//TODO maybe override by tag settings, when dim is not multiplier of 8
 				field.TypeParams = map[string]string{
-					"dim": strconv.FormatInt(int64(arrayLen*8), 10),
+					TYPE_PARAM_DIM: strconv.FormatInt(int64(arrayLen*8), 10),
 				}
 			case reflect.Float32:
 				field.DataType = FieldTypeFloatVector
 				field.TypeParams = map[string]string{
-					"dim": strconv.FormatInt(int64(arrayLen), 10),
+					TYPE_PARAM_DIM: strconv.FormatInt(int64(arrayLen), 10),
 				}
 			default:
 				return nil, fmt.Errorf("field %s is array of %v, which is not supported", f.Name, elemType)
@@ -165,7 +165,7 @@ func ParseSchema(r Row) (*Schema, error) {
 				return nil, fmt.Errorf("dim value %d is out of range", dim)
 			}
 			field.TypeParams = map[string]string{
-				"dim": dimStr,
+				TYPE_PARAM_DIM: dimStr,
 			}
 			elemType := ft.Elem()
 			switch elemType.Kind() {
@@ -273,7 +273,7 @@ func RowsToColumns(rows []Row, schemas ...*Schema) ([]Column, error) {
 			nameColumns[field.Name] = col
 		case FieldTypeFloatVector:
 			data := make([][]float32, 0, rowsLen)
-			dimStr, has := field.TypeParams["dim"]
+			dimStr, has := field.TypeParams[TYPE_PARAM_DIM]
 			if !has {
 				return []Column{}, errors.New("vector field with no dim")
 			}
@@ -285,7 +285,7 @@ func RowsToColumns(rows []Row, schemas ...*Schema) ([]Column, error) {
 			nameColumns[field.Name] = col
 		case FieldTypeBinaryVector:
 			data := make([][]byte, 0, rowsLen)
-			dimStr, has := field.TypeParams["dim"]
+			dimStr, has := field.TypeParams[TYPE_PARAM_DIM]
 			if !has {
 				return []Column{}, errors.New("vector field with no dim")
 			}
diff --git a/entity/schema.go b/entity/schema.go
index a0dba1b..5036daf 100644
--- a/entity/schema.go
+++ b/entity/schema.go
@@ -16,6 +16,11 @@ import (
 	"github.com/milvus-io/milvus-sdk-go/v2/internal/proto/schema"
 )
 
+const (
+	// TYPE_PARAM_DIM is the const for field type param dimension
+	TYPE_PARAM_DIM = "dim"
+)
+
 // Schema represents schema info of collection in milvus
 type Schema struct {
 	CollectionName string
diff --git a/examples/basic/basic.go b/examples/basic/basic.go
index ddc3de9..061aa6a 100644
--- a/examples/basic/basic.go
+++ b/examples/basic/basic.go
@@ -56,7 +56,7 @@ func main() {
 				Name:     "vector",
 				DataType: entity.FieldTypeFloatVector,
 				TypeParams: map[string]string{ // the vector dim may changed def method in release
-					"dim": "128",
+					entity.TYPE_PARAM_DIM: "128",
 				},
 			},
 		},
diff --git a/examples/calcdistance/calc_distance.go b/examples/calcdistance/calc_distance.go
index 1150bc5..6412bec 100644
--- a/examples/calcdistance/calc_distance.go
+++ b/examples/calcdistance/calc_distance.go
@@ -64,7 +64,7 @@ func main() {
 				Name:     "Vector",
 				DataType: entity.FieldTypeFloatVector,
 				TypeParams: map[string]string{
-					"dim": "8",
+					entity.TYPE_PARAM_DIM: "8",
 				},
 			},
 		},
diff --git a/examples/index/index.go b/examples/index/index.go
index ea78b96..83aabd8 100644
--- a/examples/index/index.go
+++ b/examples/index/index.go
@@ -65,7 +65,7 @@ func main() {
 				Name:     "Vector",
 				DataType: entity.FieldTypeFloatVector,
 				TypeParams: map[string]string{
-					"dim": "8",
+					entity.TYPE_PARAM_DIM: "8",
 				},
 			},
 		},
diff --git a/examples/insert/insert.go b/examples/insert/insert.go
index 4a21785..5d336ae 100644
--- a/examples/insert/insert.go
+++ b/examples/insert/insert.go
@@ -65,7 +65,7 @@ func main() {
 				Name:     "Vector",
 				DataType: entity.FieldTypeFloatVector,
 				TypeParams: map[string]string{
-					"dim": "8",
+					entity.TYPE_PARAM_DIM: "8",
 				},
 			},
 		},


Test patch:
diff --git a/client/client_grpc_collection_test.go b/client/client_grpc_collection_test.go
index 1ff0aaf..b045ec8 100644
--- a/client/client_grpc_collection_test.go
+++ b/client/client_grpc_collection_test.go
@@ -139,7 +139,7 @@ func TestGrpcClientCreateCollection(t *testing.T) {
 					{
 						Name:       "vector",
 						DataType:   entity.FieldTypeFloatVector,
-						TypeParams: map[string]string{"dim": "128"},
+						TypeParams: map[string]string{entity.TYPE_PARAM_DIM: "128"},
 					},
 				},
 			},
@@ -160,7 +160,7 @@ func TestGrpcClientCreateCollection(t *testing.T) {
 					{
 						Name:       "vector",
 						DataType:   entity.FieldTypeFloatVector,
-						TypeParams: map[string]string{"dim": "128"},
+						TypeParams: map[string]string{entity.TYPE_PARAM_DIM: "128"},
 					},
 				},
 			},
@@ -183,7 +183,7 @@ func TestGrpcClientCreateCollection(t *testing.T) {
 					{
 						Name:       "vector",
 						DataType:   entity.FieldTypeFloatVector,
-						TypeParams: map[string]string{"dim": "128"},
+						TypeParams: map[string]string{entity.TYPE_PARAM_DIM: "128"},
 					},
 				},
 			},
@@ -199,7 +199,7 @@ func TestGrpcClientCreateCollection(t *testing.T) {
 					{
 						Name:       "vector",
 						DataType:   entity.FieldTypeFloatVector,
-						TypeParams: map[string]string{"dim": "128"},
+						TypeParams: map[string]string{entity.TYPE_PARAM_DIM: "128"},
 					},
 				},
 			},
diff --git a/client/client_grpc_data_test.go b/client/client_grpc_data_test.go
index 532d559..0f33a84 100644
--- a/client/client_grpc_data_test.go
+++ b/client/client_grpc_data_test.go
@@ -817,14 +817,14 @@ func TestEstRowSize(t *testing.T) {
 				Name:     testVectorField,
 				DataType: entity.FieldTypeFloatVector,
 				TypeParams: map[string]string{
-					"dim": fmt.Sprintf("%d", testVectorDim),
+					entity.TYPE_PARAM_DIM: fmt.Sprintf("%d", testVectorDim),
 				},
 			},
 			{
 				Name:     "binary_vector",
 				DataType: entity.FieldTypeBinaryVector,
 				TypeParams: map[string]string{
-					"dim": fmt.Sprintf("%d", testVectorDim),
+					entity.TYPE_PARAM_DIM: fmt.Sprintf("%d", testVectorDim),
 				},
 			},
 		},
diff --git a/client/client_mock_test.go b/client/client_mock_test.go
index a304f79..65a8abb 100644
--- a/client/client_mock_test.go
+++ b/client/client_mock_test.go
@@ -86,7 +86,7 @@ func defaultSchema() *entity.Schema {
 				Name:     testVectorField,
 				DataType: entity.FieldTypeFloatVector,
 				TypeParams: map[string]string{
-					"dim": fmt.Sprintf("%d", testVectorDim),
+					entity.TYPE_PARAM_DIM: fmt.Sprintf("%d", testVectorDim),
 				},
 			},
 		},
diff --git a/tests/client_collection_test.go b/tests/client_collection_test.go
index 44f031f..236d96c 100644
--- a/tests/client_collection_test.go
+++ b/tests/client_collection_test.go
@@ -55,7 +55,7 @@ func TestCreateCollection(t *testing.T) {
 				Name:     "vector",
 				DataType: entity.FieldTypeFloatVector,
 				TypeParams: map[string]string{
-					"dim": "128",
+					entity.TYPE_PARAM_DIM: "128",
 				},
 			},
 		},
diff --git a/tests/testutil/utils.go b/tests/testutil/utils.go
index c63671f..ab6bcf6 100644
--- a/tests/testutil/utils.go
+++ b/tests/testutil/utils.go
@@ -2,9 +2,10 @@ package testutil
 
 import (
 	"fmt"
-	"github.com/milvus-io/milvus-sdk-go/v2/entity"
 	"math/rand"
 	"time"
+
+	"github.com/milvus-io/milvus-sdk-go/v2/entity"
 )
 
 var r *rand.Rand = nil
@@ -25,7 +26,7 @@ func GenRandomString(n int) string {
 }
 
 func GenDefaultFields(dim int64) []*entity.Field {
-	var fields = []*entity.Field {
+	var fields = []*entity.Field{
 		{
 			Name:       DefaultIntFieldName,
 			DataType:   entity.FieldTypeInt64,
@@ -38,19 +39,19 @@ func GenDefaultFields(dim int64) []*entity.Field {
 		{
 			Name:     DefaultFloatVecFieldName,
 			DataType: entity.FieldTypeFloatVector,
-			TypeParams: map[string]string {
-				"dim": fmt.Sprintf("%d", dim),
+			TypeParams: map[string]string{
+				entity.TYPE_PARAM_DIM: fmt.Sprintf("%d", dim),
 			},
 		},
 	}
 	return fields
 }
 
-func GenSchema(name string, autoId bool, fields []*entity.Field) *entity.Schema {
+func GenSchema(name string, autoID bool, fields []*entity.Field) *entity.Schema {
 	schema := &entity.Schema{
 		CollectionName: name,
-		AutoID: autoId,
-		Fields: fields,
+		AutoID:         autoID,
+		Fields:         fields,
 	}
 	return schema
-}
\ No newline at end of file
+}
diff --git a/tests/util.go b/tests/util.go
index 73e847d..b275705 100644
--- a/tests/util.go
+++ b/tests/util.go
@@ -72,7 +72,7 @@ func defaultSchema() *entity.Schema {
 				Name:     testVectorField,
 				DataType: entity.FieldTypeFloatVector,
 				TypeParams: map[string]string{
-					"dim": fmt.Sprintf("%d", testVectorDim),
+					entity.TYPE_PARAM_DIM: fmt.Sprintf("%d", testVectorDim),
 				},
 				IndexParams: map[string]string{
 					"metric_type": "L2",