Skip to content

Commit 55088ba

Browse files
committed
Use type predicate validators
1 parent 054176e commit 55088ba

File tree

1 file changed

+21
-12
lines changed

1 file changed

+21
-12
lines changed

packages/compass-collection/src/transform-schema-to-field-info.ts

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,19 @@ import type {
66
DocumentSchemaType,
77
PrimitiveSchemaType,
88
} from 'mongodb-schema';
9+
10+
// Type guards for mongodb-schema types
11+
function isArraySchemaType(type: SchemaType): type is ArraySchemaType {
12+
return type.name === 'Array';
13+
}
14+
15+
function isDocumentSchemaType(type: SchemaType): type is DocumentSchemaType {
16+
return type.name === 'Document';
17+
}
18+
19+
function isPrimitiveSchemaType(type: SchemaType): type is PrimitiveSchemaType {
20+
return !isArraySchemaType(type) && !isDocumentSchemaType(type);
21+
}
922
import type { FieldInfo } from './schema-analysis-types';
1023

1124
/**
@@ -82,32 +95,28 @@ function processType(
8295
return;
8396
}
8497

85-
if (type.name === 'Array' || type.bsonType === 'Array') {
98+
if (isArraySchemaType(type)) {
8699
// Array: add [] to path and recurse into element type
87-
const arrayType = type as ArraySchemaType;
88-
const elementType = getMostFrequentType(arrayType.types || []);
100+
const elementType = getMostFrequentType(type.types || []);
89101

90102
if (!elementType) {
91103
return;
92104
}
93105

94106
const arrayPath = `${currentPath}[]`;
95107
processType(elementType, arrayPath, result, fieldProbability);
96-
} else if (type.name === 'Document' || type.bsonType === 'Document') {
108+
} else if (isDocumentSchemaType(type)) {
97109
// Document: Process nested document fields
98-
99-
const docType = type as DocumentSchemaType;
100-
if (docType.fields) {
101-
for (const nestedField of docType.fields) {
110+
if (type.fields) {
111+
for (const nestedField of type.fields) {
102112
processNamedField(nestedField, currentPath, result);
103113
}
104114
}
105-
} else {
115+
} else if (isPrimitiveSchemaType(type)) {
106116
// Primitive: Create entry
107-
const primitiveType = type as PrimitiveSchemaType;
108117
const fieldInfo: FieldInfo = {
109-
type: primitiveType.name,
110-
sample_values: primitiveType.values.slice(0, 10).map((value) => {
118+
type: type.name,
119+
sample_values: type.values.slice(0, 10).map((value) => {
111120
// Convert BSON values to their primitive equivalents, but keep Date objects as-is
112121
if (value instanceof Date) {
113122
return value;

0 commit comments

Comments
 (0)