1
0
mirror of https://github.com/MontFerret/ferret.git synced 2025-02-13 13:58:32 +02:00

Refactored types lib

This commit is contained in:
Tim Voronov 2018-10-07 01:07:44 -04:00
parent e64ad4ec0e
commit e0a7a8e6a1
27 changed files with 843 additions and 214 deletions

View File

@ -1774,21 +1774,21 @@ func TestParam(t *testing.T) {
})
}
func TestHtml(t *testing.T) {
Convey("Should load a document", t, func() {
c := compiler.New()
out, err := c.MustCompile(`
LET doc = DOCUMENT("http://getbootstrap.com/docs/4.1/components/collapse/", true)
CLICK(doc, "#headingTwo > h5 > button")
WAIT_CLASS(doc, "#collapseTwo", "bar")
RETURN TRUE
`).Run(context.Background())
So(err, ShouldBeNil)
So(string(out), ShouldEqual, `"int"`)
})
}
//func TestHtml(t *testing.T) {
// Convey("Should load a document", t, func() {
// c := compiler.New()
//
// out, err := c.MustCompile(`
//LET doc = DOCUMENT("http://getbootstrap.com/docs/4.1/components/collapse/", true)
//
//CLICK(doc, "#headingTwo > h5 > button")
//WAIT_CLASS(doc, "#collapseTwo", "bar")
//
//RETURN TRUE
// `).Run(context.Background())
//
// So(err, ShouldBeNil)
//
// So(string(out), ShouldEqual, `"int"`)
// })
//}

View File

@ -12,6 +12,10 @@ type DateTime struct {
time.Time
}
var ZeroDateTime = DateTime{
time.Time{},
}
func NewCurrentDateTime() DateTime {
return DateTime{time.Now()}
}

View File

@ -1,73 +0,0 @@
package types
import (
"context"
"github.com/MontFerret/ferret/pkg/runtime/collections"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values"
)
func ToBool(_ context.Context, inputs ...core.Value) (core.Value, error) {
if len(inputs) == 0 {
return values.None, core.ErrMissedArgument
}
return values.ParseBoolean(inputs[0].Unwrap())
}
func ToInt(_ context.Context, inputs ...core.Value) (core.Value, error) {
if len(inputs) == 0 {
return values.None, core.ErrMissedArgument
}
return values.ParseInt(inputs[0].Unwrap())
}
func ToFloat(_ context.Context, inputs ...core.Value) (core.Value, error) {
if len(inputs) == 0 {
return values.None, core.ErrMissedArgument
}
return values.ParseFloat(inputs[0].Unwrap())
}
func ToString(_ context.Context, inputs ...core.Value) (core.Value, error) {
if len(inputs) == 0 {
return values.None, core.ErrMissedArgument
}
return values.NewString(inputs[0].String()), nil
}
func ToDateTime(_ context.Context, inputs ...core.Value) (core.Value, error) {
if len(inputs) == 0 {
return values.None, core.ErrMissedArgument
}
return values.ParseDateTime(inputs[0].String())
}
func ToArray(_ context.Context, inputs ...core.Value) (core.Value, error) {
if len(inputs) == 0 {
return values.None, core.ErrMissedArgument
}
value := inputs[0]
switch value.Type() {
case core.BooleanType,
core.IntType,
core.FloatType,
core.StringType,
core.DateTimeType,
core.HTMLElementType,
core.HTMLDocumentType:
return values.NewArrayWith(value), nil
case core.ArrayType:
return value, nil
case core.ObjectType:
return collections.ToArray(collections.NewObjectIterator(value.(*values.Object)))
default:
return values.NewArray(0), nil
}
}

View File

@ -1,107 +0,0 @@
package types
import (
"context"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values"
)
func isTypeof(value core.Value, ctype core.Type) core.Value {
return values.NewBoolean(value.Type() == ctype)
}
func IsNone(_ context.Context, inputs ...core.Value) (core.Value, error) {
if len(inputs) == 0 {
return values.None, core.ErrMissedArgument
}
return isTypeof(inputs[0], core.NoneType), nil
}
func IsBool(_ context.Context, inputs ...core.Value) (core.Value, error) {
if len(inputs) == 0 {
return values.None, core.ErrMissedArgument
}
return isTypeof(inputs[0], core.BooleanType), nil
}
func IsInt(_ context.Context, inputs ...core.Value) (core.Value, error) {
if len(inputs) == 0 {
return values.None, core.ErrMissedArgument
}
return isTypeof(inputs[0], core.IntType), nil
}
func IsFloat(_ context.Context, inputs ...core.Value) (core.Value, error) {
if len(inputs) == 0 {
return values.None, core.ErrMissedArgument
}
return isTypeof(inputs[0], core.FloatType), nil
}
func IsString(_ context.Context, inputs ...core.Value) (core.Value, error) {
if len(inputs) == 0 {
return values.None, core.ErrMissedArgument
}
return isTypeof(inputs[0], core.StringType), nil
}
func IsDateTime(_ context.Context, inputs ...core.Value) (core.Value, error) {
if len(inputs) == 0 {
return values.None, core.ErrMissedArgument
}
return isTypeof(inputs[0], core.DateTimeType), nil
}
func IsArray(_ context.Context, inputs ...core.Value) (core.Value, error) {
if len(inputs) == 0 {
return values.None, core.ErrMissedArgument
}
return isTypeof(inputs[0], core.ArrayType), nil
}
func IsObject(_ context.Context, inputs ...core.Value) (core.Value, error) {
if len(inputs) == 0 {
return values.None, core.ErrMissedArgument
}
return isTypeof(inputs[0], core.ObjectType), nil
}
func IsHTMLElement(_ context.Context, inputs ...core.Value) (core.Value, error) {
if len(inputs) == 0 {
return values.None, core.ErrMissedArgument
}
return isTypeof(inputs[0], core.HTMLElementType), nil
}
func IsHTMLDocument(_ context.Context, inputs ...core.Value) (core.Value, error) {
if len(inputs) == 0 {
return values.None, core.ErrMissedArgument
}
return isTypeof(inputs[0], core.HTMLDocumentType), nil
}
func IsBinary(_ context.Context, inputs ...core.Value) (core.Value, error) {
if len(inputs) == 0 {
return values.None, core.ErrMissedArgument
}
return isTypeof(inputs[0], core.BinaryType), nil
}
func TypeName(_ context.Context, inputs ...core.Value) (core.Value, error) {
if len(inputs) == 0 {
return values.None, core.ErrMissedArgument
}
return values.NewString(inputs[0].Type().String()), nil
}

View File

@ -0,0 +1,22 @@
package types
import (
"context"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values"
)
/*
* Checks whether value is an array value.
* @param value (Value) - Input value of arbitrary type.
* @returns (Boolean) - Returns true if value is array, otherwise false.
*/
func IsArray(_ context.Context, args ...core.Value) (core.Value, error) {
err := core.ValidateArgs(args, 1, 1)
if err != nil {
return values.None, err
}
return isTypeof(args[0], core.ArrayType), nil
}

View File

@ -0,0 +1,22 @@
package types
import (
"context"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values"
)
/*
* Checks whether value is a binary value.
* @param value (Value) - Input value of arbitrary type.
* @returns (Boolean) - Returns true if value is binary, otherwise false.
*/
func IsBinary(_ context.Context, args ...core.Value) (core.Value, error) {
err := core.ValidateArgs(args, 1, 1)
if err != nil {
return values.None, err
}
return isTypeof(args[0], core.BinaryType), nil
}

View File

@ -0,0 +1,22 @@
package types
import (
"context"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values"
)
/*
* Checks whether value is a boolean value.
* @param value (Value) - Input value of arbitrary type.
* @returns (Boolean) - Returns true if value is boolean, otherwise false.
*/
func IsBool(_ context.Context, args ...core.Value) (core.Value, error) {
err := core.ValidateArgs(args, 1, 1)
if err != nil {
return values.None, err
}
return isTypeof(args[0], core.BooleanType), nil
}

View File

@ -0,0 +1,22 @@
package types
import (
"context"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values"
)
/*
* Checks whether value is a date time value.
* @param value (Value) - Input value of arbitrary type.
* @returns (Boolean) - Returns true if value is date time, otherwise false.
*/
func IsDateTime(_ context.Context, args ...core.Value) (core.Value, error) {
err := core.ValidateArgs(args, 1, 1)
if err != nil {
return values.None, err
}
return isTypeof(args[0], core.DateTimeType), nil
}

View File

@ -0,0 +1,22 @@
package types
import (
"context"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values"
)
/*
* Checks whether value is a float value.
* @param value (Value) - Input value of arbitrary type.
* @returns (Boolean) - Returns true if value is float, otherwise false.
*/
func IsFloat(_ context.Context, args ...core.Value) (core.Value, error) {
err := core.ValidateArgs(args, 1, 1)
if err != nil {
return values.None, err
}
return isTypeof(args[0], core.FloatType), nil
}

View File

@ -0,0 +1,22 @@
package types
import (
"context"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values"
)
/*
* Checks whether value is a HTMLDocument value.
* @param value (Value) - Input value of arbitrary type.
* @returns (Boolean) - Returns true if value is HTMLDocument, otherwise false.
*/
func IsHTMLDocument(_ context.Context, args ...core.Value) (core.Value, error) {
err := core.ValidateArgs(args, 1, 1)
if err != nil {
return values.None, err
}
return isTypeof(args[0], core.HTMLDocumentType), nil
}

View File

@ -0,0 +1,22 @@
package types
import (
"context"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values"
)
/*
* Checks whether value is a HTMLElement value.
* @param value (Value) - Input value of arbitrary type.
* @returns (Boolean) - Returns true if value is HTMLElement, otherwise false.
*/
func IsHTMLElement(_ context.Context, args ...core.Value) (core.Value, error) {
err := core.ValidateArgs(args, 1, 1)
if err != nil {
return values.None, err
}
return isTypeof(args[0], core.HTMLElementType), nil
}

View File

@ -0,0 +1,22 @@
package types
import (
"context"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values"
)
/*
* Checks whether value is a int value.
* @param value (Value) - Input value of arbitrary type.
* @returns (Boolean) - Returns true if value is int, otherwise false.
*/
func IsInt(_ context.Context, args ...core.Value) (core.Value, error) {
err := core.ValidateArgs(args, 1, 1)
if err != nil {
return values.None, err
}
return isTypeof(args[0], core.IntType), nil
}

View File

@ -0,0 +1,22 @@
package types
import (
"context"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values"
)
/*
* Checks whether value is a none value.
* @param value (Value) - Input value of arbitrary type.
* @returns (Boolean) - Returns true if value is none, otherwise false.
*/
func IsNone(_ context.Context, args ...core.Value) (core.Value, error) {
err := core.ValidateArgs(args, 1, 1)
if err != nil {
return values.None, err
}
return isTypeof(args[0], core.NoneType), nil
}

View File

@ -0,0 +1,22 @@
package types
import (
"context"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values"
)
/*
* Checks whether value is an object value.
* @param value (Value) - Input value of arbitrary type.
* @returns (Boolean) - Returns true if value is object, otherwise false.
*/
func IsObject(_ context.Context, args ...core.Value) (core.Value, error) {
err := core.ValidateArgs(args, 1, 1)
if err != nil {
return values.None, err
}
return isTypeof(args[0], core.ObjectType), nil
}

View File

@ -0,0 +1,22 @@
package types
import (
"context"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values"
)
/*
* Checks whether value is a string value.
* @param value (Value) - Input value of arbitrary type.
* @returns (Boolean) - Returns true if value is string, otherwise false.
*/
func IsString(_ context.Context, args ...core.Value) (core.Value, error) {
err := core.ValidateArgs(args, 1, 1)
if err != nil {
return values.None, err
}
return isTypeof(args[0], core.StringType), nil
}

View File

