mirror of
https://github.com/MontFerret/ferret.git
synced 2025-11-23 21:54:45 +02:00
add IO::FS::READ function
This commit is contained in:
15
pkg/stdlib/io/fs/lib.go
Normal file
15
pkg/stdlib/io/fs/lib.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package fs
|
||||
|
||||
import (
|
||||
"github.com/MontFerret/ferret/pkg/runtime/core"
|
||||
)
|
||||
|
||||
// RegisterLib register `FS` namespace functions.
|
||||
func RegisterLib(ns core.Namespace) error {
|
||||
return ns.
|
||||
Namespace("FS").
|
||||
RegisterFunctions(
|
||||
core.NewFunctionsFromMap(map[string]core.Function{
|
||||
"READ": Read,
|
||||
}))
|
||||
}
|
||||
37
pkg/stdlib/io/fs/read.go
Normal file
37
pkg/stdlib/io/fs/read.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package fs
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io/ioutil"
|
||||
|
||||
"github.com/MontFerret/ferret/pkg/runtime/core"
|
||||
"github.com/MontFerret/ferret/pkg/runtime/values"
|
||||
"github.com/MontFerret/ferret/pkg/runtime/values/types"
|
||||
)
|
||||
|
||||
// Read reads from a given file.
|
||||
// @params path (String) - path to file to read from.
|
||||
// @returns data (Binary) - the read file in binary format.
|
||||
func Read(_ context.Context, args ...core.Value) (core.Value, error) {
|
||||
err := core.ValidateArgs(args, 1, 1)
|
||||
|
||||
if err != nil {
|
||||
return values.None, core.Error(err, "validate arguments number")
|
||||
}
|
||||
|
||||
err = core.ValidateType(args[0], types.String)
|
||||
|
||||
if err != nil {
|
||||
return values.None, core.Error(err, "validate [0] argument")
|
||||
}
|
||||
|
||||
path := args[0].String()
|
||||
|
||||
data, err := ioutil.ReadFile(path)
|
||||
|
||||
if err != nil {
|
||||
return values.None, core.Error(err, "read file")
|
||||
}
|
||||
|
||||
return values.NewBinary(data), nil
|
||||
}
|
||||
79
pkg/stdlib/io/fs/read_test.go
Normal file
79
pkg/stdlib/io/fs/read_test.go
Normal file
@@ -0,0 +1,79 @@
|
||||
package fs_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/MontFerret/ferret/pkg/runtime/core"
|
||||
"github.com/MontFerret/ferret/pkg/runtime/values"
|
||||
"github.com/MontFerret/ferret/pkg/runtime/values/types"
|
||||
"github.com/MontFerret/ferret/pkg/stdlib/io/fs"
|
||||
|
||||
. "github.com/smartystreets/goconvey/convey"
|
||||
)
|
||||
|
||||
func TestRead(t *testing.T) {
|
||||
|
||||
Convey("Arguments passed", t, func() {
|
||||
|
||||
Convey("No arguments passed", func() {
|
||||
out, err := fs.Read(context.Background())
|
||||
|
||||
So(out, ShouldEqual, values.None)
|
||||
So(err, ShouldBeError)
|
||||
})
|
||||
|
||||
Convey("Passed not a string", func() {
|
||||
args := []core.Value{values.NewInt(0)}
|
||||
out, err := fs.Read(context.Background(), args...)
|
||||
|
||||
So(out, ShouldEqual, values.None)
|
||||
So(err, ShouldBeError)
|
||||
})
|
||||
|
||||
Convey("Passed more that one argument", func() {
|
||||
args := []core.Value{
|
||||
values.NewString("filepath"),
|
||||
values.NewInt(0),
|
||||
}
|
||||
out, err := fs.Read(context.Background(), args...)
|
||||
|
||||
So(out, ShouldEqual, values.None)
|
||||
So(err, ShouldBeError)
|
||||
})
|
||||
})
|
||||
|
||||
Convey("Read from file", t, func() {
|
||||
|
||||
Convey("File exists", func() {
|
||||
file, err := ioutil.TempFile("", "readtest")
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
defer func() {
|
||||
file.Close()
|
||||
os.Remove(file.Name())
|
||||
}()
|
||||
|
||||
text := "s string"
|
||||
file.WriteString(text)
|
||||
|
||||
fname := values.NewString(file.Name())
|
||||
|
||||
out, err := fs.Read(context.Background(), fname)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
So(out.Type().ID(), ShouldEqual, types.Binary.ID())
|
||||
So(out.String(), ShouldEqual, text)
|
||||
})
|
||||
|
||||
Convey("File does not exist", func() {
|
||||
fname := values.NewString("not_exist.file")
|
||||
|
||||
out, err := fs.Read(context.Background(), fname)
|
||||
So(out, ShouldEqual, values.None)
|
||||
So(err, ShouldBeError)
|
||||
})
|
||||
})
|
||||
}
|
||||
18
pkg/stdlib/io/lib.go
Normal file
18
pkg/stdlib/io/lib.go
Normal file
@@ -0,0 +1,18 @@
|
||||
package io
|
||||
|
||||
import (
|
||||
"github.com/MontFerret/ferret/pkg/runtime/core"
|
||||
"github.com/MontFerret/ferret/pkg/stdlib/io/fs"
|
||||
)
|
||||
|
||||
// RegisterLib register `IO` namespace functions.
|
||||
func RegisterLib(ns core.Namespace) error {
|
||||
io := ns.Namespace("IO")
|
||||
|
||||
err := fs.RegisterLib(io)
|
||||
if err != nil {
|
||||
return core.Error(err, "register `FS`")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"github.com/MontFerret/ferret/pkg/stdlib/arrays"
|
||||
"github.com/MontFerret/ferret/pkg/stdlib/collections"
|
||||
"github.com/MontFerret/ferret/pkg/stdlib/html"
|
||||
"github.com/MontFerret/ferret/pkg/stdlib/io"
|
||||
"github.com/MontFerret/ferret/pkg/stdlib/math"
|
||||
"github.com/MontFerret/ferret/pkg/stdlib/objects"
|
||||
"github.com/MontFerret/ferret/pkg/stdlib/strings"
|
||||
@@ -41,5 +42,9 @@ func RegisterLib(ns core.Namespace) error {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := io.RegisterLib(ns); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return utils.RegisterLib(ns)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user