github.com/Jeffail/benthos/v3@v3.65.0/public/bloblang/method.go (about) 1 package bloblang 2 3 import ( 4 "time" 5 6 "github.com/Jeffail/benthos/v3/internal/bloblang/query" 7 ) 8 9 // Method defines a Bloblang function that executes on a value. Arguments are 10 // provided to the constructor, allowing the implementation of this method to 11 // resolve them statically when possible. 12 // 13 // In order to avoid type checking the value use one of the typed variants such 14 // as StringMethod. 15 type Method func(v interface{}) (interface{}, error) 16 17 // MethodConstructor defines a constructor for a Bloblang method, where a 18 // variadic list of arguments are provided. 19 // 20 // When a method is parsed from a mapping with static arguments the constructor 21 // will be called only once at parse time. When a method is parsed with dynamic 22 // arguments, such as a value derived from the mapping input, the constructor 23 // will be called on each invocation of the mapping with the derived arguments. 24 // 25 // For a convenient way to perform type checking and coercion on the arguments 26 // use an ArgSpec. 27 type MethodConstructor func(args ...interface{}) (Method, error) 28 29 // MethodConstructorV2 defines a constructor for a Bloblang method where 30 // parameters are parsed using a ParamsSpec provided when registering the 31 // method. 32 // 33 // When a method is parsed from a mapping with static arguments the constructor 34 // will be called only once at parse time. When a method is parsed with dynamic 35 // arguments, such as a value derived from the mapping input, the constructor 36 // will be called on each invocation of the mapping with the derived arguments. 37 type MethodConstructorV2 func(args *ParsedParams) (Method, error) 38 39 //------------------------------------------------------------------------------ 40 41 // StringMethod creates a general method signature from a string method by 42 // performing type checking on the method target. 43 func StringMethod(methodFn func(string) (interface{}, error)) Method { 44 return func(v interface{}) (interface{}, error) { 45 str, err := query.IGetString(v) 46 if err != nil { 47 return nil, err 48 } 49 return methodFn(str) 50 } 51 } 52 53 // BytesMethod creates a general method signature from a byte slice method by 54 // performing type checking on the method target. 55 func BytesMethod(methodFn func([]byte) (interface{}, error)) Method { 56 return func(v interface{}) (interface{}, error) { 57 b, err := query.IGetBytes(v) 58 if err != nil { 59 return nil, err 60 } 61 return methodFn(b) 62 } 63 } 64 65 // TimestampMethod creates a general method signature from a timestamp method by 66 // performing type checking on the method target. 67 func TimestampMethod(methodFn func(time.Time) (interface{}, error)) Method { 68 return func(v interface{}) (interface{}, error) { 69 t, err := query.IGetTimestamp(v) 70 if err != nil { 71 return nil, err 72 } 73 return methodFn(t) 74 } 75 } 76 77 // ArrayMethod creates a general method signature from an array method by 78 // performing type checking on the method target. 79 func ArrayMethod(methodFn func([]interface{}) (interface{}, error)) Method { 80 return func(v interface{}) (interface{}, error) { 81 arr, ok := v.([]interface{}) 82 if !ok { 83 return nil, query.NewTypeError(v, query.ValueArray) 84 } 85 return methodFn(arr) 86 } 87 } 88 89 // BoolMethod creates a general method signature from a bool method by 90 // performing type checking on the method target. 91 func BoolMethod(methodFn func(bool) (interface{}, error)) Method { 92 return func(v interface{}) (interface{}, error) { 93 b, err := query.IGetBool(v) 94 if err != nil { 95 return nil, err 96 } 97 return methodFn(b) 98 } 99 } 100 101 // Int64Method creates a general method signature from an int method by 102 // performing type checking on the method target. 103 func Int64Method(methodFn func(int64) (interface{}, error)) Method { 104 return func(v interface{}) (interface{}, error) { 105 i, err := query.IGetInt(v) 106 if err != nil { 107 return nil, err 108 } 109 return methodFn(i) 110 } 111 } 112 113 // Float64Method creates a general method signature from a float method by 114 // performing type checking on the method target. 115 func Float64Method(methodFn func(float64) (interface{}, error)) Method { 116 return func(v interface{}) (interface{}, error) { 117 f, err := query.IGetNumber(v) 118 if err != nil { 119 return nil, err 120 } 121 return methodFn(f) 122 } 123 } 124 125 // ObjectMethod creates a general method signature from an object method by 126 // performing type checking on the method target. 127 func ObjectMethod(methodFn func(obj map[string]interface{}) (interface{}, error)) Method { 128 return func(v interface{}) (interface{}, error) { 129 obj, ok := v.(map[string]interface{}) 130 if !ok { 131 return nil, query.NewTypeError(v, query.ValueObject) 132 } 133 return methodFn(obj) 134 } 135 }