Skip to content

Commit 285a37d

Browse files
committed
use type switch for handling different types as Scale argument
Signed-off-by: Florian Bacher <[email protected]>
1 parent 213fcce commit 285a37d

File tree

1 file changed

+14
-16
lines changed

1 file changed

+14
-16
lines changed

pkg/ottl/ottlfuncs/func_scale.go

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,32 +33,30 @@ func Scale[K any](value ottl.Getter[K], multiplier float64) (ottl.ExprFunc[K], e
3333
if err != nil {
3434
return nil, err
3535
}
36-
if floatVal, ok := get.(float64); ok {
37-
return floatVal * multiplier, nil
38-
}
39-
if intVal, ok := get.(int64); ok {
40-
return multiplier * (float64(intVal)), nil
41-
}
42-
if datapoints, ok := get.(pmetric.NumberDataPointSlice); ok {
36+
37+
switch get.(type) {
38+
case float64:
39+
return get.(float64) * multiplier, nil
40+
case int64:
41+
return float64(get.(int64)) * multiplier, nil
42+
case pmetric.NumberDataPointSlice:
4343
scaledMetric := pmetric.NewNumberDataPointSlice()
44-
datapoints.CopyTo(scaledMetric)
44+
get.(pmetric.NumberDataPointSlice).CopyTo(scaledMetric)
4545
scaleMetric(scaledMetric, multiplier)
4646
return scaledMetric, nil
47-
}
48-
if datapoints, ok := get.(pmetric.HistogramDataPointSlice); ok {
47+
case pmetric.HistogramDataPointSlice:
4948
scaledMetric := pmetric.NewHistogramDataPointSlice()
50-
datapoints.CopyTo(scaledMetric)
49+
get.(pmetric.HistogramDataPointSlice).CopyTo(scaledMetric)
5150
scaleHistogram(scaledMetric, multiplier)
5251
return scaledMetric, nil
53-
}
54-
if datapoints, ok := get.(pmetric.ExponentialHistogramDataPointSlice); ok {
52+
case pmetric.ExponentialHistogramDataPointSlice:
5553
scaledMetric := pmetric.NewExponentialHistogramDataPointSlice()
56-
datapoints.CopyTo(scaledMetric)
54+
get.(pmetric.ExponentialHistogramDataPointSlice).CopyTo(scaledMetric)
5755
scaleExponentialHistogram(scaledMetric, multiplier)
5856
return scaledMetric, nil
57+
default:
58+
return nil, errors.New("unsupported data type")
5959
}
60-
61-
return nil, errors.New("unsupported data type")
6260
}, nil
6361
}
6462

0 commit comments

Comments
 (0)