From ca47db4200d6ee4428e0271b4da655fdd8c44a58 Mon Sep 17 00:00:00 2001 From: Theo Truong Date: Thu, 16 May 2024 11:28:34 -0600 Subject: [PATCH] # Enum Loglevel Signed-off-by: Theo Truong --- tools/src/Logger.ts | 30 ++++++++++++++++++++++ tools/src/linter/SchemasValidator.ts | 7 ++--- tools/src/logger.ts | 25 ------------------ tools/src/merger/OpenApiMerger.ts | 4 +-- tools/src/merger/SupersededOpsGenerator.ts | 4 +-- tools/tests/merger/OpenApiMerger.test.ts | 3 ++- 6 files changed, 40 insertions(+), 33 deletions(-) create mode 100644 tools/src/Logger.ts delete mode 100644 tools/src/logger.ts diff --git a/tools/src/Logger.ts b/tools/src/Logger.ts new file mode 100644 index 000000000..07d2d9a9a --- /dev/null +++ b/tools/src/Logger.ts @@ -0,0 +1,30 @@ +export enum LogLevel { + info = 1, + warn = 2, + error = 3 +} + +export class Logger { + level: LogLevel + + constructor (level: LogLevel) { + this.level = level + } + + info (message: string): void { + this.#log(LogLevel.info, message) + } + + warn (message: string): void { + this.#log(LogLevel.warn, message) + } + + error (message: string): void { + this.#log(LogLevel.error, message) + } + + #log (level: LogLevel, message: string): void { + if (level < this.level) return + console.log(`[${level}] ${message}`) + } +} diff --git a/tools/src/linter/SchemasValidator.ts b/tools/src/linter/SchemasValidator.ts index eb29a0f41..21ca3630a 100644 --- a/tools/src/linter/SchemasValidator.ts +++ b/tools/src/linter/SchemasValidator.ts @@ -2,6 +2,7 @@ import AJV from 'ajv' import addFormats from 'ajv-formats' import OpenApiMerger from '../merger/OpenApiMerger' import { type ValidationError } from '../types' +import { LogLevel } from '../Logger' const IGNORED_ERROR_PREFIXES = [ 'can\'t resolve reference', // errors in referenced schemas will also cause reference errors @@ -12,7 +13,7 @@ const ADDITIONAL_KEYWORDS = [ 'x-version-added', 'x-version-deprecated', 'x-version-removed', - 'x-deprecation-message', + 'x-deprecation-message' ] export default class SchemasValidator { @@ -24,11 +25,11 @@ export default class SchemasValidator { this.root_folder = root_folder this.ajv = new AJV({ strict: true, discriminator: true }) addFormats(this.ajv) - for(const keyword of ADDITIONAL_KEYWORDS) this.ajv.addKeyword(keyword) + for (const keyword of ADDITIONAL_KEYWORDS) this.ajv.addKeyword(keyword) } validate (): ValidationError[] { - this.spec = new OpenApiMerger(this.root_folder, 'error').merge().components as Record + this.spec = new OpenApiMerger(this.root_folder, LogLevel.error).merge().components as Record const named_schemas_errors = this.validate_named_schemas() if (named_schemas_errors.length > 0) return named_schemas_errors return [ diff --git a/tools/src/logger.ts b/tools/src/logger.ts deleted file mode 100644 index de0f7d457..000000000 --- a/tools/src/logger.ts +++ /dev/null @@ -1,25 +0,0 @@ -export type LogLevel = 'info' | 'warn' | 'error' - -export class Logger { - level: string - - constructor (level: LogLevel) { - this.level = level - } - - log (level: LogLevel, message: string): void { - console.log(`[${level}] ${message}`) - } - - info (message: string): void { - if (this.level === 'info') this.log('info', message) - } - - warn (message: string): void { - if (this.level === 'info' || this.level === 'warn') this.log('warn', message) - } - - error (message: string): void { - this.log('error', message) - } -} diff --git a/tools/src/merger/OpenApiMerger.ts b/tools/src/merger/OpenApiMerger.ts index 68050dc37..a541293d7 100644 --- a/tools/src/merger/OpenApiMerger.ts +++ b/tools/src/merger/OpenApiMerger.ts @@ -4,7 +4,7 @@ import _ from 'lodash' import { read_yaml, write_yaml } from '../../helpers' import SupersededOpsGenerator from './SupersededOpsGenerator' import GlobalParamsGenerator from './GlobalParamsGenerator' -import { type LogLevel, Logger } from '../logger' +import { Logger, LogLevel } from '../Logger' // Create a single-file OpenAPI spec from multiple files for OpenAPI validation and programmatic consumption export default class OpenApiMerger { @@ -15,7 +15,7 @@ export default class OpenApiMerger { paths: Record> = {} // namespace -> path -> path_item_object schemas: Record> = {} // category -> schema -> schema_object - constructor (root_folder: string, log_level: LogLevel = 'warn') { + constructor (root_folder: string, log_level: LogLevel = LogLevel.warn) { this.logger = new Logger(log_level) this.root_folder = fs.realpathSync(root_folder) this.spec = { diff --git a/tools/src/merger/SupersededOpsGenerator.ts b/tools/src/merger/SupersededOpsGenerator.ts index c467e639f..c8bffd3c1 100644 --- a/tools/src/merger/SupersededOpsGenerator.ts +++ b/tools/src/merger/SupersededOpsGenerator.ts @@ -1,14 +1,14 @@ import { type OperationSpec, type SupersededOperationMap } from 'types' import _ from 'lodash' import { read_yaml } from '../../helpers' -import { Logger } from '../logger' +import { Logger, LogLevel } from '../Logger' export default class SupersededOpsGenerator { logger: Logger superseded_ops: SupersededOperationMap constructor (root_path: string, logger: Logger | undefined) { - this.logger = logger ?? new Logger('warn') + this.logger = logger ?? new Logger(LogLevel.warn) const file_path = root_path + '/_superseded_operations.yaml' this.superseded_ops = read_yaml(file_path) as SupersededOperationMap delete this.superseded_ops.$schema diff --git a/tools/tests/merger/OpenApiMerger.test.ts b/tools/tests/merger/OpenApiMerger.test.ts index b84c68fff..33146327f 100644 --- a/tools/tests/merger/OpenApiMerger.test.ts +++ b/tools/tests/merger/OpenApiMerger.test.ts @@ -1,8 +1,9 @@ import OpenApiMerger from 'merger/OpenApiMerger' import fs from 'fs' +import { LogLevel } from '../../src/Logger' test('merge()', async () => { - const merger = new OpenApiMerger('./tools/tests/merger/fixtures/spec/', 'error') + const merger = new OpenApiMerger('./tools/tests/merger/fixtures/spec/', LogLevel.error) merger.merge('./tools/tests/merger/opensearch-openapi.yaml') expect(fs.readFileSync('./tools/tests/merger/fixtures/expected.yaml', 'utf8')) .toEqual(fs.readFileSync('./tools/tests/merger/opensearch-openapi.yaml', 'utf8'))