Skip to content

Commit

Permalink
feat(debugapi): enum support
Browse files Browse the repository at this point in the history
  • Loading branch information
shhdgit committed May 26, 2021
1 parent fed1d4f commit e0896f5
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 2 deletions.
1 change: 1 addition & 0 deletions pkg/apiserver/debugapi/endpoint/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ type APIParam struct {

type APIParamModel struct {
Type string `json:"type"`
Data interface{} `json:"data"`
Transformer ModelTransformer `json:"-"`
}

Expand Down
13 changes: 11 additions & 2 deletions pkg/apiserver/debugapi/endpoint/endpoint_def.go
Original file line number Diff line number Diff line change
Expand Up @@ -507,8 +507,17 @@ var pdRegionCheck APIModel = APIModel{
Method: MethodGet,
PathParams: []APIParam{
{
Name: "state",
Model: APIParamModelText,
Name: "state",
Model: CreateAPIParamModelEnum([]EnumItem{
{Name: "miss-peer"},
{Name: "extra-peer"},
{Name: "down-peer"},
{Name: "pending-peer"},
{Name: "offline-peer"},
{Name: "empty-peer"},
{Name: "hist-peer"},
{Name: "hist-keys"},
}),
},
},
QueryParams: []APIParam{
Expand Down
18 changes: 18 additions & 0 deletions pkg/apiserver/debugapi/endpoint/param_models.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,24 @@ var APIParamModelInt APIParamModel = APIParamModel{
},
}

type EnumItem struct {
Name string `json:"name"`
Value string `json:"value"`
}

func CreateAPIParamModelEnum(items []EnumItem) APIParamModel {
items = funk.Map(items, func(item EnumItem) EnumItem {
if item.Value == "" {
item.Value = item.Name
}
return item
}).([]EnumItem)
return APIParamModel{
Type: "enum",
Data: items,
}
}

var APIParamModelDB APIParamModel = APIParamModel{
Type: "db",
}
Expand Down
20 changes: 20 additions & 0 deletions ui/lib/apps/DebugAPI/apilist/widgets/Enum.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import React from 'react'
import { Select } from 'antd'
import { useTranslation } from 'react-i18next'

import type { ApiFormWidget } from './index'

export const EnumWidget: ApiFormWidget = ({ param }) => {
const { t } = useTranslation()
return (
<Select placeholder={t(`debug_api.widgets.enum`, { param: param.name })}>
{(param.model?.data as { name: string; value: string }[]).map(
(option) => (
<Select.Option key={option.value} value={option.value}>
{option.name}
</Select.Option>
)
)}
</Select>
)
}
2 changes: 2 additions & 0 deletions ui/lib/apps/DebugAPI/apilist/widgets/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type { Topology } from '../ApiForm'
import { TextWidget } from './Text'
import { TagsWidget } from './Tags'
import { IntWidget } from './Int'
import { EnumWidget } from './Enum'
import { HostSelectWidget } from './Host'
import { DatabaseWidget } from './Database'
import { TableWidget } from './Table'
Expand Down Expand Up @@ -39,6 +40,7 @@ export const paramModelWidgets: Widgets = {
text: TextWidget,
tags: createJSXElementWrapper(TagsWidget),
int: IntWidget,
enum: EnumWidget,
db: createJSXElementWrapper(DatabaseWidget),
table: createJSXElementWrapper(TableWidget),
table_id: createJSXElementWrapper(TableIDWidget),
Expand Down
1 change: 1 addition & 0 deletions ui/lib/apps/DebugAPI/translations/en.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ debug_api:
text: Please enter the {{param}}
tags: Please enter the {{param}}
int: Please enter the {{param}}
enum: Please select the {{param}}
db: Please select the database or enter the full database name
table: Please select the table or enter the full table name
table_id: Please select the table ID or enter the full table ID
Expand Down
1 change: 1 addition & 0 deletions ui/lib/apps/DebugAPI/translations/zh.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ debug_api:
text: 请输入 {{param}}
tags: 请输入 {{param}}
int: 请输入 {{param}}
enum: 请选择 {{param}}
db: 请从列表中选择 database 或输入完整的 database 名称
table: 请从列表中选择 table 或输入完整的 table 名称
table_id: 请从列表中选择 table ID 或输入完整的 table ID
Expand Down

0 comments on commit e0896f5

Please sign in to comment.