config

package module
v0.0.0-...-e60230f Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 31, 2022 License: Apache-2.0 Imports: 5 Imported by: 1

README

go-orb/config

Package go-orb/config is a pluggable config provider for loosely coupled components.

It provides 2 main functions

config.Read

Read reads urls into []source.Data where Data is basicaly map[string]any.

This is done over config/source.Plugins and currently there are 3 Plugins for config.source:

  • cli provides config from cli/env sources.
  • file provides config from file sources.
  • http provides config from http sources.

It's straight forward to write Plugins for config.source and we will provide more:

  • nats
  • etcd
  • consul

An example:

package main

import (
    "log"
    "net/url"

    "github.com/go-orb/config"
    _ "github.com/go-orb/config-plugins/marshaler/json"
    _ "github.com/go-orb/config-plugins/marshaler/yaml"
    _ "github.com/go-orb/config-plugins/source/file"
    _ "github.com/go-orb/config-plugins/source/http"
)

func main() {
    // https://raw.githubusercontent.com/go-orb/config-plugins/main/test/data/set1/registry1.yaml
    u1, err := url.Parse("./data/set1/registry1")
    if err != nil {
        log.Fatal(err)
    }

    u2, err := url.Parse("https://raw.githubusercontent.com/go-orb/config-plugins/main/test/data/set1/registry2.json")
    if err != nil {
        log.Fatal(err)
    }

    datas, err := config.Read([]*url.URL{u1, u2}, []string{"app"})
    if err != nil {
        log.Fatal(err)
    }
}

config.Parse

Parse parses the config from config.Read into the given struct.

Example:

    // extend the config.Read example here

    //
    // All from here is in the plugin itself.
    //
    cfg := newRegistryMdnsConfig()
    err := config.Parse([]string{"app", "registry"}, datas, cfg)
    if err != nil {
        log.Fatal(err)
    }

Authors

License

Orb is Apache 2.0 licensed.

Documentation

Overview

Package config provides config handling for orb.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrUnknownPlugin happens when there's no config factory for the given plugin.
	ErrUnknownPlugin = errors.New("unknown config given")

	// ErrNotExistent happens when a config key is not existent.
	ErrNotExistent = errors.New("no such config key")

	// ErrTypesDontMatch happens when types don't match during Get[T]().
	ErrTypesDontMatch = errors.New("config key requested type and actual type don't match")

	// ErrUnknownScheme happens when you didn't import the plugin for the scheme or the scheme is unknown.
	ErrUnknownScheme = errors.New("unknown config source scheme")

	// ErrNoSuchFile happens when theres no file.
	ErrNoSuchFile = errors.New("no such file or no marshaler found")
)

Functions

func Get

func Get[T any](data map[string]any, key string, def T) (T, error)

Get returns either the value of "key" in "data" or the default value "def". * If types don't match it returns ErrTypesDontMatch. * If key hasn't been found it returns ErrNotExistent as well as the default value "def". * * It supports the following datatypes: * - any non-container (string/float64/uvm.) * - []string slice * - []any slice * - map[string]string * - map[string]any * .

func Parse

func Parse(sections []string, configs []source.Data, target any) error

Parse parses the config from config.Read into the given struct.

func Read

func Read(urls []*url.URL, prependSections []string) ([]source.Data, error)

Read reads urls into []Data where Data is basically map[string]any.

By default it will error out if any of these config URL's fail, but you can ignore errors for a single url by adding "?ignore_error=true".

prependSections is for url's that don't support sections (cli for example), theier result will be prepended, also you can add sections to a single url with "?add_section=true".

Types

This section is empty.

Directories

Path Synopsis
Package marshaler contains a basic interface for marshalers.
Package marshaler contains a basic interface for marshalers.
Package source is a base for all config sources.
Package source is a base for all config sources.
cli
Package cli is the Cli component of orb.
Package cli is the Cli component of orb.
util
container
Package container contains generic containers.
Package container contains generic containers.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL