Move review object explode function into utils

Signed-off-by: Stephen Glass <stephen@stephen.glass>
This commit is contained in:
Stephen Glass
2024-06-30 00:53:24 -04:00
parent 7193d02b32
commit b141c20b53
3 changed files with 238 additions and 32 deletions
@@ -15,9 +15,10 @@
*/
import React from 'react';
import { StructuredMetadataTable } from '@backstage/core-components';
import { JsonObject, JsonValue } from '@backstage/types';
import { JsonObject } from '@backstage/types';
import { Draft07 as JSONSchema } from 'json-schema-library';
import { ParsedTemplateSchema } from '../../hooks/useTemplateSchema';
import { flattenObject, isJsonObject } from './util';
/**
* The props for the {@link ReviewState} component.
@@ -28,37 +29,6 @@ export type ReviewStateProps = {
formState: JsonObject;
};
function flattenObject(
obj: JsonObject,
prefix: string,
schema: JSONSchema,
formState: JsonObject,
): [string, JsonValue | undefined][] {
return Object.entries(obj).flatMap(([key, value]) => {
const prefixedKey = prefix ? `${prefix}/${key}` : key;
const definitionInSchema = schema.getSchema({
pointer: `#/${prefixedKey}`,
data: formState,
});
if (definitionInSchema) {
const backstageReviewOptions = definitionInSchema['ui:backstage']?.review;
// Recurse into nested objects
if (backstageReviewOptions?.explode && isJsonObject(value)) {
return flattenObject(value, prefixedKey, schema, formState);
}
}
return [[key, value]];
});
}
function isJsonObject(value?: JsonValue): value is JsonObject {
return typeof value === 'object' && value !== null && !Array.isArray(value);
}
/**
* The component used by the {@link Stepper} to render the review step.
* @alpha
@@ -0,0 +1,187 @@
/* eslint-disable no-console */
/*
* Copyright 2022 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { flattenObject, isJsonObject } from './util';
import { Draft07 as JSONSchema } from 'json-schema-library';
import { ParsedTemplateSchema } from '../../hooks/useTemplateSchema';
describe('isJsonObject', () => {
it('should return true for non-null objects', () => {
expect(isJsonObject({})).toBe(true);
expect(isJsonObject({ key: 'value' })).toBe(true);
});
it('should return false for null', () => {
expect(isJsonObject(null)).toBe(false);
});
it('should return false for arrays', () => {
expect(isJsonObject([])).toBe(false);
expect(isJsonObject([1, 2, 3])).toBe(false);
});
it('should return false for non-objects', () => {
expect(isJsonObject('string')).toBe(false);
expect(isJsonObject(123)).toBe(false);
expect(isJsonObject(true)).toBe(false);
expect(isJsonObject(undefined)).toBe(false);
});
});
describe('flattenObject', () => {
it('should handle an empty object', () => {
const schemas: ParsedTemplateSchema[] = [
{
mergedSchema: {
type: 'object',
properties: {
name: {
type: 'object',
'ui:backstage': {
review: {
explode: true,
},
},
properties: {},
},
},
},
schema: {},
title: 'test',
uiSchema: {},
},
];
const parsedSchema = new JSONSchema(schemas[0].mergedSchema);
const result = flattenObject({}, '', parsedSchema, {});
expect(result).toEqual([]);
});
it('should flatten a simple object', () => {
const formState = {
name: {
foo: 'value1',
bar: 'value2',
},
};
const schemas: ParsedTemplateSchema[] = [
{
mergedSchema: {
type: 'object',
properties: {
name: {
type: 'object',
'ui:backstage': {
review: {
explode: true,
},
},
properties: {
foo: {
type: 'string',
},
bar: {
type: 'string',
},
},
},
},
},
schema: {},
title: 'test',
uiSchema: {},
},
];
const [key, value] = Object.entries(formState)[0];
const parsedSchema = new JSONSchema(schemas[0].mergedSchema);
const result = flattenObject(value, key, parsedSchema, formState);
expect(result).toEqual([
['foo', 'value1'],
['bar', 'value2'],
]);
});
it('should recurse into a nested object', () => {
const formState = {
name: {
foo: 'value1',
bar: 'value2',
example: {
test: 'value3',
},
},
};
const schemas: ParsedTemplateSchema[] = [
{
mergedSchema: {
type: 'object',
properties: {
name: {
type: 'object',
'ui:backstage': {
review: {
explode: true,
},
},
properties: {
foo: {
type: 'string',
},
bar: {
type: 'string',
},
example: {
type: 'object',
'ui:backstage': {
review: {
explode: true,
},
},
properties: {
test: {
type: 'string',
},
},
},
},
},
},
},
schema: {},
title: 'test',
uiSchema: {},
},
];
const [key, value] = Object.entries(formState)[0];
const parsedSchema = new JSONSchema(schemas[0].mergedSchema);
const result = flattenObject(value, key, parsedSchema, formState);
expect(result).toEqual([
['foo', 'value1'],
['bar', 'value2'],
['test', 'value3'],
]);
});
});
@@ -0,0 +1,49 @@
/*
* Copyright 2022 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { JsonObject, JsonValue } from '@backstage/types';
import { Draft07 as JSONSchema } from 'json-schema-library';
export function flattenObject(
obj: JsonObject,
prefix: string,
schema: JSONSchema,
formState: JsonObject,
): [string, JsonValue | undefined][] {
return Object.entries(obj).flatMap(([key, value]) => {
const prefixedKey = prefix ? `${prefix}/${key}` : key;
const definitionInSchema = schema.getSchema({
pointer: `#/${prefixedKey}`,
data: formState,
});
if (definitionInSchema) {
const backstageReviewOptions = definitionInSchema['ui:backstage']?.review;
// Recurse into nested objects
if (backstageReviewOptions?.explode && isJsonObject(value)) {
return flattenObject(value, prefixedKey, schema, formState);
}
}
return [[key, value]];
});
}
export function isJsonObject(value?: JsonValue): value is JsonObject {
return typeof value === 'object' && value !== null && !Array.isArray(value);
}