{"id":41340183,"url":"https://github.com/farseer-go/mapper","last_synced_at":"2026-01-23T06:48:14.328Z","repository":{"id":57747924,"uuid":"522141943","full_name":"farseer-go/mapper","owner":"farseer-go","description":"mapper","archived":false,"fork":false,"pushed_at":"2026-01-16T14:12:28.000Z","size":203,"stargazers_count":5,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2026-01-17T00:41:57.113Z","etag":null,"topics":["automapper"],"latest_commit_sha":null,"homepage":"https://farseer-go.gitee.io/","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/farseer-go.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2022-08-07T07:09:46.000Z","updated_at":"2026-01-16T14:12:31.000Z","dependencies_parsed_at":"2023-02-14T05:45:58.333Z","dependency_job_id":"24f52158-f127-4842-959c-ecba0404c24a","html_url":"https://github.com/farseer-go/mapper","commit_stats":null,"previous_names":[],"tags_count":26,"template":false,"template_full_name":null,"purl":"pkg:github/farseer-go/mapper","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/farseer-go%2Fmapper","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/farseer-go%2Fmapper/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/farseer-go%2Fmapper/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/farseer-go%2Fmapper/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/farseer-go","download_url":"https://codeload.github.com/farseer-go/mapper/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/farseer-go%2Fmapper/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28682262,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-23T05:48:07.525Z","status":"ssl_error","status_checked_at":"2026-01-23T05:48:07.129Z","response_time":59,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["automapper"],"created_at":"2026-01-23T06:48:13.163Z","updated_at":"2026-01-23T06:48:14.319Z","avatar_url":"https://github.com/farseer-go.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# mapper对象转换\n\u003e 包：`\"github.com/farseer-go/mapper\"`\n\n- `文档`\n    - [English](https://farseer-go.github.io/doc/#/en-us/)\n    - [中文](https://farseer-go.github.io/doc/)\n    - [English](https://farseer-go.github.io/doc/#/en-us/)\n- `源代码`\n    - [github](https://github.com/farseer-go/fs)\n  \n\n![](https://img.shields.io/github/stars/farseer-go?style=social)\n![](https://img.shields.io/github/license/farseer-go/mapper)\n![](https://img.shields.io/github/go-mod/go-version/farseer-go/mapper)\n![](https://img.shields.io/github/v/release/farseer-go/mapper)\n[![codecov](https://img.shields.io/codecov/c/github/farseer-go/mapper)](https://codecov.io/gh/farseer-go/mapper)\n![](https://img.shields.io/github/languages/code-size/farseer-go/mapper)\n[![Build](https://github.com/farseer-go/mapper/actions/workflows/test.yml/badge.svg)](https://github.com/farseer-go/mapper/actions/workflows/test.yml)\n![](https://goreportcard.com/badge/github.com/farseer-go/mapper)\n\n## 概述\n我们在开发当中经常会遇到实体与实体，实体集合与实体集合之间的相互转换，以及各种各样的相互转换问题。\n\n该组件很好的解决了上述转换过程中的人为赋值问题。\n\n组件支持 Map之间的转换，支持 数组之间的转换，支持 数组转List ，支持实体转Map ，支持List转List。\n\n\n\n## 1、方法Single\n\n_函数定义_\n```go\n// DO和DTO之间相互转换\nfunc Single[TEntity any](fromObjPtr any) TEntity {\n```\n_用法：_\n```go\n\ntype State int\n\nconst (\n    Running State = iota\n    Pending\n    Stopped\n)\n\nfunc (s State) String() string {\n    switch s {\n        case Running:\n        return \"Running\"\n        case Pending:\n        return \"Pending\"\n        case Stopped:\n        return \"Stopped\"\n        default:\n        return \"Unknown\"\n    }\n}\n\ntype ClientVO struct {\n    Id   int64\n    Ip   string\n    Name string\n}\n\ntype UserVO struct {\n    Id   int64\n    Name string\n}\n\ntype TaskDO struct {\n    Id       int\n    Client   ClientVO\n    Status   State\n    UserId   int64\n    UserName string\n    Data     collections.Dictionary[string, string]\n}\n\ntype TaskDTO struct {\n    Id         int\n    ClientId   int64\n    ClientIp   string\n    ClientName string\n    Status     State\n    User       UserVO\n    Data       collections.Dictionary[string, string]\n}\n\n//DO 转 DTO\ndto := TaskDTO{\n    Id:         1,\n    ClientId:   1000,\n    ClientIp:   \"127.0.0.1\",\n    ClientName: \"node\",\n    Status:     Pending,\n    User: UserVO{\n        Id:   88,\n        Name: \"steden\",\n    },\n    Data: collections.NewDictionaryFromMap(map[string]string{\"age\": \"18\", \"price\": \"88.88\"}),\n}\n\ndo := Single[TaskDO](dto)\n\n\n//DTO 转 DO\ndtoSingle := Single[TaskDTO](do)\n\n\n```\n\n## 2、方法ToMap\n\n_函数定义_\n```go\n// Map转Map\nfunc ToMap[K comparable, V any](fromObjPtr any) map[K]V\n```\n_用法：_\n```go\ntype po struct {\n    Name string\n    Age  int\n}\ntype do struct {\n    Name string\n    Age  int\n}\n\n//实体对象转map\narrPO := po{Name: \"steden\", Age: 18}\nmap := ToMap[string, any](\u0026arrPO)\n```\n\n## 3、方法Array\n\n_函数定义_\n```go\n// 数组转换\nfunc Array[T any](fromSlice any) []T\n```\n_用法：_\n```go\ntype po struct {\n    Name string\n    Age  int\n}\ntype do struct {\n    Name string\n    Age  int\n}\n\narrPO := []po{{Name: \"steden\", Age: 18}, {Name: \"steden1\", Age: 20}}\narrDO := Array[do](arrPO)\n\nassert.Equal(t, len(arrPO), len(arrDO))\n\nfor i := 0; i \u003c len(arrPO); i++ {\n    assert.Equal(t, arrPO[i].Name, arrDO[i].Name)\n    assert.Equal(t, arrPO[i].Age, arrDO[i].Age)\n}\n\n```\n\n## 4、方法ToPageList\n\n_函数定义_\n```go\n// 转成分页集合\nfunc ToPageList[TEntity any](sliceOrListOrListAny any, recordCount int64) collections.PageList[TEntity]\n```\n_用法：_\n```go\ntype po struct {\n    Name string\n    Age  int\n}\ntype do struct {\n    Name string\n    Age  int\n}\n\narrPO := []po{{Name: \"steden\", Age: 18}, {Name: \"steden1\", Age: 20}}\nlst := ToPageList[do](arrPO, 10)\n\n//返回数据数量\nlst.RecordCount\n\n```\n\n## 5、方法ToList\n\n_函数定义_\n```go\n// 转换成List\nfunc ToList[TEntity any](sliceOrListOrListAny any) collections.List[TEntity] \n```\n_用法：_\n```go\ntype po struct {\n    Name string\n    Age  int\n}\ntype do struct {\n    Name string\n    Age  int\n}\n\nlst := collections.NewList(po{Name: \"steden\", Age: 18}, po{Name: \"steden1\", Age: 20})\nlstDO := ToList[do](lst)\n\nassert.Equal(t, lst.Count(), lstDO.Count())\n\nfor i := 0; i \u003c lst.Count(); i++ {\n    assert.Equal(t, lst.Index(i).Name, lstDO.Index(i).Name)\n    assert.Equal(t, lst.Index(i).Age, lstDO.Index(i).Age)\n}\n\n//List转List\nlstAny := lst.ToListAny()\nlstDO = ToList[do](lstAny)\n\nassert.Equal(t, lstAny.Count(), lstDO.Count())\n\nfor i := 0; i \u003c lstAny.Count(); i++ {\n    po := lstAny.Index(i).(po)\n    assert.Equal(t, po.Name, lstDO.Index(i).Name)\n    assert.Equal(t, po.Age, lstDO.Index(i).Age)\n}\n//数组转List\narr := lst.ToArray()\nlstDO = ToList[do](arr)\n\nassert.Equal(t, len(arr), lstDO.Count())\n\n```\n## 6、方法ToListAny\n\n_函数定义_\n```go\n// 切片转ToListAny\nfunc ToListAny(sliceOrList any) collections.ListAny\n```\n_用法：_\n```go\ntype po struct {\n    Name string\n    Age  int\n}\ntype do struct {\n    Name string\n    Age  int\n}\n\narrPO := []po{{Name: \"steden\", Age: 18}, {Name: \"steden1\", Age: 20}}\n\nlistAny := ToListAny(arrPO)\n\nassert.Equal(t, listAny.Count(), len(arrPO))\nfor i := 0; i \u003c listAny.Count(); i++ {\n    po := listAny.Index(i).(po)\n\n    assert.Equal(t, po.Name, arrPO[i].Name)\n    assert.Equal(t, po.Age, arrPO[i].Age)\n}\n\nlst := collections.NewList(arrPO...)\nlistAny = ToListAny(lst)\n\nassert.Equal(t, listAny.Count(), len(arrPO))\nfor i := 0; i \u003c listAny.Count(); i++ {\n    po := listAny.Index(i).(po)\n\n    assert.Equal(t, po.Name, arrPO[i].Name)\n    assert.Equal(t, po.Age, arrPO[i].Age)\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffarseer-go%2Fmapper","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffarseer-go%2Fmapper","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffarseer-go%2Fmapper/lists"}