{"id":16728343,"url":"https://github.com/tmc/inj","last_synced_at":"2025-04-10T11:07:15.350Z","repository":{"id":13591577,"uuid":"16284374","full_name":"tmc/inj","owner":"tmc","description":"Package inj provides a simple mechanism of dependency injection","archived":false,"fork":false,"pushed_at":"2015-02-05T05:45:51.000Z","size":183,"stargazers_count":29,"open_issues_count":2,"forks_count":0,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-03-24T09:52:48.390Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"godoc.org/github.com/tmc/inj","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"isc","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tmc.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}},"created_at":"2014-01-27T16:14:52.000Z","updated_at":"2017-11-08T23:19:11.000Z","dependencies_parsed_at":"2022-09-10T12:02:07.895Z","dependency_job_id":null,"html_url":"https://github.com/tmc/inj","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tmc%2Finj","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tmc%2Finj/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tmc%2Finj/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tmc%2Finj/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tmc","download_url":"https://codeload.github.com/tmc/inj/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248208041,"owners_count":21065199,"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":[],"created_at":"2024-10-12T23:09:58.582Z","updated_at":"2025-04-10T11:07:15.314Z","avatar_url":"https://github.com/tmc.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# This was a bad idea. Don't do this.\n\n# inj\n    import \"github.com/tmc/inj\"\n\nPackage inj provides a simple mechanism of dependency injection\n\nIt combines a type to value map and the capability of invoking a function with parameters supplied based on their types.\n\n- godoc: http://godoc.org/github.com/tmc/inj\n- Coverage: 100%\n- License: ISC\n\nExample:\n\n```go\n\ti := inj.New()\n\ti.Register(\"foobar\")\n\ti.Register(42)\n\t\n\tvals, _ := i.Call(func(a int, b string) string {\n\t    return fmt.Sprintf(\"%T:%v %T:%v\", a, a, b, b)\n\t})\n\tfmt.Print(vals)\n\t// Output:\n\t// [int:42 string:foobar]\n```\n\n## Variables\n```go\nvar (\n    // ErrNotFunc is returned by Call() if the provided value is not a function\n    ErrNotFunc = errors.New(\"inj: Provided value is not a function\")\n    // ErrNotInterface is returned by RegisterAs() if the second argument is not an interface type\n    ErrNotInterface = errors.New(\"inj: Provided value is not an interface type\")\n    // ErrDoesntImplement is returned by RegisterAs() if the first argument does not implement the second argument\n    ErrDoesntImplement = errors.New(\"inj: Provided value does not satisfy provided interface\")\n)\n```\n\n## type Injector\n```go\ntype Injector map[reflect.Type]reflect.Value\n```\nInjector is the type to value mapping that is utilized when looking up parameters in Call()\n\n### func New\n```go\nfunc New() Injector\n```\nNew prepares a new Injector\n\n### func (Injector) Call\n```go\nfunc (inj Injector) Call(fun interface{}) ([]reflect.Value, error)\n```\nCall invokes fun with parameters populated by registered types\n\n### func (Injector) Register\n``` go\nfunc (inj Injector) Register(value interface{}) (replaced bool)\n```\nRegister provides a new implementation for a provided type\n\nReturns true if this registration is replacing a previous registration\n\n\n### func (Injector) RegisterAs\n``` go\nfunc (inj Injector) RegisterAs(value interface{}, registeredType interface{}) (bool, error)\n```\nRegisterAs provides a new implementation for a provided type but attempts to register it as\nthe interface type registeredType. registeredType must be supplied as a pointer to the interface type.\n\nReturns true if this registration is replacing a previous registration.\nReturns an error if the second argument isn't an interface or the first argument doesn't satisify the second.\n\nExample:\n\n```go\n\ti := inj.New()\n\ti.RegisterAs(os.Stdin, (*io.Reader)(nil))\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftmc%2Finj","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftmc%2Finj","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftmc%2Finj/lists"}