unicfg

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: May 8, 2023 License: MIT Imports: 8 Imported by: 0

README

unicfg

codecov

unicfg is an open-source library written in Go that aims to parse various configuration file formats and convert them to a unified method linkage interface.

currently supported configuration formats:

  • json
  • ini

Installation

Install unicfg using the go get command:

go get -u github.com/harley9293/unicfg

Usage

First, import the unicfg library:

import "github.com/harley9293/unicfg"

Then, parse the configuration file using New:

elem, err := unicfg.New("path/to/your/config/file")
if err != nil {
    log.Fatalf("Error parsing config: %v", err)
}

Retrieve configuration information using the unified interface:

// Get basic data types
s := elem.Key("test1").String()
i := elem.Key("test2.test3").MustInt(100)
b := elem.Key("test4").Key("test5").Bool()

// Get array data types
for i := elem.Key("test6").Next(); i != nil; i = i.Next() {
 // ...
}

// Get map data types
for k, v := range elem.Children() {
 // ...
}

unicfg also supports adding custom tags, allowing configuration files to be directly parsed into corresponding structures.

type Person struct {
    Name    string             `unicfg:"name"`
    Age     int                `unicfg:"age"`
    Working bool               `unicfg:"working"`
    Address map[string]string  `unicfg:"address"`
    Family  []PersonTestUnicfg `unicfg:"family"`
    Ignore  int
}

person := Person{}
err := Parse("test.json", &person)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Parse

func Parse(configPath string, st any) error

Types

type Elem

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

func New

func New(configPath string) (*Elem, error)

func (*Elem) Bool

func (e *Elem) Bool() bool

func (*Elem) Children

func (e *Elem) Children() map[string]*Elem

func (*Elem) Float

func (e *Elem) Float() float64

func (*Elem) Int

func (e *Elem) Int() int

func (*Elem) Int64

func (e *Elem) Int64() int64

func (*Elem) Key

func (e *Elem) Key(key string) *Elem

func (*Elem) Len

func (e *Elem) Len() int

func (*Elem) MustBool

func (e *Elem) MustBool(defaultValue bool) bool

func (*Elem) MustFloat

func (e *Elem) MustFloat(defaultValue float64) float64

func (*Elem) MustInt

func (e *Elem) MustInt(defaultValue int) int

func (*Elem) MustInt64

func (e *Elem) MustInt64(defaultValue int64) int64

func (*Elem) MustString

func (e *Elem) MustString(defaultValue string) string

func (*Elem) Next

func (e *Elem) Next() *Elem

func (*Elem) Parse

func (e *Elem) Parse(st any)

func (*Elem) String

func (e *Elem) String() string

type Loader

type Loader interface {
	// contains filtered or unexported methods
}

Jump to

Keyboard shortcuts

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