知源笔记

TThrift工具类

牧尘 · 2021 年 4 月 8 日
@Slf4j
public class TThriftUtils {
    private static Pattern ENUM_ITEM_PATTERN = Pattern.compile("^(\\w+)\\s*=*\\s*(-?\\d+)");
    private static Pattern ENUM_NAME_PATTERN = Pattern.compile("^(enum)\\s+(\\w+)\\s*(\\{)");
    private static Pattern STRUCT_NAME_PATTERN = Pattern.compile("^(struct)\\s+(\\w+)\\s*(\\{)");
    private static Pattern PARAM_PATTERN = Pattern.compile("^(\\d+):\\s+(required|optional)*\\s*([\\w.<>,\\s]+)\\s+(\\w+)");
    private static Pattern SERVICE_NAME_PATTERN = Pattern.compile("^(service)\\s+(\\w+)\\s*([\\w.]+\\s*)*(\\{)");
    private static Pattern METHOD_NAME_PATTERN = Pattern.compile("^([\\w.<>]+)\\s+(\\w+)\\s*");
    private static Pattern INCLUDE_PATTERN = Pattern.compile("^include\\s+('|\")([\\w./]+).thrift('|\")");
    private static Pattern LIST_PATTERN = Pattern.compile("^list<([\\w.]+)>");
    private static Pattern MAP_PATTERN = Pattern.compile("^map<([\\w.]+)\\s*,\\s*([\\w.]+)>");

    private static Set<String> BASE_TYPE = Sets.newHashSet("i64", "bool", "i32", "string");

    public static void main(String[] args) throws Exception {
//        String jarUrl = "/Users/zhaohuatao/Develop/code/sns-idls/tenet-note-service/service.thrift";

        String jarUrl = "http://mvn.devops.xiaohongshu.com/repository/maven-snapshots/com/xiaohongshu/tenet/note-sdk/master-SNAPSHOT/note-sdk-master-20210330.030109-21.jar";
        List<TMethodModel> tMethodModels = analyzeService(jarUrl, "service.thrift");
        log.info("success");
    }

    public static List<TMethodModel> analyzeService(String jarUrl, String thriftName) {
        TThriftPageModel tThriftPageModel = parseThriftService(jarUrl, thriftName);
        if (tThriftPageModel == null) {
            return Collections.emptyList();
        }

        List<TServiceModel> services = tThriftPageModel.getServices();
        if (CollectionUtils.isEmpty(services)) {
            return Collections.emptyList();
        }
        TServiceModel serviceModel = services.get(0);
        List<TMethodModel> methods = serviceModel.getMethods();
        if (CollectionUtils.isEmpty(methods)) {
            return Collections.emptyList();
        }
        for (TMethodModel method : methods) {
            LinkedHashMap<String, Object> params = new LinkedHashMap<>();
            List<TParamModel> request = method.getRequest();
            if (!CollectionUtils.isEmpty(request)) {
                for (int i = 1; i < request.size(); i++) {
                    TParamModel tParamModel = request.get(i);
                    Object val = parseParamItem(tParamModel);
                    params.put(tParamModel.getName(), val);
                }
            }
            method.setParams(params);
        }
        return methods;
    }

    private static Object parseParamItem(TParamModel tParamModel) {
        Object res = null;
        String typeName = tParamModel.getTypeName();

        TStructModel typeStruct = tParamModel.getTypeStruct();
        if (typeStruct == null) {
            res = typeName;
            Matcher matcher = LIST_PATTERN.matcher(typeName);
            if (matcher.find()) {
                res = Collections.singletonList(matcher.group(1));
            }
            return res;
        }
        String name = typeStruct.getName();
        Map<String, Object> paramMap = new HashMap<>();
        List<TParamModel> params = typeStruct.getParams();
        if (!CollectionUtils.isEmpty(params)) {
            for (TParamModel param : params) {
                String paramTypeName = param.getTypeName();
                if (name.equals(paramTypeName)) {
                    Matcher matcher = LIST_PATTERN.matcher(paramTypeName);
                    if (matcher.find()) {
                        paramMap.put(param.getName(), matcher.group(1));
                    } else {
                        paramMap.put(param.getName(), paramTypeName);
                    }
                } else {
                    paramMap.put(param.getName(), parseParamItem(param));
                }
            }
        }
        Matcher matcher = LIST_PATTERN.matcher(typeName);
        if (matcher.find()) {
            return Collections.singletonList(paramMap);
        }
        return paramMap;
    }

