2018-09-18 22:42:38 +02:00
|
|
|
package collections
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-02-13 19:31:18 +02:00
|
|
|
|
2018-09-18 22:42:38 +02:00
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/collections"
|
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/core"
|
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/values"
|
2019-02-13 19:31:18 +02:00
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/values/types"
|
2018-09-18 22:42:38 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func Length(_ context.Context, inputs ...core.Value) (core.Value, error) {
|
2018-09-22 02:36:33 +02:00
|
|
|
err := core.ValidateArgs(inputs, 1, 1)
|
2018-09-18 22:42:38 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return values.None, err
|
|
|
|
}
|
|
|
|
|
|
|
|
value := inputs[0]
|
|
|
|
|
2019-02-13 19:31:18 +02:00
|
|
|
c, ok := value.(collections.Collection)
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
return values.None, core.TypeError(value.Type(),
|
|
|
|
types.String,
|
|
|
|
types.Array,
|
|
|
|
types.Object,
|
|
|
|
types.HTMLElement,
|
|
|
|
types.HTMLDocument,
|
|
|
|
types.Binary,
|
|
|
|
core.NewType("Collection"),
|
|
|
|
)
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
|
|
|
|
2019-02-13 19:31:18 +02:00
|
|
|
return c.Length(), nil
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|