{"id":36583716,"url":"https://github.com/doraemonkeys/brick","last_synced_at":"2026-01-12T08:00:06.741Z","repository":{"id":274266374,"uuid":"922404599","full_name":"doraemonkeys/brick","owner":"doraemonkeys","description":"A Lightweight, Non-Intrusive Component-Based Configuration and Dependency Injection Library","archived":false,"fork":false,"pushed_at":"2025-02-14T06:19:13.000Z","size":72,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-14T07:25:57.907Z","etag":null,"topics":[],"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/doraemonkeys.png","metadata":{"files":{"readme":"README-ZH.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":"2025-01-26T05:21:52.000Z","updated_at":"2025-02-14T06:19:16.000Z","dependencies_parsed_at":"2025-02-14T07:23:28.513Z","dependency_job_id":"ff177374-f72e-4db6-a365-a69e17c3a1e6","html_url":"https://github.com/doraemonkeys/brick","commit_stats":null,"previous_names":["doraemonkeys/brick"],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/doraemonkeys/brick","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/doraemonkeys%2Fbrick","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/doraemonkeys%2Fbrick/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/doraemonkeys%2Fbrick/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/doraemonkeys%2Fbrick/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/doraemonkeys","download_url":"https://codeload.github.com/doraemonkeys/brick/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/doraemonkeys%2Fbrick/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28336957,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-12T06:09:07.588Z","status":"ssl_error","status_checked_at":"2026-01-12T06:05:18.301Z","response_time":98,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: 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":[],"created_at":"2026-01-12T08:00:05.894Z","updated_at":"2026-01-12T08:00:06.704Z","avatar_url":"https://github.com/doraemonkeys.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003ch3 align=\"center\"\u003e 中文 | \u003ca href='README.md'\u003eEnglish\u003c/a\u003e\u003c/h3\u003e\n\n\n\n# brick\n\n[![Go Reference](https://pkg.go.dev/badge/github.com/doraemonkeys/brick.svg)](https://pkg.go.dev/github.com/doraemonkeys/brick) [![Go Report Card](https://goreportcard.com/badge/github.com/doraemonkeys/brick)](https://goreportcard.com/report/github.com/doraemonkeys/brick) [![Coverage Status](https://coveralls.io/repos/github/doraemonkeys/brick/badge.svg)](https://coveralls.io/github/doraemonkeys/brick)\n\n\nbrick是一个轻量级非侵入式的组件化的配置管理与依赖注入库。轻量意味着Brick库实现逻辑简单使用方便，非侵入式意味着Brick并非是一个框架而是一个库，你可以不修改现有代码的逻辑而轻松使用Brick提供的依赖注入与配置管理功能。\n\n\n\nbrick库并非最大限度的支持默认行为，相反，为了弥补使用反射给程序员带来的不确定性，它尽可能的对一切异常注入行为进行检查，并在启动时抛出panic以便错误得到及时修正。所以你可以安心使用brick库，不必总是小心翼翼。\n\n\n\n## Installation\n\n```bash\ngo get github.com/doraemonkeys/brick\n```\n\u003e Require Go 1.23 or higher\n\n\n\n## 快速开始\n\n### 创建一个brick组件\n\n任意一个实现了 `Brick` 接口的类型都被视为一个brick组件，你只需要实现  `BrickTypeID` 方法，该方法返回一个全局唯一的常量字符串作为brick组件的类型ID。\n\n```go\ntype Brick1 struct {}\n\nfunc (l *Brick1) BrickTypeID() string {\n    return \"EG7LGZ4SX2O9L7ZZHS64\"\n}\n```\n\n### 在brick组件中注入其他brick组件\n\n你只需要在依赖的brick组件的tag中添加 `brick` 标签，brick库会自动帮你注入。\n\n```go\ntype Brick2 struct {\n    Brick1 *Brick1 `brick:\"\"`\n}\n\nfunc (l *Brick2) BrickTypeID() string {\n    return \"QQZWZH166CJIKAAAFRHV\"\n}\n```\n\n让我们来解释一下这个tag的含义，`brick` 表示brick库会自动帮你注入这个依赖，后面的字符串表示即将注入组件的实例ID(`liveID`)，`\"\"`空字符串表示这里会注入此组件的**默认liveID(默认实例)**，即 Brick1 的 TypeID。\n\n\n\n也就是等价于以下代码：\n\n```go\ntype Brick2 struct {\n    Brick1 *Brick1 `brick:\"EG7LGZ4SX2O9L7ZZHS64\"`\n}\n```\n\n当然你也完全可以指定其他全局唯一的liveID。\n\n### 解析 brick 实例的配置文件\n\n如果你依赖的brick组件需要读取外部配置文件，你只需要实现 `BrickNewer` 接口，此接口的 `NewBrick` 方法需要解析传入的配置文件的json字符串，然后返回一个新的 brick 实例即可。\n\n```go\ntype Brick2 struct {\n    ConfigString string `json:\"configString\" yaml:\"configString\" toml:\"configString\"`\n    Brick1 *Brick1 `brick:\"\"`\n}\n\nfunc (l *Brick2) NewBrick(jsonConfig []byte) brick.Brick {\n    var newBrick = \u0026Brick2{}\n    _ = json.Unmarshal(jsonConfig, newBrick)\n    return newBrick\n}\n```\n\n通过配置字段的tag可以看出，配置文件支持 json、yaml、toml 这些主流格式。\n\n\n\n配置文件示例:\n\n```json\n[\n    {\n        \"metaData\": {\n            \"name\": \"Brick2 Instances\",\n            \"typeID\": \"QQZWZH166CJIKAAAFRHV\"\n        },\n        \"lives\": [\n            {\n                \"liveID\": \"QQZWZH166CJIKAAAFRHV\",\n                \"config\": {\n                    \"configString\": \"your config string\"\n                }\n            }\n        ]\n    }\n]\n```\n\n当然你必须得告诉brick库你的配置文件在哪里，例如：\n\n```go\nerr := brick.AddConfigFile(\"config.json\")\n```\n\n### 注册brick组件\n\n你需要调用 `Register` 方法注册你顶部的brick组件，其他依赖的brick组件会自动扫描注册。\n\n```go\nbrick.RegisterNewer[*Brick2]()\n```\n\n\n### 获取brick组件实例\n\n你可以通过调用 `Get` 或 `GetOrCreate` 方法获取brick组件实例。\n\n```go\nbrick1 := brick.Get[*Brick1]()\n```\n\n\n### 示例代码\n\n```go\npackage main\n\nimport (\n\t\"encoding/json\"\n\t\"fmt\"\n\t\"os\"\n\n\t\"github.com/doraemonkeys/brick\"\n)\n\ntype Brick1 struct{}\n\nfunc (l *Brick1) BrickTypeID() string {\n\treturn \"EG7LGZ4SX2O9L7ZZHS64\"\n}\n\nfunc (l *Brick1) SayHello() string {\n\treturn \"Hello, Brick1\"\n}\n\ntype Brick2 struct {\n\tConfigString string  `json:\"configString\" yaml:\"configString\" toml:\"configString\"`\n\tBrick1       *Brick1 `brick:\"\"`\n}\n\nfunc (l *Brick2) BrickTypeID() string {\n\treturn \"QQZWZH166CJIKAAAFRHV\"\n}\n\nfunc (l *Brick2) NewBrick(jsonConfig []byte) brick.Brick {\n\tvar newBrick = \u0026Brick2{}\n\tif len(jsonConfig) \u003e 0 {\n\t\tif err := json.Unmarshal(jsonConfig, newBrick); err != nil {\n\t\t\tpanic(fmt.Errorf(\"Brick2 config error: %w\", err))\n\t\t}\n\t}\n\treturn newBrick\n}\n\nfunc (l *Brick2) SayHello() {\n\tfmt.Println(\"Hello, Brick2\")\n\tfmt.Println(l.Brick1.SayHello())\n\tfmt.Printf(\"Brick2 config: %v\", l.ConfigString)\n}\n\nconst JsonConfig = `\n[\n    {\n        \"metaData\": {\n            \"name\": \"Brick2 Instances\",\n            \"typeID\": \"QQZWZH166CJIKAAAFRHV\"\n        },\n        \"lives\": [\n            {\n                \"liveID\": \"QQZWZH166CJIKAAAFRHV\",\n                \"config\": {\n                    \"configString\": \"your config string\"\n                }\n            }\n        ]\n    }\n]\n`\n\nfunc init() {\n\tbrick.RegisterNewer[*Brick2]()\n\n\t_ = os.WriteFile(\"config.json\", []byte(JsonConfig), 0644)\n\terr := brick.AddConfigFile(\"config.json\")\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n}\n\nfunc main() {\n\tbrick2 := brick.Get[*Brick2]()\n\tbrick2.SayHello()\n}\n```\n\n\n\n### Thanks\n\nThis library draws inspiration from [dot](https://github.com/scryinfo/dot).\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdoraemonkeys%2Fbrick","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdoraemonkeys%2Fbrick","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdoraemonkeys%2Fbrick/lists"}