Skip to content

Commit fb3afce

Browse files
committed
define type mapping type
1 parent 86a20f3 commit fb3afce

File tree

1 file changed

+22
-43
lines changed

1 file changed

+22
-43
lines changed

src/support/docblocks.ts

Lines changed: 22 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -204,57 +204,32 @@ const getAttributeType = (attr: Eloquent.Attribute): string => {
204204
if (attr.nullable && type !== "mixed") {
205205
return `${type}|null`;
206206
}
207-
207+
208208
return type;
209209
};
210210

211-
const castMapping: Record<string, (string | RegExp)[]> = {
212-
array: [
213-
"json",
214-
"encrypted:json",
215-
"encrypted:array",
216-
],
217-
int: [
218-
"timestamp",
219-
],
220-
mixed: [
221-
"attribute",
222-
"accessor",
223-
"encrypted",
224-
],
225-
object: [
226-
"encrypted:object",
227-
],
228-
string: [
229-
"hashed",
230-
],
231-
"\\Illuminate\\Support\\Carbon": [
232-
"date",
233-
"datetime",
234-
],
235-
"\\Illuminate\\Support\\Collection": [
236-
"encrypted:collection",
237-
],
211+
type TypeMapping = Record<string, (string | RegExp)[]>;
212+
213+
const castMapping: TypeMapping = {
214+
array: ["json", "encrypted:json", "encrypted:array"],
215+
int: ["timestamp"],
216+
mixed: ["attribute", "accessor", "encrypted"],
217+
object: ["encrypted:object"],
218+
string: ["hashed"],
219+
"\\Illuminate\\Support\\Carbon": ["date", "datetime"],
220+
"\\Illuminate\\Support\\Collection": ["encrypted:collection"],
238221
};
239222

240-
const typeMapping: Record<string, (string | RegExp)[]> = {
241-
bool: [
242-
/^boolean(\((0|1)\))?$/,
243-
/^tinyint( unsigned)?(\(\d+\))?$/,
244-
],
223+
const typeMapping: TypeMapping = {
224+
bool: [/^boolean(\((0|1)\))?$/, /^tinyint( unsigned)?(\(\d+\))?$/],
245225
float: [
246226
"real",
247227
"money",
248228
"double precision",
249229
/^(double|decimal|numeric)(\(\d+\,\d+\))?$/,
250230
],
251-
int: [
252-
/^(big)?serial$/,
253-
/^(small|big)?int(eger)?( unsigned)?$/,
254-
],
255-
resource: [
256-
"bytea",
257-
],
231+
int: [/^(big)?serial$/, /^(small|big)?int(eger)?( unsigned)?$/],
232+
resource: ["bytea"],
258233
string: [
259234
"box",
260235
"cidr",
@@ -279,8 +254,8 @@ const typeMapping: Record<string, (string | RegExp)[]> = {
279254
};
280255

281256
const findInMapping = (
282-
mapping: Record<string, (string | RegExp)[]>,
283-
value: string | null
257+
mapping: TypeMapping,
258+
value: string | null,
284259
): string | null => {
285260
if (value === null) {
286261
return null;
@@ -302,7 +277,11 @@ const findInMapping = (
302277
};
303278

304279
const getActualType = (cast: string | null, type: string): string => {
305-
const finalType = findInMapping(castMapping, cast) || cast || findInMapping(typeMapping, type) || "mixed";
280+
const finalType =
281+
findInMapping(castMapping, cast) ||
282+
cast ||
283+
findInMapping(typeMapping, type) ||
284+
"mixed";
306285

307286
if (finalType.includes("\\") && !finalType.startsWith("\\")) {
308287
return `\\${finalType}`;

0 commit comments

Comments
 (0)