From 9550a1e394dbcda864beba500e6d1a605393c8c7 Mon Sep 17 00:00:00 2001 From: Tyler Helmuth <12352919+TylerHelmuth@users.noreply.github.com> Date: Tue, 6 Jun 2023 15:37:48 -0600 Subject: [PATCH] Use errors.As --- pkg/ottl/expression.go | 30 +++++++++++++++------------- pkg/ottl/ottlfuncs/func_is_map.go | 13 ++++++------ pkg/ottl/ottlfuncs/func_is_string.go | 13 ++++++------ 3 files changed, 30 insertions(+), 26 deletions(-) diff --git a/pkg/ottl/expression.go b/pkg/ottl/expression.go index 417e987bbadb..7334218271b5 100644 --- a/pkg/ottl/expression.go +++ b/pkg/ottl/expression.go @@ -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. @@ -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: @@ -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)} } } @@ -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: @@ -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)} } } @@ -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: @@ -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)} } } @@ -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: @@ -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) @@ -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)} } } diff --git a/pkg/ottl/ottlfuncs/func_is_map.go b/pkg/ottl/ottlfuncs/func_is_map.go index 766cc9897606..b2d379e89036 100644 --- a/pkg/ottl/ottlfuncs/func_is_map.go +++ b/pkg/ottl/ottlfuncs/func_is_map.go @@ -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" @@ -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 } } diff --git a/pkg/ottl/ottlfuncs/func_is_string.go b/pkg/ottl/ottlfuncs/func_is_string.go index 85c20e8b82fe..5d4bf7541d78 100644 --- a/pkg/ottl/ottlfuncs/func_is_string.go +++ b/pkg/ottl/ottlfuncs/func_is_string.go @@ -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" @@ -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 } }