feat(encoding): 具名化 JS 导出并动态包裹错误(by AI)

This commit is contained in:
AI Engineer 2026-06-21 10:11:57 +08:00
parent d1ff9159e4
commit b05c586efb
3 changed files with 62 additions and 23 deletions

View File

@ -1,5 +1,10 @@
# Changelog: @go/encoding # Changelog: @go/encoding
## v1.5.4 (2026-06-21)
- **重构与错误堆栈支持**:
- 重构 `js_export.go`,将 `EncodeInt` 等匿名包装以及 `UnHex`/`UnBase64`/`UnURLBase64`/`UnURLEncode` 直接抛错的 Go 接口重写为具名函数,并动态使用 `jsmod.MakeError` 包裹错误。
- 升级 `jsmod` 依赖至 v1.5.3`cast` 依赖至 v1.5.3。
## v1.5.3 (2026-06-11) ## v1.5.3 (2026-06-11)
- **依赖对齐**: 升级 `jsmod` 依赖至 v1.5.2,升级 `cast` 依赖至 v1.5.2。 - **依赖对齐**: 升级 `jsmod` 依赖至 v1.5.2,升级 `cast` 依赖至 v1.5.2。

4
go.mod
View File

@ -2,6 +2,6 @@ module apigo.cc/go/encoding
go 1.25.0 go 1.25.0
require apigo.cc/go/cast v1.5.2 require apigo.cc/go/cast v1.5.3
require apigo.cc/go/jsmod v1.5.2 require apigo.cc/go/jsmod v1.5.3

View File

@ -6,24 +6,58 @@ func init() {
jsmod.Register("encoding", map[string]any{ jsmod.Register("encoding", map[string]any{
"Base64": Base64, "Base64": Base64,
"Base64Raw": Base64Raw, "Base64Raw": Base64Raw,
"UnBase64": UnBase64, "UnBase64": jsUnBase64,
"URLBase64": URLBase64, "URLBase64": URLBase64,
"URLBase64Raw": URLBase64Raw, "URLBase64Raw": URLBase64Raw,
"UnURLBase64": UnURLBase64, "UnURLBase64": jsUnURLBase64,
"Hex": Hex, "Hex": Hex,
"UnHex": UnHex, "UnHex": jsUnHex,
"URLEncode": URLEncode, "URLEncode": URLEncode,
"UnURLEncode": UnURLEncode, "UnURLEncode": jsUnURLEncode,
"HTMLEscape": HTMLEscape, "HTMLEscape": HTMLEscape,
"HTMLUnescape": HTMLUnescape, "HTMLUnescape": HTMLUnescape,
"UTF8Valid": UTF8Valid, "UTF8Valid": UTF8Valid,
"SortJoin": SortJoin, "SortJoin": SortJoin,
"EncodeInt": func(u uint64) string { "EncodeInt": jsEncodeInt,
return string(EncodeInt(u))
},
"DecodeInt": DecodeInt, "DecodeInt": DecodeInt,
"FillInt": FillInt, "FillInt": FillInt,
"ExchangeInt": ExchangeInt, "ExchangeInt": ExchangeInt,
"HashInt": HashInt, "HashInt": HashInt,
}) })
} }
func jsUnBase64(data any) ([]byte, error) {
res, err := UnBase64(data)
if err != nil {
return nil, jsmod.MakeError(err)
}
return res, nil
}
func jsUnURLBase64(data any) ([]byte, error) {
res, err := UnURLBase64(data)
if err != nil {
return nil, jsmod.MakeError(err)
}
return res, nil
}
func jsUnHex(data any) ([]byte, error) {
res, err := UnHex(data)
if err != nil {
return nil, jsmod.MakeError(err)
}
return res, nil
}
func jsUnURLEncode(data any) ([]byte, error) {
res, err := UnURLEncode(data)
if err != nil {
return nil, jsmod.MakeError(err)
}
return res, nil
}
func jsEncodeInt(u uint64) string {
return string(EncodeInt(u))
}