configobj

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Dec 15, 2025 License: MIT Imports: 13 Imported by: 0

README

configobj (Go)

Go port of Python ConfigObj (5.x): reads/writes ini-like configs with nested sections, preserved comments, list values, unrepr, and interpolation.

Install

go get github.com/svanichkin/configobj

Quick start

package main

import (
	"os"

	"github.com/svanichkin/configobj"
)

func main() {
	cfg, err := configobj.Load("reticulum.conf")
	if err != nil {
		panic(err)
	}

	root := cfg.Section("root")
	path, _ := root.Get("path")
	_ = path

	_, _ = cfg.WriteTo(os.Stdout)
}

Tests

  • go test ./...
  • Optional parity oracle tests require python3 (scripts live under testdata/oracle/).

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetExtraValues

func GetExtraValues(conf *Section) [][2]any

GetExtraValues mirrors configobj.get_extra_values after validation.

Types

type Config

type Config struct {
	BOM bool
	// contains filtered or unexported fields
}

func CloneConfig

func CloneConfig(src *Config) (*Config, error)

func CloneConfigWithOptions

func CloneConfigWithOptions(src *Config, opts Options) (*Config, error)

func Load

func Load(path string) (*Config, error)

func LoadFromMap

func LoadFromMap(data map[string]any) (*Config, error)

func LoadFromMapWithOptions

func LoadFromMapWithOptions(data map[string]any, opts Options) (*Config, error)

func LoadLines

func LoadLines(lines []string) (*Config, error)

func LoadLinesWithOptions

func LoadLinesWithOptions(lines []string, opts Options) (*Config, error)

func LoadReader

func LoadReader(r io.Reader) (*Config, error)

func LoadReaderWithOptions

func LoadReaderWithOptions(r io.Reader, opts Options) (*Config, error)

func LoadWithOptions

func LoadWithOptions(path string, opts Options) (*Config, error)

func (*Config) HasSection

func (c *Config) HasSection(name string) bool

func (*Config) Reload

func (c *Config) Reload() error

func (*Config) Reset

func (c *Config) Reset()

func (*Config) Save

func (c *Config) Save(path string) error

func (*Config) Section

func (c *Config) Section(name string) *Section

func (*Config) Sections

func (c *Config) Sections() []string

func (*Config) SetConfigspec

func (c *Config) SetConfigspec(spec *Config)

func (*Config) Validate

func (c *Config) Validate(v Validator, opts ...ValidateOptions) (any, error)

Validate checks configspec presence, using a minimal Validator interface. It returns: - true if everything passes - false if everything fails (only possible when missing-only failures) - map[string]any mirroring ConfigObj's nested result dict

func (*Config) Write

func (c *Config) Write() ([]string, error)

func (*Config) WriteTo

func (c *Config) WriteTo(w io.Writer) (int64, error)

type ConfigErrorDetail

type ConfigErrorDetail struct {
	Type       string
	LineNumber int
	Line       string
	Message    string
}

func (ConfigErrorDetail) Error

func (d ConfigErrorDetail) Error() string

type FlatError

type FlatError struct {
	Sections []string
	Key      *string
	Result   any
}

FlattenErrors mirrors configobj.flatten_errors for results of Validate(). Each item is: (sectionsPath, keyOrNil, result).

func FlattenErrors

func FlattenErrors(cfg *Config, res any) []FlatError

type InterpolationError

type InterpolationError struct {
	// contains filtered or unexported fields
}

func (*InterpolationError) Error

func (e *InterpolationError) Error() string

type InterpolationLoopError

type InterpolationLoopError struct {
	InterpolationError
	// contains filtered or unexported fields
}

type MissingInterpolationOption

type MissingInterpolationOption struct {
	InterpolationError
	// contains filtered or unexported fields
}

type Options

type Options struct {
	Interpolation     *bool
	InterpolationName string
	RaiseErrors       *bool
	ListValues        *bool
	Stringify         *bool
	Unrepr            *bool
	WriteEmptyValues  *bool
	CreateEmpty       *bool
	FileError         *bool
	IndentType        *string
	Encoding          *string
	DefaultEncoding   *string
	Configspec        *Config
}

type ParseErrorsError

type ParseErrorsError struct {
	Errors []ConfigErrorDetail
}

func (*ParseErrorsError) Error

func (e *ParseErrorsError) Error() string

type ReloadError

type ReloadError struct{}

func (ReloadError) Error

func (ReloadError) Error() string

type Section

type Section struct {
	// contains filtered or unexported fields
}

func (*Section) AsBool

func (s *Section) AsBool(key string) (bool, error)

func (*Section) AsFloat

func (s *Section) AsFloat(key string) (float64, error)

func (*Section) AsInt

func (s *Section) AsInt(key string) (int, error)

func (*Section) AsList

func (s *Section) AsList(key string) []string

func (*Section) Created

func (s *Section) Created() bool

func (*Section) DefaultValuesMap

func (s *Section) DefaultValuesMap() map[string]any

func (*Section) DefaultsList

func (s *Section) DefaultsList() []string

func (*Section) Delete

func (s *Section) Delete(key string) bool

func (*Section) Dict

func (s *Section) Dict() map[string]any

func (*Section) ExtraValuesList

func (s *Section) ExtraValuesList() []string

func (*Section) Get

func (s *Section) Get(key string) (string, bool)

Get behaves like ConfigObj's __getitem__: it interpolates if enabled.

func (*Section) GetAny

func (s *Section) GetAny(key string) (any, bool)

func (*Section) GetRaw

func (s *Section) GetRaw(key string) (string, bool)

GetRaw behaves like ConfigObj's get(): it bypasses interpolation.

func (*Section) GetValue

func (s *Section) GetValue(key string) (any, bool)

GetValue mirrors Python's __getitem__, returning strings/lists with interpolation.

func (*Section) HasKey

func (s *Section) HasKey(key string) bool

func (*Section) Keys

func (s *Section) Keys() []string

func (*Section) Merge

func (s *Section) Merge(indict map[string]any)

func (*Section) Rename

func (s *Section) Rename(oldkey, newkey string) error

func (*Section) RestoreDefault

func (s *Section) RestoreDefault(key string) (any, error)

func (*Section) RestoreDefaults

func (s *Section) RestoreDefaults()

func (*Section) ScalarsList

func (s *Section) ScalarsList() []string

func (*Section) Sections

func (s *Section) Sections() []string

func (*Section) Set

func (s *Section) Set(key, value string)

func (*Section) SetAny

func (s *Section) SetAny(key string, value any) error

SetAny mirrors Python's __setitem__: accepts scalars, lists, maps, and respects stringify rules.

func (*Section) Subsection

func (s *Section) Subsection(name string) *Section

func (*Section) Walk

func (s *Section) Walk(fn func(*Section, string) (any, error), callOnSections, raiseErrors bool) (map[string]any, error)

type SimpleVal

type SimpleVal struct{}

SimpleVal mirrors configobj.SimpleVal: presence-only validator.

func (SimpleVal) Check

func (SimpleVal) Check(_ string, _ any, missing bool) (any, error)

func (SimpleVal) GetDefaultValue

func (SimpleVal) GetDefaultValue(_ string) (any, error)

func (SimpleVal) IsMissingValue

func (SimpleVal) IsMissingValue(_ error) bool

type ValidateOptions

type ValidateOptions struct {
	PreserveErrors bool
	Copy           bool
	Section        *Section
}

type Validator

type Validator interface {
	Check(spec string, member any, missing bool) (any, error)
}

type Value

type Value struct {
	// contains filtered or unexported fields
}

Jump to

Keyboard shortcuts

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