config

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 29, 2021 License: MIT Imports: 9 Imported by: 2

Documentation

Overview

Package config is used to read and write configuration files.

Configuration files are similar to Windows INI files. They store a list of properties (key/value pairs); they may also be grouped into sections.

The basic syntax of a configuration file is as follows:

name1=value1
name2=value2
...

[section]
name3=value3
name4=value4
...

Properties not in a section are placed in the unnamed (empty "") section.

Index

Constants

This section is empty.

Variables

View Source
var DefaultDialect = &Dialect{
	AssignChars:    "=:",
	CommentChars:   ";#",
	ReadEmptyKeys:  true,
	WriteEmptyKeys: false,
	Strict:         false,
}

DefaultDialect contains the default configuration dialect. It is compatible with Windows INI files.

Functions

func ReadFunc

func ReadFunc(
	reader io.Reader, fn func(sect, name string, valu interface{})) error

func Write

func Write(writer io.Writer, conf Config) error

Write writes a configuration to the supplied writer using the default dialect.

func WriteTyped

func WriteTyped(writer io.Writer, conf TypedConfig) error

WriteTyped writes a typed configuration to the supplied writer using the default dialect.

Types

type Config

type Config map[string]Section

Config is used to store a configuration as string properties.

When using Get, Set, Delete to manipulate properties the property names follow the syntax SECTION.PROPNAME

func Read

func Read(reader io.Reader) (Config, error)

Read reads a configuration from the supplied reader using the default dialect.

func (Config) Delete

func (conf Config) Delete(k string)

Delete deletes a property from the configuration.

func (Config) Get

func (conf Config) Get(k string) string

Get gets a property from the configuration.

func (Config) Set

func (conf Config) Set(k string, v string)

Set sets a property in the configuration.

type Dialect

type Dialect struct {
	// AssignChars contains the characters used for property assignment.
	// The first character in AssignChars is the character used during
	// writing.
	AssignChars string

	// CommentChars contains the characters used for comments.
	CommentChars string

	// ReadEmptyKeys determines whether to read properties with missing values.
	// The properties so created will be interpretted as empty strings for Read
	// and boolean true for ReadTyped.
	ReadEmptyKeys bool

	// WriteEmptyKeys determines whether to write properties with missing values.
	// This is only important when writing boolean true properties with
	// WriteTyped; these will be written with missing values.
	WriteEmptyKeys bool

	// Strict determines whether parse errors should be reported.
	Strict bool
}

Dialect is used to represent different dialects of configuration files.

func (*Dialect) Read

func (dialect *Dialect) Read(reader io.Reader) (Config, error)

Read reads a configuration from the supplied reader.

func (*Dialect) ReadFunc

func (dialect *Dialect) ReadFunc(
	reader io.Reader, fn func(sect, name string, valu interface{})) error

func (*Dialect) ReadTyped

func (dialect *Dialect) ReadTyped(reader io.Reader) (TypedConfig, error)

ReadTyped reads a typed configuration from the supplied reader.

func (*Dialect) Write

func (dialect *Dialect) Write(writer io.Writer, conf Config) error

Write writes a configuration to the supplied writer.

func (*Dialect) WriteTyped

func (dialect *Dialect) WriteTyped(writer io.Writer, conf TypedConfig) error

WriteTyped writes a typed configuration to the supplied writer.

type Section

type Section map[string]string

Section is used to store a configuration section as string properties.

type TypedConfig

type TypedConfig map[string]TypedSection

TypedConfig is used to store a configuration as typed properties.

When using Get, Set, Delete to manipulate properties the property names follow the syntax SECTION.PROPNAME

func ReadTyped

func ReadTyped(reader io.Reader) (TypedConfig, error)

ReadTyped reads a typed configuration from the supplied reader using the default dialect.

func (TypedConfig) Delete

func (conf TypedConfig) Delete(k string)

Delete deletes a property from the configuration.

func (TypedConfig) Get

func (conf TypedConfig) Get(k string) interface{}

Get gets a property from the configuration.

func (TypedConfig) Set

func (conf TypedConfig) Set(k string, v interface{})

Set sets a property in the configuration.

type TypedSection

type TypedSection map[string]interface{}

TypedSection is used to store a configuration section as typed properties.

Directories

Path Synopsis
Package flag facilitates use of the standard library package flag with package config.
Package flag facilitates use of the standard library package flag with package config.

Jump to

Keyboard shortcuts

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