|
| 1 | +package protocols |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "reflect" |
| 6 | + |
| 7 | + "github.com/Velocidex/ordereddict" |
| 8 | + vql_subsystem "www.velocidex.com/golang/velociraptor/vql" |
| 9 | + "www.velocidex.com/golang/vfilter" |
| 10 | + "www.velocidex.com/golang/vfilter/types" |
| 11 | +) |
| 12 | + |
| 13 | +type _BoolDict struct{} |
| 14 | + |
| 15 | +func (self _BoolDict) Applicable(a vfilter.Any) bool { |
| 16 | + switch a.(type) { |
| 17 | + case ordereddict.Dict, *ordereddict.Dict: |
| 18 | + return true |
| 19 | + } |
| 20 | + |
| 21 | + rt := reflect.TypeOf(a) |
| 22 | + if rt == nil { |
| 23 | + return false |
| 24 | + } |
| 25 | + return rt.Kind() == reflect.Slice || rt.Kind() == reflect.Map |
| 26 | +} |
| 27 | + |
| 28 | +func (self _BoolDict) Bool(ctx context.Context, scope vfilter.Scope, a vfilter.Any) bool { |
| 29 | + switch t := a.(type) { |
| 30 | + case ordereddict.Dict: |
| 31 | + return t.Len() > 0 |
| 32 | + case *ordereddict.Dict: |
| 33 | + return t.Len() > 0 |
| 34 | + } |
| 35 | + |
| 36 | + rt := reflect.TypeOf(a) |
| 37 | + if rt == nil { |
| 38 | + return false |
| 39 | + } |
| 40 | + if rt.Kind() == reflect.Slice || rt.Kind() == reflect.Map { |
| 41 | + a_slice := reflect.ValueOf(a) |
| 42 | + return a_slice.Len() > 0 |
| 43 | + } |
| 44 | + |
| 45 | + return false |
| 46 | +} |
| 47 | + |
| 48 | +type _AddDict struct{} |
| 49 | + |
| 50 | +func (self _AddDict) Applicable(a types.Any, b types.Any) bool { |
| 51 | + _, a_ok := a.(*ordereddict.Dict) |
| 52 | + _, b_ok := b.(*ordereddict.Dict) |
| 53 | + return a_ok && b_ok |
| 54 | +} |
| 55 | + |
| 56 | +func (self _AddDict) Add(scope types.Scope, a types.Any, b types.Any) types.Any { |
| 57 | + a_dict, a_ok := a.(*ordereddict.Dict) |
| 58 | + if !a_ok { |
| 59 | + return &vfilter.Null{} |
| 60 | + } |
| 61 | + b_dict, b_ok := b.(*ordereddict.Dict) |
| 62 | + if !b_ok { |
| 63 | + return &vfilter.Null{} |
| 64 | + } |
| 65 | + |
| 66 | + res := ordereddict.NewDict() |
| 67 | + |
| 68 | + for _, k := range a_dict.Keys() { |
| 69 | + v, _ := a_dict.Get(k) |
| 70 | + res.Set(k, v) |
| 71 | + } |
| 72 | + |
| 73 | + for _, k := range b_dict.Keys() { |
| 74 | + v, _ := b_dict.Get(k) |
| 75 | + res.Set(k, v) |
| 76 | + } |
| 77 | + |
| 78 | + return res |
| 79 | +} |
| 80 | + |
| 81 | +func init() { |
| 82 | + vql_subsystem.RegisterProtocol(&_BoolDict{}) |
| 83 | + vql_subsystem.RegisterProtocol(&_AddDict{}) |
| 84 | +} |
0 commit comments