Usage

One or more config sources can be applied. They will be merged into map[string]interface{}, then you could use Scan or Value to get the values.

  1. c := config.New(
  2. config.WithSource(
  3. file.NewSource(path),
  4. ),
  5. config.WithDecoder(func(kv *config.KeyValue, v map[string]interface{}) error {
  6. // kv.Key
  7. // kv.Value
  8. // kv.Metadata
  9. // Configuration center can use the metadata to determine the type of the config.
  10. return yaml.Unmarshal(kv.Value, v)
  11. }),
  12. )
  13. // load config source
  14. if err := c.Load(); err != nil {
  15. panic(err)
  16. }
  17. // get value
  18. name, err := c.Value("service").String()
  19. // parse the values to the struct. (the json tags are required for parsing)
  20. var v struct {
  21. Service string `json:"service"`
  22. Version string `json:"version"`
  23. }
  24. if err := c.Scan(&v); err != nil {
  25. panic(err)
  26. }
  27. // watch the changing of the value
  28. c.Watch("service.name", func(key string, value config.Value) {
  29. // callback of this event
  30. })