From 868c35fe0ba30f76b2a98d271f3a8516fb184815 Mon Sep 17 00:00:00 2001
From: ko_oler <kooler89@gmail.com>
Date: Tue, 4 Jul 2023 13:16:59 +0300
Subject: [PATCH] =?UTF-8?q?-=20=D0=B8=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5=D0=BD?=
 =?UTF-8?q?=20=D1=82=D0=B8=D0=BF=20metadata=20=D0=B2=20CollectionConfig=20?=
 =?UTF-8?q?=D0=BD=D0=B0=20[]string=20-=20=D0=B8=D0=B7=D0=BC=D0=B5=D0=BD?=
 =?UTF-8?q?=D0=B5=D0=BD=D0=B0=20=D1=81=D0=B8=D0=B3=D0=BD=D0=B0=D1=82=D1=83?=
 =?UTF-8?q?=D1=80=D0=B0=20=D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8=D0=B8=20Set?=
 =?UTF-8?q?SchemaMetadata=20-=20=D0=BC=D0=B5=D0=BB=D0=BA=D0=B8=D0=B5=20?=
 =?UTF-8?q?=D0=BF=D1=80=D0=B0=D0=B2=D0=BA=D0=B8?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 pkg/setup/collection.go      | 15 +++++++--------
 pkg/setup/collection_test.go |  6 +++---
 2 files changed, 10 insertions(+), 11 deletions(-)

diff --git a/pkg/setup/collection.go b/pkg/setup/collection.go
index 989f70b1..bd8bea92 100644
--- a/pkg/setup/collection.go
+++ b/pkg/setup/collection.go
@@ -23,7 +23,7 @@ type DeleteCollectionFn func(s *Setup, col *collections.Collection) bool
 
 type CollectionConfig struct {
 	collection *collections.Collection
-	metadata   map[string]string
+	metadata   []string
 	UpdateFn   UpdateCollectionFn
 	DeleteFn   DeleteCollectionFn
 }
@@ -81,9 +81,9 @@ func UpdateExistingCollection() CollectionsOption {
 	}
 }
 
-func SetSchemaMetadata(md map[string]string) CollectionsOption {
+func SetSchemaMetadata(kv ...string) CollectionsOption {
 	return func(c *CollectionConfig) {
-		c.metadata = md
+		c.metadata = kv
 	}
 }
 
@@ -147,7 +147,7 @@ func (s *Setup) InstallCollection(ctx context.Context, c CollectionConfig) (setS
 	} else {
 		var upd bool
 		if c.metadata != nil {
-			collection.Schema.Metadata = c.metadata
+			collection.Schema.WithMetadata(c.metadata...)
 			err = s.checkSchemaMetadata(collection, exist)
 			if err != nil {
 				return false, err
@@ -172,12 +172,11 @@ func (s *Setup) InstallCollection(ctx context.Context, c CollectionConfig) (setS
 	return setSchema, nil
 }
 
+// checkSchemaMetadata проверяет изменилось ли значение Metadata в схеме коллекции
 func (s *Setup) checkSchemaMetadata(collection *collections.Collection, exist *collections.Collection) error {
 	if collection.Schema.Metadata != nil {
-		if _, ok := collection.Schema.Metadata["extension"]; ok {
-			if _, ok := exist.Schema.Metadata["extension"]; !ok && !s.IsForce() {
-				return errors.WithHint(errors.New("fail to update collection"), "collection has the same id as extension's collection, change yours collection id")
-			}
+		if exist.Schema.Metadata["extension"] != collection.Schema.Metadata["extension"] && !s.IsForce() {
+			return errors.WithHint(errors.New("fail to update collection"), "collection has the same id as extension's collection, change yours collection id")
 		}
 	}
 	return nil
diff --git a/pkg/setup/collection_test.go b/pkg/setup/collection_test.go
index 027c13b9..a3a6bafa 100644
--- a/pkg/setup/collection_test.go
+++ b/pkg/setup/collection_test.go
@@ -107,7 +107,7 @@ func TestSetup_InstallCollections(t *testing.T) {
 			wantErr: func(t *testing.T, err error) {
 				assert.NoError(t, err)
 			},
-			co: SetSchemaMetadata(map[string]string{"extension": "test-extension"}),
+			co: SetSchemaMetadata("extension", "test-extension"),
 		},
 		{
 			name:        "Fail to update collection with the same id",
@@ -118,7 +118,7 @@ func TestSetup_InstallCollections(t *testing.T) {
 			wantErr: func(t *testing.T, err error) {
 				assert.Error(t, err)
 			},
-			co: SetSchemaMetadata(map[string]string{"extension": "test-extension"}),
+			co: SetSchemaMetadata("extension", "test-extension"),
 		},
 		{
 			name:        "Update collection with the same id, with force",
@@ -134,7 +134,7 @@ func TestSetup_InstallCollections(t *testing.T) {
 			wantErr: func(t *testing.T, err error) {
 				assert.NoError(t, err)
 			},
-			co:    SetSchemaMetadata(map[string]string{"extension": "test-extension"}),
+			co:    SetSchemaMetadata("extension", "test-extension"),
 			force: true,
 		},
 	}
-- 
GitLab