-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[filebeat][httpjson]- Added min & max functions to the template engine (
#36036) * added min/max functions * updated methods to accept integer list * updated the methods to accept atleast 1 arg * extended the min/max functions to be more generic * reworked the min/max implementation * made the min max funcs generic with a custom number ingterface * Implement min/max using text/template lt func --------- Co-authored-by: Andrew Kroh <[email protected]>
- Loading branch information
1 parent
b6c377c
commit 2429af1
Showing
5 changed files
with
208 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
// or more contributor license agreements. Licensed under the Elastic License; | ||
// you may not use this file except in compliance with the Elastic License. | ||
|
||
package httpjson | ||
|
||
import ( | ||
"errors" | ||
"reflect" | ||
) | ||
|
||
// These functions come from Go's text/template/funcs.go (1.19). | ||
// | ||
// Copyright 2009 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
var ( | ||
errBadComparisonType = errors.New("invalid type for comparison") | ||
errBadComparison = errors.New("incompatible types for comparison") | ||
) | ||
|
||
type kind int | ||
|
||
const ( | ||
invalidKind kind = iota | ||
boolKind | ||
complexKind | ||
intKind | ||
floatKind | ||
stringKind | ||
uintKind | ||
) | ||
|
||
func basicKind(v reflect.Value) (kind, error) { | ||
switch v.Kind() { | ||
case reflect.Bool: | ||
return boolKind, nil | ||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: | ||
return intKind, nil | ||
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: | ||
return uintKind, nil | ||
case reflect.Float32, reflect.Float64: | ||
return floatKind, nil | ||
case reflect.Complex64, reflect.Complex128: | ||
return complexKind, nil | ||
case reflect.String: | ||
return stringKind, nil | ||
} | ||
return invalidKind, errBadComparisonType | ||
} | ||
|
||
// indirectInterface returns the concrete value in an interface value, | ||
// or else the zero reflect.Value. | ||
// That is, if v represents the interface value x, the result is the same as reflect.ValueOf(x): | ||
// the fact that x was an interface value is forgotten. | ||
func indirectInterface(v reflect.Value) reflect.Value { | ||
if v.Kind() != reflect.Interface { | ||
return v | ||
} | ||
if v.IsNil() { | ||
return reflect.Value{} | ||
} | ||
return v.Elem() | ||
} | ||
|
||
// lt evaluates the comparison a < b. | ||
func lt(arg1, arg2 reflect.Value) (bool, error) { | ||
arg1 = indirectInterface(arg1) | ||
k1, err := basicKind(arg1) | ||
if err != nil { | ||
return false, err | ||
} | ||
arg2 = indirectInterface(arg2) | ||
k2, err := basicKind(arg2) | ||
if err != nil { | ||
return false, err | ||
} | ||
truth := false | ||
if k1 != k2 { | ||
// Special case: Can compare integer values regardless of type's sign. | ||
switch { | ||
case k1 == intKind && k2 == uintKind: | ||
truth = arg1.Int() < 0 || uint64(arg1.Int()) < arg2.Uint() | ||
case k1 == uintKind && k2 == intKind: | ||
truth = arg2.Int() >= 0 && arg1.Uint() < uint64(arg2.Int()) | ||
default: | ||
return false, errBadComparison | ||
} | ||
} else { | ||
switch k1 { | ||
case boolKind, complexKind: | ||
return false, errBadComparisonType | ||
case floatKind: | ||
truth = arg1.Float() < arg2.Float() | ||
case intKind: | ||
truth = arg1.Int() < arg2.Int() | ||
case stringKind: | ||
truth = arg1.String() < arg2.String() | ||
case uintKind: | ||
truth = arg1.Uint() < arg2.Uint() | ||
default: | ||
panic("invalid kind") | ||
} | ||
} | ||
return truth, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters