diff --git a/pkg/drivers/cdp/document.go b/pkg/drivers/cdp/document.go index 21f3c3a5c..b6fc719cd 100644 --- a/pkg/drivers/cdp/document.go +++ b/pkg/drivers/cdp/document.go @@ -3,7 +3,6 @@ package cdp import ( "context" "fmt" - "github.com/mafredri/cdp/protocol/dom" "hash/fnv" "sync" "time" @@ -13,7 +12,9 @@ import ( "github.com/MontFerret/ferret/pkg/runtime/core" "github.com/MontFerret/ferret/pkg/runtime/logging" "github.com/MontFerret/ferret/pkg/runtime/values" + "github.com/MontFerret/ferret/pkg/runtime/values/types" "github.com/mafredri/cdp" + "github.com/mafredri/cdp/protocol/dom" "github.com/mafredri/cdp/protocol/input" "github.com/mafredri/cdp/protocol/page" "github.com/mafredri/cdp/rpcc" @@ -142,7 +143,7 @@ func (doc *HTMLDocument) MarshalJSON() ([]byte, error) { } func (doc *HTMLDocument) Type() core.Type { - return core.HTMLDocumentType + return types.HTMLDocument } func (doc *HTMLDocument) String() string { @@ -176,22 +177,17 @@ func (doc *HTMLDocument) Copy() core.Value { return values.None } -func (doc *HTMLDocument) Compare(other core.Value) int { +func (doc *HTMLDocument) Compare(other core.Value) int64 { doc.Lock() defer doc.Unlock() - switch other.Type() { - case core.HTMLDocumentType: + if other.Type() == types.HTMLDocument { other := other.(*HTMLDocument) return doc.url.Compare(other.url) - default: - if other.Type() > core.HTMLDocumentType { - return -1 - } - - return 1 } + + return types.Compare(other.Type(), types.HTMLDocument) } func (doc *HTMLDocument) Close() error { @@ -399,7 +395,7 @@ func (doc *HTMLDocument) ClickBySelector(selector values.String) (values.Boolean return values.False, err } - if res.Type() == core.BooleanType { + if res.Type() == types.Boolean { return res.(values.Boolean), nil } @@ -431,7 +427,7 @@ func (doc *HTMLDocument) ClickBySelectorAll(selector values.String) (values.Bool return values.False, err } - if res.Type() == core.BooleanType { + if res.Type() == types.Boolean { return res.(values.Boolean), nil } @@ -461,7 +457,7 @@ func (doc *HTMLDocument) InputBySelector(selector values.String, value core.Valu return values.False, err } - if res.Type() == core.BooleanType && res.(values.Boolean) == values.False { + if res.Type() == types.Boolean && res.(values.Boolean) == values.False { return values.False, nil } @@ -531,7 +527,7 @@ func (doc *HTMLDocument) SelectBySelector(selector values.String, value *values. return arr, nil } - return nil, core.TypeError(core.ArrayType, res.Type()) + return nil, core.TypeError(types.Array, res.Type()) } func (doc *HTMLDocument) HoverBySelector(selector values.String) error { diff --git a/pkg/drivers/http/document.go b/pkg/drivers/http/document.go index 7f6fad7bf..86098019d 100644 --- a/pkg/drivers/http/document.go +++ b/pkg/drivers/http/document.go @@ -3,6 +3,7 @@ package http import ( "github.com/MontFerret/ferret/pkg/runtime/core" "github.com/MontFerret/ferret/pkg/runtime/values" + "github.com/MontFerret/ferret/pkg/runtime/values/types" "github.com/PuerkitoBio/goquery" ) @@ -33,22 +34,17 @@ func NewHTMLDocument( } func (doc *HTMLDocument) Type() core.Type { - return core.HTMLDocumentType + return types.HTMLDocument } -func (doc *HTMLDocument) Compare(other core.Value) int { - switch other.Type() { - case core.HTMLDocumentType: +func (doc *HTMLDocument) Compare(other core.Value) int64 { + if other.Type() == types.HTMLDocument { otherDoc := other.(values.HTMLDocument) return doc.url.Compare(otherDoc.URL()) - default: - if other.Type() > core.HTMLDocumentType { - return -1 - } - - return 1 } + + return types.Compare(other.Type(), types.HTMLDocument) } func (doc *HTMLDocument) URL() core.Value { diff --git a/pkg/drivers/http/element.go b/pkg/drivers/http/element.go index a1ea4f217..4b8358fc3 100644 --- a/pkg/drivers/http/element.go +++ b/pkg/drivers/http/element.go @@ -7,6 +7,7 @@ import ( "github.com/MontFerret/ferret/pkg/drivers/common" "github.com/MontFerret/ferret/pkg/runtime/core" "github.com/MontFerret/ferret/pkg/runtime/values" + "github.com/MontFerret/ferret/pkg/runtime/values/types" "github.com/PuerkitoBio/goquery" ) @@ -29,25 +30,20 @@ func (el *HTMLElement) MarshalJSON() ([]byte, error) { } func (el *HTMLElement) Type() core.Type { - return core.HTMLElementType + return types.HTMLElement } func (el *HTMLElement) String() string { return el.InnerHTML().String() } -func (el *HTMLElement) Compare(other core.Value) int { - switch other.Type() { - case core.HTMLElementType: +func (el *HTMLElement) Compare(other core.Value) int64 { + if other.Type() == types.HTMLElement { // TODO: complete the comparison return -1 - default: - if other.Type() > core.HTMLElementType { - return -1 - } - - return 1 } + + return types.Compare(other.Type(), types.HTMLElement) } func (el *HTMLElement) Unwrap() interface{} { diff --git a/pkg/runtime/core/value_test.go b/pkg/runtime/core/value_test.go deleted file mode 100644 index 9d7271171..000000000 --- a/pkg/runtime/core/value_test.go +++ /dev/null @@ -1,76 +0,0 @@ -package core_test - -import ( - "testing" - "time" - - "github.com/MontFerret/ferret/pkg/drivers/http" - "github.com/MontFerret/ferret/pkg/runtime/core" - "github.com/MontFerret/ferret/pkg/runtime/values" - . "github.com/smartystreets/goconvey/convey" -) - -func TestTypeString(t *testing.T) { - Convey("The string representation of the type should match this type", t, func() { - So(core.Type(0).String(), ShouldEqual, "none") - So(core.Type(1).String(), ShouldEqual, "boolean") - So(core.Type(2).String(), ShouldEqual, "int") - So(core.Type(3).String(), ShouldEqual, "float") - So(core.Type(4).String(), ShouldEqual, "string") - So(core.Type(5).String(), ShouldEqual, "datetime") - So(core.Type(6).String(), ShouldEqual, "array") - So(core.Type(7).String(), ShouldEqual, "object") - So(core.Type(8).String(), ShouldEqual, "HTMLElement") - So(core.Type(9).String(), ShouldEqual, "HTMLDocument") - So(core.Type(10).String(), ShouldEqual, "BinaryType") - }) -} - -func TestIsTypeOf(t *testing.T) { - Convey("Check type by value", t, func() { - - So(core.IsTypeOf(values.None, core.NoneType), ShouldBeTrue) - So(core.IsTypeOf(values.NewBoolean(true), core.BooleanType), ShouldBeTrue) - So(core.IsTypeOf(values.NewInt(1), core.IntType), ShouldBeTrue) - So(core.IsTypeOf(values.NewFloat(1.1), core.FloatType), ShouldBeTrue) - So(core.IsTypeOf(values.NewString("test"), core.StringType), ShouldBeTrue) - So(core.IsTypeOf(values.NewDateTime(time.Now()), core.DateTimeType), ShouldBeTrue) - So(core.IsTypeOf(values.NewArray(1), core.ArrayType), ShouldBeTrue) - So(core.IsTypeOf(values.NewObject(), core.ObjectType), ShouldBeTrue) - So(core.IsTypeOf(&http.HTMLElement{}, core.HTMLElementType), ShouldBeTrue) - So(core.IsTypeOf(&http.HTMLDocument{}, core.HTMLDocumentType), ShouldBeTrue) - So(core.IsTypeOf(values.NewBinary([]byte{}), core.BinaryType), ShouldBeTrue) - }) -} - -func TestValidateType(t *testing.T) { - Convey("Value should match type", t, func() { - - So(core.ValidateType(values.None, core.NoneType), ShouldBeNil) - So(core.ValidateType(values.NewBoolean(true), core.BooleanType), ShouldBeNil) - So(core.ValidateType(values.NewInt(1), core.IntType), ShouldBeNil) - So(core.ValidateType(values.NewFloat(1.1), core.FloatType), ShouldBeNil) - So(core.ValidateType(values.NewString("test"), core.StringType), ShouldBeNil) - So(core.ValidateType(values.NewDateTime(time.Now()), core.DateTimeType), ShouldBeNil) - So(core.ValidateType(values.NewArray(1), core.ArrayType), ShouldBeNil) - So(core.ValidateType(values.NewObject(), core.ObjectType), ShouldBeNil) - So(core.ValidateType(&http.HTMLElement{}, core.HTMLElementType), ShouldBeNil) - So(core.ValidateType(&http.HTMLDocument{}, core.HTMLDocumentType), ShouldBeNil) - So(core.ValidateType(values.NewBinary([]byte{}), core.BinaryType), ShouldBeNil) - }) - - Convey("Value should not match type", t, func() { - - So(core.ValidateType(values.None, core.BooleanType), ShouldBeError) - So(core.ValidateType(values.NewBoolean(true), core.IntType, core.NoneType), ShouldBeError) - So(core.ValidateType(values.NewInt(1), core.NoneType), ShouldBeError) - So(core.ValidateType(values.NewFloat(1.1), core.StringType), ShouldBeError) - So(core.ValidateType(values.NewString("test"), core.IntType, core.FloatType), ShouldBeError) - So(core.ValidateType(values.NewDateTime(time.Now()), core.BooleanType), ShouldBeError) - So(core.ValidateType(values.NewArray(1), core.StringType), ShouldBeError) - So(core.ValidateType(values.NewObject(), core.BooleanType), ShouldBeError) - So(core.ValidateType(&http.HTMLElement{}, core.ArrayType), ShouldBeError) - So(core.ValidateType(&http.HTMLDocument{}, core.HTMLElementType), ShouldBeError) - So(core.ValidateType(values.NewBinary([]byte{}), core.NoneType), ShouldBeError) - }) -} diff --git a/pkg/runtime/expressions/param_test.go b/pkg/runtime/expressions/param_test.go index 063fdb480..9aac59156 100644 --- a/pkg/runtime/expressions/param_test.go +++ b/pkg/runtime/expressions/param_test.go @@ -2,6 +2,7 @@ package expressions_test import ( "context" + "github.com/MontFerret/ferret/pkg/runtime/values/types" "testing" "github.com/MontFerret/ferret/pkg/runtime/values/types" diff --git a/pkg/stdlib/html/parse.go b/pkg/stdlib/html/parse.go index d88288026..efadafe81 100644 --- a/pkg/stdlib/html/parse.go +++ b/pkg/stdlib/html/parse.go @@ -6,6 +6,7 @@ import ( "github.com/MontFerret/ferret/pkg/drivers" "github.com/MontFerret/ferret/pkg/runtime/core" "github.com/MontFerret/ferret/pkg/runtime/values" + "github.com/MontFerret/ferret/pkg/runtime/values/types" ) // Parse parses a given HTML string and returns a HTML document. @@ -19,7 +20,7 @@ func Parse(ctx context.Context, args ...core.Value) (core.Value, error) { return values.None, err } - err = core.ValidateType(args[0], core.StringType) + err = core.ValidateType(args[0], types.String) if err != nil { return values.None, err diff --git a/pkg/stdlib/html/pdf.go b/pkg/stdlib/html/pdf.go index 1a8df97af..d651a9d76 100644 --- a/pkg/stdlib/html/pdf.go +++ b/pkg/stdlib/html/pdf.go @@ -7,6 +7,7 @@ import ( "github.com/MontFerret/ferret/pkg/runtime/core" "github.com/MontFerret/ferret/pkg/runtime/values" + "github.com/MontFerret/ferret/pkg/runtime/values/types" ) func ValidatePageRanges(pageRanges string) (bool, error) { @@ -59,7 +60,7 @@ func PDF(ctx context.Context, args ...core.Value) (core.Value, error) { if len(args) == 2 { arg2 := args[1] - err = core.ValidateType(arg2, core.ObjectType) + err = core.ValidateType(arg2, types.Object) if err != nil { return values.None, err @@ -74,7 +75,7 @@ func PDF(ctx context.Context, args ...core.Value) (core.Value, error) { landscape, found := params.Get("landscape") if found { - err = core.ValidateType(landscape, core.BooleanType) + err = core.ValidateType(landscape, types.Boolean) if err != nil { return values.None, err @@ -86,7 +87,7 @@ func PDF(ctx context.Context, args ...core.Value) (core.Value, error) { displayHeaderFooter, found := params.Get("displayHeaderFooter") if found { - err = core.ValidateType(displayHeaderFooter, core.BooleanType) + err = core.ValidateType(displayHeaderFooter, types.Boolean) if err != nil { return values.None, err @@ -98,7 +99,7 @@ func PDF(ctx context.Context, args ...core.Value) (core.Value, error) { printBackground, found := params.Get("printBackground") if found { - err = core.ValidateType(printBackground, core.BooleanType) + err = core.ValidateType(printBackground, types.Boolean) if err != nil { return values.None, err @@ -110,13 +111,13 @@ func PDF(ctx context.Context, args ...core.Value) (core.Value, error) { scale, found := params.Get("scale") if found { - err = core.ValidateType(scale, core.FloatType, core.IntType) + err = core.ValidateType(scale, types.Float, types.Int) if err != nil { return values.None, err } - if scale.Type() == core.IntType { + if scale.Type() == types.Int { pdfParams.Scale = values.Float(scale.(values.Int)) } else { pdfParams.Scale = scale.(values.Float) @@ -126,13 +127,13 @@ func PDF(ctx context.Context, args ...core.Value) (core.Value, error) { paperWidth, found := params.Get("paperWidth") if found { - err = core.ValidateType(paperWidth, core.FloatType, core.IntType) + err = core.ValidateType(paperWidth, types.Float, types.Int) if err != nil { return values.None, err } - if paperWidth.Type() == core.IntType { + if paperWidth.Type() == types.Int { pdfParams.PaperWidth = values.Float(paperWidth.(values.Int)) } else { pdfParams.PaperWidth = paperWidth.(values.Float) @@ -142,13 +143,13 @@ func PDF(ctx context.Context, args ...core.Value) (core.Value, error) { paperHeight, found := params.Get("paperHeight") if found { - err = core.ValidateType(paperHeight, core.FloatType, core.IntType) + err = core.ValidateType(paperHeight, types.Float, types.Int) if err != nil { return values.None, err } - if paperHeight.Type() == core.IntType { + if paperHeight.Type() == types.Int { pdfParams.PaperHeight = values.Float(paperHeight.(values.Int)) } else { pdfParams.PaperHeight = paperHeight.(values.Float) @@ -158,13 +159,13 @@ func PDF(ctx context.Context, args ...core.Value) (core.Value, error) { marginTop, found := params.Get("marginTop") if found { - err = core.ValidateType(marginTop, core.FloatType, core.IntType) + err = core.ValidateType(marginTop, types.Float, types.Int) if err != nil { return values.None, err } - if marginTop.Type() == core.IntType { + if marginTop.Type() == types.Int { pdfParams.MarginTop = values.Float(marginTop.(values.Int)) } else { pdfParams.MarginTop = marginTop.(values.Float) @@ -174,13 +175,13 @@ func PDF(ctx context.Context, args ...core.Value) (core.Value, error) { marginBottom, found := params.Get("marginBottom") if found { - err = core.ValidateType(marginBottom, core.FloatType, core.IntType) + err = core.ValidateType(marginBottom, types.Float, types.Int) if err != nil { return values.None, err } - if marginBottom.Type() == core.IntType { + if marginBottom.Type() == types.Int { pdfParams.MarginBottom = values.Float(marginBottom.(values.Int)) } else { pdfParams.MarginBottom = marginBottom.(values.Float) @@ -190,13 +191,13 @@ func PDF(ctx context.Context, args ...core.Value) (core.Value, error) { marginLeft, found := params.Get("marginLeft") if found { - err = core.ValidateType(marginLeft, core.FloatType, core.IntType) + err = core.ValidateType(marginLeft, types.Float, types.Int) if err != nil { return values.None, err } - if marginLeft.Type() == core.IntType { + if marginLeft.Type() == types.Int { pdfParams.MarginLeft = values.Float(marginLeft.(values.Int)) } else { pdfParams.MarginLeft = marginLeft.(values.Float) @@ -206,13 +207,13 @@ func PDF(ctx context.Context, args ...core.Value) (core.Value, error) { marginRight, found := params.Get("marginRight") if found { - err = core.ValidateType(marginRight, core.FloatType, core.IntType) + err = core.ValidateType(marginRight, types.Float, types.Int) if err != nil { return values.None, err } - if marginRight.Type() == core.IntType { + if marginRight.Type() == types.Int { pdfParams.MarginRight = values.Float(marginRight.(values.Int)) } else { pdfParams.MarginRight = marginRight.(values.Float) @@ -222,7 +223,7 @@ func PDF(ctx context.Context, args ...core.Value) (core.Value, error) { pageRanges, found := params.Get("pageRanges") if found { - err = core.ValidateType(pageRanges, core.StringType) + err = core.ValidateType(pageRanges, types.String) if err != nil { return values.None, err @@ -244,7 +245,7 @@ func PDF(ctx context.Context, args ...core.Value) (core.Value, error) { ignoreInvalidPageRanges, found := params.Get("ignoreInvalidPageRanges") if found { - err = core.ValidateType(ignoreInvalidPageRanges, core.BooleanType) + err = core.ValidateType(ignoreInvalidPageRanges, types.Boolean) if err != nil { return values.None, err @@ -256,7 +257,7 @@ func PDF(ctx context.Context, args ...core.Value) (core.Value, error) { headerTemplate, found := params.Get("headerTemplate") if found { - err = core.ValidateType(headerTemplate, core.StringType) + err = core.ValidateType(headerTemplate, types.String) if err != nil { return values.None, err @@ -268,7 +269,7 @@ func PDF(ctx context.Context, args ...core.Value) (core.Value, error) { footerTemplate, found := params.Get("footerTemplate") if found { - err = core.ValidateType(footerTemplate, core.StringType) + err = core.ValidateType(footerTemplate, types.String) if err != nil { return values.None, err @@ -280,7 +281,7 @@ func PDF(ctx context.Context, args ...core.Value) (core.Value, error) { preferCSSPageSize, found := params.Get("preferCSSPageSize") if found { - err = core.ValidateType(preferCSSPageSize, core.BooleanType) + err = core.ValidateType(preferCSSPageSize, types.Boolean) if err != nil { return values.None, err diff --git a/pkg/stdlib/objects/keep_keys.go b/pkg/stdlib/objects/keep_keys.go index c39fc1229..5d0f300fc 100644 --- a/pkg/stdlib/objects/keep_keys.go +++ b/pkg/stdlib/objects/keep_keys.go @@ -5,6 +5,7 @@ import ( "github.com/MontFerret/ferret/pkg/runtime/core" "github.com/MontFerret/ferret/pkg/runtime/values" + "github.com/MontFerret/ferret/pkg/runtime/values/types" ) // KeepKeys returns a new object with only given keys. @@ -18,7 +19,7 @@ func KeepKeys(_ context.Context, args ...core.Value) (core.Value, error) { return values.None, err } - err = core.ValidateType(args[0], core.ObjectType) + err = core.ValidateType(args[0], types.Object) if err != nil { return values.None, err @@ -26,11 +27,11 @@ func KeepKeys(_ context.Context, args ...core.Value) (core.Value, error) { keys := values.NewArrayWith(args[1:]...) - if len(args) == 2 && args[1].Type() == core.ArrayType { + if len(args) == 2 && args[1].Type().Equals(types.Array) { keys = args[1].(*values.Array) } - err = validateArrayOf(core.StringType, keys) + err = validateArrayOf(types.String, keys) if err != nil { return values.None, err @@ -47,9 +48,12 @@ func KeepKeys(_ context.Context, args ...core.Value) (core.Value, error) { key = keyVal.(values.String) if val, exists = obj.Get(key); exists { - if values.IsCloneable(val) { - val = val.(core.Cloneable).Clone() + cloneable, ok := val.(core.Cloneable) + + if ok { + val = cloneable.Clone() } + resultObj.Set(key, val) }