Skip to content

Commit

Permalink
improve(cli): helper function for extracting schema options
Browse files Browse the repository at this point in the history
  • Loading branch information
LAST7 authored and Red-Asuka committed Aug 23, 2024
1 parent d28945b commit 1665514
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 27 deletions.
16 changes: 2 additions & 14 deletions cli/src/lib/sub.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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[] = []

Expand Down
36 changes: 23 additions & 13 deletions cli/src/utils/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.')
Expand Down Expand Up @@ -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 = {
Expand Down Expand Up @@ -446,6 +455,7 @@ export {
parseFileSave,
parseFileWrite,
parseFormat,
parseSchemaOptions,
parseOutputMode,
parseConnectOptions,
parsePublishOptions,
Expand Down

0 comments on commit 1665514

Please sign in to comment.