github.com/go-graphite/carbonapi@v0.17.0/pkg/parser/interface.go (about) 1 package parser 2 3 import ( 4 "errors" 5 "fmt" 6 "strings" 7 ) 8 9 // MetricRequest contains all necessary data to request a metric. 10 type MetricRequest struct { 11 Metric string 12 ConsolidationFunc string // explicitly use function for consolidation. Check https://graphite.readthedocs.io/en/latest/functions.html?highlight=consolidateBy#graphite.render.functions.consolidateBy 13 From int64 14 Until int64 15 } 16 17 // ExprType defines a type for expression types constants (e.x. functions, values, constants, parameters, strings) 18 type ExprType int 19 20 const ( 21 // EtName is a const for 'Series Name' type expression 22 EtName ExprType = iota 23 // EtFunc is a const for 'Function' type expression 24 EtFunc 25 // EtConst is a const for 'Constant' type expression 26 EtConst 27 // EtString is a const for 'String' type expression 28 EtString 29 // EtBool is a constant for 'Bool' type expression 30 EtBool 31 ) 32 33 var ( 34 // ErrMissingExpr is a parse error returned when an expression is missing. 35 ErrMissingExpr = errors.New("missing expression") 36 // ErrMissingComma is a parse error returned when an expression is missing a comma. 37 ErrMissingComma = errors.New("missing comma") 38 // ErrMissingQuote is a parse error returned when an expression is missing a quote. 39 ErrMissingQuote = errors.New("missing quote") 40 // ErrUnexpectedCharacter is a parse error returned when an expression contains an unexpected character. 41 ErrUnexpectedCharacter = errors.New("unexpected character") 42 // ErrBadType is an eval error returned when a argument has wrong type. 43 ErrBadType = errors.New("bad type") 44 // ErrMissingArgument is an eval error returned when a argument is missing. 45 ErrMissingArgument = errors.New("missing argument") 46 // ErrMissingTimeseries is an eval error returned when a time series argument is missing. 47 ErrMissingTimeseries = errors.New("missing time series argument") 48 // ErrMissingValues is an eval error returned when an empty time series is returned but is expected to be non-empty. 49 ErrMissingValues = errors.New("unexpected empty time series") 50 // ErrUnknownTimeUnits is an eval error returned when a time unit is unknown to system 51 ErrUnknownTimeUnits = errors.New("unknown time units") 52 // ErrInvalidArg is eval error for invalid or mismatch function arg 53 ErrInvalidArg = errors.New("invalid function arg") 54 // ErrInvalidInterval is an eval error returned when an interval is set to 0 55 ErrInvalidInterval = errors.New("invalid interval arg") 56 ) 57 58 // NodeOrTag structure contains either Node (=integer) or Tag (=string) 59 // They are distinguished by "IsTag" = true in case it's tag. 60 type NodeOrTag struct { 61 IsTag bool 62 Value interface{} 63 } 64 65 // IntOrInf is either positive infinity or an integer value. 66 // They are distinguished by "IsInf" = true if it is positive infinity. 67 type IntOrInf struct { 68 IsInf bool 69 IntVal int 70 } 71 72 // Expr defines an interface to talk with expressions 73 type Expr interface { 74 // IsName checks if Expression is 'Series Name' expression 75 IsName() bool 76 // IsFunc checks if Expression is 'Function' expression 77 IsFunc() bool 78 // IsConst checks if Expression is 'Constant' expression 79 IsConst() bool 80 // IsString checks if Expression is 'String' expression 81 IsString() bool 82 // IsBool checks if Expression is 'Bool' expression 83 IsBool() bool 84 // Type returns type of the expression 85 Type() ExprType 86 // Target returns target value for expression 87 Target() string 88 // SetTarget changes target for the expression 89 SetTarget(string) 90 // MutateTarget changes target for the expression and returns new interface. Please note that it doesn't copy object yet 91 MutateTarget(string) Expr 92 // ToString returns string representation of expression 93 ToString() string 94 95 // FloatValue returns float value for expression. 96 FloatValue() float64 97 98 // StringValue returns value of String-typed expression (will return empty string for ConstExpr for example). 99 StringValue() string 100 // SetValString changes value of String-typed expression 101 SetValString(string) 102 // MutateValString changes ValString for the expression and returns new interface. Please note that it doesn't copy object yet 103 MutateValString(string) Expr 104 105 // Arg returns argument with index (parsed, as Expr interface as well) 106 Arg(int) Expr 107 // Args returns slice of arguments (parsed, as Expr interface as well) 108 Args() []Expr 109 // ArgsLen return arguments count 110 ArgsLen() int 111 // NamedArgs returns map of named arguments. E.x. for nonNegativeDerivative(metric1,maxValue=32) it will return map{"maxValue": constExpr(32)} 112 NamedArgs() map[string]Expr 113 // NamedArg returns named argument and boolean flag for check arg exist. 114 NamedArg(string) (Expr, bool) 115 // RawArgs returns string that contains all arguments of expression exactly the same order they appear 116 RawArgs() string 117 // SetRawArgs changes raw argument list for current expression. 118 SetRawArgs(args string) 119 // MutateRawArgs changes raw argument list for the expression and returns new interface. Please note that it doesn't copy object yet 120 MutateRawArgs(args string) Expr 121 122 // Metrics returns list of metric requests 123 Metrics(from, until int64) []MetricRequest 124 125 // GetIntervalArg returns interval typed argument. 126 GetIntervalArg(n int, defaultSign int) (int32, error) 127 128 // GetIntervalNamedOrPosArgDefault returns interval typed argument that can be passed as a named argument or as position or replace it with default if none found. 129 GetIntervalNamedOrPosArgDefault(k string, n, defaultSign int, v int64) (int64, error) 130 131 // GetStringArg returns n-th argument as string. 132 GetStringArg(n int) (string, error) 133 // GetStringArgs returns n-th argument as slice of strings. 134 GetStringArgs(n int) ([]string, error) 135 // GetStringArgDefault returns n-th argument as string. It will replace it with Default value if none present. 136 GetStringArgDefault(n int, s string) (string, error) 137 // GetStringNamedOrPosArgDefault returns specific positioned string-typed argument or replace it with default if none found. 138 GetStringNamedOrPosArgDefault(k string, n int, s string) (string, error) 139 140 // GetFloatArg returns n-th argument as float-typed (if it's convertible to float) 141 GetFloatArg(n int) (float64, error) 142 // GetFloatArgDefault returns n-th argument as float. It will replace it with Default value if none present. 143 GetFloatArgDefault(n int, v float64) (float64, error) 144 // GetFloatNamedOrPosArgDefault returns specific positioned float64-typed argument or replace it with default if none found. 145 GetFloatNamedOrPosArgDefault(k string, n int, v float64) (float64, error) 146 147 // GetIntArg returns n-th argument as int-typed 148 GetIntArg(n int) (int, error) 149 // GetIntArgs returns n-th argument as slice of ints 150 GetIntArgs(n int) ([]int, error) 151 // GetIntArgDefault returns n-th argument as int. It will replace it with Default value if none present. 152 GetIntArgDefault(n int, d int) (int, error) 153 // GetIntArgWithIndication returns n-th argument as int. If argument wasn't present, second return value will be `false`. Even if there was error in parsing data, but it was there, second value will be `true` 154 GetIntArgWithIndication(n int) (int, bool, error) 155 // GetIntNamedOrPosArgWithIndication returns specific positioned int-typed argument. If argument wasn't present, second return value will be `false`. Even if there was error in parsing data, but it was there, second value will be `true` 156 GetIntNamedOrPosArgWithIndication(k string, n int) (int, bool, error) 157 // GetIntNamedOrPosArgDefault returns specific positioned int-typed argument or replace it with default if none found. 158 GetIntNamedOrPosArgDefault(k string, n int, d int) (int, error) 159 160 GetIntOrInfArg(n int) (IntOrInf, error) 161 GetIntOrInfArgDefault(n int, d IntOrInf) (IntOrInf, error) 162 GetIntOrInfNamedOrPosArgDefault(k string, n int, d IntOrInf) (IntOrInf, error) 163 164 GetNamedArg(name string) Expr 165 166 // GetBoolArgDefault returns n-th argument as bool. It will replace it with Default value if none present. 167 GetBoolArgDefault(n int, b bool) (bool, error) 168 // GetBoolNamedOrPosArgDefault returns specific positioned bool-typed argument or replace it with default if none found. 169 GetBoolNamedOrPosArgDefault(k string, n int, b bool) (bool, error) 170 171 // GetNodeOrTagArgs returns the last arguments starting from the n-th as a slice of NodeOrTag structures. If `single` is `true`, only the n-th argument is taken. 172 GetNodeOrTagArgs(n int, single bool) ([]NodeOrTag, error) 173 174 IsInterfaceNil() bool 175 176 toExpr() interface{} 177 } 178 179 var _ Expr = &expr{} 180 181 // Parse parses string as an expression. 182 func Parse(e string) (Expr, string, error) { 183 return ParseExpr(e) 184 } 185 186 // NewTargetExpr Creates new expression with specified target only. 187 func NewTargetExpr(target string) Expr { 188 e := &expr{ 189 target: target, 190 argString: target, 191 } 192 return e 193 } 194 195 // NewNameExpr Creates new expression with specified name only. 196 func NewNameExpr(name string) Expr { 197 e := &expr{ 198 target: name, 199 etype: EtName, 200 argString: name, 201 } 202 return e 203 } 204 205 // NewConstExpr Creates new Constant expression. 206 func NewConstExpr(value float64) Expr { 207 e := &expr{ 208 val: value, 209 etype: EtConst, 210 argString: fmt.Sprintf("%v", value), 211 } 212 return e 213 } 214 215 // NewValueExpr Creates new Value expression. 216 func NewValueExpr(value string) Expr { 217 e := &expr{ 218 valStr: value, 219 etype: EtString, 220 argString: value, 221 } 222 return e 223 } 224 225 // ArgName is a type for Name Argument 226 type ArgName string 227 228 // ArgValue is a type for Value Argument 229 type ArgValue string 230 231 // NamedArgs is a type for Hashmap of Named Arguments. 232 type NamedArgs map[string]interface{} 233 234 // NewExpr creates a new expression with specified target and arguments. It will do best it can to identify type of argument 235 func NewExpr(target string, vaArgs ...interface{}) Expr { 236 var nArgsFinal map[string]*expr 237 args, nArgs := sliceExpr(vaArgs) 238 if args == nil { 239 panic(fmt.Sprintf("unsupported argument list for target=%v\n", target)) 240 } 241 242 var a []*expr 243 var argStrs []string 244 for _, arg := range args { 245 argStrs = append(argStrs, arg.RawArgs()) 246 a = append(a, arg) 247 } 248 249 if nArgs != nil { 250 nArgsFinal = make(map[string]*expr) 251 for k, v := range nArgs { 252 nArgsFinal[k] = v 253 argStrs = append(argStrs, k+"="+v.RawArgs()) 254 } 255 } 256 257 e := &expr{ 258 target: target, 259 etype: EtFunc, 260 args: a, 261 argString: strings.Join(argStrs, ","), 262 } 263 264 if nArgsFinal != nil { 265 e.namedArgs = nArgsFinal 266 } 267 268 return e 269 } 270 271 // NewExprTyped creates a new expression with specified target and arguments. Strictly typed one. 272 func NewExprTyped(target string, args []Expr) Expr { 273 var a []*expr 274 var argStrs []string 275 for _, arg := range args { 276 argStrs = append(argStrs, arg.Target()) 277 a = append(a, arg.toExpr().(*expr)) 278 } 279 280 e := &expr{ 281 target: target, 282 etype: EtFunc, 283 args: a, 284 argString: strings.Join(argStrs, ","), 285 } 286 287 return e 288 }