Skip to content

Commit

Permalink
polish
Browse files Browse the repository at this point in the history
Signed-off-by: odubajDT <[email protected]>
  • Loading branch information
odubajDT committed Dec 16, 2024
1 parent c30a711 commit 9d72077
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 29 deletions.
8 changes: 4 additions & 4 deletions pkg/ottl/contexts/internal/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ func GetMapValue[K any](ctx context.Context, tCtx K, m pcommon.Map, keys []ottl.
return nil, err
}
if s == nil {
p, err := keys[0].Path()
p, err := keys[0].PathGetter()
if err != nil {
return nil, err
}
if p != nil {
res, err := GetMapValue[K](ctx, tCtx, m, p.Keys())
res, err := p.Get(ctx, tCtx)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -59,12 +59,12 @@ func SetMapValue[K any](ctx context.Context, tCtx K, m pcommon.Map, keys []ottl.
return err
}
if s == nil {
p, err := keys[0].Path()
p, err := keys[0].PathGetter()
if err != nil {
return err
}
if p != nil {
res, err := GetMapValue[K](ctx, tCtx, m, p.Keys())
res, err := p.Get(ctx, tCtx)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/ottl/contexts/internal/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ var _ ottl.Key[any] = &TestKey[any]{}
type TestKey[K any] struct {
S *string
I *int64
P *ottl.Path[K]
P *ottl.GetSetter[K]
}

func (k *TestKey[K]) String(_ context.Context, _ K) (*string, error) {
Expand All @@ -57,6 +57,6 @@ func (k *TestKey[K]) Int(_ context.Context, _ K) (*int64, error) {
return k.I, nil
}

func (k *TestKey[K]) Path() (ottl.Path[K], error) {
func (k *TestKey[K]) PathGetter() (ottl.GetSetter[K], error) {
return *k.P, nil
}
43 changes: 20 additions & 23 deletions pkg/ottl/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,17 +216,13 @@ func (p *Parser[K]) newKeys(keys []key) ([]Key[K], error) {
}
ks := make([]Key[K], len(keys))
for i := range keys {
var par *basePath[K]
var par GetSetter[K]
if keys[i].Path != nil {
pp, err := p.newPath(keys[i].Path) // newGetter here
arg, err := p.buildGetSetterFromPath(keys[i].Path)
if err != nil {
return nil, err
}
// arg, err := p.parsePath()
// if err != nil {
// return nil, err
// }
par = pp
par = arg
}
ks[i] = &baseKey[K]{
s: keys[i].String,
Expand All @@ -252,15 +248,15 @@ type Key[K any] interface {
// If Key experiences an error retrieving the value it is returned.
Int(context.Context, K) (*int64, error)

Path() (Path[K], error)
PathGetter() (GetSetter[K], error)
}

var _ Key[any] = &baseKey[any]{}

type baseKey[K any] struct {
s *string
i *int64
p *basePath[K]
p GetSetter[K]
}

func (k *baseKey[K]) String(_ context.Context, _ K) (*string, error) {
Expand All @@ -271,10 +267,8 @@ func (k *baseKey[K]) Int(_ context.Context, _ K) (*int64, error) {
return k.i, nil
}

func (k *baseKey[K]) Path() (Path[K], error) {
var path Path[K]
path = k.p
return path, nil
func (k *baseKey[K]) PathGetter() (GetSetter[K], error) {
return k.p, nil
}

func (p *Parser[K]) parsePath(ip *basePath[K]) (GetSetter[K], error) {
Expand Down Expand Up @@ -515,6 +509,18 @@ func (p *Parser[K]) buildSliceArg(argVal value, argType reflect.Type) (any, erro
}
}

func (p *Parser[K]) buildGetSetterFromPath(path *path) (GetSetter[K], error) {
np, err := p.newPath(path)
if err != nil {
return nil, err
}
arg, err := p.parsePath(np)
if err != nil {
return nil, err
}
return arg, nil
}

// Handle interfaces that can be passed as arguments to OTTL functions.
func (p *Parser[K]) buildArg(argVal value, argType reflect.Type) (any, error) {
name := argType.Name()
Expand All @@ -523,16 +529,7 @@ func (p *Parser[K]) buildArg(argVal value, argType reflect.Type) (any, error) {
fallthrough
case strings.HasPrefix(name, "GetSetter"):
if argVal.Literal != nil && argVal.Literal.Path != nil {
np, err := p.newPath(argVal.Literal.Path)
if err != nil {
return nil, err
}
arg, err := p.parsePath(np)
if err != nil {
return nil, err
}
//tu
return arg, nil
return p.buildGetSetterFromPath(argVal.Literal.Path)
}
return nil, fmt.Errorf("must be a path")
case strings.HasPrefix(name, "Getter"):
Expand Down

0 comments on commit 9d72077

Please sign in to comment.