Skip to content
Draft
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,22 @@ type Dictionary key value
to_text : Text
to_text self = @Builtin_Method "Dictionary.to_text"

## ---
private: true
---
to_display_text : Text
to_display_text self =
texts = self.keys.take (..First 40) . map k-> k.to_display_text + "=" + (self.get k . to_display_text)
suffix = if self.length > 40 then " and " + (self.length - 40).to_text + " more elements}" else "}"
"{" + (texts.join ", ") + suffix

## ---
private: true
---
pretty : Text
pretty self =
"Dictionary.from_vector " + self.to_vector.pretty

## ---
private: true
---
Expand All @@ -538,7 +554,9 @@ key_value_widget -> Widget =
item_editor = Single_Choice display=..Always values=[pair]
Vector_Editor item_editor=item_editor display=..Always item_default=default

## ---
private: true
---
Dictionary.from (that:Vector) = Dictionary.from_vector that
## Convert a Vector of key-value pairs to a Dictionary.
Dictionary.from (that:Vector) (error_on_duplicates:Boolean=True) =
Dictionary.from_vector that error_on_duplicates

## Convert Dictionary to a Vector.
Vector.from (that:Dictionary) = that.to_vector
28 changes: 27 additions & 1 deletion distribution/lib/Standard/Base/0.0.0-dev/src/Data/Hashset.enso
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,33 @@ type Hashset
private: true
---
to_text : Text
to_text self = self.to_vector.map .pretty . join ", " "Hashset{" "}"
to_text self =
inner = self.to_vector.to_text
"{" + (inner.drop (..First 1) . drop (..Last 1)) + "}"

## ---
private: true
---
to_display_text : Text
to_display_text self =
texts = self.underlying_dictionary.keys.take (..First 40) . map _.to_display_text
suffix = if self.length > 40 then " and " + (self.length - 40).to_text + " more elements}" else "}"
"{" + (texts.join ", ") + suffix

## ---
private: true
---
pretty : Text
pretty self =
"Hashset.from_vector " + self.to_vector.pretty

## Convert from a Vector to a Hashset
Hashset.from (that:Vector) (error_on_duplicates:Boolean=False) =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • The name Hashset is strange knowing we have Dictionary and not Hashmap
  • Shouldn't it be just a Set?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was named a long time ago to avoid some naming clashes at the time - Set and Map had too many meanings within the Enso context. Map was hence chosen to be Dictionary, and Set was called Hashset (allowing the technical details to leak as its a more internal class).

Hashset.from_vector that error_on_duplicates

## Convert from a Hashset to a Vector
Vector.from (that:Hashset) =
that.to_vector

## ---
private: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,27 @@ from project.Data.Text.Extensions import all
Any.to_json : Text
Any.to_json self = Json.stringify self

## ---
group: Conversions
icon: convert
---
Converts the given value to a JSON serialized value.
Any.json_stringify self -> Text = self.to_json
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we have to_json and json_stringify?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For compatibility with Table. Table needs a row-based JSON conversion function, and to_json is the standard API we use for serialising objects to JSON. Table is serialised by the to_json and uses json_stringify for the row-based operation.

Having the same method on Any is for compatibility.


## ---
private: true
---
Converts the given value to a JSON serialized value.
Error.to_json : Text
Error.to_json self = self.to_js_object.to_text

## ---
group: Conversions
icon: convert
---
Converts the given value to a JSON serialized value.
Error.json_stringify self -> Text = self

## ---
private: true
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,12 @@ import Standard.Base.Errors.Common.Index_Out_Of_Bounds
import Standard.Base.Errors.Illegal_Argument.Illegal_Argument
import Standard.Base.Errors.Illegal_State.Illegal_State
import Standard.Base.Internal.Rounding_Helpers
from Standard.Base.Metadata.Widget import Text_Input
from Standard.Base.Widget_Helpers import make_format_chooser

