Skip to content

Commit

Permalink
chore:Adjust code position to optimize program running efficiency.
Browse files Browse the repository at this point in the history
  • Loading branch information
taolx0 committed Oct 11, 2024
1 parent 9f041a8 commit e6c1e4e
Showing 1 changed file with 58 additions and 58 deletions.
116 changes: 58 additions & 58 deletions sqle/api/controller/v1/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -1500,74 +1500,74 @@ func ExportRuleTemplateFile(c echo.Context) error {
}

func exportTemplateFile(c echo.Context, exportType ExportType, templateFile interface{}, templateName string) error {
var name, desc, dbType string
var columnNameList []string
var columnContentList [][]string

ctx := c.Request().Context()
defaultColumnNameList := []string{
locale.Bundle.LocalizeMsgByCtx(ctx, locale.RuleTemplateRuleName),
locale.Bundle.LocalizeMsgByCtx(ctx, locale.RuleTemplateRuleDesc),
locale.Bundle.LocalizeMsgByCtx(ctx, locale.RuleTemplateRuleAnnotation),
locale.Bundle.LocalizeMsgByCtx(ctx, locale.RuleTemplateRuleLevel),
locale.Bundle.LocalizeMsgByCtx(ctx, locale.RuleTemplateRuleCategory),
locale.Bundle.LocalizeMsgByCtx(ctx, locale.RuleTemplateInstType),
locale.Bundle.LocalizeMsgByCtx(ctx, locale.RuleTemplateRuleParam),
}

convertToContentList := func(ruleTemplateInfo RuleTemplateRuleInfo) ([]string, error) {
paramsBytes, err := json.Marshal(ruleTemplateInfo.Params)
if err != nil {
return nil, err
switch exportType {
case CsvExportType:
var name, desc, dbType string
var columnNameList []string
var columnContentList [][]string

ctx := c.Request().Context()
defaultColumnNameList := []string{
locale.Bundle.LocalizeMsgByCtx(ctx, locale.RuleTemplateRuleName),
locale.Bundle.LocalizeMsgByCtx(ctx, locale.RuleTemplateRuleDesc),
locale.Bundle.LocalizeMsgByCtx(ctx, locale.RuleTemplateRuleAnnotation),
locale.Bundle.LocalizeMsgByCtx(ctx, locale.RuleTemplateRuleLevel),
locale.Bundle.LocalizeMsgByCtx(ctx, locale.RuleTemplateRuleCategory),
locale.Bundle.LocalizeMsgByCtx(ctx, locale.RuleTemplateInstType),
locale.Bundle.LocalizeMsgByCtx(ctx, locale.RuleTemplateRuleParam),
}

return []string{
ruleTemplateInfo.Name,
ruleTemplateInfo.Desc,
ruleTemplateInfo.Annotation,
ruleTemplateInfo.Level,
ruleTemplateInfo.Typ,
ruleTemplateInfo.DBType,
string(paramsBytes),
}, nil
}

if ruleTemplateExport, ok := templateFile.(*RuleTemplateExport); ok {
name = ruleTemplateExport.Name
desc = ruleTemplateExport.Desc
dbType = ruleTemplateExport.DBType
columnNameList = defaultColumnNameList
for _, res := range ruleTemplateExport.RuleList {
contentList, err := convertToContentList(res.RuleTemplateRuleInfo)
convertToContentList := func(ruleTemplateInfo RuleTemplateRuleInfo) ([]string, error) {
paramsBytes, err := json.Marshal(ruleTemplateInfo.Params)
if err != nil {
return controller.JSONBaseErrorReq(c, err)
return nil, err
}

columnContentList = append(columnContentList, contentList)
return []string{
ruleTemplateInfo.Name,
ruleTemplateInfo.Desc,
ruleTemplateInfo.Annotation,
ruleTemplateInfo.Level,
ruleTemplateInfo.Typ,
ruleTemplateInfo.DBType,
string(paramsBytes),
}, nil
}
} else if ruleTemplateExportErr, ok := templateFile.(*RuleTemplateExportErr); ok {
name = ruleTemplateExportErr.Name
desc = ruleTemplateExportErr.Desc
dbType = ruleTemplateExportErr.DBType
columnNameList = append(defaultColumnNameList, locale.Bundle.LocalizeMsgByCtx(ctx, locale.RuleTemplateRuleErr))

for _, res := range ruleTemplateExportErr.RuleList {
contentList, err := convertToContentList(res.RuleTemplateRuleInfo)
if err != nil {
return controller.JSONBaseErrorReq(c, err)

if ruleTemplateExport, ok := templateFile.(*RuleTemplateExport); ok {
name = ruleTemplateExport.Name
desc = ruleTemplateExport.Desc
dbType = ruleTemplateExport.DBType
columnNameList = defaultColumnNameList
for _, res := range ruleTemplateExport.RuleList {
contentList, err := convertToContentList(res.RuleTemplateRuleInfo)
if err != nil {
return controller.JSONBaseErrorReq(c, err)
}

columnContentList = append(columnContentList, contentList)
}
} else if ruleTemplateExportErr, ok := templateFile.(*RuleTemplateExportErr); ok {
name = ruleTemplateExportErr.Name
desc = ruleTemplateExportErr.Desc
dbType = ruleTemplateExportErr.DBType
columnNameList = append(defaultColumnNameList, locale.Bundle.LocalizeMsgByCtx(ctx, locale.RuleTemplateRuleErr))

for _, res := range ruleTemplateExportErr.RuleList {
contentList, err := convertToContentList(res.RuleTemplateRuleInfo)
if err != nil {
return controller.JSONBaseErrorReq(c, err)
}

columnContentList = append(columnContentList,
contentList,
[]string{res.RuleErr},
)
columnContentList = append(columnContentList,
contentList,
[]string{res.RuleErr},
)
}
} else {
return controller.JSONBaseErrorReq(c, errors.New(errors.DataInvalid, fmt.Errorf("template file is invalid")))
}
} else {
return controller.JSONBaseErrorReq(c, errors.New(errors.DataInvalid, fmt.Errorf("template file is invalid")))
}

switch exportType {
case CsvExportType:
buf := new(bytes.Buffer)
buf.WriteString("\xEF\xBB\xBF") // 写入UTF-8 BOM

Expand Down

0 comments on commit e6c1e4e

Please sign in to comment.