Skip to content

Commit

Permalink
create labels that did not exist, with a random color
Browse files Browse the repository at this point in the history
  • Loading branch information
radeusgd committed Oct 5, 2024
1 parent 9604c61 commit edfde57
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 12 deletions.
6 changes: 5 additions & 1 deletion distribution/lib/Standard/Base/0.0.0-dev/src/Data/Color.enso
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
import project.Data.Numbers.Integer

## Represents a color.
type Color
HCL lightness chroma hue
## A color represented in the HCL color space.
HCL hue:Integer chroma:Integer lightness:Integer
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,8 @@ type Enso_File
- The file on which it was called.
set_description : Text -> Enso_File
set_description self (description : Text) -> Enso_File =
update_asset_description self description . if_not_error self
Context.Output.if_enabled disabled_message="Cannot set a description when writing is disabled. Press the Write button ▶ to perform the operation." panic=False <|
update_asset_description self description . if_not_error self

## GROUP Metadata
ICON metadata
Expand Down Expand Up @@ -195,10 +196,12 @@ 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 =
labels = self.labels
if labels.contains label then self else
new_labels = labels+[label]
update_asset_labels self new_labels . if_not_error self
Context.Output.if_enabled disabled_message="Cannot add a label when writing is disabled. Press the Write button ▶ to perform the operation." panic=False <|
labels = self.labels
if labels.contains label then self else
ensure_tags_exist [label] . if_not_error <|
new_labels = labels+[label]
update_asset_labels self new_labels . if_not_error self

## GROUP Metadata
ICON data_output
Expand All @@ -216,10 +219,11 @@ type Enso_File
same asset at the same time, some changes may be lost.
remove_label : Text -> Boolean
remove_label self (label : Text) -> Boolean =
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
Context.Output.if_enabled disabled_message="Cannot remove a label when writing is disabled. Press the Write button ▶ to perform the operation." panic=False <|
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
Expand All @@ -237,7 +241,9 @@ type Enso_File
- The file on which it was called.
set_labels : Vector Text -> Enso_File
set_labels self (labels : Vector Text) -> Enso_File =
update_asset_labels self labels . if_not_error self
Context.Output.if_enabled disabled_message="Cannot update labels when writing is disabled. Press the Write button ▶ to perform the operation." panic=False <|
ensure_tags_exist labels . if_not_error <|
update_asset_labels self labels . if_not_error self

## GROUP Metadata
ICON data_output
Expand All @@ -248,7 +254,8 @@ type Enso_File
- color: The color of the label to create.
create_label : Text -> Text -> Nothing
create_label (name : Text) (color : Color) =
"TODO"
Context.Output.if_enabled disabled_message="Cannot create a label when writing is disabled. Press the Write button ▶ to perform the operation." panic=False <|
create_tag name color . if_not_error Nothing

## GROUP Metadata
ICON metadata
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
private

import project.Any.Any
import project.Data.Color.Color
import project.Data.Dictionary.Dictionary
import project.Data.Hashset.Hashset
import project.Data.Json.Invalid_JSON
import project.Data.Json.JS_Object
import project.Data.Text.Text
Expand All @@ -20,6 +22,7 @@ import project.Network.HTTP.HTTP_Method.HTTP_Method
import project.Network.HTTP.Request_Error
import project.Network.URI.URI
import project.Nothing.Nothing
import project.Random.Random
import project.System.File.File
import project.System.Output_Stream.Output_Stream
from project.Data.Boolean import Boolean, False, True
Expand Down Expand Up @@ -148,3 +151,43 @@ update_asset_labels (asset : Enso_File) (new_labels : Vector Text) =
payload = JS_Object.from_pairs [["labels", new_labels]]
Asset_Cache.invalidate asset
Utils.http_request HTTP_Method.Patch existing_asset.asset_uri+"/labels" payload

## PRIVATE
create_tag (name : Text) (color : Color) =
color_as_json = JS_Object.from_pairs <| case color of
Color.HCL h c l -> [["lightness", l], ["chroma", c], ["hue", h]]
_ -> Error.throw (Illegal_Argument.Error "Only colors expressed in HCL color-space are supported when creating labels.")
payload = JS_Object.from_pairs <|
[["value", name], ["color", color_as_json]]
Utils.http_request_as_json HTTP_Method.Post Utils.tags_api payload

## PRIVATE
Returns a list of known tags.
list_tags -> Vector Tag =
response = Utils.http_request_as_json HTTP_Method.Get Utils.tags_api
tags = get_required_field "tags" response expected_type=Vector
tags.map tag_json->
id = get_required_field "id" tag_json expected_type=Text
value = get_required_field "value" tag_json expected_type=Text
Tag.Value value id

## PRIVATE
type Tag
## PRIVATE
We are not including the color, as we are not using it yet.
Once needed, it should be added.
Value value:Text id:Text

## PRIVATE
random_tag_color -> Color =
lightness = 50
chroma = 66
hue = Random.integer 0 360
Color.HCL hue chroma lightness

## PRIVATE
ensure_tags_exist (names : Vector Text) =
existing_tags = Hashset.from_vector <| list_tags.map .value
tags_to_create = (Hashset.from_vector names).difference existing_tags . to_vector
tags_to_create.each name->
create_tag name random_tag_color
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ secrets_api = cloud_root_uri + "secrets"
Root address for DataLinks API.
datalinks_api = cloud_root_uri + "datalinks"

## PRIVATE
Root address for managing labels.
tags_api = cloud_root_uri + "tags"

## PRIVATE
Flushes all cloud caches, including the authentication data
(so the next request will re-read the credentials file).
Expand Down

0 comments on commit edfde57

Please sign in to comment.