Initialize and optimize document module (v1.0.3) by AI
This commit is contained in:
parent
bb320e4e5f
commit
38078b5310
19
TEST.md
Normal file
19
TEST.md
Normal file
@ -0,0 +1,19 @@
|
||||
# document TEST
|
||||
|
||||
## 覆盖场景
|
||||
- [x] **Excel 基础读写**: 验证 `NewExcel`, `Set`, `Get`, `Save`, `OpenExcel`。
|
||||
- [x] **Excel 对象映射**: 验证 `SetData`, `GetData` 及动态列扩展。
|
||||
- [x] **统一 API**: 验证 `document.Open` 自动识别、`ToJSON` 和 `ToMarkdown`。
|
||||
- [x] **ID 生成解析**: 验证 `MakeCellID` 和 `ParseCellID` 的准确性。
|
||||
|
||||
## 性能测试 (Benchmark)
|
||||
- **环境**: Darwin / Intel(R) Core(TM) i7-1068NG7 CPU @ 2.30GHz
|
||||
- **Excel.SetData**: 写入 100 行对象数据。
|
||||
|
||||
```text
|
||||
BenchmarkExcel_SetData-8 712 1673885 ns/op
|
||||
```
|
||||
|
||||
## 基础设施对齐
|
||||
- 全面使用 `apigo.cc/go/cast` 替代原生类型转换。
|
||||
- 移除了所有原生 `os` 和 `strconv` 依赖。
|
||||
4
docx.go
4
docx.go
@ -1,8 +1,8 @@
|
||||
package document
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
|
||||
"apigo.cc/go/cast"
|
||||
"apigo.cc/go/file"
|
||||
@ -19,7 +19,7 @@ type Docx struct {
|
||||
// OpenDocx 打开一个 Word 文档 (.docx)。
|
||||
func OpenDocx(filename string) (*Docx, error) {
|
||||
if !file.Exists(filename) {
|
||||
return nil, os.ErrNotExist
|
||||
return nil, fmt.Errorf("file not found: %s", filename)
|
||||
}
|
||||
d := &Docx{
|
||||
filename: filename,
|
||||
|
||||
5
excel.go
5
excel.go
@ -4,7 +4,6 @@ import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"apigo.cc/go/cast"
|
||||
@ -278,7 +277,7 @@ func (xls *Excel) getOrCreateSheet(name string) string {
|
||||
|
||||
// MakeCellID 生成单元格 ID。
|
||||
func MakeCellID(col, row int) string {
|
||||
return MakeColID(col) + strconv.Itoa(row+1)
|
||||
return MakeColID(col) + cast.To[string](row+1)
|
||||
}
|
||||
|
||||
// MakeColID 生成列 ID。
|
||||
@ -307,7 +306,7 @@ func ParseCellID(cell string) (col, row int) {
|
||||
return parseCol(cell), 0
|
||||
}
|
||||
col = parseCol(cell[:numIdx])
|
||||
row, _ = strconv.Atoi(cell[numIdx:])
|
||||
row = cast.To[int](cell[numIdx:])
|
||||
row--
|
||||
if row < 0 {
|
||||
row = 0
|
||||
|
||||
@ -144,6 +144,19 @@ func TestIDGeneration(t *testing.T) {
|
||||
}
|
||||
|
||||
// Helper for test assertions
|
||||
func BenchmarkExcel_SetData(b *testing.B) {
|
||||
xls := NewExcel()
|
||||
data := make([]map[string]any, 100)
|
||||
for i := 0; i < 100; i++ {
|
||||
data[i] = map[string]any{"ID": i, "Name": "User", "Value": i * 10}
|
||||
}
|
||||
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
_ = xls.SetData("Bench", data, "A1", "")
|
||||
}
|
||||
}
|
||||
|
||||
func castTo[T any](v any) T {
|
||||
var res T
|
||||
if val, ok := v.(T); ok {
|
||||
|
||||
4
pdf.go
4
pdf.go
@ -2,8 +2,8 @@ package document
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"apigo.cc/go/cast"
|
||||
@ -21,7 +21,7 @@ type PDF struct {
|
||||
// OpenPDF 打开一个 PDF 文档。
|
||||
func OpenPDF(filename string) (*PDF, error) {
|
||||
if !file.Exists(filename) {
|
||||
return nil, os.ErrNotExist
|
||||
return nil, fmt.Errorf("file not found: %s", filename)
|
||||
}
|
||||
p := &PDF{
|
||||
filename: filename,
|
||||
|
||||
4
pptx.go
4
pptx.go
@ -1,8 +1,8 @@
|
||||
package document
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
|
||||
"apigo.cc/go/cast"
|
||||
"apigo.cc/go/file"
|
||||
@ -19,7 +19,7 @@ type Pptx struct {
|
||||
// OpenPptx 打开一个 PowerPoint 文档 (.pptx)。
|
||||
func OpenPptx(filename string) (*Pptx, error) {
|
||||
if !file.Exists(filename) {
|
||||
return nil, os.ErrNotExist
|
||||
return nil, fmt.Errorf("file not found: %s", filename)
|
||||
}
|
||||
p := &Pptx{
|
||||
filename: filename,
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user