@ -1,6 +1,9 @@
package types
import "github.com/MontFerret/ferret/pkg/runtime/core"
import (
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values"
)
func NewLib() map[string]core.Function {
return map[string]core.Function{
@ -24,3 +27,7 @@ func NewLib() map[string]core.Function {
"TYPENAME": TypeName,
}
}
func isTypeof(value core.Value, ctype core.Type) core.Value {
return values.NewBoolean(core.IsTypeOf(value, ctype))
}

View File

@ -0,0 +1,54 @@
package types
import (
"context"
"github.com/MontFerret/ferret/pkg/runtime/collections"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values"
)
/*
* Takes an input value of any type and convert it into an array value.
* @param (Value) - Input value of arbitrary type.
* @returns (Array)
* None is converted to an empty array
* Boolean values, numbers and strings are converted to an array containing the original value as its single element
* Arrays keep their original value
* Objects / HTML nodes are converted to an array containing their attribute values as array elements.
*/
func ToArray(_ context.Context, args ...core.Value) (core.Value, error) {
err := core.ValidateArgs(args, 1, 1)
if err != nil {
return values.None, err
}
arg := args[0]
switch arg.Type() {
case core.BooleanType,
core.IntType,
core.FloatType,
core.StringType,
core.DateTimeType:
return values.NewArrayWith(arg), nil
case core.HTMLElementType,
core.HTMLDocumentType:
val := arg.(values.HTMLNode)
attrs := val.GetAttributes()
obj, ok := attrs.(*values.Object)
if !ok {
return values.NewArray(0), nil
}
return collections.ToArray(collections.NewObjectIterator(obj))
case core.ArrayType:
return arg, nil
case core.ObjectType:
return collections.ToArray(collections.NewObjectIterator(arg.(*values.Object)))
default:
return values.NewArray(0), nil
}
}

View File

@ -0,0 +1,67 @@
package types
import (
"context"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values"
)
/*
* Takes an input value of any type and converts it into the appropriate boolean value.
* @param value (Value) - Input value of arbitrary type.
* @returns (Boolean) -
* None is converted to false
* Numbers are converted to true, except for 0, which is converted to false
* Strings are converted to true if they are non-empty, and to false otherwise
* Dates are converted to true if they are not zero, and to false otherwise
* Arrays are always converted to true (even if empty)
* Objects / HtmlNodes / Binary are always converted to true
*/
func ToBool(_ context.Context, args ...core.Value) (core.Value, error) {
err := core.ValidateArgs(args, 1, 1)
if err != nil {
return values.None, err
}
arg := args[0]
switch arg.Type() {
case core.BooleanType:
return arg, nil
case core.IntType:
val := arg.(values.Int)
if val != 0 {
return values.True, nil
}
return values.False, nil
case core.FloatType:
val := arg.(values.Float)
if val != 0 {
return values.True, nil
}
return values.False, nil
case core.StringType:
if arg.String() != "" {
return values.True, nil
}
return values.False, nil
case core.DateTimeType:
val := arg.(values.DateTime)
if !val.IsZero() {
return values.True, nil
}
return values.False, nil
case core.NoneType:
return values.False, nil
default:
return values.True, nil
}
}

View File

@ -0,0 +1,177 @@
package types_test
import (
"context"
"github.com/MontFerret/ferret/pkg/runtime/values"
"github.com/MontFerret/ferret/pkg/stdlib/types"
. "github.com/smartystreets/goconvey/convey"
"testing"
)
func TestToBool(t *testing.T) {
Convey("Bool", t, func() {
Convey("When true should return true", func() {
out, err := types.ToBool(
context.Background(),
values.True,
)
So(err, ShouldBeNil)
So(out, ShouldEqual, values.True)
})
Convey("When false should return false", func() {
out, err := types.ToBool(
context.Background(),
values.False,
)
So(err, ShouldBeNil)
So(out, ShouldEqual, values.False)
})
})
Convey("Int", t, func() {
Convey("When > 0 should return true", func() {
out, err := types.ToBool(
context.Background(),
values.NewInt(1),
)
So(err, ShouldBeNil)
So(out, ShouldEqual, values.True)
})
Convey("When < 0 should return true", func() {
out, err := types.ToBool(
context.Background(),
values.NewInt(-1),
)
So(err, ShouldBeNil)
So(out, ShouldEqual, values.True)
})
Convey("When 0 should return false", func() {
out, err := types.ToBool(
context.Background(),
values.ZeroInt,
)
So(err, ShouldBeNil)
So(out, ShouldEqual, values.False)
})
})
Convey("Float", t, func() {
Convey("When > 0 should return true", func() {
out, err := types.ToBool(
context.Background(),
values.NewFloat(1.1),
)
So(err, ShouldBeNil)
So(out, ShouldEqual, values.True)
})
Convey("When < 0 should return true", func() {
out, err := types.ToBool(
context.Background(),
values.NewFloat(-1.1),
)
So(err, ShouldBeNil)
So(out, ShouldEqual, values.True)
})
Convey("When 0 should return false", func() {
out, err := types.ToBool(
context.Background(),
values.ZeroFloat,
)
So(err, ShouldBeNil)
So(out, ShouldEqual, values.False)
})
})
Convey("String", t, func() {
Convey("When != '' should return true", func() {
out, err := types.ToBool(
context.Background(),
values.NewString("foobar"),
)
So(err, ShouldBeNil)
So(out, ShouldEqual, values.True)
})
Convey("When == '' should return false", func() {
out, err := types.ToBool(
context.Background(),
values.NewString(""),
)
So(err, ShouldBeNil)
So(out, ShouldEqual, values.False)
})
})
Convey("DateTime", t, func() {
Convey("When > 0 should return true", func() {
out, err := types.ToBool(
context.Background(),
values.NewCurrentDateTime(),
)
So(err, ShouldBeNil)
So(out, ShouldEqual, values.True)
})
Convey("When == 0 should return false", func() {
out, err := types.ToBool(
context.Background(),
values.ZeroDateTime,
)
So(err, ShouldBeNil)
So(out, ShouldEqual, values.False)
})
})
Convey("None", t, func() {
Convey("Should return false", func() {
out, err := types.ToBool(
context.Background(),
values.None,
)
So(err, ShouldBeNil)
So(out, ShouldEqual, values.False)
})
})
Convey("Array", t, func() {
Convey("Should return true", func() {
out, err := types.ToBool(
context.Background(),
values.NewArray(0),
)
So(err, ShouldBeNil)
So(out, ShouldEqual, values.True)
})
})
Convey("Object", t, func() {
Convey("Should return true", func() {
out, err := types.ToBool(
context.Background(),
values.NewObject(),
)
So(err, ShouldBeNil)
So(out, ShouldEqual, values.True)
})
})
}

View File

@ -0,0 +1,22 @@
package types
import (
"context"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values"
)
/*
* Takes an input value of any type and converts it into the appropriate date time value.
* @param value (Value) - Input value of arbitrary type.
* @returns (DateTime) - Parsed date time.
*/
func ToDateTime(_ context.Context, args ...core.Value) (core.Value, error) {
err := core.ValidateArgs(args, 1, 1)
if err != nil {
return values.None, err
}
return values.ParseDateTime(args[0].String())
}

View File

@ -0,0 +1,86 @@
package types
import (
"context"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values"
"strconv"
)
/*
* Takes an input value of any type and convert it into a float value.
* @param value (Value) - Input value of arbitrary type.
* @returns (Float) -
* None and false are converted to the value 0
* true is converted to 1
* Numbers keep their original value
* Strings are converted to their numeric equivalent if the string contains a valid representation of a number.
* String values that do not contain any valid representation of a number will be converted to the number 0.
* An empty array is converted to 0, an array with one member is converted into the result of TO_NUMBER() for its sole member.
* An array with two or more members is converted to the number 0.
* An object / HTML node is converted to the number 0.
*/
func ToFloat(ctx context.Context, args ...core.Value) (core.Value, error) {
err := core.ValidateArgs(args, 1, 1)
if err != nil {
return values.None, err
}
arg := args[0]
switch arg.Type() {
case core.BooleanType:
val := arg.(values.Boolean)
if val {
return values.NewFloat(1), nil
}
return values.ZeroFloat, nil
case core.IntType:
val := arg.(values.Int)
return values.Float(val), nil
case core.FloatType:
return arg, nil
case core.StringType:
str := arg.String()
if str == "" {
return values.ZeroFloat, nil
}
num, err := strconv.ParseFloat(str, 64)
if err != nil {
return values.ZeroFloat, nil
}
return values.NewFloat(num), nil
case core.DateTimeType:
val := arg.(values.DateTime)
if val.IsZero() {
return values.ZeroFloat, nil
}
return values.NewFloat(float64(val.Unix())), nil
case core.NoneType:
return values.ZeroFloat, nil
case core.ArrayType:
val := arg.(*values.Array)
if val.Length() == 0 {
return values.ZeroFloat, nil
}
if val.Length() == 1 {
return ToFloat(ctx, val.Get(0))
}
return values.ZeroFloat, nil
default:
return values.ZeroFloat, nil
}
}

View File

@ -0,0 +1,86 @@
package types
import (
"context"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values"
"strconv"
)
/*
* Takes an input value of any type and convert it into an integer value.
* @param value (Value) - Input value of arbitrary type.
* @returns (Int) -
* None and false are converted to the value 0
* true is converted to 1
* Numbers keep their original value
* Strings are converted to their numeric equivalent if the string contains a valid representation of a number.
* String values that do not contain any valid representation of a number will be converted to the number 0.
* An empty array is converted to 0, an array with one member is converted into the result of TO_NUMBER() for its sole member.
* An array with two or more members is converted to the number 0.
* An object / HTML node is converted to the number 0.
*/
func ToInt(ctx context.Context, args ...core.Value) (core.Value, error) {
err := core.ValidateArgs(args, 1, 1)
if err != nil {
return values.None, err
}
arg := args[0]
switch arg.Type() {
case core.BooleanType:
val := arg.(values.Boolean)
if val {
return values.NewInt(1), nil
}
return values.ZeroInt, nil
case core.IntType:
return arg, nil
case core.FloatType:
val := arg.(values.Float)
return values.Int(val), nil
case core.StringType:
str := arg.String()
if str == "" {
return values.ZeroInt, nil
}
num, err := strconv.Atoi(str)
if err != nil {
return values.ZeroInt, nil
}
return values.NewInt(num), nil
case core.DateTimeType:
val := arg.(values.DateTime)
if val.IsZero() {
return values.ZeroInt, nil
}
return values.NewInt(int(val.Unix())), nil
case core.NoneType:
return values.ZeroInt, nil
case core.ArrayType:
val := arg.(*values.Array)
if val.Length() == 0 {
return values.ZeroInt, nil
}
if val.Length() == 1 {
return ToInt(ctx, val.Get(0))
}
return values.ZeroInt, nil
default:
return values.ZeroInt, nil
}
}

View File

@ -0,0 +1,22 @@
package types
import (
"context"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values"
)
/*
* Takes an input value of any type and convert it into a string value.
* @param value (Value) - Input value of arbitrary type.
* @return (String) - String representation of a given value.
*/
func ToString(_ context.Context, args ...core.Value) (core.Value, error) {
err := core.ValidateArgs(args, 1, 1)
if err != nil {
return values.None, err
}
return values.NewString(args[0].String()), nil
}

View File

@ -0,0 +1,22 @@
package types
import (
"context"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values"
)
/*
* Returns the data type name of value.
* @param value (Value) - Input value of arbitrary type.
* @returns (Boolean) - Returns string representation of a type.
*/
func TypeName(_ context.Context, args ...core.Value) (core.Value, error) {
err := core.ValidateArgs(args, 1, 1)
if err != nil {
return values.None, err
}
return values.NewString(args[0].Type().String()), nil
}

31
pkg/stdlib/utils/log.go Normal file
View File

@ -0,0 +1,31 @@
package utils
import (
"context"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/logging"
"github.com/MontFerret/ferret/pkg/runtime/values"
)
/*
* Writes messages into the system log.
*/
func Log(ctx context.Context, args ...core.Value) (core.Value, error) {
err := core.ValidateArgs(args, 1, core.MaxArgs)
if err != nil {
return values.None, err
}
messages := make([]interface{}, 0, len(args))
for _, input := range args {
messages = append(messages, input)
}
logger := logging.FromContext(ctx)
logger.Print(messages...)
return values.None, nil
}

View File

@ -3,11 +3,14 @@ package utils
import (
"context"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/logging"
"github.com/MontFerret/ferret/pkg/runtime/values"
"time"
)
/*
* Pauses the execution for a given period.
* @param timeout (Int) - Integer value indication for how long to pause.
*/
func Wait(_ context.Context, inputs ...core.Value) (core.Value, error) {
err := core.ValidateArgs(inputs, 1, 1)
@ -29,17 +32,3 @@ func Wait(_ context.Context, inputs ...core.Value) (core.Value, error) {
return values.None, nil
}
func Log(ctx context.Context, inputs ...core.Value) (core.Value, error) {
args := make([]interface{}, 0, len(inputs)+1)
for _, input := range inputs {
args = append(args, input)
}
logger := logging.FromContext(ctx)
logger.Print(args...)
return values.None, nil
}