SBIN2010 commented on code in PR #44907: URL: https://github.com/apache/superset/pull/44907#discussion_r4172455698
########## superset-frontend/plugins/plugin-chart-ag-grid-table/src/renderers/parseJsonCellValue.ts: ########## @@ -0,0 +1,175 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +export type JsonContainer = Record<string, unknown> | unknown[]; + +// Visible cells are parsed on the render path. Above this size the cell stays +// plain text. Parsed results are reused for the same string. +const MAX_JSON_CELL_LENGTH = 100_000; +const PARSE_CACHE_LIMIT = 200; + +const parsedJsonCache = new Map<string, JsonContainer | null>(); + +function isJsonContainer(value: unknown): value is JsonContainer { + if (value === null || typeof value !== 'object') { + return false; + } + if (Array.isArray(value)) { + return true; + } + if (value instanceof Date) { + return false; + } + const prototype = Object.getPrototypeOf(value); + return prototype === Object.prototype || prototype === null; +} + +function isPlainJsonObject(value: unknown): value is Record<string, unknown> { + return isJsonContainer(value) && !Array.isArray(value); +} + +function rememberParse( + text: string, + parsed: JsonContainer | null, +): JsonContainer | null { + if (parsedJsonCache.size >= PARSE_CACHE_LIMIT) { + const oldest = parsedJsonCache.keys().next().value; + if (oldest !== undefined) { + parsedJsonCache.delete(oldest); + } + } + parsedJsonCache.set(text, parsed); + return parsed; +} + +/** + * Accept a JSON object or array, either already parsed or as text. + * Scalars, invalid JSON, and non-JSON text return null so the cell stays + * on the plain-text renderer. + */ +export function parseJsonCellValue(value: unknown): JsonContainer | null { + if (isJsonContainer(value)) { + return value; + } + if (typeof value !== 'string') { + return null; + } + const trimmed = value.trim(); + if ( + trimmed.length === 0 || + trimmed.length > MAX_JSON_CELL_LENGTH || Review Comment: fix in [d926922](https://github.com/apache/superset/pull/44907/commits/d926922770b5273aff323f9171c3415ac7334550) -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
