Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

⭐ add typemaps for simple types #3463

Merged
merged 2 commits into from
Mar 2, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
🟢 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 <dominik.richter@gmail.com>
arlimus committed Mar 2, 2024
commit 4514f5bee3660891c1dee7fe710c2f39231bb42b
4 changes: 2 additions & 2 deletions llx/builtin_global.go
Original file line number Diff line number Diff line change
@@ -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,
}
}

7 changes: 6 additions & 1 deletion mqlc/mqlc.go
Original file line number Diff line number Diff line change
@@ -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)
24 changes: 13 additions & 11 deletions mqlc/typemaps.go
Original file line number Diff line number Diff line change
@@ -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},
},
})