tinyini

package module
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2023 License: Apache-2.0 Imports: 6 Imported by: 2

README

tinyini

tinyini is a minimalistic library for parsing INI-like configuration files.

Tests Documentation

example configuration file

globalkey = globalvalue

[section]
key = first-value
key = second-value
empty=
anotherkey = "  has whitespace   "

[änöther-section] ; this is a comment and ignored
key = different value

Documentation

Overview

package tinyini provides an extremely bare-bones library for parsing INI-like configuration files. For details, see the documentation for function Parse.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ForEachCallback added in v0.7.0

type ForEachCallback func(section, key, value string) bool

ForEachCallback is the callback function type for iterating over values.

type IniError

type IniError struct {
	Lineno int
	// contains filtered or unexported fields
}

IniError describes a parsing error and provides its line number.

func (*IniError) Error

func (i *IniError) Error() string

func (*IniError) Unwrap

func (i *IniError) Unwrap() error

type Pair added in v0.4.0

type Pair struct {
	Value  string
	Lineno int
}

type Section

type Section map[string][]Pair

Section contains all configuration key-values of a single bracketed section ("[example-section]"). All key-values may contain multiple values. The values are given in the order of occurrence.

type Sections added in v0.6.0

type Sections map[string]Section

Sections is a convenience type over map[string]Section.

Example
package main

import (
	"fmt"
	"os"
	"strconv"
	"strings"

	"github.com/susji/tinyini"
)

func main() {
	config := `
value=123
another_value=" xyz "

[section]
value=321
another_value="abc"
`
	sections, errs := tinyini.Parse(strings.NewReader(config))
	if len(errs) > 0 {
		fmt.Println("parsing errors: ", errs)
		os.Exit(1)
	}

	var value int
	var anotherValue string
	sections.ForEach(func(section, k, v string) bool {
		switch section {
		case "":
			switch k {
			case "value":
				value, _ = strconv.Atoi(v)
			}
		case "section":
			switch k {
			case "another_value":
				anotherValue = v
			}
		}
		return true
	})

	fmt.Printf("value is %d and anotherValue is %s\n", value, anotherValue)
}
Output:

value is 123 and anotherValue is abc

func Parse

func Parse(r io.Reader) (result Sections, errs []error)

Parse will produce a map of Section from an io.Reader which contains key-values and sections in tinyini's liking. Parse returns a slice of errors in the order of occurrence, so the condition for total success is len(errs) == 0.

Parse will parse as much as possible even when encountering errors, so result may contain something useful even if len(errs) > 0.

The global section is given with the empty section name "". Otherwise the section names will be whatever valid UTF-8 is found between the brackets '[' and ']'.

Parse ignores whitespace around section headers, keys, and non-quoted values. If the value should contain whitespace in its beginning or end, enclose the whole value in quotes (" value with whitespaces ").

Quotes may be contained in quoted values by escaping them with the backslash like \". Escaped quotes will be unquoted when parsing, but all other seemingly "escaped" values like \n are ignored and left verbatim.

All keys may contain multiple values. Multiple values will be stored in their order of appearance.

func (Sections) ForEach added in v0.6.0

func (s Sections) ForEach(callback ForEachCallback)

ForEach is a convenience function for simple iteration over Sections. ForEach invokes the callback with Sections and different keys in random order.

If a specific key-value pair has multiple definitions, each different value will be passed on to the callback with separate, sequential invocations and in their order of appearance.

If the callback returns false, iteration is stopped.

Jump to

Keyboard shortcuts

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