From c92a9ae03e8d7f53d9d74554b6004bb5d4419c72 Mon Sep 17 00:00:00 2001 From: Lorenzo Blasa Date: Mon, 21 Nov 2022 04:51:23 -0800 Subject: [PATCH] Enrich raw data with metadata Summary: As metadata got split from attributes, raw data became harder to read. This change annotates raw data with attribute names to ease readability and thus usability. Reviewed By: antonk52 Differential Revision: D41400622 fbshipit-source-id: 8bebb2bd368490b9d7a2b4435749cdf0570b7571 --- .../sidebar/inspector/AttributesInspector.tsx | 3 +- .../public/ui-debugger/dataTransform.tsx | 71 +++++++++++++++++++ 2 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 desktop/plugins/public/ui-debugger/dataTransform.tsx diff --git a/desktop/plugins/public/ui-debugger/components/sidebar/inspector/AttributesInspector.tsx b/desktop/plugins/public/ui-debugger/components/sidebar/inspector/AttributesInspector.tsx index 12cc3c62b..e4bdd1c34 100644 --- a/desktop/plugins/public/ui-debugger/components/sidebar/inspector/AttributesInspector.tsx +++ b/desktop/plugins/public/ui-debugger/components/sidebar/inspector/AttributesInspector.tsx @@ -33,6 +33,7 @@ import { TextAttributeValueStyle, } from './Styles'; import {Glyph} from 'flipper'; +import {transform} from '../../../dataTransform'; const NumberValue = styled.span(NumberAttributeValueStyle); const TextValue = styled.span(TextAttributeValueStyle); @@ -240,7 +241,7 @@ export const AttributesInspector: React.FC = ({ {...sections} {rawDisplayEnabled && ( - + )} diff --git a/desktop/plugins/public/ui-debugger/dataTransform.tsx b/desktop/plugins/public/ui-debugger/dataTransform.tsx new file mode 100644 index 000000000..daa865335 --- /dev/null +++ b/desktop/plugins/public/ui-debugger/dataTransform.tsx @@ -0,0 +1,71 @@ +/** + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @format + */ + +import {Inspectable, InspectableObject, Metadata, MetadataId} from './types'; + +function transformAny( + metadata: Map, + inspectable: Inspectable, +): any { + switch (inspectable.type) { + case 'boolean': + case 'text': + case 'number': + case 'color': + case 'size': + case 'bounds': + case 'coordinate': + case 'coordinate3d': + case 'space': + return inspectable.value; + case 'enum': + return inspectable.value.value; + case 'object': + return transformObject(metadata, inspectable); + default: + return JSON.parse(JSON.stringify(inspectable)); + } +} + +function transformObject( + metadata: Map, + inspectableObject: InspectableObject, +): any { + const object: any = {}; + Object.keys(inspectableObject.fields).forEach((key, _index) => { + const metadataId: number = Number(key); + const meta = metadata.get(metadataId); + if (!meta) { + return; + } + + const inspectable = inspectableObject.fields[metadataId]; + object[meta.name] = transformAny(metadata, inspectable); + }); + + return object; +} + +export function transform( + attributes: Record, + metadata: Map, +): any { + const object: any = {}; + Object.keys(attributes).forEach((key) => { + const metadataId: number = Number(key); + const meta = metadata.get(metadataId); + if (!meta) { + return; + } + + const inspectable = attributes[metadataId] as InspectableObject; + object[meta.name] = transformObject(metadata, inspectable); + }); + return object; +}