| 1 |
// Copyright 2021-2026 Buf Technologies, Inc. |
| 2 |
// |
| 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 |
import { ScalarType } from "../descriptors.js"; |
| 15 |
/** |
| 16 |
* Return the TypeScript type (as a string) for the given scalar type. |
| 17 |
*/ |
| 18 |
export function scalarTypeScriptType(scalar, longAsString) { |
| 19 |
switch (scalar) { |
| 20 |
case ScalarType.STRING: |
| 21 |
return "string"; |
| 22 |
case ScalarType.BOOL: |
| 23 |
return "boolean"; |
| 24 |
case ScalarType.UINT64: |
| 25 |
case ScalarType.SFIXED64: |
| 26 |
case ScalarType.FIXED64: |
| 27 |
case ScalarType.SINT64: |
| 28 |
case ScalarType.INT64: |
| 29 |
return longAsString ? "string" : "bigint"; |
| 30 |
case ScalarType.BYTES: |
| 31 |
return "Uint8Array"; |
| 32 |
default: |
| 33 |
return "number"; |
| 34 |
} |
| 35 |
} |
| 36 |
/** |
| 37 |
* Return the JSON type (as a string) for the given scalar type. |
| 38 |
*/ |
| 39 |
export function scalarJsonType(scalar) { |
| 40 |
switch (scalar) { |
| 41 |
case ScalarType.DOUBLE: |
| 42 |
case ScalarType.FLOAT: |
| 43 |
return `number | "NaN" | "Infinity" | "-Infinity"`; |
| 44 |
case ScalarType.UINT64: |
| 45 |
case ScalarType.SFIXED64: |
| 46 |
case ScalarType.FIXED64: |
| 47 |
case ScalarType.SINT64: |
| 48 |
case ScalarType.INT64: |
| 49 |
return "string"; |
| 50 |
case ScalarType.INT32: |
| 51 |
case ScalarType.FIXED32: |
| 52 |
case ScalarType.UINT32: |
| 53 |
case ScalarType.SFIXED32: |
| 54 |
case ScalarType.SINT32: |
| 55 |
return "number"; |
| 56 |
case ScalarType.STRING: |
| 57 |
return "string"; |
| 58 |
case ScalarType.BOOL: |
| 59 |
return "boolean"; |
| 60 |
case ScalarType.BYTES: |
| 61 |
return "string"; |
| 62 |
} |
| 63 |
} |