{"id":20250431,"url":"https://github.com/3timeslazy/anti-dig","last_synced_at":"2025-04-10T22:50:31.309Z","repository":{"id":149994932,"uuid":"622507628","full_name":"3timeslazy/anti-dig","owner":"3timeslazy","description":"🚀 Blazingly fast getting rid of uber-dig in your code","archived":false,"fork":false,"pushed_at":"2024-08-06T21:17:06.000Z","size":2594,"stargazers_count":23,"open_issues_count":0,"forks_count":0,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-03-24T19:45:08.982Z","etag":null,"topics":["anti-di","dependency-injection","go","golang"],"latest_commit_sha":null,"homepage":"","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/3timeslazy.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}},"created_at":"2023-04-02T10:24:32.000Z","updated_at":"2024-09-08T05:22:20.000Z","dependencies_parsed_at":"2023-10-16T10:33:35.253Z","dependency_job_id":"4c24dd36-94a9-4716-ac68-a6af15f0cb53","html_url":"https://github.com/3timeslazy/anti-dig","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/3timeslazy%2Fanti-dig","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/3timeslazy%2Fanti-dig/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/3timeslazy%2Fanti-dig/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/3timeslazy%2Fanti-dig/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/3timeslazy","download_url":"https://codeload.github.com/3timeslazy/anti-dig/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248312168,"owners_count":21082637,"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":["anti-di","dependency-injection","go","golang"],"created_at":"2024-11-14T09:58:16.170Z","updated_at":"2025-04-10T22:50:31.285Z","avatar_url":"https://github.com/3timeslazy.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# :carpentry_saw: anti-dig\n\nAn anti-dependency-injection drop-in replacement toolkit for `go.uber.org/dig`.\n\n## Why?\n\nI have worked in many companies. In every one of them, I've seen someone using `go.uber.org/dig`. And in each case, after a while the team wanted to get rid of it, but it didn't always work because it always took a lot of time and effort. Faced with this problem again, I decided to write a tool to help others get rid of the library.\n\n[Here are the slides](https://docs.google.com/presentation/d/1127nLUsjkI1hLz8KCybCWeYVzfbnS2Te_evwbXXwLmM/edit#slide=id.g2538112cbef_0_0) from the original Berlin Go Meetup, where `anti-dig` was initially presented. In the case Google killed the Slides, there is [a pdf file in the repo](./Getting\u0026#32;rid\u0026#32;of\u0026#32;uber's\u0026#32;dig.pdf).\n\n## Table of Contents\n\n* [Guide](#guide)\n* [Example](#example)\n* [Configuration](#configuration)\n* [Limitations](#limitations)\n\n## Guide\n\nTo use **anti-dig**, follow the steps:\n\n1. Replace `go.uber.org/dig` with `github.com/3timeslazy/anti-dig` everywhere in your code\n\n\n```go\nimport (\n    dig \"github.com/3timeslazy/anti-dig\" // instead of \"go.uber.org/dig\"\n)\n```\n\n2. Download the package by running `go mod tidy`\n\n3. Run your `main()` function. It will generate a new file with explicit initialization of your dependencies\n\n## Example\n\nSuppose we have a `main()` function with several providers passed to dig\n```go\npackage main\n\nimport (\n\t\"github.com/3timeslazy/anti-dig/example/config\"\n\t\"github.com/3timeslazy/anti-dig/example/consumer\"\n\t\"github.com/3timeslazy/anti-dig/example/consumer/queue\"\n\t\"github.com/3timeslazy/anti-dig/example/cron\"\n\t\"github.com/3timeslazy/anti-dig/example/db\"\n\tgrpcserv \"github.com/3timeslazy/anti-dig/example/grpc/server\"\n\t\"github.com/3timeslazy/anti-dig/example/handlers/flatten\"\n\t\"github.com/3timeslazy/anti-dig/example/handlers/handlerv0\"\n\t\"github.com/3timeslazy/anti-dig/example/handlers/handlerv1\"\n\thttpserv \"github.com/3timeslazy/anti-dig/example/http/server\"\n\t\"github.com/3timeslazy/anti-dig/example/observability\"\n\n \tdig \"github.com/3timeslazy/anti-dig\" // \"go.uber.org/dig\"\n)\n\nfunc main() {\n\tcontainer := dig.New()\n\n\terr := container.Provide(observability.NewObservability)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\terr = container.Provide(flatten.NewListOfHandlers)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\terr = container.Provide(handlerv0.NewHandlerV0, dig.Group(\"http_handlers\"))\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\terr = container.Provide(handlerv1.NewHandlerV1)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\terr = container.Provide(cron.NewCron)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\terr = container.Provide(db.NewDB)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\terr = container.Provide(config.NewConfig)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\terr = container.Provide(httpserv.NewServer)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\terr = container.Provide(grpcserv.NewServer)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\terr = container.Provide(consumer.New)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\terr = container.Provide(queue.New1, dig.Name(\"queue_1\"))\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\terr = container.Provide(queue.New2, dig.Name(\"queue_2\"))\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\terr = container.Invoke(Run)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n}\n```\n\nThe code above will generate the following:\n```go\npackage main\n\nimport (\n\t\"github.com/3timeslazy/anti-dig/example/config\"\n\t\"github.com/3timeslazy/anti-dig/example/consumer\"\n\t\"github.com/3timeslazy/anti-dig/example/consumer/queue\"\n\t\"github.com/3timeslazy/anti-dig/example/cron\"\n\t\"github.com/3timeslazy/anti-dig/example/db\"\n\tgrpcserver \"github.com/3timeslazy/anti-dig/example/grpc/server\"\n\t\"github.com/3timeslazy/anti-dig/example/handlers\"\n\t\"github.com/3timeslazy/anti-dig/example/handlers/flatten\"\n\t\"github.com/3timeslazy/anti-dig/example/handlers/handlerv0\"\n\t\"github.com/3timeslazy/anti-dig/example/handlers/handlerv1\"\n\t\"github.com/3timeslazy/anti-dig/example/http/server\"\n\t\"github.com/3timeslazy/anti-dig/example/observability\"\n)\n\nfunc Provide() (cron.Cron, *server.Server, *grpcserver.Server) {\n\tconfig := config.NewConfig()\n\tdb, err := db.NewDB(config)\n\tif err != nil {\n\t\treturn nil, nil, nil\n\t}\n\tqueue1 := queue.New1()\n\tqueue := queue.New2()\n\tconsumerParams := consumer.ConsumerParams{\n\t\tQueue1: queue1,\n\t\tQueue2: queue,\n\t}\n\tconsumer := consumer.New(consumerParams)\n\tcron := cron.NewCron(db, consumer, config)\n\tobservability := observability.NewObservability(config)\n\n\tlistOfHandlers := flatten.NewListOfHandlers(observability.Metrics)\n\n\thandler, err := handlerv0.NewHandlerV0(db)\n\tif err != nil {\n\t\treturn nil, nil, nil\n\t}\n\thttpHandlers := []handlers.Handler{\n\t\thandler,\n\t}\n\thttpHandlers = append(httpHandlers, listOfHandlers.Handlers...)\n\tserverParams := server.ServerParams{\n\t\tConfig:   config,\n\t\tHandlers: httpHandlers,\n\t}\n\tserver := server.NewServer(serverParams)\n\thandlerV1 := handlerv1.NewHandlerV1()\n\n\tgrpcHandlers := []handlers.Handler{\n\t\thandlerV1.Handler,\n\t}\n\tgrpcserverServerParams := grpcserver.ServerParams{\n\t\tHandlers: grpcHandlers,\n\t}\n\tgrpcserverServer := grpcserver.NewServer(grpcserverServerParams)\n\treturn cron, server, grpcserverServer\n}\n```\n\nBefore running the generated code, make sure to perform the following steps:\n1. Fix any error handling in the generated file, if required\n2. Replace the `Invoke(Run)` statement in the original file with `Run(Provide())`\n3. Remove go.uber.org/dig from your project 🥳🥳🥳 \n\nFollowing these steps will allow you to execute the generated code, providing a clear view of your dependencies\n\n## Configuration\n\n**anti-dig** has two configuration options.\n\n### Optimise\n\nThis option is enabled by default. \n\nIf true, then anti-dig will generate code with some 'optimisations' such as human-readable variables, deleted unused variables, etc.\n\n```go\npackage main\n\nimport dig \"github.com/3timeslazy/anti-dig\"\n\nfunc main() {\n\tdig.Anti.Optimise(true)\n}\n```\n\n### Rename\n\nThis option is disabled by default. \n\nIt is often the case that functions passed to dig as a provider are private functions not visible in the main package. In this case, you can enable this parameter. Then anti-dig will not only generate code but also change the names of used functions, making them public and available in the main\n\n```go\npackage main\n\nimport dig \"github.com/3timeslazy/anti-dig\"\n\nfunc main() {\n\tdig.Anti.Rename(true)\n}\n```\n\n## Limitations\n\nWhen creating this library, I focused on the go.uber.org/dig features used in my company. Therefore, at the moment, **not** all features are supported, such as:\n- dig.As\n- Decorators\n- Callbacks\n- Perhaps, something else\n\nAlso, not everything can be easily and correctly generated. For example, anonymous function calls\n\nSo if you are using a feature not supported by anti-dig, feel free to contribute by raising PRs and issues!\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F3timeslazy%2Fanti-dig","html_url":"https://awesome.ecosyste.ms/projects/github.com%2F3timeslazy%2Fanti-dig","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F3timeslazy%2Fanti-dig/lists"}