{"id":13413438,"url":"https://gitlab.com/cosban/di","last_synced_at":"2025-03-14T19:32:18.854Z","repository":{"id":53845180,"uuid":"18745814","full_name":"cosban/di","owner":"cosban","description":"dependency injection in golang made easy through code generation","archived":false,"fork":false,"pushed_at":null,"size":null,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":null,"default_branch":"master","last_synced_at":"2024-07-31T20:52:20.593Z","etag":null,"topics":["code generation","dependency injection","go"],"latest_commit_sha":null,"homepage":null,"language":null,"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":null,"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}},"created_at":"2020-05-13T01:10:44.085Z","updated_at":"2023-12-16T22:03:36.768Z","dependencies_parsed_at":"2022-08-22T21:11:06.226Z","dependency_job_id":null,"html_url":"https://gitlab.com/cosban/di","commit_stats":null,"previous_names":[],"tags_count":0,"template":null,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/gitlab.com/repositories/cosban%2Fdi","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/gitlab.com/repositories/cosban%2Fdi/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/gitlab.com/repositories/cosban%2Fdi/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/gitlab.com/repositories/cosban%2Fdi/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/gitlab.com/owners/cosban","download_url":"https://gitlab.com/cosban/di/-/archive/master/di-master.zip","host":{"name":"gitlab.com","url":"https://gitlab.com","kind":"gitlab","repositories_count":4515562,"owners_count":6497,"icon_url":"https://github.com/gitlab.png","version":null,"created_at":"2022-05-30T11:31:42.605Z","updated_at":"2024-07-18T11:24:13.055Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/gitlab.com","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/gitlab.com/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/gitlab.com/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/gitlab.com/owners"}},"keywords":["code generation","dependency injection","go"],"created_at":"2024-07-30T20:01:40.387Z","updated_at":"2024-10-26T05:30:54.080Z","avatar_url":null,"language":null,"funding_links":[],"categories":["Microsoft Office","Miscellaneous","杂项"],"sub_categories":["Dependency Injection","依赖注入"],"readme":"# di\n\n\u003e di (dependency-injection) saves you from writing boiler-plate setup code in your main (and elsewhere) function by \n\u003e automating the creation of functions which build the providers you want injected.\n\n## Installation \n\n`go install gitlab.com/cosban/di/v2/cmd/di@latest`\n\n## Usage\n\n1. Create a `provider` file.\n2. Add a `go:generate` comment to provider file like so\n```golang\n//go:generate di generate --source=filename.go --output=filename.gen.go\n```\n\nIf the `output` flag is not specified, the generated code will be printed to stdout.\n\n### What is a provider file?\n\nAt its core, a provider is simply an exported function that delivers a specific output. This output may or may not serve\nas a dependency for other providers.\n\nA provider file serves as a central repository for defining providers and their respective prerequisites. It essentially\nencapsulates functions that won't be explicitly invoked by your code.\n\n**Examples**\nMore detailed examples are located within the [examples/|examples/] directory\n\n```golang\n//go:generate di generate --source=example.go\npackage example\n\nimport \"fmt\"\n\nfunc Path() string {\n    return \"localhost\"\n}\n\nfunc Port() int {\n    return 80\n}\n\nfunc Address(host string, port int) string {\n    return fmt.Sprintf(\"%s:%d\", host, port)\n}\n```\n\nUsing the `go generate` command on `example.go` would produce the following output\n```golang\n// Code generated by di; DO NOT EDIT.\n//\n//\tSource: example.go\n//\tCommand: di generate --source=example.go\n//\tVersion: gitlab.com/cosban/di/v2/cmd/di (devel)\npackage example\n\nvar singleton *ExampleComponent\n\nfunc GetExampleComponent() *ExampleComponent {\n\tif singleton == nil {\n\t\tsingleton = \u0026ExampleComponent{}\n\t}\n\treturn singleton\n}\n\ntype ExampleComponent struct {\n}\n\nfunc (c *ExampleComponent) Path() string {\n\treturn Path()\n}\nfunc (c *ExampleComponent) Port() int {\n\treturn Port()\n}\nfunc (c *ExampleComponent) Address() string {\n\treturn Address(\n\t\tc.Path(),\n\t\tc.Port(),\n\t)\n}\n```\nWhich could then be used in your `main.go` to retrieve a correctly formatted address without having to write the \nboilerplate or determine the order of which providers to create first.\n```golang\npackage main \n\nimport \"example\"\n\nfunc main() {\n    component := example.GetExampleComponent()\n    fmt.Printf(component.Address())\n}\n```\n\n## Troubleshooting Errors\n\n| Type                 | Example Message                                                                                                                     | Mitigation                                                                                                                |\n|----------------------|-------------------------------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------|\n| Missing dependencies | `di error: missing dependency detected: Dependency provider(s) for Foo() Bar are missing with the following identifiers: Baz() Qux` | Create a provider function named `Baz` which returns a `Qux` or rename the dependency to match another provider of `Qux`  |\n| Circular dependency  | `di error: circular dependency detected: Foo() Bar \u003c-\u003e Baz() Qux and Baz() Qux \u003c-\u003e Foo() Bar`                                       | Modify the dependency structure between `Foo` and `Baz` to eliminate any cyclical dependencies.                           |\n| Invalid source       | `di error: unable to parse source`                                                                                                  | Fix your code.                                                                                                            |\n\n## Features\n\n### Singleton Providers\n\nSingleton providers are providers which are only created once and then cached for future use. They're singletons. \n\nTo create a singleton, insert a `// @singleton` comment above the provider function.\n\n```golang\n// @singleton\nfunc Bar() string {\n    return \"bar\"\n}\n```\n\n### Component Embedding\n\nComponents, the generated structures that utilize providers to acquire their dependencies, can be integrated within \nprovider files. This integration enables the reuse and modification of providers not only for testing purposes but also \nto eliminate redundant boilerplate code and facilitate the management of complex configurations.\n\nTo embed a component, insert `// @component` comment above the provider function responsible for returning a \npointer to the component.\n\n```golang\n// @component\nfunc ExampleComponent() *example.Component {\n\treturn example.GetExampleComponent()\n}\n```\n\nKey points to note:\n* All providers from the embedded component will be available to the newly generated component.\n* Provider definitions within components that share identical identifiers (name and return type) with those defined in \nthe provider file will be overridden by the latter.\n* Multiple components may be embedded within the same provider file but errors will occur if two embedded components\nshare providers with identical identifiers if that provider is not overridden by the current provider file.\n* The component generation process supports nested components, allowing the usage of components which themselves have\nembedded other components without any additional configuration.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/gitlab.com%2Fcosban%2Fdi","html_url":"https://awesome.ecosyste.ms/projects/gitlab.com%2Fcosban%2Fdi","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/gitlab.com%2Fcosban%2Fdi/lists"}