{"id":21275689,"url":"https://github.com/halimath/appconf","last_synced_at":"2025-03-15T13:13:47.063Z","repository":{"id":57659901,"uuid":"474172531","full_name":"halimath/appconf","owner":"halimath","description":"Opinonated application configuration for golang","archived":false,"fork":false,"pushed_at":"2022-03-27T08:26:26.000Z","size":31,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-22T03:27:31.793Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/halimath.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":"2022-03-25T21:47:01.000Z","updated_at":"2022-03-25T21:47:19.000Z","dependencies_parsed_at":"2022-09-18T04:12:02.234Z","dependency_job_id":null,"html_url":"https://github.com/halimath/appconf","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/halimath%2Fappconf","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/halimath%2Fappconf/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/halimath%2Fappconf/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/halimath%2Fappconf/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/halimath","download_url":"https://codeload.github.com/halimath/appconf/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243732303,"owners_count":20338839,"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-11-21T09:36:09.976Z","updated_at":"2025-03-15T13:13:47.038Z","avatar_url":"https://github.com/halimath.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# appconf\n\n![CI Status][ci-img-url] \n[![Go Report Card][go-report-card-img-url]][go-report-card-url] \n[![Package Doc][package-doc-img-url]][package-doc-url] \n[![Releases][release-img-url]][release-url]\n\nOpinonated configuration loader for golang applications.\n\n# Usage\n\n## Installation\n\nThis module uses golang modules and can be installed with\n\n```shell\ngo get github.com/halimath/appconf@main\n```\n\n## Basic Usage\n\n`appconf` provides a type `AppConfig` which collects configuration input from different _loaders_, merges them\ntogether and provides a type-safe API to query fields from the config. Consider this example:\n\n\n```go\nc, err := appconf.New(\n\tappconf.Static(map[string]interface{}{\n\t\t\"web.address\": \"http://example.com\",\n\t\t\"DB\": map[string]interface{}{\n\t\t\t\"Timeout\": 2 * time.Second,\n\t\t},\n\t}),\n\tappconf.JSONFile(\"./testdata/config.json\", true),\n)\nif err != nil {\n\tpanic(err)\n}\n\nfmt.Println(c.HasKey(\"Web\"))\nfmt.Println(c.HasKey(\"Foo\"))\nfmt.Println(c.GetString(\"Web.Address\"))\nfmt.Println(c.GetDuration(\"db.timeout\"))\n```\n\n### Loaders\n\nThe example uses two loaders to provide input for the configuration: a `Static` loader providing default\nvalues and a `JSONFile` loader overriding some (or all of the values). \n\nThe module contains loaders for the following kind of input:\n* JSON (both from a `Reader` and from a file)\n* YAML (both from a `Reader` and from a file)\n* TOML (both from a `Reader` and from a file)\n* environment variables\n\nYou can create your own loader by implementing the `Loader` interface. See below for details.\n\nOnce the configuration is loaded the individual values can be queried using their _key_.\n\n### Keys\n\nKeys are used to access configuration values. Keys are considered case-insensitive due to the fact that not \nall loaders are able to deliver case-sensitive keys (such as environment variables). Keys can be nested.\nWhen queriying nested keys use a single dot to separate the parts (this is called a _key path_).\n\n### Getters\n\nWhen queriying values you can use different getters to convert the value to a desired type. The following\ngetters are available:\n\n* `GetString`\n* `GetInt`\n* `GetInt64`\n* `GetUint`\n* `GetUint64`\n* `GetFloat32`\n* `GetFloat64`\n* `GetComplex128`\n* `GetBool`\n* `GetDuration`\n\nEach of these getters always returns a valid value. If the key is not defined or if the underlying value can\nnot be converted to the given type, they return the type's default value. There is a corresponding\n`Get...E` version of the getter, which returns an `error` in addition to the value.\n\n### Using sub-configurations\n\nYou can call the configuration's `Sub` method to query a key and return the configuration structure rooted at\nthat key. This is usefull if you want to pass only parts of the configuration to downstream code, such as the\ndatabase configuration rooted at key `db`: Simply call `conf.Sub(\"db\")`.\n\nAs with the getters described above, `Sub` always returns a valid configuration. It is empty when the given\nkey is not found. There is a corresponding `SubE` method, which returns a configuration and an optional\n`error`.\n\n### Binding\n\n`appconf` supports binding configuration to `struct` values. This is done using reflection and it works\nsimilar to unmarshaling of i.e. JSON code:\n\n```go\nc, _ := appconf.Load(...)\n\nvar config ConfigStruct\n\nif err := c.Bind(\u0026config); err != nil {\n\tpanic(err)\n}\n```\n\nThe above code shows how to bind to a `ConfigStruct` value. By default each struct field is assigned the\nvalue of the config value with a key formed by converting the field name to lower case. If you want to bind\na different key, add a field tag of the form \n\n```go\ntype Config struct {\n\tSomeValue string `appconf:\"somekey\"`\n}\n```\n\nIf you want to ignore a struct field during binding add the field tag `appconf:\",ignore\"`. Note the comma \nbefore `ignore` which is important as otherwise the field would be bound to a key named `ignore`.\n\nBindings works with nested structs and nested slices. The keys for slice elements are formed by putting the\nindex as a single key path element, i.e. `db.hosts.0.name`.\n\nYou can also bind the configuration to a `map[string]interface{}`. Keep in mind, that all leaf values are\nadded as `string`s.\n\n### Implementing a custom loader\n\nTo implement a custom configuration loader you create a type which implements the `Loader` interface. This \ninterface contains a single method which loads the configuration and returns it as a `Node` in addition to any\n`error`. `Node`s form a tree with keys annotated to each `Node`. Leaf `Node`s carry the configuration values.\nThis is the internal representation this modules uses to store, merge and query values. Trees of `Node`s can\nbe constructed manually or by using the factory function `ConvertToNode` which accepts a \n`map[string]interface{}`.\n\n# License\n\nCopyright 2022 Alexander Metzner.\n\nLicensed under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License.\nYou may obtain a copy of the License at\n\n[http://www.apache.org/licenses/LICENSE-2.0](http://www.apache.org/licenses/LICENSE-2.0)\n\nUnless required by applicable law or agreed to in writing, software\ndistributed under the License is distributed on an \"AS IS\" BASIS,\nWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\nSee the License for the specific language governing permissions and\nlimitations under the License.\n\n[ci-img-url]: https://github.com/halimath/appconf/workflows/CI/badge.svg\n[go-report-card-img-url]: https://goreportcard.com/badge/github.com/halimath/appconf\n[go-report-card-url]: https://goreportcard.com/report/github.com/halimath/appconf\n[package-doc-img-url]: https://img.shields.io/badge/GoDoc-Reference-blue.svg\n[package-doc-url]: https://pkg.go.dev/github.com/halimath/appconf\n[release-img-url]: https://img.shields.io/github/v/release/halimath/appconf.svg\n[release-url]: https://github.com/halimath/appconf/releases","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhalimath%2Fappconf","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhalimath%2Fappconf","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhalimath%2Fappconf/lists"}