From 166551418593c863da7123b9a8537c03ce8d222d Mon Sep 17 00:00:00 2001 From: Last Date: Fri, 23 Aug 2024 13:37:15 +0800 Subject: [PATCH] improve(cli): helper function for extracting schema options --- cli/src/lib/sub.ts | 16 ++-------------- cli/src/utils/parse.ts | 36 +++++++++++++++++++++++------------- 2 files changed, 25 insertions(+), 27 deletions(-) diff --git a/cli/src/lib/sub.ts b/cli/src/lib/sub.ts index 4ca35b2e0..09b5a2e9d 100644 --- a/cli/src/lib/sub.ts +++ b/cli/src/lib/sub.ts @@ -1,6 +1,6 @@ import * as mqtt from 'mqtt' import logWrapper, { Signale, msgLog, basicLog, benchLog, singaleConfig, signale } from '../utils/logWrapper' -import { parseConnectOptions, parseSubscribeOptions, checkTopicExists } from '../utils/parse' +import { parseConnectOptions, parseSubscribeOptions, parseSchemaOptions, checkTopicExists } from '../utils/parse' import delay from '../utils/delay' import convertPayload from '../utils/convertPayload' import { handleSaveOptions, handleLoadOptions } from '../utils/options' @@ -159,19 +159,7 @@ const sub = (options: SubscribeOptions) => { client.on('message', (topic, payload, packet) => { const { format, protobufPath, protobufMessageName, avscPath, fileSave, fileWrite, delimiter } = options - let schemaOptions: SchemaOptions | undefined - if (protobufPath && protobufMessageName) { - schemaOptions = { - type: 'protobuf', - protobufPath, - protobufMessageName, - } - } else if (avscPath) { - schemaOptions = { - type: 'avro', - avscPath, - } - } + const schemaOptions: SchemaOptions | undefined = parseSchemaOptions(protobufPath, protobufMessageName, avscPath) const msgData: MsgItem[] = [] diff --git a/cli/src/utils/parse.ts b/cli/src/utils/parse.ts index 9f202cba3..f37fefd9d 100644 --- a/cli/src/utils/parse.ts +++ b/cli/src/utils/parse.ts @@ -141,6 +141,27 @@ const parseFormat = (value: string) => { return value } +const parseSchemaOptions = ( + protobufPath?: string, + protobufMessageName?: string, + avscPath?: string, +): SchemaOptions | undefined => { + if (protobufPath && protobufMessageName) { + return { + type: 'protobuf', + protobufPath, + protobufMessageName, + } + } else if (avscPath) { + return { + type: 'avro', + avscPath, + } + } else { + return undefined + } +} + const parseOutputMode = (value: string) => { if (!['clean', 'default'].includes(value)) { logWrapper.fail('Not a valid output mode.') @@ -359,19 +380,7 @@ const parsePublishOptions = (options: PublishOptions) => { dup, } - let schemaOptions: SchemaOptions | undefined - if (protobufPath && protobufMessageName) { - schemaOptions = { - type: 'protobuf', - protobufPath, - protobufMessageName, - } - } else if (avscPath) { - schemaOptions = { - type: 'avro', - avscPath, - } - } + const schemaOptions: SchemaOptions | undefined = parseSchemaOptions(protobufPath, protobufMessageName, avscPath) if (options.mqttVersion === 5) { const properties = { @@ -446,6 +455,7 @@ export { parseFileSave, parseFileWrite, parseFormat, + parseSchemaOptions, parseOutputMode, parseConnectOptions, parsePublishOptions,