jdaugherty commented on code in PR #16736:
URL: https://github.com/apache/grails-core/pull/16736#discussion_r4419218816


##########
grails-datastore-core/src/main/groovy/org/grails/datastore/mapping/config/ConfigurationBuilder.groovy:
##########
@@ -425,6 +432,396 @@ abstract class ConfigurationBuilder<B, C> {
 
     }
 
+    private Map resolveMapProperty(String propertyPath, Map fallBackValue) {

Review Comment:
   On the open design question, I support limiting the Environment walk to 
flattened names. Grounds:
   
   Spring Boot's `MapBinder.bindAggregate` (spring-boot 4.1.2) reads a value 
stored at the exact path only when no source has a descendant of the name; 
otherwise it binds the entries from the descendants of every source, and the 
first source to define a key wins (`map.computeIfAbsent`). It never merges a 
lower map behind higher flattened keys, never treats a literal dotted key as an 
alias of a nested path, and never converts a string at the path while 
descendants exist.
   
   Every loader Grails ships flattens. `YamlPropertySourceLoader` and 
`GroovyConfigPropertySourceLoader`, the two that `grails-core` registers in 
`META-INF/spring.factories`, produce a `NavigableMapPropertySource`, which 
returns `null` for a map and lists a list as `name[n]`; Boot's own YAML loader, 
`.properties` files, system properties, environment variables and the command 
line are flat too. A map at the exact path reaches this walk only from a 
`MapPropertySource` built in code and from the `ConfigObjectPropertySource` 
inside a `DatastoreEnvironment`. Of the 24 features this PR adds to 
`ConfigurationBuilderSpec`, 19 store a map, list or string at the exact path of 
some source, and `create preserves a nonempty direct properties map without 
merging flattened keys` pins the opposite of the Boot rule for one source 
holding both shapes.
   
   With those two rules the walk for a raw `ConfigurableEnvironment` is: 
enumerate `prefix.*` names source by source, highest first; skip a name whose 
path the result already holds, or whose ancestor holds a non-map; build 
`name[n]` into a list; resolve each name through `readEnvironmentValue`, which 
keeps the env-var and placeholder handling; when no name matched, return the 
direct `Map` read as before. `DatastoreEnvironment` is prepared and takes the 
early return (see my reply on matrei's `DatastoreEnvironment` thread). That 
keeps the three behaviours #16567 needs and removes `withoutSourceAliases`, 
`exactPathPresent`, `indexedLiteralClaimed`, `nonMapLiteralPresent`, 
`convertSourceMap`, `firstRawValueIsMap`, `withoutResolvedDescendants`, 
`leafAlreadySet`, the four-argument `mergeMissing`, the identity return at L538 
and the JVM-source skip at L454, plus the features that only pin them. A 
one-line comment that a map stored at the exact path is used only when no 
flattened name exists
  documents the rule.



##########
grails-datasource/src/test/groovy/org/grails/plugins/datasource/DataSourceEnvironmentYamlSpec.groovy:
##########
@@ -0,0 +1,215 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    https://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+package org.grails.plugins.datasource
+
+import java.nio.file.Files
+import java.nio.file.Path
+
+import javax.sql.DataSource
+
+import com.zaxxer.hikari.HikariDataSource
+
+import org.springframework.core.env.StandardEnvironment
+import org.springframework.core.io.ByteArrayResource
+import org.springframework.core.io.FileSystemResource
+import org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy
+import org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy
+
+import grails.util.Environment
+import org.grails.config.yaml.YamlPropertySourceLoader
+import org.grails.core.cfg.GroovyConfigPropertySourceLoader
+import 
org.grails.datastore.gorm.jdbc.connections.DataSourceConnectionSourceFactory
+import org.grails.datastore.mapping.core.connections.ConnectionSource
+
+import spock.lang.Specification
+
+class DataSourceEnvironmentYamlSpec extends Specification {
+
+    private static final String APPLICATION_YML = '''\
+dataSource:
+    pooled: true
+    driverClassName: org.h2.Driver
+    username: sa
+    password: ''
+    type: com.zaxxer.hikari.HikariDataSource
+    properties:
+        maximumPoolSize: 4
+        minimumIdle: 1
+        connectionTimeout: 1000
+environments:
+    development:
+        dataSource:
+            dbCreate: create-drop
+            url: jdbc:h2:mem:devDb;DB_CLOSE_DELAY=-1
+            properties:
+                maximumPoolSize: 16
+                minimumIdle: 8
+                connectionTimeout: 2000
+    test:
+        dataSource:
+            dbCreate: update
+            url: jdbc:h2:mem:testDb;DB_CLOSE_DELAY=-1
+            properties:
+                maximumPoolSize: 2
+                minimumIdle: 1
+    production:
+        dataSource:
+            dbCreate: none
+            url: jdbc:h2:mem:prodDb;DB_CLOSE_DELAY=-1
+            properties:
+                maximumPoolSize: 32
+                minimumIdle: 4
+                connectionTimeout: 5000
+'''
+
+    private String previousEnvironment
+
+    def setup() {
+        previousEnvironment = System.getProperty(Environment.KEY)
+    }
+
+    def cleanup() {
+        if (previousEnvironment == null) {
+            System.clearProperty(Environment.KEY)
+        }
+        else {
+            System.setProperty(Environment.KEY, previousEnvironment)
+        }
+        Environment.reset()
+    }
+
+    void "application.yml #environment block supplies the dataSource 
properties"() {
+        given:
+        System.setProperty(Environment.KEY, environment)
+        Environment.reset()
+        def connectionSource = createFromApplicationYaml()
+        def dataSource = unwrap(connectionSource.source)
+
+        expect:
+        connectionSource.settings.url == url
+        dataSource instanceof HikariDataSource
+        dataSource.maximumPoolSize == maximumPoolSize
+        dataSource.minimumIdle == minimumIdle
+        dataSource.connectionTimeout == connectionTimeout
+
+        cleanup:
+        connectionSource?.close()
+
+        where:
+        environment   | url                                    | 
maximumPoolSize | minimumIdle | connectionTimeout
+        'development' | 'jdbc:h2:mem:devDb;DB_CLOSE_DELAY=-1'  | 16            
  | 8           | 2000
+        'test'        | 'jdbc:h2:mem:testDb;DB_CLOSE_DELAY=-1' | 2             
  | 1           | 1000
+        'production'  | 'jdbc:h2:mem:prodDb;DB_CLOSE_DELAY=-1' | 32            
  | 4           | 5000
+    }
+
+    void "unset grails.env uses the development dataSource properties"() {
+        given:
+        System.clearProperty(Environment.KEY)
+        Environment.reset()
+        def connectionSource = createFromApplicationYaml()
+        def dataSource = unwrap(connectionSource.source)
+
+        expect:
+        connectionSource.settings.url == 'jdbc:h2:mem:devDb;DB_CLOSE_DELAY=-1'
+        dataSource instanceof HikariDataSource
+        dataSource.maximumPoolSize == 16
+        dataSource.minimumIdle == 8
+        dataSource.connectionTimeout == 2000
+
+        cleanup:
+        connectionSource?.close()
+    }
+
+    void "application.yml binds a list under dataSource properties"() {

Review Comment:
   Follow-up, not for this PR: the same YAML through the datastore path still 
binds this list as literal keys. `HibernateDatastore(PropertyResolver, ...)` 
passes the environment through `DatastoreUtils.preparePropertyResolver`, whose 
`ConfigSlurper` parse keeps `connectionInitSqls[0]` and `[1]` as key names, and 
the walk here then copies that map as stored:
   
   ```
   raw Environment:         [maximumPoolSize:16, minimumIdle:8, 
connectionInitSqls:[SET A=1, SET B=2]]
   preparePropertyResolver: [minimumIdle:8, maximumPoolSize:16, 
connectionInitSqls[1]:SET B=2, connectionInitSqls[0]:SET A=1]
   ```
   
   Base behaves the same on that path, so this is the `preparePropertyResolver` 
gap matrei noted rather than a regression. An issue for `createFlatConfig` to 
fold `name[n]` into a list would cover the Boot autoconfiguration, which is the 
caller that reaches it with a raw environment.



##########
grails-datastore-core/src/main/groovy/org/grails/datastore/mapping/config/ConfigurationBuilder.groovy:
##########
@@ -425,6 +431,387 @@ abstract class ConfigurationBuilder<B, C> {
 
     }
 
+    private Map resolveMapProperty(String propertyPath, Map fallBackValue) {
+        Map directValue
+        RuntimeException conversionFailure = null
+        try {
+            directValue = propertyResolver.getProperty(propertyPath, Map)
+        } catch (ConversionFailedException | ConverterNotFoundException e) {
+            conversionFailure = e
+            directValue = null
+        }
+        if (!(propertyResolver instanceof ConfigurableEnvironment) && 
directValue != null && !directValue.isEmpty()) {

Review Comment:
   > Returning the highest `getProperty(..., Map)` alone drops those lower keys.
   
   That is what the base branch does for this resolver. With this PR's 
`ConfigurationBuilderSpec` run against 8.0.x, `Test DatastoreUtils resolver 
keeps keys from a lower configuration map` fails and `Test DatastoreUtils 
resolver retains existing map-backed builder binding` passes. 
`createPropertyResolvers` with more than one map has no caller outside 
`DatastoreUtils` and the tests, so keeping the lower keys is new behaviour with 
no production reader. The early return proposed above keeps the base result for 
every datastore built from a `Map` and removes the JVM-source skip at L454; my 
comment on `resolveMapProperty` has the rest.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to