Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cli): allow multiple occurrences of the same name in user properties #1157

Merged
merged 2 commits into from
Nov 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions cli/src/lib/sub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,19 @@ const sub = (options: SubscribeOptions) => {

packet.retain && msgData.push({ label: 'retain', value: packet.retain })

packet.properties?.userProperties &&
msgData.push({ label: 'user properties', value: { ...packet.properties.userProperties } })
if (packet.properties?.userProperties) {
const up: { key: string; value: string }[] = []
Object.entries(packet.properties.userProperties).forEach(([key, value]) => {
if (typeof value === 'string') {
up.push({ key, value })
} else {
value.forEach((v) => {
up.push({ key, value: v })
})
}
})
msgData.push({ label: 'userProperties', value: up })
}

msgLog(msgData)
})
Expand Down
8 changes: 4 additions & 4 deletions cli/src/types/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ declare global {
topicAliasMaximum?: number
reqResponseInfo?: boolean
reqProblemInfo?: boolean
userProperties?: Record<string, string>
userProperties?: Record<string, string | string[]>
// will message
willTopic?: string
willMessage?: string
Expand All @@ -45,7 +45,7 @@ declare global {
willContentType?: string
willResponseTopic?: string
willCorrelationData?: string
willUserProperties?: Record<string, string>
willUserProperties?: Record<string, string | string[]>
save?: boolean | string
config?: boolean | string
}
Expand All @@ -66,7 +66,7 @@ declare global {
correlationData?: string
subscriptionIdentifier?: number
contentType?: string
connUserProperties?: Record<string, string>
connUserProperties?: Record<string, string | string[]>
}

interface SubscribeOptions extends ConnectOptions {
Expand All @@ -79,7 +79,7 @@ declare global {
subscriptionIdentifier?: number[]
format?: FormatType
verbose: boolean
connUserProperties?: Record<string, string>
connUserProperties?: Record<string, string | string[]>
}

interface BenchConnectOptions extends ConnectOptions {
Expand Down
17 changes: 15 additions & 2 deletions cli/src/utils/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,23 @@ const parseMQTTVersion = (value: string) => {
return dict[value as '3.1' | '3.1.1' | '5']
}

const parseUserProperties = (value: string, previous: Record<string, unknown> | undefined) => {
const parseUserProperties = (value: string, previous?: Record<string, string | string[]>) => {
const [key, val] = value.split(': ')
if (key && val) {
return previous ? { ...previous, [key]: val } : { [key]: val }
if (!previous) {
return { [key]: val }
} else {
Comment on lines +41 to +42
Copy link
Member

@ysfscream ysfscream Nov 30, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

else is needless after the return statement.

if (!previous) return { [key]: value }
if (xxx) {
    xxxx
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know that else is unnecessary, but I think that in some cases else can make the code clearer. This is the code without else, what do you think?

    if (!previous) return { [key]: val }
    if (previous[key]) {
      if (Array.isArray(previous[key])) {
        ;(previous[key] as string[]).push(val)
      } else {
        previous[key] = [previous[key] as string, val]
      }
      return previous
    } else {
      return { ...previous, [key]: val }
    }

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The final code:

    if (!previous) return { [key]: val }
    if (previous[key]) {
      if (Array.isArray(previous[key])) {
        const currentVal = previous[key] as string[]
        currentVal.push(val)
        return { ...previous, [key]: currentVal }
      }
      previous[key] = [previous[key] as string, val]
      return previous
    }
    return { ...previous, [key]: val }

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or

    if (!previous) return { [key]: val }
    if (!previous[key]) return { ...previous, [key]: val }
    if (Array.isArray(previous[key])) {
      const currentVal = previous[key] as string[]
      currentVal.push(val)
      return { ...previous, [key]: currentVal }
    }
    previous[key] = [previous[key] as string, val]
    return previous

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

else needs continuity, and return means execution is over. It doesn't say here that else doesn't need to be used, I think this code is just fine

if (!previous) return { [key]: val }
if (previous[key]) {
  if (Array.isArray(previous[key])) {
	const currentVal = previous[key] as string[]
     currentVal.push(val)
  } else {
  	const currentVal = previous[key] as string
    previous[key] = [currentVal, val]
  }
  return previous
}
 return { ...previous, [key]: val }

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Anyway, I will merge first. But the principle of my judgment is that if there is a return in the if, there will be no need for else; if not, then there is a need for else; this proposal should be in the rules of Eslint also had, personal preference, code readability is our goal.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a nice job.

if (previous[key]) {
if (Array.isArray(previous[key])) {
;(previous[key] as string[]).push(val)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please declare an internal variable to represent previous[key], For example:

const objValues = previous[key] as string[]

Looks cumbersome, but more clear type

} else {
previous[key] = [previous[key] as string, val]
}
return previous
} else {
return { ...previous, [key]: val }
}
}
} else {
signale.error('Not a valid user properties.')
process.exit(1)
Expand Down