Skip to content

Commit

Permalink
New type system (MontFerret#232)
Browse files Browse the repository at this point in the history
* New type system

* Fixed dot notation for HTML elements
  • Loading branch information
ziflex authored and Владимир Фетисов committed Apr 10, 2019
1 parent abeffe8 commit e26b7b2
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 140 deletions.
26 changes: 11 additions & 15 deletions pkg/drivers/cdp/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cdp
import (
"context"
"fmt"
"github.com/mafredri/cdp/protocol/dom"
"hash/fnv"
"sync"
"time"
Expand All @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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 {
Expand Down
16 changes: 6 additions & 10 deletions pkg/drivers/http/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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 {
Expand Down
16 changes: 6 additions & 10 deletions pkg/drivers/http/element.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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{} {
Expand Down
76 changes: 0 additions & 76 deletions pkg/runtime/core/value_test.go

This file was deleted.

1 change: 1 addition & 0 deletions pkg/runtime/expressions/param_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
3 changes: 2 additions & 1 deletion pkg/stdlib/html/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down
Loading

0 comments on commit e26b7b2

Please sign in to comment.