From 9604c614ca15109a0ba74ec1a3381bc3f712c81c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rados=C5=82aw=20Wa=C5=9Bko?= Date: Fri, 4 Oct 2024 16:16:46 +0200 Subject: [PATCH] getting and updating labels --- .../0.0.0-dev/src/Enso_Cloud/Enso_File.enso | 17 +++++++------- .../Internal/Enso_File_Helpers.enso | 8 +++++++ .../Network/Enso_Cloud/Enso_File_Spec.enso | 23 +++++++++++++++++++ 3 files changed, 40 insertions(+), 8 deletions(-) diff --git a/distribution/lib/Standard/Base/0.0.0-dev/src/Enso_Cloud/Enso_File.enso b/distribution/lib/Standard/Base/0.0.0-dev/src/Enso_Cloud/Enso_File.enso index 7409d6bbe61a..df357d4808e9 100644 --- a/distribution/lib/Standard/Base/0.0.0-dev/src/Enso_Cloud/Enso_File.enso +++ b/distribution/lib/Standard/Base/0.0.0-dev/src/Enso_Cloud/Enso_File.enso @@ -195,9 +195,10 @@ type Enso_File same asset at the same time, some changes may be lost. add_label : Text -> Enso_File add_label self (label : Text) -> Enso_File = - asset = Existing_Enso_Asset.get_asset_reference_for self - #asset.add_label label - self + labels = self.labels + if labels.contains label then self else + new_labels = labels+[label] + update_asset_labels self new_labels . if_not_error self ## GROUP Metadata ICON data_output @@ -215,8 +216,10 @@ type Enso_File same asset at the same time, some changes may be lost. remove_label : Text -> Boolean remove_label self (label : Text) -> Boolean = - asset = Existing_Enso_Asset.get_asset_reference_for self - "TODO" + labels = self.labels + if labels.contains label . not then False else + new_labels = labels.filter l-> l != label + update_asset_labels self new_labels . if_not_error True ## GROUP Metadata ICON data_output @@ -234,9 +237,7 @@ type Enso_File - The file on which it was called. set_labels : Vector Text -> Enso_File set_labels self (labels : Vector Text) -> Enso_File = - asset = Existing_Enso_Asset.get_asset_reference_for self - #asset.set_labels labels - self + update_asset_labels self labels . if_not_error self ## GROUP Metadata ICON data_output diff --git a/distribution/lib/Standard/Base/0.0.0-dev/src/Enso_Cloud/Internal/Enso_File_Helpers.enso b/distribution/lib/Standard/Base/0.0.0-dev/src/Enso_Cloud/Internal/Enso_File_Helpers.enso index af5c0a85bc3a..5beba9d2542d 100644 --- a/distribution/lib/Standard/Base/0.0.0-dev/src/Enso_Cloud/Internal/Enso_File_Helpers.enso +++ b/distribution/lib/Standard/Base/0.0.0-dev/src/Enso_Cloud/Internal/Enso_File_Helpers.enso @@ -5,6 +5,7 @@ import project.Data.Dictionary.Dictionary import project.Data.Json.Invalid_JSON import project.Data.Json.JS_Object import project.Data.Text.Text +import project.Data.Vector.Vector import project.Enso_Cloud.Enso_File.Enso_Asset_Type import project.Enso_Cloud.Enso_File.Enso_File import project.Enso_Cloud.Errors.Enso_Cloud_Error @@ -140,3 +141,10 @@ update_asset_description (asset : Enso_File) (description : Text) = payload = JS_Object.from_pairs [["description", description]] Asset_Cache.invalidate asset Utils.http_request HTTP_Method.Patch existing_asset.asset_uri payload + +## PRIVATE +update_asset_labels (asset : Enso_File) (new_labels : Vector Text) = + existing_asset = Existing_Enso_Asset.get_asset_reference_for asset + payload = JS_Object.from_pairs [["labels", new_labels]] + Asset_Cache.invalidate asset + Utils.http_request HTTP_Method.Patch existing_asset.asset_uri+"/labels" payload diff --git a/test/Base_Tests/src/Network/Enso_Cloud/Enso_File_Spec.enso b/test/Base_Tests/src/Network/Enso_Cloud/Enso_File_Spec.enso index 000c0a7dfea5..2dc562a94187 100644 --- a/test/Base_Tests/src/Network/Enso_Cloud/Enso_File_Spec.enso +++ b/test/Base_Tests/src/Network/Enso_Cloud/Enso_File_Spec.enso @@ -280,6 +280,29 @@ add_specs suite_builder setup:Cloud_Tests_Setup = suite_builder.group "Enso Clou nested_file.set_description "" . should_equal nested_file nested_file.description . should_equal "" + group_builder.specify "should be able to get and modify labels on assets" <| + f = test_root.get / "test_file.json" + f.labels . should_equal [] + + f.set_labels ["A", "B", "C", "D"] . should_equal f + f.labels . should_equal_ignoring_order ["A", "B", "C", "D"] + + f.add_label "E" . should_equal f + f.add_label "A" . should_equal f + f.labels . should_equal_ignoring_order ["A", "B", "C", "D", "E"] + + f.remove_label "B" . should_equal True + f.remove_label "C" . should_equal True + f.labels . should_equal_ignoring_order ["A", "D", "E"] + + f.remove_label "non-existent" . should_equal False + # B is also already removed + f.remove_label "B" . should_equal False + f.labels . should_equal_ignoring_order ["A", "D", "E"] + + f.set_labels [] . should_equal f + f.labels . should_equal [] + group_builder.specify "allows / as well as .. and . in resolve" <| (Enso_File.root / "a/b/c") . should_equal (Enso_File.root / "a" / "b" / "c") (Enso_File.root / "a///b/c") . should_equal (Enso_File.root / "a" / "b" / "c")