From eba6ba49c08ca82f4635f62abdfc4cd613d24e35 Mon Sep 17 00:00:00 2001 From: Max Date: Mon, 31 Mar 2025 08:52:25 +0800 Subject: [PATCH] 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. --- widgets/table/process.go | 33 +++++++++++++++++++++++++++-- widgets/table/process_test.go | 40 +++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 2 deletions(-) diff --git a/widgets/table/process.go b/widgets/table/process.go index ca8f609b..0b02480e 100644 --- a/widgets/table/process.go +++ b/widgets/table/process.go @@ -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 +} diff --git a/widgets/table/process_test.go b/widgets/table/process_test.go index 883e0886..549e2b9a 100644 --- a/widgets/table/process_test.go +++ b/widgets/table/process_test.go @@ -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(