-
Notifications
You must be signed in to change notification settings - Fork 326
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Widget support for Data.fetch and Data.post. #11199
Changes from 7 commits
beff637
9bf2f82
0093588
a34b24d
754e871
78440c2
1283888
42c65b3
719bf5a
2580bb8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,12 +4,18 @@ import project.Data.Pair.Pair | |
import project.Data.Text.Text | ||
import project.Data.Vector.Vector | ||
import project.Error.Error | ||
import project.Errors.Common.Missing_Argument | ||
import project.Errors.Illegal_Argument.Illegal_Argument | ||
import project.Errors.No_Such_Key.No_Such_Key | ||
import project.Meta | ||
import project.Metadata.Widget | ||
import project.Nothing.Nothing | ||
import project.Panic.Panic | ||
from project.Data.Boolean import Boolean, False, True | ||
from project.Data.Text.Extensions import all | ||
from project.Metadata.Choice import Option | ||
from project.Metadata.Widget import Single_Choice, Vector_Editor | ||
from project.Widget_Helpers import make_all_selector | ||
|
||
## A key-value store. It is possible to use any type as keys and values and mix | ||
them in one Dictionary. Keys are checked for equality based on their hash | ||
|
@@ -32,14 +38,12 @@ from project.Data.Text.Extensions import all | |
to pass a foreign map into Enso, where it will be treated as a Dictionary. | ||
@Builtin_Type | ||
type Dictionary key value | ||
## PRIVATE | ||
ADVANCED | ||
## ICON array_new2 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unrelated: Why is the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just the name in figma, there are a few "array_new" variants. |
||
Returns an empty dictionary. | ||
empty : Dictionary | ||
empty = @Builtin_Method "Dictionary.empty" | ||
|
||
## PRIVATE | ||
ADVANCED | ||
## ICON array_new2 | ||
Returns a single-element dictionary with the given key and value. | ||
A Call to `Dictionary.singleton key value` is the same as a call to | ||
`Dictionary.empty.insert key value`. | ||
|
@@ -53,12 +57,14 @@ type Dictionary key value | |
value 2. | ||
|
||
example_singleton = Dictionary.singleton "my_key" 2 | ||
@key (make_all_selector ..Always) | ||
@value (make_all_selector ..Always) | ||
singleton : Any -> Any -> Dictionary | ||
singleton key value = Dictionary.empty.insert key value | ||
|
||
## ALIAS dictionary, lookup table | ||
GROUP Constants | ||
ICON convert | ||
ICON array_new2 | ||
Builds a dictionary from two Vectors. The first vector contains the keys, | ||
and the second vector contains the values. The two vectors must be of the | ||
same length. | ||
|
@@ -71,6 +77,8 @@ type Dictionary key value | |
meaning that if two entries in the vector share the same key, an | ||
`Illegal_Argument` error is raised. If set to `False`, the last entry | ||
with a given key will be kept. | ||
@keys (Vector_Editor item_editor=make_all_selector display=..Always item_default="Nothing") | ||
@values (Vector_Editor item_editor=make_all_selector display=..Always item_default="Nothing") | ||
from_keys_and_values : Vector Any -> Vector Any -> Boolean -> Dictionary ! Illegal_Argument | ||
from_keys_and_values keys:Vector values:Vector error_on_duplicates:Boolean=True = | ||
if keys.length != values.length then Error.throw (Illegal_Argument.Error "`Dictionary.from_keys_and_values` encountered two vectors of different lengths.") else | ||
|
@@ -80,7 +88,7 @@ type Dictionary key value | |
|
||
## ALIAS dictionary, lookup table | ||
GROUP Constants | ||
ICON convert | ||
ICON array_new2 | ||
Builds a dictionary from a vector of key-value pairs, with each key-value | ||
pair represented as a 2 element vector. | ||
|
||
|
@@ -96,8 +104,9 @@ type Dictionary key value | |
Building a dictionary containing two key-value pairs. | ||
|
||
example_from_vector = Dictionary.from_vector [["A", 1], ["B", 2]] | ||
@vec key_value_widget | ||
from_vector : Vector Any -> Boolean -> Dictionary ! Illegal_Argument | ||
from_vector vec error_on_duplicates=True = | ||
from_vector vec error_on_duplicates:Boolean=True = | ||
vec.fold Dictionary.empty m-> el-> if el.length != 2 then Error.throw (Illegal_Argument.Error "`Dictionary.from_vector` encountered an invalid value. Each value in the vector has to be a key-value pair - it must have exactly 2 elements.") else | ||
key = el.at 0 | ||
value = el.at 1 | ||
|
@@ -152,8 +161,11 @@ type Dictionary key value | |
import Standard.Examples | ||
|
||
example_insert = Examples.dictionary.insert 7 "seven" | ||
@key (make_all_selector ..Always) | ||
@value (make_all_selector ..Always) | ||
insert : Any -> Any -> Dictionary | ||
insert self key value = @Builtin_Method "Dictionary.insert" | ||
insert self key=(Missing_Argument.throw "key") value=(Missing_Argument.throw "value") = | ||
self.insert_builtin key value | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's auto-registered the same as the "remove_builtin" - we should do a review of what we need to do with the builtins to make it the best set up whenever suits you. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
## GROUP Selections | ||
ICON table_clean | ||
|
@@ -170,8 +182,9 @@ type Dictionary key value | |
import Standard.Examples | ||
|
||
Examples.dictionary.remove "A" | ||
@key key_widget | ||
remove : Any -> Dictionary ! No_Such_Key | ||
remove self key = | ||
remove self key=(Missing_Argument.throw "key") = | ||
Panic.catch Any (self.remove_builtin key) _-> | ||
Error.throw (No_Such_Key.Error self key) | ||
|
||
|
@@ -191,6 +204,7 @@ type Dictionary key value | |
import Standard.Examples | ||
|
||
example_at = Examples.dictionary.at "A" | ||
@key key_widget | ||
at : Any -> Any ! No_Such_Key | ||
at self key = self.get key (Error.throw (No_Such_Key.Error self key)) | ||
|
||
|
@@ -211,12 +225,14 @@ type Dictionary key value | |
import Standard.Examples | ||
|
||
example_get = Examples.dictionary.get 2 "zero" | ||
@key key_widget | ||
get : Any -> Any -> Any | ||
get self key ~if_missing=Nothing = self.get_builtin key if_missing | ||
|
||
## GROUP Logical | ||
ICON preparation | ||
Returns True iff the Dictionary contains the given `key`. | ||
@key (make_all_selector ..Always) | ||
contains_key : Any -> Boolean | ||
contains_key self key = @Builtin_Method "Dictionary.contains_key" | ||
|
||
|
@@ -423,3 +439,19 @@ type Dictionary key value | |
## PRIVATE | ||
get_builtin : Any -> Any -> Any | ||
get_builtin self key ~if_missing = @Builtin_Method "Dictionary.get_builtin" | ||
|
||
## PRIVATE | ||
key_widget dict:Dictionary -> Widget = | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can these methods be moved into their own There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We often need to get the widgets in other functions so tend to keep them as |
||
values = dict.keys.map k-> Option k.to_text k.pretty | ||
Single_Choice display=..Always values=values | ||
|
||
## PRIVATE | ||
key_value_widget -> Widget = | ||
fqn = Meta.get_qualified_type_name Pair . drop (..Last 5) | ||
default = 'pair "key" Nothing' | ||
pair = Option "Pair" fqn+".pair" [["first", make_all_selector], ["second", make_all_selector]] | ||
item_editor = Single_Choice display=..Always values=[pair] | ||
Vector_Editor item_editor=item_editor display=..Always item_default=default | ||
|
||
## PRIVATE | ||
Dictionary.from (that:Vector) = Dictionary.from_vector that |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -295,3 +295,12 @@ check_start_valid start function max=3 = | |
used_start = if start < 0 then start + 2 else start | ||
if used_start < 0 || used_start >= max then Error.throw (Index_Out_Of_Bounds.Error start max) else | ||
function used_start | ||
|
||
## ICON array_new | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ICON There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
Create a new Pair from two elements. | ||
|
||
Arguments: | ||
- first: The first element. | ||
- second: The second element. | ||
pair : Any -> Any -> Pair | ||
pair first second -> Pair = Pair.new first second |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -826,6 +826,11 @@ type Date | |
format self format:Date_Time_Formatter=Date_Time_Formatter.iso_date = | ||
format.format_date self | ||
|
||
## PRIVATE | ||
Convert to a Enso code representation of this Date. | ||
pretty : Text | ||
pretty self = "(Date.new " + self.year.to_text + " " + self.month.to_text + " " + self.day.to_text + ")" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Eh! Another way to persist a value. Wouldn't it be better to have There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is matching our current |
||
|
||
## PRIVATE | ||
week_days_between start end = | ||
## We split the interval into 3 periods: the first week (containing the | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could be a helper in
Context
since it's repeated so much.