MCPcopy Create free account
hub / github.com/crossplane-contrib/function-patch-and-transform / resolveMathClamp

Function resolveMathClamp

transforms.go:141–167  ·  view source on GitHub ↗

resolveMathClamp resolves a clamp transform, returning an error if the input is not a number. depending on the type of clamp, the result will be either the input or the clamp value, preserving their original types.

(t *v1beta1.MathTransform, input any)

Source from the content-addressed store, hash-verified

139// is not a number. depending on the type of clamp, the result will be either
140// the input or the clamp value, preserving their original types.
141func resolveMathClamp(t *v1beta1.MathTransform, input any) (any, error) {
142 var in int64
143 switch i := input.(type) {
144 case int:
145 in = int64(i)
146 case int64:
147 in = i
148 case float64:
149 in = int64(i)
150 default:
151 // should never happen as we validate the input type in ResolveMath
152 return nil, errors.Errorf(errFmtMathInputNonNumber, input)
153 }
154 switch t.Type { //nolint:exhaustive // We validate the type in ResolveMath
155 case v1beta1.MathTransformTypeClampMin:
156 if in < *t.ClampMin {
157 return *t.ClampMin, nil
158 }
159 case v1beta1.MathTransformTypeClampMax:
160 if in > *t.ClampMax {
161 return *t.ClampMax, nil
162 }
163 default:
164 return nil, errors.Errorf(errMathTransformTypeFailed, string(t.Type))
165 }
166 return input, nil
167}
168
169// ResolveMap resolves a Map transform.
170func ResolveMap(t *v1beta1.MapTransform, input any) (any, error) {

Callers 1

ResolveMathFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected