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