{"id":13786635,"url":"https://github.com/danhper/structomap","last_synced_at":"2025-04-13T02:33:20.727Z","repository":{"id":31968398,"uuid":"35538481","full_name":"danhper/structomap","owner":"danhper","description":"Easily and dynamically generate maps from Go static structures","archived":false,"fork":false,"pushed_at":"2019-05-24T14:07:40.000Z","size":25,"stargazers_count":144,"open_issues_count":0,"forks_count":11,"subscribers_count":7,"default_branch":"master","last_synced_at":"2024-05-19T17:31:17.032Z","etag":null,"topics":["json","serializer"],"latest_commit_sha":null,"homepage":"https://godoc.org/github.com/danhper/structomap","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/danhper.png","metadata":{"files":{"readme":"README.md","changelog":"History.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-05-13T08:54:11.000Z","updated_at":"2024-01-15T23:16:55.000Z","dependencies_parsed_at":"2022-08-25T08:00:35.877Z","dependency_job_id":null,"html_url":"https://github.com/danhper/structomap","commit_stats":null,"previous_names":["tuvistavie/serializer","tuvistavie/structomap"],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danhper%2Fstructomap","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danhper%2Fstructomap/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danhper%2Fstructomap/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danhper%2Fstructomap/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/danhper","download_url":"https://codeload.github.com/danhper/structomap/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248657830,"owners_count":21140842,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":["json","serializer"],"created_at":"2024-08-03T19:01:22.313Z","updated_at":"2025-04-13T02:33:20.700Z","avatar_url":"https://github.com/danhper.png","language":"Go","funding_links":[],"categories":["Relational Databases","Serialization"],"sub_categories":["HTTP Clients"],"readme":"# structomap [![Build Status](https://travis-ci.org/danhper/structomap.svg)](https://travis-ci.org/danhper/structomap) [![Coverage Status](https://coveralls.io/repos/danhper/structomap/badge.svg?branch=master)](https://coveralls.io/r/danhper/structomap?branch=master) [![GoDoc](https://godoc.org/github.com/danhper/structomap?status.svg)](https://godoc.org/github.com/danhper/structomap)\n\nThis package helps you to transform your `struct` into `map` easily. It provides a `structomap.Serializer` interface implemented by the `structomap.Base` type which contains chainable function to add, remove or modify fields. The `struct` is transformed to a `map[string]interface{}` using the `Transform(entity interface{})` method.\nIt is then up to you to encode the result in JSON, XML or whatever you like.\n\nHere is an example.\n\n```go\nimport \"github.com/danhper/structomap\"\n\ntype User struct {\n    ID        int\n    Email     string\n    HideEmail bool\n    FirstName string\n    LastName  string\n    CreatedAt time.Time\n    UpdatedAt time.Time\n}\n\ncurrentTime := time.Date(2015, 05, 13, 15, 30, 0, 0, time.UTC)\n\nuser := User{\n    ID: 1, Email: \"x@example.com\", FirstName: \"Foo\", LastName:  \"Bar\",\n    HideEmail: true, CreatedAt: currentTime, UpdatedAt: currentTime,\n}\nuserSerializer := structomap.New().\n              UseSnakeCase().\n              Pick(\"ID\", \"FirstName\", \"LastName\", \"Email\").\n              PickFunc(func(t interface{}) interface{} {\n                  return t.(time.Time).Format(time.RFC3339)\n              }, \"CreatedAt\", \"UpdatedAt\").\n              OmitIf(func(u interface{}) bool {\n                  return u.(User).HideEmail\n              }, \"Email\").\n              Add(\"CurrentTime\", time.Date(2015, 5, 15, 17, 41, 0, 0, time.UTC)).\n              AddFunc(\"FullName\", func(u interface{}) interface{} {\n                  return u.(User).FirstName + \" \" + u.(User).LastName\n              })\n\nuserMap := userSerializer.Transform(user)\nstr, _ := json.MarshalIndent(userMap, \"\", \"  \")\nfmt.Println(string(str))\n```\n\nwill give:\n\n```json\n{\n  \"created_at\": \"2015-05-13T15:30:00Z\",\n  \"current_time\": \"2015-05-15T17:41:00Z\",\n  \"first_name\": \"Foo\",\n  \"full_name\": \"Foo Bar\",\n  \"id\": 1,\n  \"last_name\": \"Bar\",\n  \"updated_at\": \"2015-05-13T15:30:00Z\"\n}\n```\n\n\n## Working with slices and arrays\n\nYou can also use structomap to transform slices and arrays, it will be applied to\nall elements. The only thing to do is to call `TransformArray(entities)` on a slice or an array. As `TransformArray` expects an `interface{}`, but in fact\nreally wants a slice or an array, a second `error` argument is returned. If you do not want it, you can use `MustTransformArray`, which will panic instead of returning an error.\n\nHere in an example reusing the above serializer.\n\n```go\notherUser := User{ID: 2, FirstName: \"Ping\", LastName: \"Pong\", CreatedAt: createdAt, UpdatedAt: createdAt}\nusers := []User{user, otherUser}\nresult, _ := userSerializer.TransformArray(users)\nstr, _ := json.MarshalIndent(result, \"\", \"  \")\nfmt.Println(string(str))\n```\n\nThis will give:\n\n```json\n[\n  {\n    \"created_at\": \"2015-05-13T15:30:00Z\",\n    \"current_time\": \"2015-05-15T17:41:00Z\",\n    \"first_name\": \"Foo\",\n    \"full_name\": \"Foo Bar\",\n    \"id\": 1,\n    \"last_name\": \"Bar\",\n    \"updated_at\": \"2015-05-13T15:30:00Z\"\n  },\n  {\n    \"created_at\": \"2015-05-13T15:30:00Z\",\n    \"current_time\": \"2015-05-15T17:41:00Z\",\n    \"email\": \"\",\n    \"first_name\": \"Ping\",\n    \"full_name\": \"Ping Pong\",\n    \"id\": 2,\n    \"last_name\": \"Pong\",\n    \"updated_at\": \"2015-05-13T15:30:00Z\"\n  }\n]\n```\n\n\n## Choosing a key format\n\nYou can set the key format for the output map using `UseSnakeCase()`, `UsePascalCase()` or `UseCamelCase()` on the serializer object.\nYou can also set the default case for all new serializers by using\n`structomap.SetDefaultCase(structomap.SnakeCase)` (`structomap.CamelCase` and `structomap.PascalCase` are also available). The `init()` function would be a good place to set this.\n\n## Building your own serializer\n\nWith `structomap.Base` as a base, you can easily build your serializer.\n\n```go\ntype UserSerializer struct {\n  *structomap.Base\n}\n\nfunc NewUserSerializer() *UserSerializer {\n  u := \u0026UserSerializer{structomap.New()}\n  u.Pick(\"ID\", \"CreatedAt\", \"UpdatedAt\", \"DeletedAt\")\n  return u\n}\n\nfunc (u *UserSerializer) WithPrivateInfo() *UserSerializer {\n  u.Pick(\"Email\")\n  return u\n}\n\nuserMap := NewUserSerializer().WithPrivateInfo().Transform(user)\n```\n\nNote that the `u.Pick`, and all other methods do modify the serializer, they do not return a new serializer each time. This is why it works\neven when ignoring `u.Pick` return value.\n\n## License\n\nThis is released under the MIT license. See the [LICENSE](./LICENSE) file for more information.\n\n## Godoc\n\nThe full documentation is available at https://godoc.org/github.com/danhper/structomap.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdanhper%2Fstructomap","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdanhper%2Fstructomap","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdanhper%2Fstructomap/lists"}