@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 249 lines 6.53 KB
Raw
1 import type { AttributeValue, ScalarAttributeType } from "@distilled.cloud/aws/dynamodb";
2 import {
3 getSetValueAST,
4 isClassSchema,
5 isListSchema,
6 isMapSchema,
7 isNumberSchema,
8 isRecordLikeSchema,
9 isRecordSchema,
10 isSetSchema,
11 isStringSchema,
12 isStructSchema,
13 } from "alchemy/Schema";
14 import * as Data from "effect/Data";
15 import * as Effect from "effect/Effect";
16 import * as S from "effect/Schema";
17 import * as AST from "effect/SchemaAST";
18
19 // this seems important for handling S.Struct.Fields https://effect.website/docs/schema/classes/#recursive-types-with-different-encoded-and-type
20 // interface CategoryEncoded extends Schema.Struct.Encoded<typeof fields> { .. }
21
22 /**
23 * Raised when a JavaScript value cannot be marshalled to a DynamoDB
24 * `AttributeValue` (e.g. a function or symbol).
25 */
26 export class InvalidAttributeValue extends Data.TaggedError("InvalidAttributeValue")<{
27 message: string;
28 value: any;
29 }> {}
30
31 /**
32 * Marshal a plain JavaScript value into a DynamoDB `AttributeValue`.
33 *
34 * @example Marshal a nested object
35 * ```typescript
36 * const item = yield* toAttributeValue({ name: "Alice", scores: [1, 2, 3] });
37 * // { M: { name: { S: "Alice" }, scores: { L: [{ N: "1" }, ...] } } }
38 * ```
39 */
40 export const toAttributeValue: (
41 value: any,
42 ) => Effect.Effect<AttributeValue, InvalidAttributeValue, never> = Effect.fn(function* (
43 value: any,
44 ) {
45 if (value === undefined) {
46 return {
47 NULL: false,
48 };
49 } else if (value === null) {
50 return {
51 NULL: true,
52 };
53 } else if (typeof value === "boolean") {
54 return {
55 BOOL: value,
56 };
57 } else if (typeof value === "string") {
58 return {
59 S: value,
60 };
61 } else if (typeof value === "number") {
62 return {
63 N: value.toString(10),
64 };
65 } else if (Array.isArray(value)) {
66 return {
67 L: yield* Effect.forEach(value, toAttributeValue),
68 };
69 } else if (value instanceof Set) {
70 const setType = getType(value);
71 if (setType === "EMPTY_SET") {
72 return {
73 SS: [],
74 };
75 } else if (Array.isArray(setType)) {
76 return {
77 L: yield* Effect.forEach(setType, toAttributeValue),
78 };
79 } else if (setType === "SS") {
80 return {
81 SS: Array.from(value.values()),
82 };
83 } else if (setType === "NS") {
84 return {
85 NS: Array.from(value.values()).map((value) => value.toString(10)),
86 };
87 } else if (setType === "BS") {
88 return {
89 BS: Array.from(value.values()),
90 };
91 } else {
92 return {
93 L: yield* Effect.forEach(Array.from(value.values()), toAttributeValue),
94 };
95 }
96 } else if (Buffer.isBuffer(value)) {
97 return {
98 B: new Uint8Array(value),
99 };
100 } else if (value instanceof File) {
101 return {
102 B: new Uint8Array(yield* Effect.promise(() => value.arrayBuffer())),
103 };
104 } else if (value instanceof Uint8Array) {
105 return {
106 B: value,
107 };
108 } else if (value instanceof ArrayBuffer) {
109 return {
110 B: new Uint8Array(value),
111 };
112 } else if (typeof value === "object") {
113 return {
114 M: Object.fromEntries(
115 yield* Effect.forEach(Object.entries(value), ([key, value]) =>
116 toAttributeValue(value).pipe(Effect.map((value) => [key, value])),
117 ),
118 ),
119 };
120 }
121
122 return yield* new InvalidAttributeValue({
123 message: `Unknown value type: ${typeof value}`,
124 value,
125 });
126 });
127
128 /**
129 * Unmarshal a DynamoDB `AttributeValue` back into a plain JavaScript value
130 * (inverse of {@link toAttributeValue}).
131 */
132 export const fromAttributeValue = (value: AttributeValue): any => {
133 if (value.NULL) {
134 return null;
135 } else if (typeof value.BOOL === "boolean") {
136 return value.BOOL;
137 } else if (value.L) {
138 return value.L.map(fromAttributeValue);
139 } else if (value.M) {
140 return Object.fromEntries(
141 Object.entries(value.M).map(([key, value]) => [key, fromAttributeValue(value!)]),
142 );
143 } else if (value.N) {
144 return parseFloat(value.N);
145 } else if (value.S) {
146 // how do we know if this is a date?
147 return value.S;
148 } else if (value.SS) {
149 return new Set(value.SS);
150 } else if (value.NS) {
151 return new Set(value.NS);
152 } else if (value.BS) {
153 return new Set(value.BS);
154 } else {
155 throw new Error(`Unknown attribute value: ${JSON.stringify(value)}`);
156 }
157 };
158
159 type ValueType =
160 | "L"
161 | "BOOL"
162 | "EMPTY_SET"
163 | "M"
164 | "NULL"
165 | "N"
166 | "S"
167 | "SS"
168 | "BS"
169 | "NS"
170 | "undefined";
171
172 const getType = (value: any): ValueType | ValueType[] => {
173 if (value === undefined) {
174 return "undefined";
175 } else if (value === null) {
176 return "NULL";
177 } else if (typeof value === "boolean") {
178 return "BOOL";
179 } else if (typeof value === "string") {
180 return "S";
181 } else if (typeof value === "number") {
182 return "N";
183 } else if (Array.isArray(value)) {
184 return "L";
185 } else if (value instanceof Set) {
186 return value.size === 0
187 ? "EMPTY_SET"
188 : (() => {
189 const types = Array.from(value.values())
190 .flatMap(getType)
191 .filter((type, i, arr) => arr.indexOf(type) === i);
192
193 return types.length === 1
194 ? types[0] === "S"
195 ? "SS"
196 : types[0] === "N"
197 ? "NS"
198 : types[0] === "BOOL"
199 ? "BS"
200 : types[0]
201 : "L";
202 })();
203 } else if (value instanceof Map) {
204 return "M";
205 } else if (typeof value === "object") {
206 return "M";
207 } else {
208 throw new Error(`Unknown value type: ${typeof value}`);
209 }
210 };
211
212 export const isScalarAttributeType = (type: string): type is ScalarAttributeType =>
213 type === "S" || type === "N" || type === "B";
214
215 export const toAttributeType = (schema: S.Schema<any>) => {
216 if (isStringSchema(schema)) {
217 return "S";
218 } else if (isNumberSchema(schema)) {
219 return "N";
220 } else if (isRecordLikeSchema(schema)) {
221 return "M";
222 } else if (isStringSetSchema(schema)) {
223 return "SS";
224 } else if (isNumberSetSchema(schema)) {
225 return "NS";
226 } else if (isListSchema(schema)) {
227 return "L";
228 }
229 return "S";
230 };
231
232 export const isMapSchemaType = (schema: S.Schema<any>) =>
233 isMapSchema(schema) ||
234 isRecordSchema(schema) ||
235 isStructSchema(schema) ||
236 isClassSchema(schema) ||
237 false;
238
239 export const isStringSetSchema = (schema: S.Schema<any>) => {
240 if (!isSetSchema(schema)) return false;
241 const valueAST = getSetValueAST(schema);
242 return valueAST !== undefined && AST.isString(valueAST);
243 };
244
245 export const isNumberSetSchema = (schema: S.Schema<any>) => {
246 if (!isSetSchema(schema)) return false;
247 const valueAST = getSetValueAST(schema);
248 return valueAST !== undefined && AST.isNumber(valueAST);
249 };