1/*
2 * Copyright (C) 2023 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17import {ObjectUtils} from 'common/object_utils';
18import {StringUtils} from 'common/string_utils';
19
20export type FakeProto = any;
21
22export class FakeProtoBuilder {
23  private proto = {};
24
25  addArg(
26    key: string,
27    valueType: string,
28    intValue: bigint | undefined,
29    realValue: number | undefined,
30    stringValue: string | undefined,
31  ): FakeProtoBuilder {
32    const keyCamelCase = key
33      .split('.')
34      .map((token) => {
35        return StringUtils.convertSnakeToCamelCase(token);
36      })
37      .join('.');
38    const value = this.makeValue(valueType, intValue, realValue, stringValue);
39    ObjectUtils.setProperty(this.proto, keyCamelCase, value);
40    return this;
41  }
42
43  build(): FakeProto {
44    return this.proto;
45  }
46
47  private makeValue(
48    valueType: string,
49    intValue: bigint | undefined,
50    realValue: number | undefined,
51    stringValue: string | undefined,
52  ): string | bigint | number | boolean | null | undefined {
53    switch (valueType) {
54      case 'bool':
55        return Boolean(intValue);
56      case 'int':
57        return intValue;
58      case 'null':
59        return null;
60      case 'real':
61        return realValue;
62      case 'string':
63        return stringValue;
64      case 'uint':
65        return intValue;
66      default:
67        throw new Error(`Unsupported type ${valueType}`);
68    }
69  }
70}
71