首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Groovy ConfigSlurper在解析时只部分地构建了结构。

Groovy ConfigSlurper在解析时只部分地构建了结构。
EN

Stack Overflow用户
提问于 2022-01-21 18:15:05
回答 1查看 66关注 0票数 1

我正在为自己构建一个库,并使用ConfigSlurperconfig.groovy文件和其他配置源解析为聚合的ConfigObject,而我正在使用Micronaught编译时IOC注入

代码语言:javascript
复制
    @Bean
    @Named('config')
    ConfigObject config () {

        Map envMap = [:]
        def sysProps = System.getenv()
        if (System.getProperty("env") ) {
            envMap = System.getenv().findResult { it.key?.toLowerCase().contains "env" }.collect { [(it.key?.toLowerCase().substring(0, 2)): it.value.toLowerCase()] }
        } else
            envMap.get ('env', "development")
        def env = envMap.get('env')

        def resourcePath = "src${File.separatorChar}${env =="test" ?: "main"}${File.separatorChar}resources${File.separatorChar}"
        ConfigObject config = new ConfigSlurper().parse(new File("${resourcePath}ApplicationConfig.groovy").text /*.toURI().toURL()*/)
        config.put('systemProperties', sysProps)
        config.putAll(envMap)

        config.put('projectPath', System.getProperty("user.dir"))
        config.put('resourcesPath', resourcePath.toString())

        File resourceDirectory = new File ("${System.getProperty("user.dir")}$File.separatorChar$resourcePath")
        FilenameFilter filter = {file, name -> name.matches(~/^.*properties$/) }
        List propsFiles = resourceDirectory.listFiles (filter)

        filter = {file, name -> name.matches(~/^.*yaml$/) }
        List yamlFiles = resourceDirectory.listFiles (filter)

        propsFiles.each {file ->
            Properties prop = new Properties()
            prop.load(new FileInputStream (file) )
            Map propsMap = [:]
            for (key in prop.stringPropertyNames()) {
                propsMap.put(key, prop.getProperty(key))
            }
            config.putAll(propsMap)
        }

        yamlFiles.each {file ->
            def yamlConfig = new YamlSlurper().parseText(file.text)
            config.putAll(yamlConfig)
        }

        config
    }

resources/applicationConfig.groovy文件如下所示

代码语言:javascript
复制
    framework {
        environments {
            development {
                server = "local" // choice of {local|clustered}
                vertxOptions {
    
                }
    
            }
            test {
                server = "local" // choice of {local|clustered}
                vertxOptions {
    
                }
    
            }
            production {
                server = "clustered" // choice of {local|clustered}
                vertxOptions {
    
                }
    
            }
        }
    }

代码解析了groovy文件,但是当我查看configObject中的内容时,它创建了tope级别的框架映射条目--但是底层不存在。

在代码中调用configObject.framework返回对象,但它是size() == 0!

我不明白为什么建筑的其他部分不被建造。

有人能告诉我为什么内部结构没有被解析和建造。

有人能告诉我我做错了什么吗?

我刚做了一个像这样的剥离版

代码语言:javascript
复制
    def configText = """
    
    framework {
        environments {
            development {
                server = "local" // choice of {local|clustered}
                vertxOptions {
    
                }
    
            }
            test {
                server = "local" // choice of {local|clustered}
                vertxOptions {
    
                }
    
            }
            production {
                server = "clustered" // choice of {local|clustered}
                vertxOptions {
    
                }
    
            }
        }
    }
    """
    
    ConfigObject conf = new ConfigSlurper().parse(configText)
    assert conf.framework?.environments?.size() == 3

其中,错误显示如下

代码语言:javascript
复制
    Caught: Assertion failed: 
    
    assert conf.framework?.environments?.size() == 3
           |    |          |             |      |
           |    |          [:]           0      false
           |    ['environments':[:]]
           ['framework':['environments':[:]]]
    
    Assertion failed: 
    
    assert conf.framework?.environments?.size() == 3
           |    |          |             |      |
           |    |          [:]           0      false
           |    ['environments':[:]]
           ['framework':['environments':[:]]]
    
        at scripts.configSlurperTest.run(configSlurperTest.groovy:33)
        at
EN

回答 1

Stack Overflow用户

发布于 2022-01-21 22:04:11

当configslurper解析时,environments部分是以传递给ConfigSlurper本身的构造函数的环境为条件的。

假设您希望开发配置是默认的,但是当在testproduction中运行时,您想要覆盖这个配置,您将执行以下操作:

代码语言:javascript
复制
def configText = """

framework {

    // defaults
    
    mode = 'development'
    server = "local" // choice of {local|clustered}
    vertxOptions {

    }
    
    environments {
        // test settings
        test {
            mode = 'testing'
            
            server = "local" // choice of {local|clustered}
            vertxOptions {

            }

        }
        
        // production settings
        production {
            mode = 'live!'
            
            server = "clustered" // choice of {local|clustered}
            vertxOptions {

            }

        }
    }
}
"""

// Default values
assert new ConfigSlurper().parse(configText).framework.mode == 'development'
// Test values
assert new ConfigSlurper('test').parse(configText).framework.mode == 'testing'
// Production values
assert new ConfigSlurper('production').parse(configText).framework.mode == 'live!'
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70805852

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档