go_ini

package module
v0.0.0-...-04b5151 Latest Latest
Warning

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

Go to latest
Published: Dec 16, 2020 License: MIT Imports: 10 Imported by: 0

README

go-ini

ini file parser for golang.

Example

package main

import "ioutils"
import parser "github.com/Luncert/go-ini"

confFile, _ := ioutils.ReadFile("./test.ini")
cfg := parser.ParseIni(string(confFile))
addr := cfg.Section("server").Variable("address").String()
port := cfg.Section("server").Variable("port").Int()
fmt.Printf("server listen on %s:%d", addr, port)

Documentation

Index

Examples

Constants

View Source
const (
	BoolType = iota
	StringType
	IntegerType
	DecimalType
	ListType
)

Variables

This section is empty.

Functions

func DuplicatedSectionNameError

func DuplicatedSectionNameError(sectionName string) error

func DuplicatedVariableNameError

func DuplicatedVariableNameError(variableName, sectionName string) error

func Marshal

func Marshal(cfg *Config) string
Example
cfg := NewConfig()
cfg.AddSection(
	NewSection("a").
		AddVariable(NewVariable("name", "Joy")).
		AddVariable(NewVariable("age", 18))).
	AddSection(
		NewSection("b").
			AddVariable(NewVariable("single", false)).
			AddVariable(NewVariable("money", 813.01))).
	AddSection(
		NewSection("c").
			AddVariable(NewVariable("contact", []string{"12345", "123456"})))

fmt.Println(Marshal(cfg))
Output:

[a]
age = 18
name = "Joy"

[b]
money = 813.010000
single = no

[c]
contact = "12345", "123456"

func WriteConfigFile

func WriteConfigFile(filename string, cfg *Config) error

Types

type BoolValue

type BoolValue struct {
	V bool
}

func NewBoolValue

func NewBoolValue(v bool) *BoolValue

func (*BoolValue) ToString

func (b *BoolValue) ToString() string

func (*BoolValue) Type

func (b *BoolValue) Type() int

func (*BoolValue) Value

func (b *BoolValue) Value() interface{}

type Config

type Config struct {
	Sections map[string]*Section
}

func NewConfig

func NewConfig() *Config
Example
cfg := NewConfig()
cfg.AddSection(
	NewSection("a").
		AddVariable(NewVariable("name", "Joy")).
		AddVariable(NewVariable("age", 18))).
	AddSection(
		NewSection("b").
			AddVariable(NewVariable("single", false)).
			AddVariable(NewVariable("money", 813.01))).
	AddSection(
		NewSection("c").
			AddVariable(NewVariable("contact", []string{"12345", "123456"})))

fmt.Print(cfg.ToString())
Output:

Config {
 Sections = [
   Section(a) {
     Variables = {
       age  = 18
       name = "Joy"
     }
   }
   Section(b) {
     Variables = {
       money  = 813.010000
       single = no
     }
   }
   Section(c) {
     Variables = {
       contact = "12345", "123456"
     }
   }
 ]
}

func ReadConfigFile

func ReadConfigFile(filename string) (*Config, error)

func Unmarshal

func Unmarshal(data string) *Config

func (*Config) AddSection

func (c *Config) AddSection(section *Section) *Config

func (*Config) CreateIfAbsent

func (c *Config) CreateIfAbsent(sectionName string) *Section

func (*Config) Section

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

func (*Config) SetIfAbsent

func (c *Config) SetIfAbsent(section *Section) *Section

func (*Config) ToString

func (c *Config) ToString() string

type DecimalValue

type DecimalValue struct {
	V float64
}

func NewDecimalValue

func NewDecimalValue(v float64) *DecimalValue

func (*DecimalValue) ToString

func (d *DecimalValue) ToString() string

func (*DecimalValue) Type

func (d *DecimalValue) Type() int

func (*DecimalValue) Value

func (d *DecimalValue) Value() interface{}

type IntegerValue

type IntegerValue struct {
	V int
}

func NewIntegerValue

func NewIntegerValue(v int) *IntegerValue

func (*IntegerValue) ToString

func (i *IntegerValue) ToString() string

func (*IntegerValue) Type

func (i *IntegerValue) Type() int

func (*IntegerValue) Value

func (i *IntegerValue) Value() interface{}

type ListValue

type ListValue struct {
	V []VariableValue
}

func NewListValue

func NewListValue(v ...VariableValue) *ListValue

func (*ListValue) ToString

func (l *ListValue) ToString() string

func (*ListValue) Type

func (l *ListValue) Type() int

func (*ListValue) Value

func (l *ListValue) Value() interface{}

type Section

type Section struct {
	Name      string
	Variables map[string]*Variable
}

func NewSection

func NewSection(name string) *Section

func (*Section) AddVariable

func (s *Section) AddVariable(v *Variable) *Section

func (*Section) CreateIfAbsent

func (s *Section) CreateIfAbsent(name string) *Variable

func (*Section) SetIfAbsent

func (s *Section) SetIfAbsent(variable *Variable) *Variable

func (*Section) ToString

func (s *Section) ToString() string

func (*Section) Variable

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

type StringValue

type StringValue struct {
	V string
}

func NewStringValue

func NewStringValue(v string) *StringValue

func (*StringValue) ToString

func (s *StringValue) ToString() string

func (*StringValue) Type

func (s *StringValue) Type() int

func (*StringValue) Value

func (s *StringValue) Value() interface{}

type Variable

type Variable struct {
	Name  string
	Value VariableValue
}

func NewVariable

func NewVariable(name string, value interface{}) *Variable

NewVariable creates a new variable, supported value type are: bool, string, int, float64 and slice of these primitive types

func (*Variable) Bool

func (v *Variable) Bool() bool

func (*Variable) Float32

func (v *Variable) Float32() float32

func (*Variable) Float64

func (v *Variable) Float64() float64

func (*Variable) Int

func (v *Variable) Int() int

func (*Variable) List

func (v *Variable) List() []VariableValue

func (*Variable) SetValue

func (v *Variable) SetValue(value VariableValue)

func (*Variable) String

func (v *Variable) String() string

func (*Variable) Type

func (v *Variable) Type() int

Type equals to VariableValue.Type()

type VariableValue

type VariableValue interface {
	Type() int
	Value() interface{}
	ToString() string
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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