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 b18990a
Showing
8 changed files
with
275 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
139 changes: 139 additions & 0 deletions
139
hermes-console/src/views/topic/schema-panel/avro-viewer/avro-records-registry.spec.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,139 @@ | ||
import { createRecordsRegistry } from '@/views/topic/schema-panel/avro-viewer/avro-records-registry'; | ||
import { describe, expect } from 'vitest'; | ||
import type { AvroSchema, Field } from '@/views/topic/schema-panel/AvroTypes'; | ||
|
||
describe('avro records registry', () => { | ||
const createSchema = (fields: any, namespace?: string): AvroSchema => ({ | ||
name: 'Test', | ||
namespace, | ||
fields: fields, | ||
type: { | ||
type: 'record', | ||
fields: fields, | ||
}, | ||
doc: '', | ||
}); | ||
|
||
const createStringField = (name: string): Field => ({ | ||
name, | ||
type: 'string', | ||
fields: [], | ||
}); | ||
|
||
it('should handle no sub records', () => { | ||
// given | ||
const rootFields = [ | ||
createStringField('field1'), | ||
createStringField('field2'), | ||
]; | ||
const schema = createSchema(rootFields); | ||
|
||
// when | ||
const result = createRecordsRegistry(schema); | ||
|
||
// then | ||
const expectedResult = new Set([]); | ||
expect(new Set(result.entries())).toEqual(expectedResult); | ||
}); | ||
|
||
it('should handle subrecord with namespace for root with namespace', () => { | ||
// given | ||
const subRecord = { | ||
type: 'record', | ||
name: 'SubRecord', | ||
namespace: 'com.example2', | ||
fields: [createStringField('x')], | ||
}; | ||
const rootFields = [ | ||
{ | ||
name: 'field1', | ||
type: ['null', subRecord], | ||
fields: [], | ||
}, | ||
]; | ||
const schema = createSchema(rootFields, 'com.example'); | ||
|
||
// when | ||
const result = createRecordsRegistry(schema); | ||
|
||
// then | ||
const expectedResult = new Set([['com.example2.SubRecord', subRecord]]); | ||
expect(new Set(result.entries())).toEqual(expectedResult); | ||
}); | ||
|
||
it('should handle subrecord with namespace for root without namespace', () => { | ||
// given | ||
const subRecord = { | ||
type: 'record', | ||
name: 'SubRecord', | ||
namespace: 'com.example2', | ||
fields: [createStringField('x')], | ||
}; | ||
const rootFields = [ | ||
{ | ||
name: 'field1', | ||
type: ['null', subRecord], | ||
fields: [], | ||
}, | ||
]; | ||
const schema = createSchema(rootFields); | ||
|
||
// when | ||
const result = createRecordsRegistry(schema); | ||
|
||
// then | ||
const expectedResult = new Set([['com.example2.SubRecord', subRecord]]); | ||
expect(new Set(result.entries())).toEqual(expectedResult); | ||
}); | ||
|
||
it('should handle subrecord without namespace for root with namespace', () => { | ||
// given | ||
const subRecord = { | ||
type: 'record', | ||
name: 'SubRecord', | ||
fields: [createStringField('x')], | ||
}; | ||
const rootFields = [ | ||
{ | ||
name: 'field1', | ||
type: ['null', subRecord], | ||
fields: [], | ||
}, | ||
]; | ||
const schema = createSchema(rootFields, 'com.example'); | ||
|
||
// when | ||
const result = createRecordsRegistry(schema); | ||
|
||
// then | ||
const expectedResult = new Set([ | ||
['com.example.SubRecord', subRecord], | ||
['SubRecord', subRecord], | ||
]); | ||
expect(new Set(result.entries())).toEqual(expectedResult); | ||
}); | ||
|
||
it('should handle subrecord without namespace for root without namespace', () => { | ||
// given | ||
const subRecord = { | ||
type: 'record', | ||
name: 'SubRecord', | ||
fields: [createStringField('x')], | ||
}; | ||
const rootFields = [ | ||
{ | ||
name: 'field1', | ||
type: ['null', subRecord], | ||
fields: [], | ||
}, | ||
]; | ||
const schema = createSchema(rootFields); | ||
|
||
// when | ||
const result = createRecordsRegistry(schema); | ||
|
||
// then | ||
const expectedResult = new Set([['SubRecord', subRecord]]); | ||
expect(new Set(result.entries())).toEqual(expectedResult); | ||
}); | ||
}); |
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); | ||
}; |