Skip to content

Commit

Permalink
Merge branch 'master' into bowen/#289-1
Browse files Browse the repository at this point in the history
  • Loading branch information
hwbrzzl authored Nov 4, 2023
2 parents 3bf096e + 7bfd957 commit 5b75309
Show file tree
Hide file tree
Showing 17 changed files with 188 additions and 90 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ This project exists thanks to all the people who contribute, to participate in t
<a href="https://github.com/Marian0" target="_blank"><img src="https://avatars.githubusercontent.com/u/624592?v=4" width="48" height="48"></a>
<a href="https://github.com/ahmed3mar" target="_blank"><img src="https://avatars.githubusercontent.com/u/12982325?v=4" width="48" height="48"></a>
<a href="https://github.com/flc1125" target="_blank"><img src="https://avatars.githubusercontent.com/u/14297703?v=4" width="48" height="48"></a>
<a href="https://github.com/zzpwestlife" target="_blank"><img src="https://avatars.githubusercontent.com/u/12382180?v=4" width="48" height="48"></a>

## Sponsor

Better development of the project is inseparable from your support, reward us by [Open Collective](https://opencollective.com/goravel).

<p align="left"><img src="https://www.goravel.dev/reward.png" width="200"></p>

## Group

Expand Down
7 changes: 7 additions & 0 deletions README_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ Laravel!
<a href="https://github.com/Marian0" target="_blank"><img src="https://avatars.githubusercontent.com/u/624592?v=4" width="48" height="48"></a>
<a href="https://github.com/ahmed3mar" target="_blank"><img src="https://avatars.githubusercontent.com/u/12982325?v=4" width="48" height="48"></a>
<a href="https://github.com/flc1125" target="_blank"><img src="https://avatars.githubusercontent.com/u/14297703?v=4" width="48" height="48"></a>
<a href="https://github.com/zzpwestlife" target="_blank"><img src="https://avatars.githubusercontent.com/u/12382180?v=4" width="48" height="48"></a>

## 打赏

开源项目的发展离不开您的支持,感谢微信打赏。

<p align="left"><img src="https://www.goravel.dev/reward-wechat.jpg" width="200"></p>

## 群组

Expand Down
6 changes: 2 additions & 4 deletions config/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,13 @@ func NewApplication(envPath string) *Application {
app.vip = viper.New()
app.vip.SetConfigType("env")
app.vip.SetConfigFile(envPath)
app.vip.AutomaticEnv()

if err := app.vip.ReadInConfig(); err != nil {
color.Redln("Invalid Config error: " + err.Error())
os.Exit(0)
}

app.vip.SetEnvPrefix("goravel")
app.vip.AutomaticEnv()

appKey := app.Env("APP_KEY")
if support.Env != support.EnvArtisan {
if appKey == nil {
Expand All @@ -47,7 +45,7 @@ func NewApplication(envPath string) *Application {
}

if len(appKey.(string)) != 32 {
color.Redln("Invalid APP_KEY, please reset it.")
color.Redln("Invalid APP_KEY, the length must be 32, please reset it.")
color.Warnln("Example command: \ngo run . artisan key:generate")
os.Exit(0)
}
Expand Down
82 changes: 54 additions & 28 deletions config/application_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,30 @@ import (

type ApplicationTestSuite struct {
suite.Suite
config *Application
config2 *Application
config *Application
customConfig *Application
}

func TestApplicationTestSuite(t *testing.T) {
assert.Nil(t, file.Create(".env", "APP_KEY=12345678901234567890123456789012"))
assert.Nil(t, file.Create(".env", `
APP_KEY=12345678901234567890123456789012
APP_DEBUG=true
DB_PORT=3306
`))
temp, err := os.CreateTemp("", "goravel.env")
assert.Nil(t, err)
defer os.Remove(temp.Name())
_, err = temp.Write([]byte("APP_KEY=12345678901234567890123456789012"))
_, err = temp.Write([]byte(`
APP_KEY=12345678901234567890123456789012
APP_DEBUG=true
DB_PORT=3306
`))
assert.Nil(t, err)
assert.Nil(t, temp.Close())

suite.Run(t, &ApplicationTestSuite{
config: NewApplication(".env"),
config2: NewApplication(temp.Name()),
config: NewApplication(".env"),
customConfig: NewApplication(temp.Name()),
})

assert.Nil(t, file.Remove(".env"))
Expand All @@ -37,43 +45,61 @@ func (s *ApplicationTestSuite) SetupTest() {

}

func (s *ApplicationTestSuite) TestOsVariables() {
s.Nil(os.Setenv("APP_KEY", "12345678901234567890123456789013"))
s.Nil(os.Setenv("OS_APP_NAME", "goravel"))
s.Nil(os.Setenv("OS_APP_PORT", "3306"))
s.Nil(os.Setenv("OS_APP_DEBUG", "true"))

s.Equal("12345678901234567890123456789013", s.config.GetString("APP_KEY"))
s.Equal("12345678901234567890123456789013", s.customConfig.GetString("APP_KEY"))
s.Equal("goravel", s.config.GetString("OS_APP_NAME"))
s.Equal("goravel", s.customConfig.GetString("OS_APP_NAME"))
s.Equal(3306, s.config.GetInt("OS_APP_PORT"))
s.Equal(3306, s.customConfig.GetInt("OS_APP_PORT"))
s.True(s.config.GetBool("OS_APP_DEBUG"))
s.True(s.customConfig.GetBool("OS_APP_DEBUG"))
}

func (s *ApplicationTestSuite) TestEnv() {
s.Equal("12345678901234567890123456789012", s.config.Env("APP_KEY").(string))
s.Equal("goravel", s.config.Env("APP_NAME", "goravel").(string))
s.Equal("127.0.0.1", s.config.Env("DB_HOST", "127.0.0.1").(string))
s.Equal("goravel", s.config2.Env("APP_NAME", "goravel").(string))
s.Equal("127.0.0.1", s.config2.Env("DB_HOST", "127.0.0.1").(string))
s.Equal("12345678901234567890123456789012", s.customConfig.Env("APP_KEY").(string))
s.Equal("goravel", s.customConfig.Env("APP_NAME", "goravel").(string))
}

func (s *ApplicationTestSuite) TestAdd() {
s.config.Add("app", map[string]any{
"env": "local",
})
s.config2.Add("app", map[string]any{
s.customConfig.Add("app", map[string]any{
"env": "local",
})

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

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

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

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

func (s *ApplicationTestSuite) TestGet() {
s.Equal("12345678901234567890123456789012", s.config.Get("APP_KEY").(string))
s.Equal("goravel", s.config.Get("APP_NAME", "goravel").(string))
s.Equal("goravel", s.config2.Get("APP_NAME", "goravel").(string))
s.Equal("12345678901234567890123456789012", s.customConfig.Get("APP_KEY").(string))
s.Equal("goravel", s.customConfig.Get("APP_NAME", "goravel").(string))
}

func (s *ApplicationTestSuite) TestGetString() {
Expand All @@ -85,29 +111,29 @@ func (s *ApplicationTestSuite) TestGetString() {
},
},
})
s.config2.Add("database", map[string]any{
"default": s.config2.Env("DB_CONNECTION", "mysql"),
s.customConfig.Add("database", map[string]any{
"default": s.customConfig.Env("DB_CONNECTION", "mysql"),
"connections": map[string]any{
"mysql": map[string]any{
"host": s.config2.Env("DB_HOST", "127.0.0.1"),
"host": s.customConfig.Env("DB_HOST", "127.0.0.1"),
},
},
})

s.Equal("goravel", s.config.GetString("APP_NAME", "goravel"))
s.Equal("127.0.0.1", s.config.GetString("database.connections.mysql.host"))
s.Equal("mysql", s.config.GetString("database.default"))
s.Equal("goravel", s.config2.GetString("APP_NAME", "goravel"))
s.Equal("127.0.0.1", s.config2.GetString("database.connections.mysql.host"))
s.Equal("mysql", s.config2.GetString("database.default"))
s.Equal("goravel", s.customConfig.GetString("APP_NAME", "goravel"))
s.Equal("127.0.0.1", s.customConfig.GetString("database.connections.mysql.host"))
s.Equal("mysql", s.customConfig.GetString("database.default"))
}

func (s *ApplicationTestSuite) TestGetInt() {
s.Equal(s.config.GetInt("DB_PORT", 3306), 3306)
s.Equal(s.config2.GetInt("DB_PORT", 3306), 3306)
s.Equal(3306, s.config.GetInt("DB_PORT"))
s.Equal(3306, s.customConfig.GetInt("DB_PORT"))
}

func (s *ApplicationTestSuite) TestGetBool() {
s.Equal(true, s.config.GetBool("APP_DEBUG", true))
s.Equal(true, s.config2.GetBool("APP_DEBUG", true))
s.Equal(true, s.config.GetBool("APP_DEBUG"))
s.Equal(true, s.customConfig.GetBool("APP_DEBUG"))
}
4 changes: 2 additions & 2 deletions database/db/dsn.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ func (d *DsnImpl) Postgresql(config databasecontract.Config) string {
sslmode := d.config.GetString("database.connections." + d.connection + ".sslmode")
timezone := d.config.GetString("database.connections." + d.connection + ".timezone")

return fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%d sslmode=%s TimeZone=%s",
host, config.Username, config.Password, config.Database, config.Port, sslmode, timezone)
return fmt.Sprintf("postgres://%s:%s@%s:%d/%s?sslmode=%s&timezone=%s",
config.Username, config.Password, host, config.Port, config.Database, sslmode, timezone)
}

func (d *DsnImpl) Sqlite(config databasecontract.Config) string {
Expand Down
4 changes: 2 additions & 2 deletions database/db/dsn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ func (s *DsnTestSuite) TestPostgresql() {
s.mockConfig.On("GetString", fmt.Sprintf("database.connections.%s.sslmode", connection)).Return(sslmode).Once()
s.mockConfig.On("GetString", fmt.Sprintf("database.connections.%s.timezone", connection)).Return(timezone).Once()

s.Equal(fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%d sslmode=%s TimeZone=%s",
testHost, testUsername, testPassword, testDatabase, testPort, sslmode, timezone), dsn.Postgresql(testConfig))
s.Equal(fmt.Sprintf("postgres://%s:%s@%s:%d/%s?sslmode=%s&timezone=%s",
testUsername, testPassword, testHost, testPort, testDatabase, sslmode, timezone), dsn.Postgresql(testConfig))
}

func (s *DsnTestSuite) TestSqlite() {
Expand Down
15 changes: 13 additions & 2 deletions database/gorm/cursor.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import (
)

type CursorImpl struct {
row map[string]any
query *QueryImpl
row map[string]any
}

func (c *CursorImpl) Scan(value any) error {
Expand All @@ -33,7 +34,17 @@ func (c *CursorImpl) Scan(value any) error {
return err
}

return decoder.Decode(c.row)
if err := decoder.Decode(c.row); err != nil {
return err
}

for relation, args := range c.query.with {
if err := c.query.origin.Load(value, relation, args...); err != nil {
return err
}
}

return nil
}

func ToTimeHookFunc() mapstructure.DecodeHookFunc {
Expand Down
4 changes: 2 additions & 2 deletions database/gorm/dialector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ func (s *DialectorTestSuite) TestPostgresql() {
Return("UTC").Once()
dialectors, err := dialector.Make([]databasecontract.Config{s.config})
s.Equal(postgres.New(postgres.Config{
DSN: fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%d sslmode=%s TimeZone=%s",
s.config.Host, s.config.Username, s.config.Password, s.config.Database, s.config.Port, "disable", "UTC"),
DSN: fmt.Sprintf("postgres://%s:%s@%s:%d/%s?sslmode=%s&timezone=%s",
s.config.Username, s.config.Password, s.config.Host, s.config.Port, s.config.Database, "disable", "UTC"),
}), dialectors[0])
s.Nil(err)
}
Expand Down
5 changes: 4 additions & 1 deletion database/gorm/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ func (e *Event) IsClean(fields ...string) bool {
}

func (e *Event) Query() orm.Query {
return NewQueryWithWithoutEvents(e.query.instance.Session(&gorm.Session{NewDB: true}), false, e.query.config)
return NewQueryImplByInstance(e.query.instance.Session(&gorm.Session{NewDB: true}), &QueryImpl{
config: e.query.config,
withoutEvents: false,
})
}

func (e *Event) Context() context.Context {
Expand Down
28 changes: 20 additions & 8 deletions database/gorm/event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,15 @@ type TestEventModel struct {

var testNow = time.Now().Add(-1 * time.Second)
var testEventModel = TestEventModel{Name: "name", Avatar: "avatar", IsAdmin: true, IsManage: 0, AdminAt: testNow, ManageAt: testNow, high: 1}
var testQuery = NewQueryWithWithoutEvents(&gorm.DB{
var testQuery = NewQueryImplByInstance(&gorm.DB{
Statement: &gorm.Statement{
Selects: []string{},
Omits: []string{},
},
}, false, nil)
}, &QueryImpl{
config: nil,
withoutEvents: false,
})

type EventTestSuite struct {
suite.Suite
Expand All @@ -48,13 +51,16 @@ func (s *EventTestSuite) SetupTest() {

func (s *EventTestSuite) TestSetAttribute() {
dest := map[string]any{"avatar": "avatar1"}
query := NewQueryWithWithoutEvents(&gorm.DB{
query := NewQueryImplByInstance(&gorm.DB{
Statement: &gorm.Statement{
Selects: []string{},
Omits: []string{},
Dest: dest,
},
}, false, nil)
}, &QueryImpl{
config: nil,
withoutEvents: false,
})

event := NewEvent(query, &testEventModel, dest)

Expand Down Expand Up @@ -148,23 +154,29 @@ func (s *EventTestSuite) TestValidColumn() {
s.True(event.validColumn("manage"))
s.False(event.validColumn("age"))

event.query = NewQueryWithWithoutEvents(&gorm.DB{
event.query = NewQueryImplByInstance(&gorm.DB{
Statement: &gorm.Statement{
Selects: []string{"name"},
Omits: []string{},
},
}, false, nil)
}, &QueryImpl{
config: nil,
withoutEvents: false,
})
s.True(event.validColumn("Name"))
s.True(event.validColumn("name"))
s.False(event.validColumn("avatar"))
s.False(event.validColumn("Avatar"))

event.query = NewQueryWithWithoutEvents(&gorm.DB{
event.query = NewQueryImplByInstance(&gorm.DB{
Statement: &gorm.Statement{
Selects: []string{},
Omits: []string{"name"},
},
}, false, nil)
}, &QueryImpl{
config: nil,
withoutEvents: false,
})
s.False(event.validColumn("Name"))
s.False(event.validColumn("name"))
s.True(event.validColumn("avatar"))
Expand Down
Loading

0 comments on commit 5b75309

Please sign in to comment.