1/* 2 * Copyright (C) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16export class PluginConvertUtils { 17 private static crlf: string = "\n"; 18 private static leftBrace: string = "{" 19 private static rightBrace: string = "}" 20 21 public static createHdcCmd(requestString: string, time: number) { 22 return "hiprofiler_cmd \\" + this.crlf 23 + " -c - \\" + this.crlf 24 + " -o /data/local/tmp/hiprofiler_data.htrace \\" + this.crlf 25 + " -t " + time + " \\" + this.crlf 26 + "<<CONFIG" 27 + requestString 28 + "CONFIG" 29 } 30 31 public static BeanToCmdTxt(bean: any, needColon: boolean): string { 32 return this.handleObj(bean, 0, needColon); 33 } 34 35 private static handleObj(bean: object, indentation: number, needColon: boolean): string { 36 let prefixText: string = ""; 37 if (indentation == 0) { 38 prefixText = prefixText + this.crlf; 39 } else { 40 prefixText = prefixText + " " + this.leftBrace + this.crlf; 41 } 42 for (const [key, value] of Object.entries(bean)) { 43 const repeatedKey = Array.isArray(value); 44 if (repeatedKey) { 45 prefixText = prefixText + this.handleArray(key, value, indentation, needColon); 46 } else { 47 switch (typeof value) { 48 case "bigint": 49 prefixText = prefixText + ' '.repeat(indentation + 1) + this.humpToSnake(key) + ": " + value.toString() + this.crlf 50 break 51 case "boolean": 52 prefixText = prefixText + ' '.repeat(indentation + 1) + this.humpToSnake(key) + ": " + value.toString() + this.crlf 53 break 54 case "number": 55 if (value == 0) { 56 break; 57 } 58 prefixText = prefixText + ' '.repeat(indentation + 1) + this.humpToSnake(key) + ": " + value.toString() + this.crlf 59 break 60 case "string": 61 if (value == '') { 62 break 63 } 64 if (value.startsWith("LOG_")) { 65 prefixText = prefixText + ' '.repeat(indentation + 1) + this.humpToSnake(key) + ": " + value.toString() + this.crlf 66 } else { 67 prefixText = prefixText + ' '.repeat(indentation + 1) + this.humpToSnake(key) + ": \"" + value.toString() + "\"" + this.crlf 68 } 69 break 70 case "object": 71 default: 72 if (needColon) { 73 prefixText = prefixText + ' '.repeat(indentation + 1) + this.humpToSnake(key) + ": " + this.handleObj(value, indentation + 1, needColon) + "" + this.crlf 74 } else { 75 prefixText = prefixText + ' '.repeat(indentation + 1) + this.humpToSnake(key) + this.handleObj(value, indentation + 1, needColon) + "" + this.crlf 76 } 77 } 78 } 79 } 80 if (indentation == 0) { 81 return prefixText 82 } else { 83 return prefixText + ' '.repeat(indentation) + this.rightBrace; 84 } 85 } 86 87 private static handleArray(key: string, arr: Array<any>, indentation: number, needColon: boolean): string { 88 let text = ""; 89 arr.forEach(arrValue => { 90 switch (typeof arrValue) { 91 case "bigint": 92 text = text + ' '.repeat(indentation + 1) + this.humpToSnake(key) + ": " + arrValue.toString() + this.crlf 93 break 94 case "boolean": 95 text = text + ' '.repeat(indentation + 1) + this.humpToSnake(key) + ": " + arrValue.toString() + this.crlf 96 break 97 case "number": 98 text = text + ' '.repeat(indentation + 1) + this.humpToSnake(key) + ": " + arrValue.toString() + this.crlf 99 break 100 case "string": 101 if (arrValue == '') { 102 break 103 } 104 if (arrValue.startsWith("VMEMPLUGIN") || arrValue.startsWith("MEMPLUGIN")) { 105 text = text + ' '.repeat(indentation + 1) + this.humpToSnake(key) + ": " + arrValue.toString() + this.crlf 106 } else { 107 text = text + ' '.repeat(indentation + 1) + this.humpToSnake(key) + ": \"" + arrValue.toString() + "\"" + this.crlf 108 } 109 break 110 case "object": 111 default: 112 if (needColon) { 113 text = text + ' '.repeat(indentation + 1) + this.humpToSnake(key) + ": " + this.handleObj(arrValue, indentation + 1, needColon) + "" + this.crlf 114 } else { 115 text = text + ' '.repeat(indentation + 1) + this.humpToSnake(key) + this.handleObj(arrValue, indentation + 1, needColon) + "" + this.crlf 116 } 117 } 118 }) 119 return text; 120 } 121 122 private static humpToSnake(humpString: string): string { 123 return humpString.replace(/[A-Z]/g, (value) => '_' + value.toLowerCase()); 124 } 125} 126