1
0
mirror of https://github.com/MontFerret/ferret.git synced 2025-03-27 22:01:47 +02:00

64 lines
1.2 KiB
Go
Raw Normal View History

2018-09-18 16:42:38 -04:00
package expressions
import (
"context"
2018-09-18 16:42:38 -04:00
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values"
)
type MemberExpression struct {
src core.SourceMap
source core.Expression
path []*MemberPathSegment
2018-09-18 16:42:38 -04:00
}
func NewMemberExpression(src core.SourceMap, source core.Expression, path []*MemberPathSegment) (*MemberExpression, error) {
if source == nil {
return nil, core.Error(core.ErrMissedArgument, "source")
2018-09-18 16:42:38 -04:00
}
if len(path) == 0 {
2018-10-28 01:45:26 -04:00
return nil, core.Error(core.ErrMissedArgument, "path expressions")
2018-09-18 16:42:38 -04:00
}
return &MemberExpression{src, source, path}, nil
2018-09-18 16:42:38 -04:00
}
func (e *MemberExpression) Exec(ctx context.Context, scope *core.Scope) (core.Value, error) {
val, err := e.source.Exec(ctx, scope)
2018-09-18 16:42:38 -04:00
if err != nil {
return values.None, core.SourceError(
e.src,
err,
)
}
2019-09-07 01:47:58 -04:00
out := val
2019-09-07 02:02:25 -04:00
path := make([]core.Value, 1)
2018-09-18 16:42:38 -04:00
for _, seg := range e.path {
segment, err := seg.exp.Exec(ctx, scope)
2018-09-18 16:42:38 -04:00
if err != nil {
return values.None, err
}
2019-09-07 01:47:58 -04:00
path[0] = segment
c, err := values.GetIn(ctx, out, path)
2018-09-18 16:42:38 -04:00
2019-09-07 01:47:58 -04:00
if err != nil {
if !seg.optional {
return values.None, core.SourceError(e.src, err)
}
return values.None, nil
2019-09-07 01:47:58 -04:00
}
2018-09-18 16:42:38 -04:00
2019-09-07 01:47:58 -04:00
out = c
2018-09-18 16:42:38 -04:00
}
return out, nil
}