Skip to content

Commit 9e2895b

Browse files
ysfscreamKinplemelon
authored andcommitted
feat(listener): support reset default value on custom configs
1 parent b94ba11 commit 9e2895b

File tree

2 files changed

+38
-5
lines changed

2 files changed

+38
-5
lines changed

src/hooks/Config/useListenerDrawer.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { addGatewayListener, updateGatewayListener } from '@/api/gateway'
22
import { addListener, queryListenerDetail, updateListener } from '@/api/listener'
3-
import { GATEWAY_DISABLED_LISTENER_TYPE_MAP } from '@/common/constants'
3+
import { GATEWAY_DISABLED_LISTENER_TYPE_MAP, unexposedConfigs } from '@/common/constants'
44
import { checkNOmitFromObj, jumpToErrorFormItem } from '@/common/tools'
55
import { FormRules } from '@/types/common'
66
import { GatewayName, ListenerType, ListenerTypeForGateway } from '@/types/enum'
@@ -99,6 +99,7 @@ export default (props: Props, emit: Emit): useListenerDrawerReturns => {
9999
transPort,
100100
extractDifferences,
101101
hoconToObject,
102+
resetCustomConfig,
102103
} = useListenerUtils(props.gatewayName)
103104

104105
const listenerTypeOptList = computed(() => {
@@ -206,8 +207,10 @@ export default (props: Props, emit: Emit): useListenerDrawerReturns => {
206207
delete data.ssl_options.ocsp
207208
}
208209
// Handle the custom configs
209-
if (!isEmptyObj(listenerCustomConfigs.value)) {
210-
merge(data, listenerCustomConfigs.value)
210+
if (!props.gatewayName && listenerRecord.value.type !== 'quic') {
211+
const type = listenerRecord.value.type as 'tcp' | 'ssl' | 'ws' | 'wss'
212+
const customConfigs = resetCustomConfig(listenerCustomConfigs.value, unexposedConfigs[type])
213+
merge(data, customConfigs)
211214
}
212215
return data
213216
}

src/hooks/Config/useListenerUtils.ts

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export interface ListenerUtils {
4141
extractDifferences: (type: keyof typeof unexposedConfigs, data: any) => Record<string, any>
4242
objectToHocon(obj: Record<string, any>): string
4343
hoconToObject: (hoconData: string) => Record<string, any>
44+
resetCustomConfig: (data: Listener, defaultConfig: Listener) => Listener
4445
}
4546

4647
export default (gatewayName?: string | undefined): ListenerUtils => {
@@ -416,15 +417,43 @@ export default (gatewayName?: string | undefined): ListenerUtils => {
416417

417418
const hoconToObject = (hoconData: string): Record<string, any> => {
418419
try {
419-
console.log(hoconData)
420420
const parsedData = parseHoconToObject(hoconData)
421-
console.log(parsedData)
422421
return Promise.resolve(parsedData)
423422
} catch (error) {
424423
return Promise.reject(error)
425424
}
426425
}
427426

427+
/**
428+
* Resets the custom configuration by merging it with the default configuration.
429+
* If a property in the custom configuration is undefined, null, empty string, or an empty object,
430+
* it will be replaced with the corresponding property from the default configuration.
431+
* If a property in the custom configuration is an object and the corresponding property in the default configuration is also an object,
432+
* the function will recursively reset the nested properties.
433+
*
434+
* @param customData - The custom configuration to be reset.
435+
* @param defaultCustomConfig - The default configuration to merge with the custom configuration.
436+
* @returns The custom configuration after resetting.
437+
*/
438+
const resetCustomConfig = (customData: Listener, defaultCustomConfig: Listener): Listener => {
439+
for (const key in defaultCustomConfig) {
440+
if (
441+
customData[key] === undefined ||
442+
customData[key] === null ||
443+
customData[key] === '' ||
444+
(typeof customData[key] === 'object' && isEmptyObj(customData[key]))
445+
) {
446+
customData[key] = defaultCustomConfig[key]
447+
} else if (
448+
typeof customData[key] === 'object' &&
449+
typeof defaultCustomConfig[key] === 'object'
450+
) {
451+
customData[key] = resetCustomConfig(customData[key], defaultCustomConfig[key])
452+
}
453+
}
454+
return customData
455+
}
456+
428457
return {
429458
completeGatewayListenerTypeList,
430459
listenerTypeList,
@@ -451,5 +480,6 @@ export default (gatewayName?: string | undefined): ListenerUtils => {
451480
extractDifferences,
452481
objectToHocon,
453482
hoconToObject,
483+
resetCustomConfig,
454484
}
455485
}

0 commit comments

Comments
 (0)