github.com/MontFerret/ferret@v0.18.0/pkg/stdlib/collections/length.go (about)

     1  package collections
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/MontFerret/ferret/pkg/runtime/collections"
     7  	"github.com/MontFerret/ferret/pkg/runtime/core"
     8  	"github.com/MontFerret/ferret/pkg/runtime/values"
     9  	"github.com/MontFerret/ferret/pkg/runtime/values/types"
    10  )
    11  
    12  // LENGTH returns the length of a measurable value.
    13  // @param {Measurable} value - The value to measure.
    14  // @return {Int} - The length of the value.
    15  func Length(_ context.Context, inputs ...core.Value) (core.Value, error) {
    16  	err := core.ValidateArgs(inputs, 1, 1)
    17  
    18  	if err != nil {
    19  		return values.None, err
    20  	}
    21  
    22  	value := inputs[0]
    23  
    24  	c, ok := value.(collections.Measurable)
    25  
    26  	if !ok {
    27  		return values.None, core.TypeError(value.Type(),
    28  			types.String,
    29  			types.Array,
    30  			types.Object,
    31  			types.Binary,
    32  			core.NewType("Measurable"),
    33  		)
    34  	}
    35  
    36  	return c.Length(), nil
    37  }