Skip to content

Commit

Permalink
Add Facades.config.set #48 (#93)
Browse files Browse the repository at this point in the history
* Add Gorm.Expr()

* Implement db.Raw() as alias of gorm.Expr()

* Update database/db/db.go

Co-authored-by: Wenbo Han <[email protected]>

* Implement db.Raw() as alias of gorm.Expr()

* Update gorm_test.go

* Add facades.Config.Set method #48

* Add facades.Config.Set method #48

* Add test case for handling nested maps in config

---------

Co-authored-by: Alex Chang <[email protected]>
Co-authored-by: Wenbo Han <[email protected]>
  • Loading branch information
3 people authored Apr 13, 2023
1 parent 5b0e7ce commit eb2ef23
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 9 deletions.
14 changes: 7 additions & 7 deletions config/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func NewApplication(envPath string) *Application {
return app
}

//Env Get config from env.
// Env Get config from env.
func (app *Application) Env(envName string, defaultValue ...any) any {
value := app.Get(envName, defaultValue...)
if cast.ToString(value) == "" {
Expand All @@ -68,12 +68,12 @@ func (app *Application) Env(envName string, defaultValue ...any) any {
return value
}

//Add config to application.
func (app *Application) Add(name string, configuration map[string]any) {
// Add config to application.
func (app *Application) Add(name string, configuration any) {
app.vip.Set(name, configuration)
}

//Get config from application.
// Get config from application.
func (app *Application) Get(path string, defaultValue ...any) any {
if !app.vip.IsSet(path) {
if len(defaultValue) > 0 {
Expand All @@ -85,7 +85,7 @@ func (app *Application) Get(path string, defaultValue ...any) any {
return app.vip.Get(path)
}

//GetString Get string type config from application.
// GetString Get string type config from application.
func (app *Application) GetString(path string, defaultValue ...any) string {
value := cast.ToString(app.Get(path, defaultValue...))
if value == "" {
Expand All @@ -99,7 +99,7 @@ func (app *Application) GetString(path string, defaultValue ...any) string {
return value
}

//GetInt Get int type config from application.
// GetInt Get int type config from application.
func (app *Application) GetInt(path string, defaultValue ...any) int {
value := app.Get(path, defaultValue...)
if cast.ToString(value) == "" {
Expand All @@ -113,7 +113,7 @@ func (app *Application) GetInt(path string, defaultValue ...any) int {
return cast.ToInt(value)
}

//GetBool Get bool type config from application.
// GetBool Get bool type config from application.
func (app *Application) GetBool(path string, defaultValue ...any) bool {
value := app.Get(path, defaultValue...)
if cast.ToString(value) == "" {
Expand Down
9 changes: 9 additions & 0 deletions config/application_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@ func (s *ApplicationTestSuite) TestAdd() {
})

s.Equal("local", s.config.GetString("app.env"))

s.config.Add("path.with.dot.case1", "value1")
s.Equal("value1", s.config.GetString("path.with.dot.case1"))

s.config.Add("path.with.dot.case2", "value2")
s.Equal("value2", s.config.GetString("path.with.dot.case2"))

s.config.Add("path.with.dot", map[string]any{"case3": "value3"})
s.Equal("value3", s.config.GetString("path.with.dot.case3"))
}

func (s *ApplicationTestSuite) TestGet() {
Expand Down
2 changes: 1 addition & 1 deletion contracts/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ type Config interface {
//Env Get config from env.
Env(envName string, defaultValue ...any) any
//Add config to application.
Add(name string, configuration map[string]any)
Add(name string, configuration any)
//Get config from application.
Get(path string, defaultValue ...any) any
//GetString Get string type config from application.
Expand Down
2 changes: 1 addition & 1 deletion contracts/config/mocks/Config.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit eb2ef23

Please sign in to comment.