go.mondoo.com/cnquery@v0.0.0-20231005093811-59568235f6ea/mqlc/builtin_simple.go (about) 1 // Copyright (c) Mondoo, Inc. 2 // SPDX-License-Identifier: BUSL-1.1 3 4 package mqlc 5 6 import ( 7 "errors" 8 9 "go.mondoo.com/cnquery/llx" 10 "go.mondoo.com/cnquery/mqlc/parser" 11 "go.mondoo.com/cnquery/types" 12 ) 13 14 func compileStringContains(c *compiler, typ types.Type, ref uint64, id string, call *parser.Call) (types.Type, error) { 15 if call == nil || len(call.Function) != 1 { 16 return types.Nil, errors.New("function " + id + " needs one argument (function missing)") 17 } 18 19 f := call.Function[0] 20 if f.Value == nil || f.Value.Operand == nil { 21 return types.Nil, errors.New("function " + id + " needs one argument") 22 } 23 24 val, err := c.compileOperand(f.Value.Operand) 25 if err != nil { 26 return types.Nil, err 27 } 28 29 valType, err := c.dereferenceType(val) 30 if err != nil { 31 return types.Nil, err 32 } 33 34 switch valType { 35 case types.String: 36 c.addChunk(&llx.Chunk{ 37 Call: llx.Chunk_FUNCTION, 38 Id: "contains" + string(types.String), 39 Function: &llx.Function{ 40 Type: string(types.Bool), 41 Binding: ref, 42 Args: []*llx.Primitive{val}, 43 }, 44 }) 45 return types.Bool, nil 46 case types.Int: 47 c.addChunk(&llx.Chunk{ 48 Call: llx.Chunk_FUNCTION, 49 Id: "contains" + string(types.Int), 50 Function: &llx.Function{ 51 Type: string(types.Bool), 52 Binding: ref, 53 Args: []*llx.Primitive{val}, 54 }, 55 }) 56 return types.Bool, nil 57 case types.Regex: 58 c.addChunk(&llx.Chunk{ 59 Call: llx.Chunk_FUNCTION, 60 Id: "contains" + string(types.Regex), 61 Function: &llx.Function{ 62 Type: string(types.Bool), 63 Binding: ref, 64 Args: []*llx.Primitive{val}, 65 }, 66 }) 67 return types.Bool, nil 68 case types.Array(types.String): 69 c.addChunk(&llx.Chunk{ 70 Call: llx.Chunk_FUNCTION, 71 Id: "contains" + string(types.Array(types.String)), 72 Function: &llx.Function{ 73 Type: string(types.Bool), 74 Binding: ref, 75 Args: []*llx.Primitive{val}, 76 }, 77 }) 78 return types.Bool, nil 79 case types.Array(types.Int): 80 c.addChunk(&llx.Chunk{ 81 Call: llx.Chunk_FUNCTION, 82 Id: "contains" + string(types.Array(types.Int)), 83 Function: &llx.Function{ 84 Type: string(types.Bool), 85 Binding: ref, 86 Args: []*llx.Primitive{val}, 87 }, 88 }) 89 return types.Bool, nil 90 case types.Array(types.Regex): 91 c.addChunk(&llx.Chunk{ 92 Call: llx.Chunk_FUNCTION, 93 Id: "contains" + string(types.Array(types.Regex)), 94 Function: &llx.Function{ 95 Type: string(types.Bool), 96 Binding: ref, 97 Args: []*llx.Primitive{val}, 98 }, 99 }) 100 return types.Bool, nil 101 default: 102 return types.Nil, errors.New("cannot find #string.contains with this type " + types.Type(val.Type).Label()) 103 } 104 }