一个强大的 .NET 配置映射和数据转换库,支持 JSON 配置驱动的数据映射、多种数据转换器以及多平台 API 集成。
- 配置驱动的数据映射: 通过 JSON 配置文件定义复杂的数据映射规则
- 丰富的数据转换器: 内置 13 种常用的数据转换器
- API 集成管理: 支持多平台 API 配置和调用管理
- 灵活的认证方式: 支持 Basic、Bearer Token、API Key 等多种认证方式
- 场景化配置: 支持基于场景的 API 调用配置
- 高度可扩展: 支持自定义转换器和认证处理器
dotnet add package ConfigLink创建 mapping.json 配置文件:
{
"mappings": [
{
"source": "OrderId",
"target": "id"
},
{
"source": "Customer.Name",
"target": "customer_name"
},
{
"source": "OrderDate",
"target": "order_date",
"conversion": ["format"],
"conversion_params": {
"format": "yyyy-MM-dd"
}
},
{
"source": "TotalAmount",
"target": "total_amount",
"conversion": ["format", "prepend"],
"conversion_params": {
"format": "F2",
"prepend": "$"
}
}
]
}using ConfigLink;
using System.Text.Json;
// 解析映射配置
var mappingJson = File.ReadAllText("mapping.json");
var doc = JsonDocument.Parse(mappingJson);
var rules = doc.RootElement
.GetProperty("mappings")
.Deserialize<List<MappingRule>>();
// 创建映射引擎
var engine = new MappingEngine(rules);
// 源数据
var sourceData = new
{
OrderId = 1001,
Customer = new { Name = "John Doe" },
OrderDate = "2025/10/29",
TotalAmount = 59.97
};
// 执行映射转换
var result = engine.Transform(sourceData);
// 结果
// {
// "id": 1001,
// "customer_name": "John Doe",
// "order_date": "2025-10-29",
// "total_amount": "$59.97"
// }ConfigLink 提供了丰富的内置转换器:
📖 详细文档: 查看 映射配置详细指南 了解所有转换器的完整用法和示例
| 转换器 | 功能 | 示例 |
|---|---|---|
format |
格式化数据(日期、数字等) | "2025/10/29" → "2025-10-29" |
prepend |
在值前添加前缀 | "59.97" → "$59.97" |
case |
大小写转换 | "Hello" → "hello" |
trim |
去除空白字符 | " text " → "text" |
replace |
字符串替换 | "hello world" → "hello universe" |
substring |
字符串截取 | "hello" → "hel" |
join |
数组连接为字符串 | ["a", "b"] → "a,b" |
to_array |
转换为数组 | "a,b,c" → ["a", "b", "c"] |
map_array |
数组元素映射 | 对数组中每个元素执行映射 |
map_object |
对象映射 | 对嵌套对象执行映射 |
number |
数字转换 | "123" → 123 |
boolean |
布尔值转换 | "true" → true |
default |
设置默认值 | null → "默认值" |
{
"source": "TotalAmount",
"target": "total_amount",
"conversion": ["format", "prepend"],
"conversion_params": {
"format": "F2",
"prepend": "$"
}
}创建 api.config.json:
{
"PlatformA": {
"endpoint": "https://api.a.com/v1/users",
"auth": "basic",
"username": "admin",
"password": "secret",
"headers": {
"Content-Type": "application/json"
},
"timeoutSeconds": 30,
"retry": 3
},
"PlatformB": {
"endpoint": "https://api.b.com/webhook",
"auth": "bearer",
"token": "your-bearer-token",
"headers": {
"Content-Type": "application/json"
}
}
}创建 scenario.json:
{
"subscribe": {
"PlatformA": {
"path": "/api/v1/subscribe",
"method": "POST",
"mappings": [
{
"source": "email",
"target": "emailAddress"
},
{
"source": "firstName",
"target": "firstName"
}
]
}
}
}using ConfigLink;
// 加载配置
var apiConfig = ApiConfigs.FromFile("api.config.json");
var scenarioConfig = ScenarioConfigs.FromFile("scenario.json");
// 创建 API 管理器
using var apiManager = new ApiManager(apiConfig, scenarioConfig);
// 执行场景调用
var userData = new { email = "[email protected]", firstName = "John" };
var result = await apiManager.ExecuteScenarioAsync("subscribe", "PlatformA", userData);支持嵌套对象和数组的复杂映射:
{
"source": "Items",
"target": "items",
"conversion": ["map_array"],
"conversion_params": {
"map_array": [
{
"source": "ProductId",
"target": "product_id"
},
{
"source": "ProductName",
"target": "product_name"
}
]
}
}支持复杂的 JSON 路径表达式:
Customer.Name- 嵌套对象属性Items[0].ProductName- 数组元素访问$root.ShippingAddress- 根对象引用
实现 IConverter 接口创建自定义转换器:
public class CustomConverter : IConverter
{
public object? Convert(object? value, Dictionary<string, object>? parameters)
{
// 自定义转换逻辑
return transformedValue;
}
}
// 注册自定义转换器
engine.RegisterConverter("custom", new CustomConverter());ConfigLink/
├── ConfigLink/ # 核心库
│ ├── Api/ # API 管理功能
│ ├── Converters/ # 数据转换器
│ ├── IConverter.cs # 转换器接口
│ ├── MappingEngine.cs # 映射引擎
│ └── MappingRule.cs # 映射规则模型
├── Test/ # 测试项目
│ ├── config/ # 测试配置文件
│ ├── Api/ # API 测试
│ └── Converters/ # 转换器测试
└── TestConsole/ # 控制台测试项目
# 运行所有测试
dotnet test
# 运行特定测试
dotnet test --filter "TestClassName"
# 生成测试覆盖率报告
dotnet test --collect:"XPlat Code Coverage"# 构建项目
dotnet build
# 发布 Release 版本
dotnet build -c Release
# 打包 NuGet 包
dotnet pack -c Release欢迎提交 Issue 和 Pull Request!
- Fork 本仓库
- 创建特性分支 (
git checkout -b feature/AmazingFeature) - 提交更改 (
git commit -m 'Add some AmazingFeature') - 推送到分支 (
git push origin feature/AmazingFeature) - 提交 Pull Request
如果这个项目对你有帮助,请给它一个 ⭐️!