configreader

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 17, 2026 License: MIT Imports: 10 Imported by: 2

README

configreader — layered configuration parser

import "github.com/downsized-devs/sdk-go/configreader"

Stability: Stable — see STABILITY.md

Reads JSON/YAML config files via viper, with JSON-reference resolution and custom time.Duration decoding. Pairs with configbuilder.

Features

  • ReadConfig(target any) — unmarshal into your struct
  • AllSettings() — flat view of every key
  • JSON reference resolution ($ref) for shared fragments
  • Built-in time.Duration and string-to-slice decode hooks

Installation

go get github.com/downsized-devs/sdk-go/configreader

Quick Start

type AppConfig struct {
    Redis struct {
        Address    string
        DefaultTTL time.Duration
    }
    Logger struct{ Level string }
}

r := configreader.Init(configreader.Options{
    ConfigFile: "config/runtime.json",
})
var cfg AppConfig
if err := r.ReadConfig(&cfg); err != nil {
    log.Fatal(err)
}

API Reference

Symbol Signature
Init func Init(opts Options) Interface
Interface.ReadConfig (target any) error
Interface.AllSettings () map[string]any
Options.ConfigFile string — path to file.
Options.Type string (json, yaml, toml…) — defaults inferred from extension.
Options.AdditionalConfigOptions []AdditionalConfigOptions — extra files merged in.

Examples

Layer defaults + environment overrides
r := configreader.Init(configreader.Options{
    ConfigFile: "config/defaults.json",
    AdditionalConfigOptions: []configreader.AdditionalConfigOptions{
        { ConfigFile: fmt.Sprintf("config/%s.json", os.Getenv("ENV")), Type: "json" },
    },
})
Use with Duration fields
type Cfg struct {
    Timeout time.Duration // "5s", "1m30s" both decoded
}

Error Handling

ReadConfig returns descriptive errors for missing files, type mismatches, and unresolved $ref pointers. Don't continue on error — config-load failure should be fatal.

Dependencies

  • Internal: files
  • External: github.com/mitchellh/mapstructure, github.com/spf13/viper

Testing

go test ./configreader/...

Contributing

See CONTRIBUTING.md.

  • configbuilder — write side; produces the files this package reads.
  • files — path-existence checks.

Documentation

Index

Constants

View Source
const (
	JSONType string = "json"
	YAMLType string = "yaml"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type AdditionalConfigOptions

type AdditionalConfigOptions struct {
	// key is the location in the main config to append the additional config
	// with dot delimiter. e.g. 'Parser.ExcelOptions'
	ConfigKey string
	// location of the additional config
	ConfigFile string
}

type Interface

type Interface interface {
	ReadConfig(cfg interface{})
	AllSettings() map[string]interface{}
}

func Init

func Init(opt Options) Interface

type Options

type Options struct {
	// location of the main config file
	ConfigFile string
	// additional configuration to append to the main config
	AdditionalConfig []AdditionalConfigOptions
}

Jump to

Keyboard shortcuts

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