-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
⭐ add typemaps for simple types (#3463)
* ⭐ add typemaps for simple types Basically: ```coffee > int(1.23) 1 > bool(1) true > float(12) 12 > string(1.89) "1.89" > regex("w.r.d") == "world 🌎" /w.r.d/ ``` Signed-off-by: Dominik Richter <[email protected]> * 🟢 separate typemaps from resource calls Regex is the biggest offender right now and we need a long-term solution for this Signed-off-by: Dominik Richter <[email protected]> --------- Signed-off-by: Dominik Richter <[email protected]>
- Loading branch information
Showing
5 changed files
with
246 additions
and
37 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
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,57 @@ | ||
// Copyright (c) Mondoo, Inc. | ||
// SPDX-License-Identifier: BUSL-1.1 | ||
|
||
package mqlc | ||
|
||
import ( | ||
"errors" | ||
|
||
"go.mondoo.com/cnquery/v10/llx" | ||
"go.mondoo.com/cnquery/v10/mqlc/parser" | ||
"go.mondoo.com/cnquery/v10/types" | ||
) | ||
|
||
var typeConversions map[string]fieldCompiler | ||
|
||
func init() { | ||
typeConversions = map[string]fieldCompiler{ | ||
"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), | ||
} | ||
} | ||
|
||
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, errNotConversion | ||
} | ||
|
||
arg := call.Function[0] | ||
if arg == nil || arg.Value == nil || arg.Value.Operand == nil || arg.Value.Operand.Value == nil { | ||
return types.Nil, errors.New("failed to get parameter for '" + id + "'") | ||
} | ||
|
||
argValue, err := c.compileExpression(arg.Value) | ||
if err != nil { | ||
return types.Nil, err | ||
} | ||
|
||
c.addChunk(&llx.Chunk{ | ||
Call: llx.Chunk_FUNCTION, | ||
Id: llxID, | ||
Function: &llx.Function{ | ||
Type: string(typ), | ||
Args: []*llx.Primitive{argValue}, | ||
}, | ||
}) | ||
|
||
return types.String, nil | ||
} | ||
} |