    public static TThriftPageModel parseThriftService(String jarUrl, String serviceThriftName) {
        URLClassLoader urlClassLoader = null;
        try {
            URL url = new URL(jarUrl);
            urlClassLoader = new URLClassLoader(new URL[]{url}, null);

            return parseThriftFile(urlClassLoader, serviceThriftName);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (urlClassLoader != null) {
                try {
                    urlClassLoader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

    public static TThriftPageModel parseThriftFile(ClassLoader loader, String thriftName) throws Exception {
        TThriftPageModel pageModel = new TThriftPageModel();
        // 跳过base.thrift文件  不在当前包下
        if (thriftName.contains("base.thrift")) {
            return pageModel;
        }

        InputStream inputStream = loader.getResourceAsStream("IDL/" + thriftName);
        if (inputStream == null) {
            return pageModel;
        }
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
        StringBuilder builder = new StringBuilder();
        char[] charArray = new char[200];
        int number = -1;
        while ((number = reader.read(charArray)) != -1) {
            builder.append(charArray, 0, number);
        }

        List<String> lines = Arrays.asList(builder.toString().split("\n"));

        pageModel.setName(thriftName);

        List<TEnumModel> tEnumModels = pageModel.getEnums();
        List<TStructModel> tStructModels = pageModel.getStructs();
        List<TServiceModel> tServiceModels = pageModel.getServices();
        List<TMethodModel> tMethodModels = null;

        TServiceModel currService = null;
        TMethodModel currMethod = null;
        TStructModel currStruct = null;
        TEnumModel currEnum = null;

        List<String> paramLines = null;
        List<String> enumItems = null;

        String desc = "";
        StringBuilder multiDesc = null;

        for (String line : lines) {
            line = line.trim();
            // 空行
            if (StringUtils.isEmpty(line)) {
                continue;
            }

            // include别的thrift文件
            Matcher includeMatcher = INCLUDE_PATTERN.matcher(line);
            if (includeMatcher.find()) {
                String subThrift = includeMatcher.group(2);
                pageModel.getSubPages().add(parseThriftFile(loader, subThrift + ".thrift"));
            }

            // 多行注释 开始
            if (line.startsWith("/**")) {
                multiDesc = new StringBuilder();
                multiDesc.append(line);
                continue;
            }
            // 多行注释结束
            if (line.endsWith("*/") && multiDesc != null) {
                multiDesc.append("\n").append(line);
                if (paramLines != null) {
                    paramLines.add(multiDesc.toString());
                } else if (enumItems != null) {
                    enumItems.add(multiDesc.toString());
                } else {
                    desc = multiDesc.toString();
                }
                multiDesc = null;
                continue;
            }
            if (line.startsWith("*") && multiDesc != null) {
                multiDesc.append("\n").append(line);
                continue;
            }

            // 单行注释
            if (line.startsWith("//")) {
                if (paramLines != null) {
                    paramLines.add(line);
                } else if (enumItems != null) {
                    enumItems.add(line);
                } else {
                    desc = line;
                }
                continue;
            }

            // 服务开始
            if (line.startsWith("service")) {
                currService = new TServiceModel();
                currService.setDesc(desc);
                desc = "";
                tMethodModels = new ArrayList<>();
                Matcher matcher = SERVICE_NAME_PATTERN.matcher(line);
                if (matcher.find()) {
                    currService.setName(matcher.group(2));
                }
            } else if (currService != null) {
                if (paramLines == null) {
                    paramLines = new ArrayList<>();
                }
                if (currMethod == null) {
                    currMethod = new TMethodModel();
                    currMethod.setDesc(desc);
                    desc = "";
                    Matcher matcher = METHOD_NAME_PATTERN.matcher(line);
                    if (matcher.find()) {
                        String responseName = matcher.group(1);
                        currMethod.setResponseTypeName(responseName);
                        // 处理方法的response
                        currMethod.setResponseStruct(parseParamStruct(responseName, pageModel));
                        currMethod.setName(matcher.group(2));
                    }
                }
                if (line.contains(")")) {
                    // 一个方法结束
                    List<TParamModel> TParamModels = parseMethodParam(paramLines, thriftName, pageModel);
                    currMethod.setRequest(TParamModels);
                    tMethodModels.add(currMethod);
                    currMethod = null;
                    paramLines = null;
                } else {
                    paramLines.add(line);
                }
            }
            if (currService != null && line.contains("}")) {
                // 服务结束
                currService.setMethods(tMethodModels);
                tServiceModels.add(currService);
                currService = null;
                tMethodModels = null;
            }

            // 结构体开始
            if (line.startsWith("struct")) {
                currStruct = new TStructModel();
                currStruct.setDesc(desc);
                desc = "";
                paramLines = new ArrayList<>();
                Matcher matcher = STRUCT_NAME_PATTERN.matcher(line);
                if (matcher.find()) {
                    currStruct.setName(matcher.group(2));
                }
            } else if (currStruct != null && paramLines != null) {
                paramLines.add(line);
            }
            if (currStruct != null && line.contains("}")) {
                List<TParamModel> TParamModels = parseMethodParam(paramLines, thriftName, pageModel);
                currStruct.setParams(TParamModels);
                tStructModels.add(currStruct);
                currStruct = null;
                paramLines = null;
            }

            // 枚举开始
            if (line.startsWith("enum")) {
                currEnum = new TEnumModel();
                currEnum.setDesc(desc);
                desc = "";
                enumItems = new ArrayList<>();
                Matcher matcher = ENUM_NAME_PATTERN.matcher(line);
                if (matcher.find()) {
                    currEnum.setName(matcher.group(2));
                }
            } else if (currEnum != null && enumItems != null) {
                enumItems.add(line);
            }
            if (currEnum != null && line.contains("}")) {
                List<TEnumModel.TEnumItemModel> tEnumItems = parseEnumItems(enumItems);
                currEnum.setItems(tEnumItems);
                tEnumModels.add(currEnum);
                currEnum = null;
                enumItems = null;
            }
        }
        return pageModel;
    }

    private static List<TEnumModel.TEnumItemModel> parseEnumItems(List<String> lines) {
        if (CollectionUtils.isEmpty(lines)) {
            return Collections.emptyList();
        }
        List<TEnumModel.TEnumItemModel> items = new ArrayList<>();
        String desc = "";
        for (String line : lines) {
            if (line.startsWith("//") || line.startsWith("/**")) {
                desc = line;
            } else {
                Matcher matcher = ENUM_ITEM_PATTERN.matcher(line);
                if (matcher.find()) {
                    TEnumModel.TEnumItemModel itemModel = new TEnumModel.TEnumItemModel();
                    itemModel.setDesc(desc);
                    desc = "";
                    itemModel.setName(matcher.group(1));
                    items.add(itemModel);
                }
            }
        }

        return items;
    }

    /**
     * 解析参数列表
     */
    private static List<TParamModel> parseMethodParam(List<String> lines, String thriftName, TThriftPageModel result) {
        if (CollectionUtils.isEmpty(lines)) {
            return Collections.emptyList();
        }
        List<TParamModel> params = new ArrayList<>();
        String desc = "";
        for (String line : lines) {
            if (line.startsWith("//") || line.startsWith("/**")) {
                desc = line;
            } else {
                TParamModel TParamModel = parseParam(desc, line, result);
                if (TParamModel != null) {
                    desc = "";
                    params.add(TParamModel);
                }
            }
        }

        return params;
    }

    /**
     * 组装单个参数model
     */
    private static TParamModel parseParam(String desc, String line, TThriftPageModel result) {
        Matcher paramMatcher = PARAM_PATTERN.matcher(line);
        if (paramMatcher.find()) {
            TParamModel param = new TParamModel();
            param.setDesc(desc);
            String isMust = paramMatcher.group(2);
            if ("required".equals(isMust)) {
                param.setRequired(true);
            }
            param.setName(paramMatcher.group(4));
            String typeName = paramMatcher.group(3);
            param.setTypeName(typeName);

            param.setTypeStruct(parseParamStruct(typeName, result));
            return param;
        }
        return null;
    }

    private static TStructModel parseParamStruct(String name, TThriftPageModel result) {
        if (BASE_TYPE.contains(name)) {
            return null;
        }
        if (name.contains("list")) {
            Matcher matcher = LIST_PATTERN.matcher(name);
            if (matcher.find()) {
                String subName = matcher.group(1);
                return parseParamStruct(subName, result);
            }
        } else if (name.contains("map")) {
            Matcher matcher = MAP_PATTERN.matcher(name);
            if (matcher.find()) {
                String subName = matcher.group(2);
                return parseParamStruct(subName, result);
            }
        } else if (name.contains(".")) {
            String subThriftName = name.split("[.]")[0] + ".thrift";
            String structName = name.split("[.]")[1];
            List<TThriftPageModel> subPages = result.getSubPages();
            if (!CollectionUtils.isEmpty(subPages)) {
                for (TThriftPageModel subPage : subPages) {
                    if (subThriftName.equals(subPage.getName())) {
                        List<TStructModel> structs = subPage.getStructs();
                        for (TStructModel TStructModel : structs) {
                            if (structName.equals(TStructModel.getName())) {
                                return TStructModel;
                            }
                        }
                        break;
                    }
                }
            }
        } else {
            List<TStructModel> structs = result.getStructs();
            for (TStructModel TStructModel : structs) {
                if (name.equals(TStructModel.getName())) {
                    return TStructModel;
                }
            }
        }
        return null;
    }

    @Data
    public static class TThriftPageModel {
        private String name;
        private List<TServiceModel> services = new ArrayList<>();
        private List<TStructModel> structs = new ArrayList<>();
        private List<TThriftPageModel> subPages = new ArrayList<>();
        private List<TEnumModel> enums = new ArrayList<>();
    }

    @Data
    public static class TServiceModel {
        private String desc;
        private String name;
        private List<TMethodModel> methods;
    }

    @Data
    public static class TMethodModel {
        private String name;
        private String desc;
        private String responseTypeName;
        private TStructModel responseStruct;
        private List<TParamModel> request;
        private LinkedHashMap<String, Object> params;
    }

    @Data
    public static class TStructModel {
        private String desc;
        private String name;
        private List<TParamModel> params;
    }

    @Data
    public static class TEnumModel {
        private String name;
        private String desc;
        private List<TEnumItemModel> items;

        @Data
        public static class TEnumItemModel {
            private String name;
            private String desc;
        }
    }

    @Data
    public static class TParamModel {
        private String typeName;
        private TStructModel typeStruct;
        private String name;
        private String desc;
        private boolean required;
    }

}
2021 年 4 月 8 日