Skip to content

Commit

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

type TypeError struct {
msg string
}
type TypeError string

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

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

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

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

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

Expand Down
13 changes: 6 additions & 7 deletions pkg/ottl/ottlfuncs/func_is_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ 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 @@ -32,13 +31,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)
if err == nil {
return true, nil
}
var typeError *ottl.TypeError
if errors.As(err, &typeError) {
switch err.(type) {
case ottl.TypeError:
return false, nil
case nil:
return true, nil
default:
return false, err
}
return false, err
}
}
13 changes: 6 additions & 7 deletions pkg/ottl/ottlfuncs/func_is_string.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ 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 @@ -32,13 +31,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)
if err == nil {
return true, nil
}
var typeError *ottl.TypeError
if errors.As(err, &typeError) {
switch err.(type) {
case ottl.TypeError:
return false, nil
case nil:
return true, nil
default:
return false, err
}
return false, err
}
}

0 comments on commit aaf6117

Please sign in to comment.