import Standard.Table.Fill_With.Fill_With
import Standard.Table.Internal.Column_Naming_Helper.Column_Naming_Helper
import Standard.Table.Internal.Date_Time_Helpers
import Standard.Table.Internal.Problem_Builder.Problem_Builder
import Standard.Table.Internal.Value_Type_Helpers
import Standard.Table.Internal.Widget_Helpers
import Standard.Table.Refined_Types.Text_Column.Text_Column
import Standard.Table.Rows_To_Read.Rows_To_Read
from Standard.Table import Auto, Column, Data_Formatter, Previous_Value, Table, Value_Type
Expand Down Expand Up @@ -394,7 +391,6 @@ type DB_Column_Implementation
if this_column.connection.dialect.is_operation_supported operator then make_binary_op this_column operator what new_name else
Error.throw (Unsupported_Database_Operation.Error ("`trim "+where.to_text+"`"))

@new_text (Text_Input display=..Always)
text_replace (this_column : Column & DB_Column) (term : Text | Regex | Column) (new_text : Text | Column) case_sensitivity:Case_Sensitivity only_first:Boolean =
Value_Type.expect_text this_column <| case_sensitivity.disallow_non_default_locale <|
input_type = if term.is_error then term else Meta.type_of term
Expand Down Expand Up @@ -483,7 +479,6 @@ type DB_Column_Implementation
Error.throw (Illegal_State.Error "The dialect "+this_column.connection.dialect.name+" does not support a boolean type. The implementation of `is_in` should be revised to account for this. This is an internal issue with the Database library.")
DB_Column.new new_name this_column.connection new_type_ref new_expr this_column.context

@format (make_format_chooser include_number=False)
parse (this_column : Column & DB_Column) (type : Value_Type | Auto) (format : Text | Data_Formatter) on_problems:Problem_Behavior =
if type == Auto then Error.throw (Unsupported_Database_Operation.Error "`Auto` parse type") else
if format != "" then Error.throw (Unsupported_Database_Operation.Error "Custom formatting") else
Expand All @@ -494,12 +489,14 @@ type DB_Column_Implementation
Database parse just boils down to a simple CAST.
internal_do_cast this_column type on_problems


@format (this_column-> Widget_Helpers.make_format_chooser_for_type this_column.value_type)
format (this_column : Column & DB_Column) (format : Text | Date_Time_Formatter | Column) locale:Locale =
if format != "" || locale != Locale.default then Error.throw (Unsupported_Database_Operation.Error "Custom formatting")
DB_Column_Implementation.cast this_column Value_Type.Char on_problems=Problem_Behavior.Ignore

json_stringify (this_column : Column & DB_Column) =
_ = [this_column]
Error.throw <| Unsupported_Database_Operation.Error "json_stringify"

cast (this_column : Column & DB_Column) value_type:Value_Type on_problems:Problem_Behavior =
check_cast_compatibility this_column.value_type value_type allow_unsupported=True
internal_do_cast this_column value_type on_problems
Expand Down
9 changes: 9 additions & 0 deletions distribution/lib/Standard/Table/0.0.0-dev/src/Column.enso
Original file line number Diff line number Diff line change
Expand Up @@ -1858,6 +1858,15 @@ type Column
format self (format : Text | Date_Time_Formatter | Column = "") locale:Locale=Locale.default -> Column & Any ! Illegal_Argument =
self.implementation.format self format locale

## ---
aliases: [to_json]
group: Standard.Base.Conversions
icon: conversion
---
Converts a column into a JSON string representation of its values.
json_stringify self -> Column & Any =
self.implementation.json_stringify self

## ---
group: Standard.Base.Conversions
icon: convert
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ type In_Memory_Column
## ---
private: true
---
Specialised implementation of to_json for a Column.
Specialised implementation of to_json for a Column for use in visualization.
to_json_data self start:Integer=0 (row_count:Integer=(self:Column).row_count) -> Text =
JsonOperation.apply self.java_column start row_count
JsonOperation.VIZ_INSTANCE.apply self.java_column start row_count

