{"id":13410840,"url":"https://github.com/greencoda/confiq","last_synced_at":"2025-03-14T16:33:04.873Z","repository":{"id":232665488,"uuid":"784878517","full_name":"greencoda/confiq","owner":"greencoda","description":"Structured data format to config struct decoder library for Go","archived":false,"fork":false,"pushed_at":"2024-04-24T14:20:39.000Z","size":191,"stargazers_count":34,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-08-11T09:07:19.277Z","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/greencoda.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":".github/CODEOWNERS","security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-04-10T18:33:18.000Z","updated_at":"2024-06-15T15:50:03.000Z","dependencies_parsed_at":"2024-06-08T15:03:08.747Z","dependency_job_id":null,"html_url":"https://github.com/greencoda/confiq","commit_stats":null,"previous_names":["greencoda/confiq"],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/greencoda%2Fconfiq","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/greencoda%2Fconfiq/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/greencoda%2Fconfiq/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/greencoda%2Fconfiq/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/greencoda","download_url":"https://codeload.github.com/greencoda/confiq/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243610489,"owners_count":20318970,"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-07-30T20:01:09.696Z","updated_at":"2025-03-14T16:33:04.822Z","avatar_url":"https://github.com/greencoda.png","language":"Go","funding_links":[],"categories":["Configuration","配置"],"sub_categories":["Standard CLI","标准CLI"],"readme":"[![godoc for greencoda/confiq][godoc-badge]][godoc-url]\n[![Go 1.22][goversion-badge]][goversion-url]\n[![Build Status][actions-badge]][actions-url]\n[![Go Coverage][gocoverage-badge]][gocoverage-url]\n[![Go Report card][goreportcard-badge]][goreportcard-url]\n[![Mentioned in Awesome Go](https://awesome.re/mentioned-badge.svg)](https://github.com/avelino/awesome-go?tab=readme-ov-file#configuration)  \n\n\u003cp align=\"center\"\u003e\u003cimg src=\".github/splash_image.png\" width=\"500\"\u003e\u003c/p\u003e\n\n# confiq\n\n`confiq` is a Go package for populating structs or arrays from structured data formats such JSON, TOML, YAML or Env, by using struct tags to map the loaded values to fields, using a selector path syntax which can traverse maps by keys and arrays by their indices.\n\nThe data can be loaded either from files, strings, byte arrays, readers and in case of environment variable formats from the actual environment.\n\n## Install\n\n```shell\ngo get -u github.com/greencoda/confiq\n```\n\n## Example\n\nProvide the configuration data in a supported format:\n\n**config.json**\n``` json\n{\n    \"serverHost\": \"http://localhost\",\n    \"port\": 8000,\n    \"settings\": {\n        \"readOnlyMode\": true,\n        \"maxConnections\": 10,\n        \"disallowedUsernames\": [\"badUser1\", \"badUser2\"]\n    },\n    \"apiKeys\": [\n        \"testKey1\",\n        \"testKey2\"\n    ]\n}\n```\n\nCreate a new configSet using `confiq.New` and load the data with the appropriate Load method, depending on how you'd like to provide it.\nIn this example we're loading it from the file system:\n\n``` go\nconfigSet := confiq.New()\n\nif err := configSet.Load(\n    confiqjson.Load().FromFile(\"./config.json\"),\n); err != nil {\n    log.Fatal(err)\n}\n```\n\nDefine the config struct and provide the mappings in its struct tags for each field.\n\nYou may define certain fields to be `required`, or to have a `default` value if it isn't (these are mutually exclusive),\nor mark certain fields as `strict` which would cause the entire decoding process to fail if the value can not be set from the provided config data.\n\n``` go\ntype Config struct {\n\tServerHost          *url.URL `cfg:\"serverHost,required\"`\n\tServerPort          string   `cfg:\"port\"`\n\tAllowedUsernames    []string `cfg:\"settings.allowedUsernames,default=root;admin;developer\"`\n\tDisallowedUsernames []string `cfg:\"settings.disallowedUsernames\"`\n\tReadOnlyMode        bool     `cfg:\"settings.readOnlyMode\"`\n\tMaxConnections      int      `cfg:\"settings.maxConnections\"`\n\tClientID            string   `cfg:\"settings.clientId,default=defaultClient\"`\n\tAPIKey              string   `cfg:\"apiKeys[1]\"`\n}\n\nvar config Config\n```\n\nThen decode the data to this struct from the loaded config data using `Decode`:\n\n``` go\nif err := configSet.Decode(\u0026config); err != nil {\n   // ...\n}\n```\n\nYou may also use `confiq.AsStrict()` option to have all fields act as if they were `strict`:\n\n``` go\nif err := configSet.Decode(\u0026config, confiq.AsStrict()); err != nil {\n   // ...\n}\n```\n\nThe result will be an instance of the struct loaded with data from the specified addresses of the config file:\n```\n(main.Config) {\n ServerHost: (*url.URL)(0xc0000e2090)(http://localhost),\n ServerPort: (string) (len=4) \"8000\",\n AllowedUsernames: ([]string) (len=3 cap=3) {\n  (string) (len=4) \"root\",\n  (string) (len=5) \"admin\",\n  (string) (len=9) \"developer\"\n },\n DisallowedUsernames: ([]string) (len=2 cap=2) {\n  (string) (len=8) \"badUser1\",\n  (string) (len=8) \"badUser2\"\n },\n ReadOnlyMode: (bool) true,\n MaxConnections: (int) 10,\n ClientID: (string) (len=13) \"defaultClient\",\n APIKey: (string) (len=8) \"testKey2\"\n}\n```\n\n## Supported types:\n\n`confiq` supports recursively decoding values into structs with exported fields, maps and slices.\n\n### Structs\n- structs with exported fields of other supported types\n\n### Maps\n- maps with primitive types for keys, and other supported types for values\n\n### Slices\n- slices of other supported types\n- strings will be split up at semicolons, and attempted to be decoded as slices of other supported types\n\n### Primitives\n- string\n- int, int8, int16, int32, int64\n- uint, uint8, uint16, uint32, uint64\n- float32, float64\n- bool\n\n### Other common types\n- json.RawMessage\n- net.IP\n- time.Duration\n- time.Time\n- *url.URL\n\n### Custom types\n- structs implementing the Decoder interface\n- types implementing the encoding.TextUnmarshaler interface\n\n### Pointers\n- pointers to supported types are also supported\n\n## Defining decoders for custom structs\nFor fields with custom struct types, you may implement the Decoder interface by specifying a `Decode` method to your type:\n\n```go\ntype customType struct {\n\tValue string\n}\n\nfunc (t *customType) Decode(value any) error {\n\tif stringValue, ok := value.(string); !ok {\n\t\treturn errors.New(\"value is not a string\")\n\t} else {\n\t\tt.Value = stringValue\n\n\t\treturn nil\n\t}\n}\n```\n\n[godoc-badge]: https://pkg.go.dev/badge/github.com/greencoda/confiq\n[godoc-url]: https://pkg.go.dev/github.com/greencoda/confiq\n[actions-badge]: https://github.com/greencoda/confiq/actions/workflows/test.yml/badge.svg\n[actions-url]: https://github.com/greencoda/confiq/actions/workflows/test.yml\n[goversion-badge]: https://img.shields.io/badge/Go-1.22-%2300ADD8?logo=go\n[goversion-url]: https://golang.org/doc/go1.22\n[goreportcard-badge]: https://goreportcard.com/badge/github.com/greencoda/confiq\n[goreportcard-url]: https://goreportcard.com/report/github.com/greencoda/confiq\n[gocoverage-badge]: https://github.com/greencoda/confiq/wiki/coverage.svg\n[gocoverage-url]: https://raw.githack.com/wiki/greencoda/confiq/coverage.html\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgreencoda%2Fconfiq","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgreencoda%2Fconfiq","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgreencoda%2Fconfiq/lists"}