( data, typeName, )
| 30 | |
| 31 | |
| 32 | export const data2TypeInfo = ( |
| 33 | data, |
| 34 | typeName, |
| 35 | ) => { |
| 36 | if (data === null || data === undefined) { |
| 37 | return null; |
| 38 | } |
| 39 | if (Array.isArray(data)) { |
| 40 | const item = data2TypeInfo(data[0], typeName); |
| 41 | if (!item) { |
| 42 | throw new Error("empty array can't convert"); |
| 43 | } |
| 44 | return Type.list(item); |
| 45 | } |
| 46 | if (data instanceof Date) { |
| 47 | return Type.timestamp(); |
| 48 | } |
| 49 | if (typeof data === "string") { |
| 50 | return Type.string(); |
| 51 | } |
| 52 | if (data instanceof Set) { |
| 53 | return Type.set(data2TypeInfo([...data.values()][0], typeName)); |
| 54 | } |
| 55 | if (data instanceof Map) { |
| 56 | return Type.map( |
| 57 | data2TypeInfo([...data.keys()][0], typeName), |
| 58 | data2TypeInfo([...data.values()][0], typeName), |
| 59 | ); |
| 60 | } |
| 61 | if (typeof data === "boolean") { |
| 62 | return Type.bool(); |
| 63 | } |
| 64 | if (typeof data === "number") { |
| 65 | if (data > Number.MAX_SAFE_INTEGER || data < Number.MIN_SAFE_INTEGER) { |
| 66 | return Type.int64(); |
| 67 | } |
| 68 | return Type.int32(); |
| 69 | } |
| 70 | |
| 71 | if (typeof data === "object") { |
| 72 | if (isUint8Array(data)) { |
| 73 | return Type.binary(); |
| 74 | } |
| 75 | |
| 76 | return Type.struct( |
| 77 | { |
| 78 | typeName |
| 79 | }, |
| 80 | Object.fromEntries( |
| 81 | Object.entries(data) |
| 82 | .map(([key, value]) => { |
| 83 | return [key, data2TypeInfo(value, `${typeName}.${key}`)]; |
| 84 | }) |
| 85 | .filter(([, v]) => Boolean(v)), |
| 86 | ), |
| 87 | ); |
| 88 | } |
| 89 |
nothing calls this directly
no test coverage detected