GreedyStar / generator

Java code generating tool.
Apache License 2.0
260 stars 122 forks source link

IOUtil.toString()丢失换行符 #17

Open simplecode101 opened 1 year ago

simplecode101 commented 1 year ago

作者你好,不知道你最近还在不在更新,可能发现了一个bug IOUtils的toString方法中使用的是BufferedReader的readLine方法,此方法会丢失每行的换行符,会导致yaml解析时报错,具体情况如下: mapping values are not allowed here in 'reader', line 1, column 29: author: dumbcode projectName: test-1projectPath: D:\package ... ^

at org.yaml.snakeyaml.scanner.ScannerImpl.fetchValue(ScannerImpl.java:872)
at org.yaml.snakeyaml.scanner.ScannerImpl.fetchMoreTokens(ScannerImpl.java:360)
at org.yaml.snakeyaml.scanner.ScannerImpl.checkToken(ScannerImpl.java:226)
at org.yaml.snakeyaml.parser.ParserImpl$ParseBlockMappingKey.produce(ParserImpl.java:557)
at org.yaml.snakeyaml.parser.ParserImpl.peekEvent(ParserImpl.java:157)
at org.yaml.snakeyaml.parser.ParserImpl.checkEvent(ParserImpl.java:147)
at org.yaml.snakeyaml.composer.Composer.composeMappingNode(Composer.java:227)
at org.yaml.snakeyaml.composer.Composer.composeNode(Composer.java:154)
at org.yaml.snakeyaml.composer.Composer.composeDocument(Composer.java:122)
at org.yaml.snakeyaml.composer.Composer.getSingleNode(Composer.java:105)
at org.yaml.snakeyaml.constructor.BaseConstructor.getSingleData(BaseConstructor.java:140)
at org.yaml.snakeyaml.Yaml.loadFromReader(Yaml.java:524)
at org.yaml.snakeyaml.Yaml.loadAs(Yaml.java:518)
at com.greedystar.generator.utils.ConfigUtil.readConfigurationFromFile(ConfigUtil.java:68)
at com.greedystar.generator.utils.ConfigUtil.getConfiguration(ConfigUtil.java:37)
at com.greedystar.generator.db.ConnectionUtil.initConnection(ConnectionUtil.java:33)
at com.greedystar.generator.db.ConnectionUtil.getMetaData(ConnectionUtil.java:61)
at com.greedystar.generator.invoker.SingleInvoker.queryMetaData(SingleInvoker.java:19)
at com.greedystar.generator.invoker.base.AbstractInvoker.execute(AbstractInvoker.java:86)
at com.greedystar.generator.application.Main.single(Main.java:59)
at com.greedystar.generator.application.Main.main(Main.java:16)

Disconnected from the target VM, address: '127.0.0.1:50170', transport: 'socket'

Process finished with exit code 0

我的修复方式是在句末重新append,这样会让此方法改为默认拼接换行

1693279037035
ethfun commented 1 year ago

你可以把读取yaml配置那个方法readConfigurationFromFile替换为这个


    private static void readConfigurationFromFile() {
        try {
            URL url = ConfigUtil.class.getClassLoader().getResource("generator.yaml");
            if (null == url || url.getPath().contains("jar")) {
                System.err.println("Can not find file named 'generator.yaml' under resources path, please make sure that you have defined that file.");
                System.exit(0);
            } else {
                Yaml yaml = new Yaml(new Constructor(Configuration.class));
                InputStream inputStream = ConfigUtil.class.getClassLoader().getResourceAsStream("generator.yaml");
                ConfigUtil.setConfiguration(yaml.load(inputStream));

            }
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(0);
        }
    }
simplecode101 commented 1 year ago

你可以把读取yaml配置那个方法readConfigurationFromFile替换为这个


    private static void readConfigurationFromFile() {
        try {
            URL url = ConfigUtil.class.getClassLoader().getResource("generator.yaml");
            if (null == url || url.getPath().contains("jar")) {
                System.err.println("Can not find file named 'generator.yaml' under resources path, please make sure that you have defined that file.");
                System.exit(0);
            } else {
                Yaml yaml = new Yaml(new Constructor(Configuration.class));
                InputStream inputStream = ConfigUtil.class.getClassLoader().getResourceAsStream("generator.yaml");
                ConfigUtil.setConfiguration(yaml.load(inputStream));

            }
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(0);
        }
    }

感谢回复,不过这样改的话,作者在这块写的替换驼峰的代码逻辑就会被删掉了,不知道是不是因为我们版本不一样,我是master分支的最新代码:

1693559947582