Skip to content

Commit

Permalink
Tidy Standard.Base part 5 of n ... (hopefully the end...) (#3929)
Browse files Browse the repository at this point in the history
- Moved `Any`, `Error` and `Panic` to `Standard.Base`.
- Separated `Json` and `Range` extensions into own modules.
- Tidied `Case`, `Case_Sensitivity`, `Encoding`, `Matching`, `Regex_Matcher`, `Span`, `Text_Matcher`, `Text_Ordering` and `Text_Sub_Range` in `Standard.Base.Data.Text`.
- Tidied `Standard.Base.Data.Text.Extensions` and stopped it re-exporting anything.
- Tidied `Regex_Mode`. Renamed `Option` to `Regex_Option` and added type to export.
- Tidied up `Regex` space.
- Tidied up `Meta` space.
- Remove `Matching` from export.
- Moved `Standard.Base.Data.Boolean` to `Standard.Base.Boolean`.

# Important Notes
- Moved `to_json` and `to_default_visualization_data` from base types to extension methods.
  • Loading branch information
jdunkerley authored Dec 2, 2022
1 parent a2b57b4 commit 0ad70c6
Show file tree
Hide file tree
Showing 203 changed files with 3,516 additions and 3,686 deletions.
13 changes: 2 additions & 11 deletions distribution/lib/Standard/Base/0.0.0-dev/package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,13 @@ component-groups:
- Standard.Base.Data.list_directory
- Web:
exports:
- Standard.Base.Network.Http.new
- Standard.Base.Network.Http.fetch
- Standard.Base.Network.Http.get
- Standard.Base.Network.Http.post
- Standard.Base.Network.Http.post_form
- Standard.Base.Network.Http.post_json
- Standard.Base.Network.Http.put
- Standard.Base.Network.Http.put_json
- Standard.Base.Network.Http.head
- Standard.Base.Network.Http.options
- Standard.Base.Network.HTTP.HTTP.new
- Standard.Base.Network.HTTP.HTTP.fetch
- Parse:
exports:
- Standard.Base.Data.Json.Json.parse
- Standard.Base.Data.Text.Regex.compile
- Standard.Base.Data.Text.Regex.escape
- Standard.Base.Data.Text.Regex.from_flags
- Select:
exports:
- Standard.Base.Data.Vector.Vector.tail
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import project.Data.Ordering.Ordering
import project.Data.Pair.Pair
import project.Data.Range.Extensions
import project.Data.Text.Text
import project.Error.Error
import project.Nothing.Nothing
import project.Meta

from project.Data.Boolean import Boolean, True, False
from project.Data.Json import all
from project.Data.Range import all
from project.Error.Common import Error, dataflow_error_handler

## Any is the universal top-type, with all other types being subsumed by it.

Expand Down Expand Up @@ -83,18 +82,18 @@ type Any
self_meta = Meta.meta self
that_meta = Meta.meta that
case Pair.new self_meta that_meta of
Pair.Value (Meta.Atom_Data _) (Meta.Atom_Data _) ->
Pair.Value (Meta.Atom.Value _) (Meta.Atom.Value _) ->
c_1 = self_meta.constructor ...
c_2 = that_meta.constructor ...
if Meta.is_same_object c_1 c_2 . not then False else
f_1 = self_meta.fields
f_2 = that_meta.fields
0.up_to f_1.length . all i-> (f_1.at i) == (f_2.at i)
Pair.Value (Meta.Error_Data _) (Meta.Error_Data _) -> self_meta.payload == that_meta.payload
Pair.Value (Meta.Polyglot_Data o_1) (Meta.Polyglot_Data o_2) ->
langs_match = (self_meta.get_language == Meta.Java) && (that_meta.get_language == Meta.Java)
Pair.Value (Meta.Error.Value _) (Meta.Error.Value _) -> self_meta.payload == that_meta.payload
Pair.Value (Meta.Polyglot.Value o_1) (Meta.Polyglot.Value o_2) ->
langs_match = (self_meta.get_language == Meta.Language.Java) && (that_meta.get_language == Meta.Language.Java)
if langs_match.not then False else o_1.equals o_2
Pair.Value (Meta.Unresolved_Symbol_Data _) (Meta.Unresolved_Symbol_Data _) ->
Pair.Value (Meta.Unresolved_Symbol.Value _) (Meta.Unresolved_Symbol.Value _) ->
(self_meta.name == that_meta.name) && (self_meta.scope == that_meta.scope)
## Constructor comparison is covered by the identity equality.
Primitive objects should define their own equality.
Expand Down Expand Up @@ -261,13 +260,13 @@ type Any
matching type. By default this is identity.

> Example
Catching an `Illegal_Argument_Error` and returning its message.
Catching an `Illegal_Argument` and returning its message.

from Standard.Base import all

example_catch =
error = Error.throw (Illegal_Argument_Error_Data "My message")
error.catch Illegal_Argument_Error_Data (err -> err.message)
error = Error.throw (Illegal_Argument.Error "My message")
error.catch Illegal_Argument.Error (err -> err.message)

> Example
Catching any dataflow error and turning it into a regular value.
Expand Down Expand Up @@ -394,22 +393,3 @@ type Any
(+1 >> *2) 2
>> : (Any -> Any) -> (Any -> Any) -> Any -> Any
>> self ~that = x -> that (self x)

## UNSTABLE
ADVANCED

Returns a Text used to display this value in the IDE.

The particular representation is left unspecified and subject to change in
the future. The current implementation uses JSON serialization as the
default.

Types defining their own versions of this method should ensure that the
result is reasonably small and that the operation is quick to compute.

> Example
Converting the number `2` into visualization data.

2.to_default_visualization_data
to_default_visualization_data : Text
to_default_visualization_data self = self.to_json.to_text
2 changes: 1 addition & 1 deletion distribution/lib/Standard/Base/0.0.0-dev/src/Data.enso
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import project.Data.Any.Any
import project.Any.Any
import project.Data.Text.Encoding.Encoding
import project.Data.Text.Text
import project.Data.Vector.Vector
Expand Down
23 changes: 3 additions & 20 deletions distribution/lib/Standard/Base/0.0.0-dev/src/Data/Array.enso
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import project.Data.Any.Any
import project.Any.Any
import project.Data.Numbers.Integer
import project.Data.Ordering.Ordering
import project.Data.Range.Extensions
import project.Data.Text.Text
import project.Data.Vector.Vector
import project.Error.Common.Panic
import project.Meta
import project.Nothing.Nothing
import project.Panic.Panic

from project.Data.Boolean import Boolean, False
from project.Data.Range import all

## The type of primitive mutable arrays.
@Builtin_Type
Expand Down Expand Up @@ -168,20 +168,3 @@ type Array
eq_at i = self.at i == that.at i
Panic.catch_primitive handler=(_ -> False)
if self.length == that.length then 0.up_to self.length . all eq_at else False

## UNSTABLE
ADVANCED

Returns a Text used to display this value in the IDE.

The particular representation is left unspecified and subject to change in
the future. The current implementation uses JSON serialization as the
default.

> Example
Converting an array to its default visualization representation.

[1, 2, 3, 4].to_array.to_default_visualization_data
to_default_visualization_data : Text
to_default_visualization_data self =
Vector.from_polyglot_array self . to_default_visualization_data
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import project.Data.Any.Any
import project.Any.Any
import project.Data.Array.Array
import project.Data.Numbers.Integer

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import project.Data.Any.Any
import project.Any.Any
import project.Data.Ordering.Ordering
import project.Nothing.Nothing

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import project.Data.Any.Any
import project.Any.Any
import project.Data.Text.Extensions
import project.Data.Text.Regex
import project.Data.Text.Text
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import project.Data.Any.Any
import project.Any.Any
import project.Data.Numbers.Integer
import project.Data.Range.Range
import project.Data.Range.Extensions
import project.Data.Vector.Vector
import project.Error.Error
import project.Math
import project.Panic.Panic
import project.Random
import project.Runtime.Ref.Ref

from project.Data.Boolean import Boolean, True, False
from project.Data.Range import all
from project.Error.Common import Error, Panic, Index_Out_Of_Bounds_Error_Data, Illegal_Argument_Error_Data
from project.Error.Common import Index_Out_Of_Bounds_Error_Data, Illegal_Argument

type Index_Sub_Range
## Select the first `count` items.
Expand Down Expand Up @@ -64,8 +67,8 @@ resolve_ranges ranges length =
if (actual_index < 0) || (actual_index >= length) then Panic.throw (Index_Out_Of_Bounds_Error_Data descriptor length) else
actual_index
Range.Value start end step ->
if step <= 0 then Panic.throw (Illegal_Argument_Error_Data "Range step must be positive.") else
if (start < 0) || (end < 0) then Panic.throw (Illegal_Argument_Error_Data "Range start and end must not be negative.") else
if step <= 0 then Panic.throw (Illegal_Argument.Error "Range step must be positive.") else
if (start < 0) || (end < 0) then Panic.throw (Illegal_Argument.Error "Range start and end must not be negative.") else
if start >= length then Panic.throw (Index_Out_Of_Bounds_Error_Data start length) else
actual_end = Math.min end length
if actual_end < start then start.up_to start . with_step step else
Expand Down Expand Up @@ -161,7 +164,7 @@ take_helper length at single_slice slice_ranges index_sub_range = case index_sub
end = 0.up_to length . find i-> (predicate (at i)).not
true_end = if end.is_nothing then length else end
single_slice 0 true_end
Index_Sub_Range.By_Index one_or_many_descriptors -> Panic.recover [Index_Out_Of_Bounds_Error_Data, Illegal_Argument_Error_Data] <|
Index_Sub_Range.By_Index one_or_many_descriptors -> Panic.recover [Index_Out_Of_Bounds_Error_Data, Illegal_Argument.Error] <|
indices = case one_or_many_descriptors of
_ : Vector -> one_or_many_descriptors
_ -> [one_or_many_descriptors]
Expand All @@ -172,7 +175,7 @@ take_helper length at single_slice slice_ranges index_sub_range = case index_sub
indices_to_take = Random.random_indices length count rng
take_helper length at single_slice slice_ranges (Index_Sub_Range.By_Index indices_to_take)
Index_Sub_Range.Every step start ->
if step <= 0 then Error.throw (Illegal_Argument_Error_Data "Step within Every must be positive.") else
if step <= 0 then Error.throw (Illegal_Argument.Error "Step within Every must be positive.") else
if start >= length then single_slice 0 0 else
range = start.up_to length . with_step step
take_helper length at single_slice slice_ranges (Index_Sub_Range.By_Index range)
Expand Down Expand Up @@ -211,7 +214,7 @@ drop_helper length at single_slice slice_ranges index_sub_range = case index_sub
end = 0.up_to length . find i-> (predicate (at i)).not
true_end = if end.is_nothing then length else end
single_slice true_end length
Index_Sub_Range.By_Index one_or_many_descriptors -> Panic.recover [Index_Out_Of_Bounds_Error_Data, Illegal_Argument_Error_Data] <|
Index_Sub_Range.By_Index one_or_many_descriptors -> Panic.recover [Index_Out_Of_Bounds_Error_Data, Illegal_Argument.Error] <|
indices = case one_or_many_descriptors of
_ : Vector -> one_or_many_descriptors
_ -> [one_or_many_descriptors]
Expand All @@ -224,7 +227,7 @@ drop_helper length at single_slice slice_ranges index_sub_range = case index_sub
indices_to_drop = Random.random_indices length count rng
drop_helper length at single_slice slice_ranges (Index_Sub_Range.By_Index indices_to_drop)
Index_Sub_Range.Every step start ->
if step <= 0 then Error.throw (Illegal_Argument_Error_Data "Step within Every must be positive.") else
if step <= 0 then Error.throw (Illegal_Argument.Error "Step within Every must be positive.") else
if start >= length then single_slice 0 length else
range = start.up_to length . with_step step
drop_helper length at single_slice slice_ranges (Index_Sub_Range.By_Index range)
78 changes: 6 additions & 72 deletions distribution/lib/Standard/Base/0.0.0-dev/src/Data/Json.enso
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import project.Data.Any.Any
import project.Any.Any
import project.Data.Json.Internal
import project.Data.Map.Map
import project.Data.Map.No_Value_For_Key
import project.Data.Range.Extensions
import project.Data.Text.Text
import project.Data.Vector.Vector
import project.Error.Error
import project.Meta
import project.Nothing.Nothing
import project.Panic.Panic

from project.Data.Boolean import Boolean, True, False
from project.Data.Range import all
from project.Error.Common import Panic, Error, Illegal_Argument_Error_Data
from project.Error.Common import Illegal_Argument

## Represents a JSON structure.
type Json
Expand Down Expand Up @@ -164,7 +166,7 @@ type Json
Json.Object _ -> self.fields.get field . map_error case _ of
No_Value_For_Key.Error _ -> No_Such_Field.Error field
x -> x
_ -> Error.throw (Illegal_Argument_Error_Data "Json.get: self must be an Object")
_ -> Error.throw (Illegal_Argument.Error "Json.get: self must be an Object")

## UNSTABLE

Expand Down Expand Up @@ -237,71 +239,3 @@ type Marshalling_Error
"Type mismatch error: the json with type `" + json_text + "` did not match the format `" + format_text + "`."
Marshalling_Error.Missing_Field _ field _ ->
"Missing field in Json: the field `" + field.to_text "` was missing in the json."

## ALIAS To JSON

Generically converts an atom into a JSON object.

The input atom is converted into a JSON object, with a `"type"` field set to
the atom's type name and all other fields serialized with their name as
object key and the value as the object value.

> Example
Convert a vector to JSON.
[1, 2, 3, 4].to_json
Any.to_json : Json
Any.to_json self =
m = Meta.meta self
case m of
Meta.Atom_Data _ ->
cons = Meta.Constructor_Data m.constructor
fs = m.fields
fnames = cons.fields
json_fs = 0.up_to fnames.length . fold Map.empty m-> i->
m.insert (fnames.at i) (fs.at i . to_json)
with_tp = json_fs . insert "type" (Json.String cons.name)
Json.Object with_tp
Meta.Constructor_Data _ ->
Json.Object (Map.empty . insert "type" (Json.String m.name))

## The following two cases cannot be handled generically and should
instead define their own `to_json` implementations.
Meta.Polyglot_Data _ -> Json.Null
Meta.Primitive_Data _ -> Json.Null

## Text to JSON conversion.

> Example
Convert the text "Hello World!" to JSON.

"Hello World!".to_json
> Example
Convert the text "cześć" to JSON.

"cześć".to_json
Text.to_json : Json
Text.to_json self = Json.String self

## Method used by object builders to convert a value into a valid JSON key.

> Example
Ensure that the text "foo" is a JSON key.
"foo".to_json_key
Text.to_json_key : Text
Text.to_json_key self = self

## Convert a boolean to JSON.

> Example
Convert `True` to JSON.
True.to_json
Boolean.to_json : Json
Boolean.to_json self = Json.Boolean self

## Convert `Nothing` to JSON.

> Example
Convert `Nothing` to JSON.
Nothing.to_json
Nothing.to_json : Json
Nothing.to_json self = Json.Null
Loading

0 comments on commit 0ad70c6

Please sign in to comment.