ini

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: May 23, 2022 License: MIT Imports: 6 Imported by: 0

README

ini

GitHub Language GitHub license

An INI file parser which supports multipart way to parse the configuration.

Features

  • Chain-liked calling API to get the configuration
  • Using struct tag to bind specificly with struct field.

Example

The First way to parse ini file is calling Load API. Load API will return a struct if it is called successfully and you can apply a series of method like chain to get the specific configuration value.

import (
    "fmt"
    "log"

    "github.com/ChenYuTong10/ini"
)

func Example() {
    cfg, err := Load("example.ini")
    if err != nil {
        log.Fatalln(err)
    }

    name := cfg.Section("person").Field("Name").String()
    age := cfg.Section("person").Field("Age").Int64()
    height := cfg.Section("person").Field("Height").Float64()
    
    fmt.Println("name:", name)
    fmt.Println("age:", age)
    fmt.Println("height:", height)
}

What's more, you can use struct tag to bind the struct field with the configuration in ini file. It can avoid getting configurations one by one.

import (
    "fmt"
    "log"

    "github.com/ChenYuTong10/ini"
)

func Example() {
    type Foo struct {
        DB struct {
            User   string `ini:"User"`
            Passwd string `ini:"Passwd"`
            Addr   string `ini:"Addr"`
            DBName string `ini:"DBName"`
        } `ini:"db"`
        Person struct {
            Name   string  `ini:"Name"`
            Age    int64   `ini:"Age"`
            Height float64 `ini:"Height"`
            Weight float64 `ini:"Weight"`
        } `ini:"person"`
    }

    foo := &Foo{}
    if err := Bind("./example.ini", foo); err != nil {
        log.Fatalln(err)
    }

    name := foo.Person.Name
    age := foo.Person.Age
    height := foo.Person.Height

    fmt.Println("name:", name)
    fmt.Println("age:", age)
    fmt.Println("height:", height)
}

Future

⬜ Not Start ⌛ Processing ✅ Finished

  • Two ways to parse the ini file ✅
  • Check for boundary case ⌛
  • Add other configuration file parser ⬜

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrBindPtr = errors.New("bind des needs to be a pointer")

Functions

func Bind

func Bind(fPath string, des interface{}) error

Bind offers more easy way to get configuration. Bind also calls the Load and bind value to the field according to the struct tag.

Types

type Cfg

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

Cfg contains a Section map parsing from the configuration file.

func Load

func Load(fPath string) (*Cfg, error)

Load loads the configuration file according to fPath and returns a Cfg struct containing all the configuration.

func (*Cfg) Section

func (cfg *Cfg) Section(name string) *Section

Section gets the appointed section. If the section is not exist, an empty Section will be returned instead of nil pointer.

type Field

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

Field contains only one part of key-value.

func (*Field) Float64

func (f *Field) Float64() float64

func (*Field) Int64

func (f *Field) Int64() int64

func (*Field) String

func (f *Field) String() string

type Section

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

Section contains the Section name and several Fields belong it.

func (*Section) Field

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

Field will return a new empty struct instead of a nil pointer when the field name is not exist. It is same as the Section.

Jump to

Keyboard shortcuts

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