|
| 1 | +// Copyright © 2023 The VirusTotal CLI authors. All Rights Reserved. |
| 2 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +// you may not use this file except in compliance with the License. |
| 4 | +// You may obtain a copy of the License at |
| 5 | +// |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +// |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | + |
| 14 | +package csv |
| 15 | + |
| 16 | +import ( |
| 17 | + "encoding/json" |
| 18 | + "fmt" |
| 19 | + "reflect" |
| 20 | + "strings" |
| 21 | +) |
| 22 | + |
| 23 | +func flatten(i interface{}) (map[string]interface{}, error) { |
| 24 | + result := make(map[string]interface{}) |
| 25 | + err := flattenValue(reflect.ValueOf(i), "", result) |
| 26 | + return result, err |
| 27 | +} |
| 28 | + |
| 29 | +func flattenValue(v reflect.Value, prefix string, m map[string]interface{}) error { |
| 30 | + switch v.Kind() { |
| 31 | + case reflect.Map: |
| 32 | + return flattenMap(v, prefix, m) |
| 33 | + case reflect.Struct: |
| 34 | + return flattenStruct(v, prefix, m) |
| 35 | + case reflect.Slice: |
| 36 | + return flattenSlice(v, prefix, m) |
| 37 | + case reflect.Interface, reflect.Ptr: |
| 38 | + if v.IsNil() { |
| 39 | + m[prefix] = "null" |
| 40 | + } else { |
| 41 | + return flattenValue(v.Elem(), prefix, m) |
| 42 | + } |
| 43 | + default: |
| 44 | + m[prefix] = v.Interface() |
| 45 | + } |
| 46 | + return nil |
| 47 | +} |
| 48 | + |
| 49 | +func flattenSlice(v reflect.Value, prefix string, m map[string]interface{}) error { |
| 50 | + n := v.Len() |
| 51 | + if n == 0 { |
| 52 | + return nil |
| 53 | + } |
| 54 | + |
| 55 | + first := v.Index(0) |
| 56 | + if first.Kind() == reflect.Interface { |
| 57 | + if !first.IsNil() { |
| 58 | + first = first.Elem() |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + switch first.Kind() { |
| 63 | + case reflect.Map, reflect.Slice, reflect.Struct: |
| 64 | + // Add the JSON representation of lists with complex types. |
| 65 | + // Otherwise the number of CSV headers can grow significantly. |
| 66 | + b, err := json.Marshal(v.Interface()) |
| 67 | + if err != nil { |
| 68 | + return err |
| 69 | + } |
| 70 | + m[prefix] = string(b) |
| 71 | + default: |
| 72 | + values := make([]string, v.Len()) |
| 73 | + for i := 0; i < v.Len(); i++ { |
| 74 | + val := v.Index(i).Interface() |
| 75 | + if val == nil { |
| 76 | + values[i] = "null" |
| 77 | + } else { |
| 78 | + values[i] = fmt.Sprintf("%v", val) |
| 79 | + } |
| 80 | + } |
| 81 | + m[prefix] = strings.Join(values, ",") |
| 82 | + } |
| 83 | + return nil |
| 84 | +} |
| 85 | + |
| 86 | +func flattenStruct(v reflect.Value, prefix string, m map[string]interface{}) (err error) { |
| 87 | + n := v.NumField() |
| 88 | + if prefix != "" { |
| 89 | + prefix += "/" |
| 90 | + } |
| 91 | + for i := 0; i < n; i++ { |
| 92 | + typeField := v.Type().Field(i) |
| 93 | + key := typeField.Tag.Get("csv") |
| 94 | + if key == "" { |
| 95 | + key = v.Type().Field(i).Name |
| 96 | + } |
| 97 | + if err = flattenValue(v.Field(i), prefix+key, m); err != nil { |
| 98 | + return err |
| 99 | + } |
| 100 | + } |
| 101 | + return err |
| 102 | +} |
| 103 | + |
| 104 | +func flattenMap(v reflect.Value, prefix string, m map[string]interface{}) (err error) { |
| 105 | + if prefix != "" { |
| 106 | + prefix += "/" |
| 107 | + } |
| 108 | + for _, k := range v.MapKeys() { |
| 109 | + if err := flattenValue(v.MapIndex(k), fmt.Sprintf("%v%v", prefix, k.Interface()), m); err != nil { |
| 110 | + return err |
| 111 | + } |
| 112 | + } |
| 113 | + return nil |
| 114 | +} |
0 commit comments