## ---
private: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ polyglot java import org.enso.table.data.column.operation.cast.CastOperation
polyglot java import org.enso.table.data.column.operation.comparators.Comparators
polyglot java import org.enso.table.data.column.operation.IfOperation
polyglot java import org.enso.table.data.column.operation.IsInOperation
polyglot java import org.enso.table.data.column.operation.JsonOperation
polyglot java import org.enso.table.data.column.operation.text.TextIndexOf
polyglot java import org.enso.table.data.column.operation.text.TextPartOperation
polyglot java import org.enso.table.data.column.operation.text.TextPredicates
Expand Down Expand Up @@ -579,6 +580,10 @@ type In_Memory_Column_Implementation
formatter = make_value_formatter_for_value_type this_column.value_type locale format
apply_unary_map this_column this_column.name formatter Value_Type.Char on_problems=..Report_Error

json_stringify (this_column : Column & In_Memory_Column) =
new_column = JsonOperation.INSTANCE.makeJsonColumn this_column.java_column
In_Memory_Column.from_java_column new_column

cast (this_column : Column & In_Memory_Column) value_type:Value_Type on_problems:Problem_Behavior =
Cast_Helpers.check_cast_compatibility this_column.value_type value_type
target_storage_type = Storage.from_value_type value_type on_problems
Expand Down
3 changes: 1 addition & 2 deletions distribution/lib/Standard/Table/0.0.0-dev/src/Row.enso
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,7 @@ type Row
private: true
---
Converts this row into a JSON representation.
to_json_data : Text
to_json_data self =
to_json_data self -> Text =
self.java_row.toJsonData

## ---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ prepare_visualization y max_rows=1000 = time_visualization Visualization ("prepa
v : Vector -> make_json_for_vector v max_rows . to_json
v : Array -> prepare_visualization v.to_vector max_rows
v : Dictionary -> make_json_for_dictionary v max_rows
v : Hashset -> make_json_for_hashset v max_rows
v : JS_Object -> make_json_for_js_object v max_rows
v : Row -> make_json_for_row v
t : In_Memory_Table -> t.to_table_viz_json
Expand Down Expand Up @@ -164,6 +165,18 @@ make_json_for_dictionary dict max_items =
get_child_node_action_link_name = ["get_child_node_link_name", "key"]
JS_Object.from_pairs [header, data, all_rows, links, get_child_node_action_link_name, ["type", "Map"], ["child_label", "value"]] . to_json

## ---
private: true
---
Render Hashset to JSON
private make_json_for_hashset hashset:Hashset max_items:Integer -> Text =
header = ["header", ["key"]]
all_rows = ["all_rows_count", hashset.size]
as_vector = Warning.clear (hashset.to_vector.take max_items)
mapped = as_vector . map _make_json_for_value
data = ["data", [mapped]]
JS_Object.from_pairs [header, data, all_rows, ["type", "Map"]] . to_json

## ---
private: true
---
Expand Down Expand Up @@ -352,10 +365,15 @@ _make_json_for_value val level=0 = case val of
"[" + (prepared.join ", ") + "]"
array : Array -> _make_json_for_value array.to_vector level
dict : Dictionary ->
if level != 0 then "{… "+dict.size.to_text+" items}" else
if level != 0 then "{… "+dict.length.to_text+" items}" else
truncated = dict.keys.take 5 . map k-> k.to_text + ": " + (_make_json_for_value (val.get k) level+1).to_text
prepared = if dict.length > 5 then truncated + ["… " + (dict.length - 5).to_text+ " items"] else truncated
"{" + (prepared.join ", ") + "}"
set : Hashset ->
if level != 0 then "{… "+set.length.to_text+" items}" else
truncated = set.to_vector.take 5 . map k-> (_make_json_for_value k level+1).to_text
prepared = if set.length > 5 then truncated + ["… " + (set.length - 5).to_text+ " items"] else truncated
"{" + (prepared.join ", ") + "}"
row : Row ->
if level != 0 then "Row{" + row.length + " columns}" else
truncated = row.column_names.take 5 . map _.to_text
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,9 @@ public Result getResult() {
distinctJson =
"["
+ distinct.stream()
.map(o -> JsonOperation.objectToJson(o, null))
.filter(JsonOperation::nativeSupport)
.filter(Objects::nonNull)
.map(o -> JsonOperation.INSTANCE.objectToJson(o))
.sorted()
.collect(Collectors.joining())
+ "]";
Expand Down
Loading
Loading