Skip to content

Commit

Permalink
resolve allegro#1907 | handle record references when presenting avro …
Browse files Browse the repository at this point in the history
…schema
  • Loading branch information
MiloszKorman committed Oct 16, 2024
1 parent 4180225 commit 67a7e9f
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 8 deletions.
2 changes: 1 addition & 1 deletion hermes-console/json-server/db.json
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@
"topics": [
{
"id": "pl.allegro.public.group.DummyEvent",
"schema": "{\"type\":\"record\",\"name\":\"DummyEvent\",\"namespace\":\"pl.allegro.public.group.DummyEvent\",\"doc\":\"Event emitted when notification is sent to a user\",\"fields\":[{\"name\":\"__metadata\",\"type\":[\"null\",{\"type\":\"map\",\"values\":\"string\"}],\"doc\":\"Field internally used by Hermes to propagate metadata.\",\"default\":null},{\"name\":\"waybillId\",\"type\":[\"null\",{\"type\":\"record\",\"name\":\"WaybillId\",\"fields\":[{\"name\":\"waybill\",\"type\":\"string\",\"doc\":\"Waybill\"},{\"name\":\"carrierId\",\"type\":\"string\",\"doc\":\"CarrierId\"}]}],\"doc\":\"WaybillId\",\"default\":null},{\"name\":\"notificationId\",\"type\":\"string\",\"doc\":\"Notification Id\"},{\"name\":\"userId\",\"type\":\"string\",\"doc\":\"User Id\"}]}",
"schema": "{\"type\":\"record\",\"name\":\"DummyEvent\",\"namespace\":\"pl.allegro.public.group.DummyEvent\",\"doc\":\"Event emitted when notification is sent to a user\",\"fields\":[{\"name\":\"__metadata\",\"type\":[\"null\",{\"type\":\"map\",\"values\":\"string\"}],\"doc\":\"Field internally used by Hermes to propagate metadata.\",\"default\":null},{\"name\":\"waybillId\",\"type\":[\"null\",{\"type\":\"record\",\"name\":\"WaybillId\",\"fields\":[{\"name\":\"waybill\",\"type\":\"string\",\"doc\":\"Waybill\"},{\"name\":\"carrierId\",\"type\":\"string\",\"doc\":\"CarrierId\"}]}],\"doc\":\"WaybillId\",\"default\":null},{\"name\":\"otherWaybillId\",\"type\":[\"null\",\"WaybillId\"],\"doc\":\"other WaybillId\",\"default\":null},{\"name\":\"notificationId\",\"type\":\"string\",\"doc\":\"Notification Id\"},{\"name\":\"userId\",\"type\":\"string\",\"doc\":\"User Id\"}]}",
"name": "pl.allegro.public.group.DummyEvent",
"description": "Events emitted when notification is sent to a user.",
"owner": {
Expand Down
2 changes: 1 addition & 1 deletion hermes-console/json-server/topics.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[
{
"id": "pl.allegro.public.group.DummyEvent",
"schema": "{\"type\":\"record\",\"name\":\"DummyEvent\",\"namespace\":\"pl.allegro.public.group.DummyEvent\",\"doc\":\"Event emitted when notification is sent to a user\",\"fields\":[{\"name\":\"__metadata\",\"type\":[\"null\",{\"type\":\"map\",\"values\":\"string\"}],\"doc\":\"Field internally used by Hermes to propagate metadata.\",\"default\":null},{\"name\":\"waybillId\",\"type\":[\"null\",{\"type\":\"record\",\"name\":\"WaybillId\",\"fields\":[{\"name\":\"waybill\",\"type\":\"string\",\"doc\":\"Waybill\"},{\"name\":\"carrierId\",\"type\":\"string\",\"doc\":\"CarrierId\"}]}],\"doc\":\"WaybillId\",\"default\":null},{\"name\":\"notificationId\",\"type\":\"string\",\"doc\":\"Notification Id\"},{\"name\":\"userId\",\"type\":\"string\",\"doc\":\"User Id\"}]}",
"schema": "{\"type\":\"record\",\"name\":\"DummyEvent\",\"namespace\":\"pl.allegro.public.group.DummyEvent\",\"doc\":\"Event emitted when notification is sent to a user\",\"fields\":[{\"name\":\"__metadata\",\"type\":[\"null\",{\"type\":\"map\",\"values\":\"string\"}],\"doc\":\"Field internally used by Hermes to propagate metadata.\",\"default\":null},{\"name\":\"waybillId\",\"type\":[\"null\",{\"type\":\"record\",\"name\":\"WaybillId\",\"fields\":[{\"name\":\"waybill\",\"type\":\"string\",\"doc\":\"Waybill\"},{\"name\":\"carrierId\",\"type\":\"string\",\"doc\":\"CarrierId\"}]}],\"doc\":\"WaybillId\",\"default\":null},{\"name\":\"otherWaybillId\",\"type\":[\"null\",\"WaybillId\"],\"doc\":\"other WaybillId\",\"default\":null},{\"name\":\"notificationId\",\"type\":\"string\",\"doc\":\"Notification Id\"},{\"name\":\"userId\",\"type\":\"string\",\"doc\":\"User Id\"}]}",
"name": "pl.allegro.public.group.DummyEvent",
"description": "Events emitted when notification is sent to a user.",
"owner": {
Expand Down
2 changes: 1 addition & 1 deletion hermes-console/src/dummy/topic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import type { Owner } from '@/api/owner';

export const dummyTopic: TopicWithSchema = {
schema:
'{"type":"record","name":"DummyEvent","namespace":"pl.allegro.public.group.DummyEvent","doc":"Event emitted when notification is sent to a user","fields":[{"name":"__metadata","type":["null",{"type":"map","values":"string"}],"doc":"Field internally used by Hermes to propagate metadata.","default":null},{"name":"waybillId","type":["null",{"type":"record","name":"WaybillId","fields":[{"name":"waybill","type":"string","doc":"Waybill"},{"name":"carrierId","type":"string","doc":"CarrierId"}]}],"doc":"WaybillId","default":null},{"name":"notificationId","type":"string","doc":"Notification Id"},{"name":"userId","type":"string","doc":"User Id"}]}',
'{"type":"record","name":"DummyEvent","namespace":"pl.allegro.public.group.DummyEvent","doc":"Event emitted when notification is sent to a user","fields":[{"name":"__metadata","type":["null",{"type":"map","values":"string"}],"doc":"Field internally used by Hermes to propagate metadata.","default":null},{"name":"waybillId","type":["null",{"type":"record","name":"WaybillId","fields":[{"name":"waybill","type":"string","doc":"Waybill"},{"name":"carrierId","type":"string","doc":"CarrierId"}]}],"doc":"WaybillId","default":null},{"name":"otherWaybillId","type":["null","WaybillId"],"doc":"other WaybillId","default":null},{"name":"notificationId","type":"string","doc":"Notification Id"},{"name":"userId","type":"string","doc":"User Id"}]}',
name: 'pl.allegro.public.group.DummyEvent',
description: 'Events emitted when notification is sent to a user.',
owner: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
<script lang="ts" setup>
import { defineProps, ref } from 'vue';
import type { AvroSchema, Type } from '@/views/topic/schema-panel/AvroTypes';
import type {
AvroSchema,
RecordType,
Type,
} from '@/views/topic/schema-panel/AvroTypes';
const props = defineProps<{
field: AvroSchema;
recordReferenceTypes: Map<string, RecordType>;
root: boolean;
}>();
const expanded = ref(true);
Expand Down Expand Up @@ -61,11 +66,22 @@
if (type.type === 'map') return type.values.symbols;
};
const findRecordReference = (type: Type) => {
const types = Array.isArray(type) ? type : [type];
return types
.filter((type) => typeof type === 'string')
.map((typeName) => props.recordReferenceTypes.get(typeName))
.find((recordType) => !!recordType);
};
const types = getTypes(props.field.type);
const isRecord = types.some((type: Type) => type.includes('record'));
const recordReference = findRecordReference(props.field.type);
const isRecord =
!!recordReference || types.some((type: Type) => type.includes('record'));
const isEnum = types.some((type: Type) => type.includes('enum'));
const expandable = isRecord || isEnum;
const nestedType = isRecord && findNestedType(props.field.type);
const nestedType =
isRecord && (recordReference || findNestedType(props.field.type));
const enumSymbols = isEnum && findEnumSymbols(props.field.type);
const toggle = (event: Event) => {
Expand Down Expand Up @@ -100,6 +116,7 @@
v-for="nestedField in nestedType.fields"
:key="nestedField.name"
:field="nestedField"
:record-reference-types="props.recordReferenceTypes"
:root="false"
/>
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<script lang="ts" setup>
import AvroNode from '@/views/topic/schema-panel/avro-viewer/AvroNode.vue';
import type { AvroSchema } from '@/views/topic/schema-panel/AvroTypes';
import type {
AvroSchema,
RecordType,
} from '@/views/topic/schema-panel/AvroTypes';
const props = defineProps<{
schema: string;
}>();
Expand All @@ -18,11 +21,32 @@
},
};
};
// TODO nested record reference in nested record reference - does my solution handle this?
const recordReferenceTypes = (root: AvroSchema): Map<string, RecordType> => {
const getSubRecords = (a: RecordType): RecordType[] => {
return a.fields
.flatMap((it) => (Array.isArray(it.type) ? it.type : [it.type]))
.filter((it) => it.type === 'record');
};
return new Map(
getSubRecords(root.type).map((it) => {
// TODO can record reference be by just name only or is it with root namespace if namespace not clarified for record itself?
const key = it.namespace ? `${it.namespace}.${it.name}` : it.name;
return [key, it];
}),
);
};
</script>

<template>
<div class="avro-schema">
<AvroNode v-if="rootField()" :field="rootField()" :root="true" />
<AvroNode
v-if="rootField()"
:field="rootField()"
:record-reference-types="recordReferenceTypes(rootField())"
:root="true"
/>
</div>
</template>

Expand Down

0 comments on commit 67a7e9f

Please sign in to comment.