@@ -3,29 +3,29 @@ package dev.mongocamp.driver.mongodb.database
33import java .util .concurrent .TimeUnit
44import com .mongodb .MongoCompressor
55import com .mongodb .MongoCredential .createCredential
6- import com .mongodb .event .{ CommandListener , ConnectionPoolListener }
6+ import com .mongodb .event .{CommandListener , ConnectionPoolListener }
77import dev .mongocamp .driver .mongodb .database .MongoConfig ._
8- import com .typesafe .config .{ Config , ConfigFactory }
8+ import com .typesafe .config .{Config , ConfigFactory }
99import org .mongodb .scala .connection ._
10- import org .mongodb .scala .{ MongoClientSettings , MongoCredential , ServerAddress }
10+ import org .mongodb .scala .{MongoClientSettings , MongoCredential , ServerAddress }
1111
1212import scala .jdk .CollectionConverters ._
1313import scala .collection .mutable .ArrayBuffer
1414
1515case class MongoConfig (
16- database : String ,
17- host : String = DefaultHost ,
18- port : Int = DefaultPort ,
19- applicationName : String = DefaultApplicationName ,
20- userName : Option [String ] = None ,
21- password : Option [String ] = None ,
22- authDatabase : String = DefaultAuthenticationDatabaseName ,
23- poolOptions : MongoPoolOptions = MongoPoolOptions (),
24- compressors : List [String ] = List (),
25- connectionPoolListener : List [ConnectionPoolListener ] = List (),
26- commandListener : List [CommandListener ] = List (),
27- customClientSettings : Option [MongoClientSettings ] = None
28- ) {
16+ database : String ,
17+ host : String = DefaultHost ,
18+ port : Int = DefaultPort ,
19+ applicationName : String = DefaultApplicationName ,
20+ userName : Option [String ] = None ,
21+ password : Option [String ] = None ,
22+ authDatabase : String = DefaultAuthenticationDatabaseName ,
23+ poolOptions : MongoPoolOptions = MongoPoolOptions (),
24+ compressors : List [String ] = List (),
25+ connectionPoolListener : List [ConnectionPoolListener ] = List (),
26+ commandListener : List [CommandListener ] = List (),
27+ customClientSettings : Option [MongoClientSettings ] = None
28+ ) {
2929
3030 val clientSettings : MongoClientSettings = {
3131 if (customClientSettings.isDefined) {
@@ -81,71 +81,88 @@ case class MongoConfig(
8181trait ConfigHelper {
8282 val conf : Config = ConfigFactory .load()
8383
84- def stringConfig (configPath : String , key : String , default : String = " " ): Option [String ] =
84+ def stringConfig (configPath : String , key : String , default : String = " " ): Option [String ] = {
8585 if (conf.hasPath(" %s.%s" .format(configPath, key))) {
8686 val str = conf.getString(" %s.%s" .format(configPath, key))
87- if (str.nonEmpty)
87+ if (str.nonEmpty) {
8888 Some (str)
89- else
89+ }
90+ else {
9091 None
92+ }
9193 }
92- else if (default.nonEmpty)
94+ else if (default.nonEmpty) {
9395 Some (default)
94- else
96+ }
97+ else {
9598 None
99+ }
100+ }
96101
97- def intConfig (configPath : String , key : String , default : Int = 0 ): Int =
98- if (conf.hasPath(" %s.%s" .format(configPath, key)))
102+ def intConfig (configPath : String , key : String , default : Int = 0 ): Int = {
103+ if (conf.hasPath(" %s.%s" .format(configPath, key))) {
99104 conf.getInt(" %s.%s" .format(configPath, key))
100- else
105+ }
106+ else {
101107 default
108+ }
109+ }
102110
103- def booleanConfig (configPath : String , key : String , default : Boolean = false ): Boolean =
104- if (conf.hasPath(" %s.%s" .format(configPath, key)))
111+ def booleanConfig (configPath : String , key : String , default : Boolean = false ): Boolean = {
112+ if (conf.hasPath(" %s.%s" .format(configPath, key))) {
105113 conf.getBoolean(" %s.%s" .format(configPath, key))
106- else
114+ }
115+ else {
107116 default
117+ }
118+ }
108119}
109120
110121object MongoConfig extends ConfigHelper {
111- val DefaultHost = " 127.0.0.1"
112- val DefaultPort = 27017
122+ val DefaultHost = " 127.0.0.1"
123+ val DefaultPort = 27017
113124 val DefaultAuthenticationDatabaseName = " admin"
114- val DefaultApplicationName = " mongocampdb-app"
125+ val DefaultApplicationName = " mongocampdb-app"
115126
116- val DefaultPoolMaxConnectionIdleTime = 60
117- val DefaultPoolMaxSize = 50
118- val DefaultPoolMinSize = 0
119- val DefaultPoolMaxWaitQueueSize = 500
127+ val DefaultPoolMaxConnectionIdleTime = 60
128+ val DefaultPoolMaxSize = 50
129+ val DefaultPoolMinSize = 0
130+ val DefaultPoolMaxWaitQueueSize = 500
120131 val DefaultPoolMaintenanceInitialDelay = 0
121132
122133 val ComressionSnappy = " snappy"
123- val ComressionZlib = " zlib"
124- val ComressionZstd = " zstd"
134+ val ComressionZlib = " zlib"
135+ val ComressionZstd = " zstd"
125136
126137 val DefaultConfigPathPrefix = " mongodb"
127138
128139 def fromPath (configPath : String = DefaultConfigPathPrefix ): MongoConfig = {
129140
130- def poolOptionsConfig (key : String , default : Int ): Int =
131- if (conf.hasPath(" %s.pool.%s" .format(configPath, key)))
141+ def poolOptionsConfig (key : String , default : Int ): Int = {
142+ if (conf.hasPath(" %s.pool.%s" .format(configPath, key))) {
132143 conf.getInt(" %s.pool.%s" .format(configPath, key))
133- else
144+ }
145+ else {
134146 default
147+ }
148+ }
135149
136150 val port : Int = intConfig(configPath, " port" , DefaultPort )
137151
138- val compressors : List [String ] =
139- if (conf.hasPath(" %s.compressors" .format(configPath)))
152+ val compressors : List [String ] = {
153+ if (conf.hasPath(" %s.compressors" .format(configPath))) {
140154 conf.getStringList(" %s.compressors" .format(configPath)).asScala.toList
141- else
155+ }
156+ else {
142157 List ()
158+ }
159+ }
143160
144- val host = stringConfig(configPath, " host" , DefaultHost ).get
145- val database = stringConfig(configPath, " database" ).get
146- val userName = stringConfig(configPath, " userName" )
147- val password = stringConfig(configPath, " password" )
148- val authDatabase = stringConfig(configPath, " authDatabase" , DefaultAuthenticationDatabaseName ).get
161+ val host = stringConfig(configPath, " host" , DefaultHost ).get
162+ val database = stringConfig(configPath, " database" ).get
163+ val userName = stringConfig(configPath, " userName" )
164+ val password = stringConfig(configPath, " password" )
165+ val authDatabase = stringConfig(configPath, " authDatabase" , DefaultAuthenticationDatabaseName ).get
149166 val applicationName = stringConfig(configPath, " applicationName" , DefaultApplicationName ).get
150167
151168 val poolOptions = MongoPoolOptions (
0 commit comments