-
Notifications
You must be signed in to change notification settings - Fork 328
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial implementation of
Data.read_many
(#11490)
- Part of #11311 - Adds ability to read a list of files (Vector, Column, Table) into a Vector. - Reading into a Table of objects or merged will come in a next PR.
- Loading branch information
Showing
17 changed files
with
392 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
distribution/lib/Standard/Base/0.0.0-dev/src/Data/Read/Many_Files_List.enso
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import project.Data.Text.Text | ||
import project.Data.Vector.Vector | ||
|
||
## A common interface that represents a list of files that can be read. | ||
|
||
Various types (e.g. Vector, Column) can convert to this type to be able to be | ||
used in `Data.read_many`. | ||
type Many_Files_List | ||
## PRIVATE | ||
Value original_value paths_to_load:Vector | ||
|
||
## PRIVATE | ||
to_text self -> Text = | ||
"Many_Files_List "+self.original_value.to_text | ||
|
||
## PRIVATE | ||
to_display_text self -> Text = | ||
"Many_Files_List "+self.original_value.to_display_text | ||
|
||
## PRIVATE | ||
Many_Files_List.from (that : Vector) = | ||
Many_Files_List.Value that that |
78 changes: 78 additions & 0 deletions
78
distribution/lib/Standard/Base/0.0.0-dev/src/Data/Read/Return_As.enso
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import project.Any.Any | ||
import project.Data.Text.Text | ||
import project.Data.Read.Many_Files_List.Many_Files_List | ||
import project.Data.Vector.Vector | ||
import project.Error.Error | ||
import project.Errors.Common.Type_Error | ||
import project.Errors.Illegal_Argument.Illegal_Argument | ||
import project.Function.Function | ||
import project.Metadata.Display | ||
import project.Metadata.Widget | ||
import project.Nothing.Nothing | ||
import project.Panic.Panic | ||
from project.Data.Boolean import Boolean, False, True | ||
from project.Metadata.Choice import Option | ||
from project.Metadata.Widget import Single_Choice | ||
|
||
polyglot java import org.enso.base.read.ReadManyReturnSPI | ||
|
||
private _get_known_return_classes -> Vector = | ||
Vector.from_polyglot_array (ReadManyReturnSPI.get_types False) | ||
|
||
## A common interface that represents ways to return a list of files that have | ||
been read. | ||
type Return_As | ||
## PRIVATE | ||
Instance underlying | ||
|
||
## PRIVATE | ||
to_text self -> Text = self.underlying.to_text | ||
|
||
## PRIVATE | ||
to_display_text self -> Text = self.underlying.to_display_text | ||
|
||
## PRIVATE | ||
make_return self (input : Many_Files_List) (objects : Vector Any) = | ||
self.underlying.make_return input objects | ||
|
||
## PRIVATE | ||
Resolve an unresolved constructor to the actual type. | ||
private resolve value = case value of | ||
_ : Function -> | ||
types = _get_known_return_classes | ||
try_next idx = | ||
if idx >= types.length then Error.throw (Illegal_Argument.Error "Expected Return_As, but got a function.") else | ||
resolved = (types.at idx).resolve value | ||
if resolved.is_nothing then @Tail_Call try_next (idx + 1) else resolved | ||
try_next 0 | ||
_ : Return_As -> value | ||
_ -> Panic.throw (Type_Error.Error Return_As value "Expected `return` to be a Return_As type, but got {got}.") | ||
|
||
## PRIVATE | ||
default_widget : Widget | ||
default_widget = | ||
options = _get_known_return_classes.map .get_dropdown_options | ||
Single_Choice display=Display.Always values=options | ||
|
||
## PRIVATE | ||
type Return_As_Base | ||
## Will return a Vector of objects that were loaded. | ||
The order of the returned Vector is the same as in the input. | ||
Vector | ||
|
||
## PRIVATE | ||
get_dropdown_options : Vector Option | ||
get_dropdown_options = [Option "Vector" "..Vector"] | ||
|
||
## PRIVATE | ||
resolve value = | ||
Panic.catch Type_Error (value:Return_As_Base) _->Nothing | ||
|
||
## PRIVATE | ||
make_return self (input : Many_Files_List) (objects : Vector Any) = | ||
_ = input | ||
objects | ||
|
||
## PRIVATE | ||
Return_As.from (that : Return_As_Base) = | ||
Return_As.Instance that |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
distribution/lib/Standard/Table/0.0.0-dev/src/Internal/Read_Many_Helpers.enso
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
private | ||
|
||
from Standard.Base import all | ||
import Standard.Base.Data.Read.Many_Files_List.Many_Files_List | ||
import Standard.Base.Errors.Illegal_Argument.Illegal_Argument | ||
|
||
import project.Column.Column | ||
import project.Errors.Invalid_Value_Type | ||
import project.Table.Table | ||
import project.Value_Type.Value_Type | ||
|
||
find_files_list_in_table (that : Table) -> Many_Files_List = | ||
found_column = if that.column_count == 1 then that.at 0 else | ||
path_columns = that.select_columns "path" case_sensitivity=..Insensitive on_problems=..Report_Error | ||
not_found = path_columns.is_error || (path_columns.column_count == 0) | ||
if not_found then Error.throw (Illegal_Argument.Error "To use a Table as file list, it must be a single column or contain a `path` column (case insensitive).") else | ||
if path_columns.column_count > 1 then Error.throw (Illegal_Argument.Error "Multiple 'paths' column candidates found: "+path_columns.column_names.to_display_text+".") else | ||
path_columns.at 0 | ||
ensure_column_type_valid_to_be_files_list found_column <| | ||
Many_Files_List.Value that found_column.to_vector | ||
|
||
ensure_column_type_valid_to_be_files_list (column : Column) ~action = | ||
is_expected_type = case column.value_type of | ||
# Columns containing File objects will be Mixed | ||
Value_Type.Mixed -> True | ||
# Columns containing paths as Text will be Char | ||
Value_Type.Char _ _ -> True | ||
_ -> False | ||
if is_expected_type then action else | ||
Error.throw (Invalid_Value_Type.Column "Text or Mixed" column.value_type column.name) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
std-bits/base/src/main/java/org/enso/base/read/BaseReadManyReturnSPI.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package org.enso.base.read; | ||
|
||
@org.openide.util.lookup.ServiceProvider(service = ReadManyReturnSPI.class) | ||
public class BaseReadManyReturnSPI extends ReadManyReturnSPI { | ||
@Override | ||
protected String getModuleName() { | ||
return "Standard.Base.Data.Read.Return_As"; | ||
} | ||
|
||
@Override | ||
protected String getTypeName() { | ||
return "Return_As_Base"; | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
std-bits/base/src/main/java/org/enso/base/read/ReadManyReturnSPI.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package org.enso.base.read; | ||
|
||
import java.util.ServiceLoader; | ||
import org.enso.base.polyglot.EnsoMeta; | ||
import org.graalvm.polyglot.Value; | ||
|
||
public abstract class ReadManyReturnSPI { | ||
private static final ServiceLoader<ReadManyReturnSPI> loader = | ||
ServiceLoader.load(ReadManyReturnSPI.class, ReadManyReturnSPI.class.getClassLoader()); | ||
|
||
public static Value[] get_types(boolean refresh) { | ||
if (refresh) { | ||
loader.reload(); | ||
} | ||
return loader.stream().map(provider -> provider.get().getTypeObject()).toArray(Value[]::new); | ||
} | ||
|
||
public Value getTypeObject() { | ||
return EnsoMeta.getType(getModuleName(), getTypeName()); | ||
} | ||
|
||
protected abstract String getModuleName(); | ||
|
||
protected abstract String getTypeName(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.