Skip to content

Commit

Permalink
Use errors.As
Browse files Browse the repository at this point in the history
  • Loading branch information
TylerHelmuth committed Jun 8, 2023
1 parent d225b2d commit 9550a1e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 26 deletions.
30 changes: 16 additions & 14 deletions pkg/ottl/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,12 @@ func (l *listGetter[K]) Get(ctx context.Context, tCtx K) (interface{}, error) {
return evaluated, nil
}

type TypeError string
type TypeError struct {
msg string
}

func (t TypeError) Error() string {
return string(t)
return t.msg
}

// StringGetter is a Getter that must return a string.
Expand All @@ -155,7 +157,7 @@ func (g StandardStringGetter[K]) Get(ctx context.Context, tCtx K) (string, error
return "", err
}
if val == nil {
return "", TypeError("expected string but got nil")
return "", &TypeError{msg: "expected string but got nil"}
}
switch v := val.(type) {
case string:
Expand All @@ -164,9 +166,9 @@ func (g StandardStringGetter[K]) Get(ctx context.Context, tCtx K) (string, error
if v.Type() == pcommon.ValueTypeStr {
return v.Str(), nil
}
return "", TypeError(fmt.Sprintf("expected string but got %v", v.Type()))
return "", &TypeError{msg: fmt.Sprintf("expected string but got %v", v.Type())}
default:
return "", TypeError(fmt.Sprintf("expected string but got %T", val))
return "", &TypeError{msg: fmt.Sprintf("expected string but got %T", val)}
}
}

Expand All @@ -184,7 +186,7 @@ func (g StandardIntGetter[K]) Get(ctx context.Context, tCtx K) (int64, error) {
return 0, err
}
if val == nil {
return 0, TypeError("expected int64 but got nil")
return 0, &TypeError{msg: "expected int64 but got nil"}
}
switch v := val.(type) {
case int64:
Expand All @@ -193,9 +195,9 @@ func (g StandardIntGetter[K]) Get(ctx context.Context, tCtx K) (int64, error) {
if v.Type() == pcommon.ValueTypeInt {
return v.Int(), nil
}
return 0, TypeError(fmt.Sprintf("expected int64 but got %v", v.Type()))
return 0, &TypeError{msg: fmt.Sprintf("expected int64 but got %v", v.Type())}
default:
return 0, TypeError(fmt.Sprintf("expected int64 but got %T", val))
return 0, &TypeError{msg: fmt.Sprintf("expected int64 but got %T", val)}
}
}

Expand All @@ -213,7 +215,7 @@ func (g StandardFloatGetter[K]) Get(ctx context.Context, tCtx K) (float64, error
return 0, err
}
if val == nil {
return 0, TypeError("expected float64 but got nil")
return 0, &TypeError{msg: "expected float64 but got nil"}
}
switch v := val.(type) {
case float64:
Expand All @@ -222,9 +224,9 @@ func (g StandardFloatGetter[K]) Get(ctx context.Context, tCtx K) (float64, error
if v.Type() == pcommon.ValueTypeDouble {
return v.Double(), nil
}
return 0, TypeError(fmt.Sprintf("expected float64 but got %v", v.Type()))
return 0, &TypeError{msg: fmt.Sprintf("expected float64 but got %v", v.Type())}
default:
return 0, TypeError(fmt.Sprintf("expected float64 but got %T", val))
return 0, &TypeError{msg: fmt.Sprintf("expected float64 but got %T", val)}
}
}

Expand All @@ -242,7 +244,7 @@ func (g StandardPMapGetter[K]) Get(ctx context.Context, tCtx K) (pcommon.Map, er
return pcommon.Map{}, err
}
if val == nil {
return pcommon.Map{}, TypeError("expected pcommon.Map but got nil")
return pcommon.Map{}, &TypeError{msg: "expected pcommon.Map but got nil"}
}
switch v := val.(type) {
case pcommon.Map:
Expand All @@ -251,7 +253,7 @@ func (g StandardPMapGetter[K]) Get(ctx context.Context, tCtx K) (pcommon.Map, er
if v.Type() == pcommon.ValueTypeMap {
return v.Map(), nil
}
return pcommon.Map{}, TypeError(fmt.Sprintf("expected pcommon.Map but got %v", v.Type()))
return pcommon.Map{}, &TypeError{msg: fmt.Sprintf("expected pcommon.Map but got %v", v.Type())}
case map[string]any:
m := pcommon.NewMap()
err = m.FromRaw(v)
Expand All @@ -260,7 +262,7 @@ func (g StandardPMapGetter[K]) Get(ctx context.Context, tCtx K) (pcommon.Map, er
}
return m, nil
default:
return pcommon.Map{}, TypeError(fmt.Sprintf("expected pcommon.Map but got %T", val))
return pcommon.Map{}, &TypeError{msg: fmt.Sprintf("expected pcommon.Map but got %T", val)}
}
}

Expand Down
13 changes: 7 additions & 6 deletions pkg/ottl/ottlfuncs/func_is_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package ottlfuncs // import "github.com/open-telemetry/opentelemetry-collector-c

import (
"context"
"errors"
"fmt"

"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl"
Expand All @@ -31,13 +32,13 @@ func createIsMapFunction[K any](_ ottl.FunctionContext, oArgs ottl.Arguments) (o
func isMap[K any](target ottl.PMapGetter[K]) ottl.ExprFunc[K] {
return func(ctx context.Context, tCtx K) (interface{}, error) {
_, err := target.Get(ctx, tCtx)
switch err.(type) {
case ottl.TypeError:
return false, nil
case nil:
if err == nil {
return true, nil
default:
return false, err
}
var typeError *ottl.TypeError
if errors.As(err, &typeError) {
return false, nil
}
return false, err
}
}
13 changes: 7 additions & 6 deletions pkg/ottl/ottlfuncs/func_is_string.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package ottlfuncs // import "github.com/open-telemetry/opentelemetry-collector-c

import (
"context"
"errors"
"fmt"

"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl"
Expand All @@ -31,13 +32,13 @@ func createIsStringFunction[K any](_ ottl.FunctionContext, oArgs ottl.Arguments)
func isStringFunc[K any](target ottl.StringGetter[K]) ottl.ExprFunc[K] {
return func(ctx context.Context, tCtx K) (interface{}, error) {
_, err := target.Get(ctx, tCtx)
switch err.(type) {
case ottl.TypeError:
return false, nil
case nil:
if err == nil {
return true, nil
default:
return false, err
}
var typeError *ottl.TypeError
if errors.As(err, &typeError) {
return false, nil
}
return false, err
}
}

0 comments on commit 9550a1e

Please sign in to comment.