Skip to content

Commit a4b8cc2

Browse files
Treat cast and type separately when determining finalType
1 parent 62a9444 commit a4b8cc2

File tree

1 file changed

+81
-30
lines changed

1 file changed

+81
-30
lines changed

src/support/docblocks.ts

Lines changed: 81 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -208,49 +208,100 @@ const getAttributeType = (attr: Eloquent.Attribute): string => {
208208
return type;
209209
};
210210

211-
const mapType = (type: string): string => {
212-
const mapping: Record<string, (string | RegExp)[]> = {
213-
bool: [
214-
"boolean(1)",
215-
"boolean(0)",
216-
"tinyint",
217-
"tinyint unsigned",
218-
"boolean",
219-
/tinyint\(\d+\)/,
220-
],
221-
string: [
222-
"longtext",
223-
"mediumtext",
224-
"text",
225-
/varchar\(\d+\)/,
226-
/char\(\d+\)/,
227-
],
228-
float: [/double\(\d+\,\d+\)/],
229-
int: ["bigint", "bigint unsigned", "integer", "int unsigned"],
230-
mixed: ["attribute", "accessor", "encrypted"],
231-
array: ["encrypted:json", "encrypted:array", "json"],
232-
"\\Illuminate\\Support\\Carbon": ["datetime", "timestamp"],
233-
"\\Illuminate\\Support\\Collection": ["encrypted:collection"],
234-
object: ["encrypted:object"],
235-
};
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+
],
238+
};
239+
240+
const typeMapping: Record<string, (string | RegExp)[]> = {
241+
bool: [
242+
/^boolean(\((0|1)\))?$/,
243+
/^tinyint( unsigned)?(\(\d+\))?$/,
244+
],
245+
float: [
246+
"real",
247+
"money",
248+
"double precision",
249+
/^(double|decimal|numeric)(\(\d+\,\d+\))?$/,
250+
],
251+
int: [
252+
/^(big)?serial$/,
253+
/^(small|big)?int(eger)?( unsigned)?$/,
254+
],
255+
resource: [
256+
"bytea",
257+
],
258+
string: [
259+
"box",
260+
"cidr",
261+
"date",
262+
"inet",
263+
"line",
264+
"lseg",
265+
"path",
266+
"time",
267+
"uuid",
268+
"point",
269+
"circle",
270+
"polygon",
271+
"interval",
272+
/^json(b)?$/,
273+
/^macaddr(8)?$/,
274+
/^(long|medium)?text$/,
275+
/^(var)?char(acter)?( varying)??(\(\d+\))?$/,
276+
/^time(stamp)?\(\d+\)( (with|without) time zone)?$/,
277+
],
278+
};
279+
280+
const findInMapping = (
281+
mapping: Record<string, (string | RegExp)[]>,
282+
value: string | null
283+
): string | null => {
284+
if (value === null) {
285+
return null;
286+
}
236287

237288
for (const [newType, matches] of Object.entries(mapping)) {
238289
for (const match of matches) {
239-
if (type === match) {
290+
if (match === value) {
240291
return newType;
241292
}
242293

243-
if (match instanceof RegExp && type.match(match)) {
294+
if (match instanceof RegExp && value.match(match)) {
244295
return newType;
245296
}
246297
}
247298
}
248299

249-
return type;
300+
return null;
250301
};
251302

252-
const getActualType = (type: string): string => {
253-
const finalType = mapType(type);
303+
const getActualType = (cast: string | null, type: string): string => {
304+
const finalType = findInMapping(castMapping, cast) ?? findInMapping(typeMapping, type) ?? "mixed";
254305

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

0 commit comments

Comments
 (0)