From 4514f5bee3660891c1dee7fe710c2f39231bb42b Mon Sep 17 00:00:00 2001 From: Dominik Richter Date: Sat, 2 Mar 2024 02:02:19 -0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=9F=A2=20separate=20typemaps=20from=20res?= =?UTF-8?q?ource=20calls?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Regex is the biggest offender right now and we need a long-term solution for this Signed-off-by: Dominik Richter --- llx/builtin_global.go | 4 ++-- mqlc/mqlc.go | 7 ++++++- mqlc/typemaps.go | 24 +++++++++++++----------- 3 files changed, 21 insertions(+), 14 deletions(-) diff --git a/llx/builtin_global.go b/llx/builtin_global.go index 8dd18c1c66..7419adc51b 100644 --- a/llx/builtin_global.go +++ b/llx/builtin_global.go @@ -38,13 +38,13 @@ func init() { "return": returnCallV2, "createResource": globalCreateResource, // type-conversions - "semver": semverCall, "string": stringCall, - "regex": regexCall, + "$regex": regexCall, // TODO: support both the regex resource and the internal typemap! "float": floatCall, "int": intCall, "bool": boolCall, "dict": dictCall, + "semver": semverCall, } } diff --git a/mqlc/mqlc.go b/mqlc/mqlc.go index 93bff0de21..d854e966a5 100644 --- a/mqlc/mqlc.go +++ b/mqlc/mqlc.go @@ -1303,7 +1303,12 @@ func (c *compiler) compileIdentifier(id string, callBinding *variable, calls []* f = typeConversions[id] if f != nil { typ, err := f(c, id, call) - return restCalls, typ, err + // If it works or is some random error, we are done. However, we + // try to toss this fish back in the sea if it's not a conversion. + // For example: regex.ipv4 can be handled below, since it's not a conversion + if err == nil || err != errNotConversion { + return restCalls, typ, err + } } found, restCalls, typ, err = c.compileResource(id, calls) diff --git a/mqlc/typemaps.go b/mqlc/typemaps.go index 370c83de62..11c3e3b2d6 100644 --- a/mqlc/typemaps.go +++ b/mqlc/typemaps.go @@ -15,20 +15,22 @@ var typeConversions map[string]fieldCompiler func init() { typeConversions = map[string]fieldCompiler{ - "semver": compileTypeConversion(types.Semver), - "bool": compileTypeConversion(types.Bool), - "int": compileTypeConversion(types.Int), - "float": compileTypeConversion(types.Float), - "string": compileTypeConversion(types.String), - "regex": compileTypeConversion(types.Regex), - "dict": compileTypeConversion(types.Dict), + "bool": compileTypeConversion("bool", types.Bool), + "int": compileTypeConversion("int", types.Int), + "float": compileTypeConversion("float", types.Float), + "string": compileTypeConversion("string", types.String), + "regex": compileTypeConversion("$regex", types.Regex), + "dict": compileTypeConversion("dict", types.Dict), + "semver": compileTypeConversion("semver", types.Semver), } } -func compileTypeConversion(typ types.Type) fieldCompiler { +var errNotConversion = errors.New("not a type-conversion") + +func compileTypeConversion(llxID string, typ types.Type) fieldCompiler { return func(c *compiler, id string, call *parser.Call) (types.Type, error) { if call == nil || len(call.Function) < 1 { - return types.Nil, errors.New("missing parameter for '" + id + "', it requires 1") + return types.Nil, errNotConversion } arg := call.Function[0] @@ -43,9 +45,9 @@ func compileTypeConversion(typ types.Type) fieldCompiler { c.addChunk(&llx.Chunk{ Call: llx.Chunk_FUNCTION, - Id: id, + Id: llxID, Function: &llx.Function{ - Type: string(types.String), + Type: string(typ), Args: []*llx.Primitive{argValue}, }, })