forked from allegro/hermes
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
resolve allegro#1907 | handle record references when presenting avro …
…schema
- Loading branch information
1 parent
4180225
commit 419d7b7
Showing
7 changed files
with
136 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
hermes-console/src/views/topic/schema-panel/avro-viewer/avro-records-registry.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import type { | ||
AvroSchema, | ||
RecordType, | ||
} from '@/views/topic/schema-panel/AvroTypes'; | ||
|
||
interface WorkingRecord { | ||
record: RecordType; | ||
namespace?: string; | ||
justName: string; | ||
} | ||
|
||
const namespace = ( | ||
schema: AvroSchema, | ||
parentNamespace?: string, | ||
): string | undefined => { | ||
if (schema.name.includes('.')) { | ||
const lastDotIndex = schema.name.lastIndexOf('.'); | ||
return schema.name.substring(0, lastDotIndex); | ||
} | ||
if (schema.namespace) { | ||
return schema.namespace; | ||
} | ||
return parentNamespace; | ||
}; | ||
|
||
const justRecordName = (schema: AvroSchema): string => { | ||
if (schema.name.includes('.')) { | ||
const lastDotIndex = schema.name.lastIndexOf('.'); | ||
return schema.name.substring(lastDotIndex + 1); | ||
} | ||
return schema.name; | ||
}; | ||
|
||
const recordWithNamespace = ( | ||
record: RecordType, | ||
parentNamespace?: string, | ||
): WorkingRecord => { | ||
return { | ||
record, | ||
namespace: namespace(record, parentNamespace), | ||
justName: justRecordName(record), | ||
}; | ||
}; | ||
|
||
const getDirectSubRecords = (parent: WorkingRecord): WorkingRecord[] => { | ||
return parent.record.fields | ||
.flatMap((field) => (Array.isArray(field.type) ? field.type : [field.type])) | ||
.filter((field) => field.type === 'record') | ||
.map((record) => recordWithNamespace(record, parent.namespace)); | ||
}; | ||
|
||
const getAllSubRecords = ( | ||
unprocessedRecords: WorkingRecord[], | ||
processedRecords: WorkingRecord[], | ||
): WorkingRecord[] => { | ||
if (unprocessedRecords.length === 0) { | ||
return processedRecords; | ||
} | ||
const currentLevelSubRecords = | ||
unprocessedRecords.flatMap(getDirectSubRecords); | ||
return getAllSubRecords(currentLevelSubRecords, [ | ||
...processedRecords, | ||
...currentLevelSubRecords, | ||
]); | ||
}; | ||
|
||
const validQualifiers = ( | ||
record: WorkingRecord, | ||
rootNamespace?: string, | ||
): string[] => { | ||
if (!record.namespace) { | ||
return [record.justName]; | ||
} | ||
if (record.namespace === rootNamespace) { | ||
return [record.justName, `${record.namespace}.${record.justName}`]; | ||
} | ||
return [`${record.namespace}.${record.justName}`]; | ||
}; | ||
|
||
const associateByValidQualifiers = ( | ||
records: WorkingRecord[], | ||
rootNamespace?: string, | ||
): Map<string, RecordType> => { | ||
return new Map( | ||
records.flatMap((record) => { | ||
return validQualifiers(record, rootNamespace).map((qualifier) => [ | ||
qualifier, | ||
record.record, | ||
]); | ||
}), | ||
); | ||
}; | ||
|
||
export const createRecordsRegistry = ( | ||
schema: AvroSchema, | ||
): Map<string, RecordType> => { | ||
const workingRootRecord = recordWithNamespace({ | ||
...schema.type, | ||
name: schema.name, | ||
namespace: schema.namespace, | ||
}); | ||
const allRecords = getAllSubRecords([workingRootRecord], []); | ||
return associateByValidQualifiers(allRecords, workingRootRecord.namespace); | ||
}; |