Skip to content

Commit

Permalink
🟢 separate typemaps from resource calls
Browse files Browse the repository at this point in the history
Regex is the biggest offender right now and we need a long-term solution for this

Signed-off-by: Dominik Richter <[email protected]>
  • Loading branch information
arlimus committed Mar 2, 2024
1 parent c86f42c commit 4514f5b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 14 deletions.
4 changes: 2 additions & 2 deletions llx/builtin_global.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}

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

0 comments on commit 4514f5b

Please sign in to comment.