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 2041eaa commit 2995d1d
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 120 deletions.
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
47 changes: 24 additions & 23 deletions pkg/stdlib/html/pdf.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 2995d1d

Please sign in to comment.