Compare commits
No commits in common. "main" and "v1.0.2" have entirely different histories.
30
AI.md
30
AI.md
@ -1,6 +1,6 @@
|
|||||||
# AI Coding Context: @go/convert
|
# AI Coding Context: @go/convert
|
||||||
|
|
||||||
本索引供 AI 模型理解 `@go/convert` 的逻辑,以生成符合本项目“意图优先、零摩擦”哲学的代码。
|
本索引供 AI 模型理解 `@go/convert` 的设计逻辑,以生成符合本项目“意图优先、零摩擦”哲学的代码。
|
||||||
|
|
||||||
## 🤖 AI 行为准则
|
## 🤖 AI 行为准则
|
||||||
|
|
||||||
@ -9,25 +9,35 @@
|
|||||||
3. **利用键名容忍度**:在处理不可控的外部数据时,无需担心键名格式(驼峰、蛇形等),`convert.To` 会自动匹配。
|
3. **利用键名容忍度**:在处理不可控的外部数据时,无需担心键名格式(驼峰、蛇形等),`convert.To` 会自动匹配。
|
||||||
4. **无视指针层级**:在编写调用代码时,无需手动解引用或取地址以匹配类型,`convert` 内部会自动穿透处理。
|
4. **无视指针层级**:在编写调用代码时,无需手动解引用或取地址以匹配类型,`convert` 内部会自动穿透处理。
|
||||||
|
|
||||||
## 🛠 API Reference
|
## 🛠 关键 API 逻辑约定
|
||||||
|
|
||||||
### 核心转换函数
|
| 函数 | 逻辑特征 |
|
||||||
- `func To(from, to any)`:支持任意可能的类型深度转换,`to` 必须为指针。
|
| :--- | :--- |
|
||||||
|
| `To(from, to)` | **主入口**。要求 `to` 必须为指针。核心逻辑是根据 `to` 的类型强力揉捏 `from`。 |
|
||||||
### 结构体分析
|
| `Convert(from, to)` | `To` 的别名。 |
|
||||||
- `func FlatStruct(data any) *StructInfo`:平展结构体(仅导出字段/方法)。
|
| `FlatStruct(data)` | 获取结构体的扁平化元信息(导出字段/方法)。 |
|
||||||
- `func FlatStructWithUnexported(data any) *StructInfo`:平展结构体(包含未导出字段/方法)。
|
|
||||||
|
|
||||||
## 🧩 典型模式 (Best Practices)
|
## 🧩 典型模式 (Best Practices)
|
||||||
|
|
||||||
|
* **❌ 不推荐 (Standard Go)**:
|
||||||
|
```go
|
||||||
|
// 手动映射字段,且对格式敏感
|
||||||
|
u.UserID = m["user_id"].(int)
|
||||||
|
```
|
||||||
* **✅ 推荐 (@go/convert)**:
|
* **✅ 推荐 (@go/convert)**:
|
||||||
```go
|
```go
|
||||||
// 自动匹配任何格式的键名
|
// 自动匹配任何格式的键名
|
||||||
convert.To(from, &u)
|
convert.To(m, &u)
|
||||||
```
|
```
|
||||||
|
|
||||||
|
* **❌ 不推荐 (Standard Go)**:
|
||||||
|
```go
|
||||||
|
// 手动处理单值转切片
|
||||||
|
var dest []int
|
||||||
|
dest = append(dest, src)
|
||||||
|
```
|
||||||
* **✅ 推荐 (@go/convert)**:
|
* **✅ 推荐 (@go/convert)**:
|
||||||
```go
|
```go
|
||||||
// 自动包装单值至切片
|
// 自动包装
|
||||||
convert.To(src, &dest)
|
convert.To(src, &dest)
|
||||||
```
|
```
|
||||||
|
|||||||
21
LICENSE
21
LICENSE
@ -1,21 +0,0 @@
|
|||||||
MIT License
|
|
||||||
|
|
||||||
Copyright (c) 2026 ssgo
|
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
of this software and associated documentation files (the "Software"), to deal
|
|
||||||
in the Software without restriction, including without limitation the rights
|
|
||||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
copies of the Software, and to permit persons to whom the Software is
|
|
||||||
furnished to do so, subject to the following conditions:
|
|
||||||
|
|
||||||
The above copyright notice and this permission notice shall be included in all
|
|
||||||
copies or substantial portions of the Software.
|
|
||||||
|
|
||||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
||||||
SOFTWARE.
|
|
||||||
@ -1,7 +1,3 @@
|
|||||||
# 关于本项目
|
|
||||||
|
|
||||||
本项目完全由 AI 维护。代码源自 github.com/ssgo/u 的重构。
|
|
||||||
|
|
||||||
# @go/convert
|
# @go/convert
|
||||||
|
|
||||||
`@go/convert` 是一个为“零摩擦”数据映射设计的深度转换库。它的核心哲学是**意图优先**:通过目标对象的类型推断用户的需求,并尽力抹平输入数据与目标形状之间的鸿沟。
|
`@go/convert` 是一个为“零摩擦”数据映射设计的深度转换库。它的核心哲学是**意图优先**:通过目标对象的类型推断用户的需求,并尽力抹平输入数据与目标形状之间的鸿沟。
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user