ini

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: May 14, 2025 License: Apache-2.0 Imports: 8 Imported by: 0

README

ini

General ini parser

Documentation

Overview

Package ini implements a generic, simple ini file parser.

Syntax

An ini file is line oriented. It has a number of sections, each starting with a [section-name] header. Within each section is a sequence of field settings, each on the form name=value. Blank lines are ignored. Lines whose first nonblank is CommentChar (default '#') are ignored. There can be blanks at the beginning and end of all lines and on either side of the '=', and inside the brackets of the header. Section and field names must conform to [-a-zA-Z0-9_$]+, and are case-sensitive.

The fields are typed, the value must conform to the type, though blank values are accepted for strings (empty string) and booleans (true). All values can be quoted with matching quotes according to QuoteChar (default '"'), the quotes are stripped. Set QuoteChar to ' ' to disable all quote stripping. Leading and trailing blanks of the value (outside any quotes) are always stripped.

Usage

Create an ini parser with NewParser and customize any variables. Then add a new Section to it with Parser.AddSection. Add a new Field to the section with Section.Add<Type> for pre-defined types, eg Section.AddString, or the general Section.Add for user-defined types or non-standard default values or parsing.

Parse an input stream with Parser.Parse. This will return a Store (or an error). Access field values via the Field objects on the Store, or directly on the Store itself.

Errors

Errors during creation of the parser are considered programming errors and uniformly result in a panic. Errors during parsing are considered input errors and are surfaced as an error return from Parser.Parse.

Example
package main

import (
	"fmt"
	"strings"

	"github.com/lars-t-hansen/ini"
)

func main() {
	p := ini.NewParser()
	p.CommentChar = ';'

	sGlobal := p.AddSection("global")
	globalVerbose := sGlobal.AddBool("verbose")

	sUser := p.AddSection("user")
	userName := sUser.AddString("name")
	userLevel := sUser.AddUint64("level")

	store, err := p.Parse(strings.NewReader(`
; hi there
[global]
verbose = true

[user]
 name=Frank
level= 37
`))
	if err != nil {
		panic(err)
	}
	fmt.Printf("global.verbose = %v\n", globalVerbose.BoolVal(store))
	fmt.Printf("user.name = %s\n", userName.StringVal(store))
	fmt.Printf("user.level = %d\n", userLevel.Uint64Val(store))
}
Output:

global.verbose = true
user.name = Frank
user.level = 37

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ParseBool added in v0.2.0

func ParseBool(s string) (any, bool)

ParseBool accepts any string representing a bool value, returning the value and a validity flag. "true" and the empty string are true values, "false" is the false value.

func ParseFloat64 added in v0.2.0

func ParseFloat64(s string) (any, bool)

ParseFloat64 accepts any string representing a signed, decimal floating-point value in the range of float64, returning the value and a validity flag.

func ParseInt64 added in v0.2.0

func ParseInt64(s string) (any, bool)

ParseInt64 accepts any string representing a signed, decimal integer in the range of int64, returning the value and a validity flag.

func ParseString added in v0.2.0

func ParseString(s string) (any, bool)

ParseString accepts any string value, returning its input and true.

func ParseUint64 added in v0.2.0

func ParseUint64(s string) (any, bool)

ParseUint64 accepts any string representing an unsigned, decimal integer in the range of uint64, returning the value and a validity flag.

Types

type Field

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

A field represents a field within a Section and is also an accessor for the parsed value of that field within a Store.

func (*Field) BoolVal

func (field *Field) BoolVal(store *Store) bool

BoolVal returns a boolean field's value in the input, or the default if the field was not present.

func (*Field) Float64Val

func (field *Field) Float64Val(store *Store) float64

Float64Val returns a float64 field's value in the input, or the default if the field was not present.

func (*Field) Int64Val

func (field *Field) Int64Val(store *Store) int64

Int64Val returns an int64 field's value in the input, or the default if the field was not present.

func (*Field) Name

func (field *Field) Name() string

Name returns the field's name.

func (*Field) Present

func (field *Field) Present(store *Store) bool

Present returns true if the field was present in the input.

func (*Field) StringVal

func (field *Field) StringVal(store *Store) string

StringVal returns a string field's value in the input, or the default if the field was not present.

func (*Field) Type

func (field *Field) Type() FieldTy

Type returns the field's type tag.

func (*Field) Uint64Val

func (field *Field) Uint64Val(store *Store) uint64

Uint64Val returns an uint64 field's value in the input, or the default if the field was not present.

func (*Field) Value

func (field *Field) Value(store *Store) any

