feat: Add table listing and DSL retrieval processes

- Implement processList to return all loaded tables and processDSL to retrieve the DSL of a specified table.
- Add unit tests for the new processes to ensure correct functionality and data integrity.
- Update process registration in exportProcess to include the new operations.
This commit is contained in:
Max 2025-03-31 08:52:25 +08:00
parent cae4d06373
commit eba6ba49c0
2 changed files with 71 additions and 2 deletions

View file

@ -25,6 +25,8 @@ import (
// Export process
func exportProcess() {
// Table Data Operations
gouProcess.Register("yao.table.setting", processSetting)
gouProcess.Register("yao.table.xgen", processXgen)
gouProcess.Register("yao.table.component", processComponent)
@ -43,11 +45,15 @@ func exportProcess() {
gouProcess.Register("yao.table.deletewhere", processDeleteWhere)
gouProcess.Register("yao.table.deletein", processDeleteIn)
gouProcess.Register("yao.table.export", processExport)
// DSL Operations
gouProcess.Register("yao.table.exists", processExists)
gouProcess.Register("yao.table.read", processRead)
gouProcess.Register("yao.table.list", processList)
gouProcess.Register("yao.table.dsl", processDSL)
gouProcess.Register("yao.table.load", processLoad)
gouProcess.Register("yao.table.reload", processReload)
gouProcess.Register("yao.table.unload", processUnload)
gouProcess.Register("yao.table.read", processRead)
gouProcess.Register("yao.table.exists", processExists)
}
func processXgen(process *gouProcess.Process) interface{} {
@ -377,3 +383,26 @@ func processExists(process *gouProcess.Process) interface{} {
process.ValidateArgNums(1)
return Exists(process.ArgsString(0))
}
// processList returns all loaded tables
func processList(process *gouProcess.Process) interface{} {
tables := GetTables()
return tables
}
// GetTables returns all loaded tables
func GetTables() map[string]*DSL {
return Tables
}
// processGetDSL returns the DSL of a table by its ID
func processDSL(process *gouProcess.Process) interface{} {
process.ValidateArgNums(1)
id := process.ArgsString(0)
if !Exists(id) {
exception.New("table %s does not exist", 404, id).Throw()
}
tab := MustGet(process) // 0
return tab
}

View file

@ -640,6 +640,46 @@ func TestProcessRead(t *testing.T) {
assert.Equal(t, "::Pet Admin", res.(map[string]interface{})["name"])
}
func TestProcessList(t *testing.T) {
test.Prepare(t, config.Conf)
defer test.Clean()
prepare(t)
clear(t)
testData(t)
args := []interface{}{}
res, err := process.New("yao.table.list", args...).Exec()
if err != nil {
t.Fatal(err)
}
tables, ok := res.(map[string]*DSL)
assert.True(t, ok, "Expected map[string]*DSL")
assert.NotEmpty(t, tables, "Tables should not be empty")
assert.Contains(t, tables, "pet", "Should contain the pet table")
}
func TestProcessDSL(t *testing.T) {
test.Prepare(t, config.Conf)
defer test.Clean()
prepare(t)
clear(t)
testData(t)
args := []interface{}{"pet"}
res, err := process.New("yao.table.dsl", args...).Exec()
if err != nil {
t.Fatal(err)
}
dsl, ok := res.(*DSL)
assert.True(t, ok, "Expected map[string]interface{}")
assert.NotEmpty(t, dsl, "DSL should not be empty")
assert.Contains(t, dsl.Name, "Pet Admin", "DSL should contain name")
}
func testData(t *testing.T) {
pet := model.Select("pet")
err := pet.Insert(