枚举自动生成工具

因为最近一项目需要建特别多的枚举,特别是证件类型,身份证、台湾居民来往大陆通行证、回乡证等很多类型,英语又不好,每一个还要去翻译就很花费时间,百度了一下,找到一个但是还要弄成markdown格式排列好,就不满足自己的要求。就写了一个简易版的枚举生成工具

实现步骤

  1. 调用百度翻译API(需要先开通,才能有appId), 这个网上很多教程。但其实很多都是拷贝的官方文档的样例。所以最好的就是直接看官方文档就可以
  2. 将需要翻译的文本进行翻译之后在组装成想要的模板样式就可以,目前支持4种样式,枚举基本样式都差不多,就是需要的参数和code的类型有一些区别

代码

package com.example.enumtool.controller;

import com.example.enumtool.domain.bo.EnumTemplateBo;
import com.example.enumtool.domain.vo.EnumGenerateVo;
import com.example.enumtool.util.GenerateTemplate;
import com.example.enumtool.util.TranslateUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xmy
 * @version 1.0.0
 * @description 枚举生成
 * @createTime 2022年04月14日 11:06:00
 */
@RestController
public class GenerateTempController {

    /**
     * 获取模板样式
     *
     * @author xmy
     * @date 2022/4/14 11:23
     * @return
     **/
    @GetMapping("template")
    public Map<Integer, EnumTemplateBo> enumTemplate() {
        Map<Integer, EnumTemplateBo> templateBoMap = new HashMap<>();
        templateBoMap.put(1, GenerateTemplate.firstTemplate());
        templateBoMap.put(2, GenerateTemplate.secondTemplate());
        templateBoMap.put(3, GenerateTemplate.thirdTemplate());
        templateBoMap.put(4, GenerateTemplate.fourthTemplate());

        for (Map.Entry<Integer, EnumTemplateBo> entry : templateBoMap.entrySet()) {
            System.out.println(entry.getKey() + " " +entry.getValue());
        }

        return templateBoMap;
    }

    /**
     * 生成枚举模板
     *
     * @author xmy
     * @date 2022/4/14 11:11
     * @param vo:
     * @return
     **/
    @PostMapping("generate")
    public List<Map<String, Object>> generateTemple(@Valid @RequestBody EnumGenerateVo vo) {
        return TranslateUtil.generate(vo.getTemplate(), vo.getText());
    }

}

测试

使用postman获取模板列表

 选用一个模板,传入模板code和需要转换的文本

 

复制控制台生成的文本就可以了

可优化点

目前只是一个简易版,代码也用的是很基础的实现方式,还有很多地方可以优化,

1. 支持选择用哪一个三方翻译

2. 做成更方便使用的工具,例如idea插件

 

 完整代码地址:enum-tool: 转换枚举工具 (gitee.com)

代码写的很菜,虚心接受指导

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值