Value returns field's value in the input as an any, or the default value if the field was not present.

type FieldTy

type FieldTy int

A FieldTy describes the type of the field.

const (
	TyString  FieldTy = iota + 1 // The field is a string
	TyBool                       // The field is a bool
	TyInt64                      // The field is an int64
	TyUint64                     // The field is an uint64
	TyFloat64                    // The field is a float64
	TyUser                       // The field is a user-defined type (for this and higher FieldTy values)
)

type ParseError added in v0.2.0

type ParseError struct {
	Line     int    // The line number in the input where the error was discovered
	Section  string // The section name context, if not ""
	Irritant string // Informative text and context
}

A ParseError describes an error encountered during parsing with its location and nature.

func (*ParseError) Error added in v0.2.0

func (pe *ParseError) Error() string

type Parser

type Parser struct {
	// CommentChar is the character that starts line comments: lines whose first nonblank matches
	// CommentChar are stripped from the input.
	CommentChar rune

	// QuoteChar is the character that is used for quoting values: values whose first and last
	// nonblank match QuoteChar are stripped of those chars (both must be present for stripping to
	// happen).  Set to ' ' to disable quote stripping.
	QuoteChar rune
	// contains filtered or unexported fields
}

A Parser holds the structure of the ini file and its parsing options, and performs parsing.

func NewParser

func NewParser() *Parser

Make a new, empty parser with default settings.

func (*Parser) AddSection

func (parser *Parser) AddSection(name string) *Section

AddSection adds a new ini section with the given name to the parser. A section of that name must not be present in the section already, and the name must be syntactically valid (see the package documentation).

func (*Parser) Parse

func (parser *Parser) Parse(r io.Reader) (*Store, error)

Parse parses the input from the reader, returning a Store with information about field presence and values. Errors in field parsing result in a *ParseError being returned with no store. Concurrent parsing is safe, but no sections or fields may be added while parsing is happening in any goroutine.

func (*Parser) Section

func (parser *Parser) Section(name string) *Section

Section looks up the section by name and returns it if found, otherwise return nil.

type Section

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

A Section is a named container for a set of fields.

func (*Section) Add

func (section *Section) Add(name string, ty FieldTy, defaultValue any, valid func(s string) (any, bool)) *Field

Add adds a field of the given name to the section. The name must not be present in the section and must be syntactically valid (see package comments). The defaultValue will be used if the field is not present in the input. The ty can be a pre-defined type tag if that is the representation of the value, or it must be >= TyUser to indicate something non-standard. The valid function will take a string and return a parsed value and true if the value is good, otherwise an arbitrary value and false.

The defaultValue and the value returned by valid must be of the same type, and if a pre-defined type tag is used they must both be of the corresponding type. (A common error is to pass eg 1 rather than uint64(1) as a defaultValue with TyUint64 for ty.)

func (*Section) AddBool

func (section *Section) AddBool(name string) *Field

AddBool adds a new boolean field of the given name to the section. The name must not be present in the section and must be syntactically valid (see package comments). ParseBool describes the accepted values. The default value is false.

func (*Section) AddFloat64

func (section *Section) AddFloat64(name string) *Field

AddFloat64 adds a new float64 field of the given name to the section. The name must not be present in the section and must be syntactically valid (see package comments). ParseFloat64 describes the accepted values. The default value is zero.

func (*Section) AddInt64

func (section *Section) AddInt64(name string) *Field

AddInt64 adds a new int64 field of the given name to the section. The name must not be present in the section and must be syntactically valid (see package comments). ParseInt64 describes the accepted values. The default value is zero.

func (*Section) AddString

func (section *Section) AddString(name string) *Field

AddString adds a new string field of the given name to the section. The name must not be present in the section and must be syntactically valid (see package comments). ParseString describes the accepted values. The default value is the empty string.

func (*Section) AddUint64

func (section *Section) AddUint64(name string) *Field

AddUint64 adds a new uint64 field of the given name to the section. The name must not be present in the section and must be syntactically valid (see package comments). ParseUint64 describes the accepted values. The default value is zero.

func (*Section) Field

func (section *Section) Field(name string) *Field

Field returns the field of the given name from the section, or nil if there is no such field.

func (*Section) Name added in v0.2.0

func (section *Section) Name() string

Name returns the name of the section.

func (*Section) Present

func (section *Section) Present(store *Store) bool

Present returns true if the section was present in the input (even if it contained no settings).

type Store

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

A Store holds the result of a successful parse. It is passed as an argument to methods on individual Fields to retrieve those fields' values.

Jump to

Keyboard shortcuts

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