Skip to content

Commit

Permalink
# Enum Loglevel
Browse files Browse the repository at this point in the history
Signed-off-by: Theo Truong <[email protected]>
  • Loading branch information
nhtruong committed May 16, 2024
1 parent 9f68afe commit ca47db4
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 33 deletions.
30 changes: 30 additions & 0 deletions tools/src/Logger.ts
Original file line number Diff line number Diff line change
@@ -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}`)
}
}
7 changes: 4 additions & 3 deletions tools/src/linter/SchemasValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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<string, any>
this.spec = new OpenApiMerger(this.root_folder, LogLevel.error).merge().components as Record<string, any>
const named_schemas_errors = this.validate_named_schemas()
if (named_schemas_errors.length > 0) return named_schemas_errors
return [
Expand Down
25 changes: 0 additions & 25 deletions tools/src/logger.ts

This file was deleted.

4 changes: 2 additions & 2 deletions tools/src/merger/OpenApiMerger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -15,7 +15,7 @@ export default class OpenApiMerger {
paths: Record<string, Record<string, OpenAPIV3.PathItemObject>> = {} // namespace -> path -> path_item_object
schemas: Record<string, Record<string, OpenAPIV3.SchemaObject>> = {} // 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 = {
Expand Down
4 changes: 2 additions & 2 deletions tools/src/merger/SupersededOpsGenerator.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down
3 changes: 2 additions & 1 deletion tools/tests/merger/OpenApiMerger.test.ts
Original file line number Diff line number Diff line change
@@ -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'))
Expand Down

0 comments on commit ca47db4

Please sign in to comment.