config

package
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Apr 7, 2025 License: Apache-2.0 Imports: 11 Imported by: 35

README

go-orb/config

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

It provides 2 main functions and some helpers.

Functions

config.Read

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

This is done over config/source.Plugins, 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/go-orb/config"
    _ "github.com/go-orb/plugins/codecs/json"
    _ "github.com/go-orb/plugins/codecs/yaml"
    _ "github.com/go-orb/plugins/config/source/file"
    _ "github.com/go-orb/plugins/config/source/http"
)

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

    u2, err := url.Parse("https://raw.githubusercontent.com/go-orb/plugins/main/config/tests/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.

The first argument "sections" can have a alpha-numeric entry like:

[]string{"app", "config", "middleware", "5"}

When it finds that "5" it will try to parse "middleware" as slice and parses the 5th entry of that slice into the result.

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)
}

Helpers

config.ParseStruct

ParseStruct is a helper to make any struct with json tags a source.Data (map[string]any{} with some more fields) with sections.

Example:

func main() {
    cfg := log.NewConfig(log.WithLevel(log.LevelTrace), log.WithPlugin("slog"))

    data, err := config.ParseStruct([]string{"com", "example", "app", "registry", "logger"}, &cfg)
    if err != nil {
        return l, nil //nolint:nilerr
    }

    datas := []source.Data{data}
}
config.HasKey

HasKey returns a boolean which indidcates if the given sections and key exists in the configs.

HasKey has also support for alpha-numeric keys, same as Parse.

Example:

func main() {
    test := config.HasKey([]string{"com", "example", "app", "registry", "logger"}, "plugin", configs)
}
config.Dump

Dump is a helper function to dump configDatas to the console.

Authors

License

go-orb is Apache 2.0 licensed and is based on go-micro.

Documentation

Overview

Package config provides config handling for go-micro.

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. Did you import the config plugin?")

	// ErrNoSuchKey happens when a config key is not existent.
	ErrNoSuchKey = 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. Did you register the config source plugin for your scheme?")

	// ErrFileNotFound happens when theres no file.
	ErrFileNotFound = errors.New("file not found")

	// ErrCodecNotFound happens when the required codec is not found.
	ErrCodecNotFound = errors.New("marshaler for codec not found. Did you import the codec plugin for your file type?")
)

Functions

func Dump added in v0.1.0

func Dump(codecMime string, config map[string]any) ([]byte, error)

Dump is a helper function to dump config to []byte.

func HasKey added in v0.1.0

func HasKey[T any](sections []string, key string, config map[string]any) bool

HasKey returns a boolean which indidcates if the given sections and key exists in the configs.

func Merge added in v0.3.0

func Merge[T any](dst *T, src T) error

Merge merges the given source into the destination.

func Parse

func Parse[TMap any](sections []string, key string, config map[string]any, target TMap) error

Parse parses the config from config.Read into the given struct. Param target should be a pointer to the config to parse into.

func ParseSlice added in v0.3.0

func ParseSlice[TSlice any](sections []string, key string, config map[string]any, target TSlice) error

ParseSlice parses the config from config.Read into the given slice. Param target should be a pointer to the slice to parse into.

func ParseStruct added in v0.1.0

func ParseStruct[TParse any](sections []string, toParse TParse) (map[string]any, error)

ParseStruct is a helper to make any struct with `json` tags a map[string]any with sections.

func Read

func Read(url *url.URL) (map[string]any, error)

Read reads url into map[string]any.

func SingleGet added in v0.1.0

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

SingleGet 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]any

func WalkMap added in v0.3.0

func WalkMap(sections []string, in map[string]any) (map[string]any, error)

WalkMap walks into the sections and returns the map[string]any of that section.

If a section is a slice, the next section should be a number.

Example:

config := map[string]any{
    "foo": map[string]any{
        "bar": map[string]any{
            "baz": "value",
        },
    },
}

WalkMap([]string{"foo", "bar"}, config) returns map[string]any{"baz": "value"}.

Types

type Duration added in v0.3.0

type Duration time.Duration

Duration is a time.Duration that can be parsed from a string or float64. https://stackoverflow.com/a/54571600

func (Duration) MarshalJSON added in v0.3.0

func (d Duration) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface.

func (*Duration) UnmarshalJSON added in v0.3.0

func (d *Duration) UnmarshalJSON(b []byte) error

UnmarshalJSON implements the json.Unmarshaler interface.

type URL added in v0.3.0

type URL struct {
	// URL is the underlying URL.
	*url.URL
}

URL represents a URL in JSON.

func NewURL added in v0.3.0

func NewURL(s string) (*URL, error)

NewURL creates a new `json` URL.

func (*URL) Copy added in v0.3.0

func (j *URL) Copy() (*URL, error)

Copy returns a copy of the JURL.

func (*URL) MarshalJSON added in v0.3.0

func (j *URL) MarshalJSON() ([]byte, error)

MarshalJSON marshals the JURL to JSON.

func (*URL) String added in v0.3.0

func (j *URL) String() string

String returns the string representation of the JSONURL.

func (*URL) UnmarshalJSON added in v0.3.0

func (j *URL) UnmarshalJSON(data []byte) error

UnmarshalJSON unmarshals the JURL from JSON.

Directories

Path Synopsis
Package source provides a base for all config sources.
Package source provides a base for all config sources.

Jump to

Keyboard shortcuts

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