goini

package module
v0.0.0-...-8a0370c Latest Latest
Warning

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

Go to latest
Published: Dec 18, 2022 License: BSD-3-Clause-Clear Imports: 3 Imported by: 1

README

goini - A no-dependence builtin-like ini config file parser

Should work with most type with encoding.TextUnmarshaler and encoding.TextMarshaler interfaces.

Install

go get -u github.com/wildwest-productions/goini

Example

Short:

type myConfig struct {
	Dir1 string `ini:"dir"`
	Runs int    `ini:"runs"`
}

iniBytes, err := ioutil.ReadFile("somefile.ini")

c := &myConfig{}
err := goini.Unmarshal(iniBytes, &c)

Full example:

package main

import (
	"fmt"
	"os"

	"github.com/wildwest-productions/goini"
)

type testStruct struct {
	ConfigStr     string     `ini:"hello"`
	ConfigInt     int        `ini:"bar"`
	Val           string     `ini:"t"`
	Hello         subStruct  `ini:"sector"`
	SystemEnabled bool       `ini:"sys_enable"`
	Command       subStruct2 `ini:"command"`
}

type subStruct struct {
	Bar           string `ini:"hello"`
	SystemEnabled bool   `ini"sys_enable"`
}

type subStruct2 struct {
	Command string `ini:"command"`
	Runs    int    `ini:"runs"`
}

func main() {
	s := &testStruct{}

	iniData := []byte(
		`
hello=world
bar = 2
t=hi
sys_enable = true

[command]
command = echo hello
runs = 42

[sector]
hello=world-world
sys_enable = NO

`)

	err := goini.Unmarshal(iniData, &s)
	if err != nil {
		fmt.Printf("Failed to unmarshal ini: %s\n", err)
		os.Exit(1)
	}

	fmt.Printf("Converted ini:\n%s\n", string(iniData))

	fmt.Printf("Into struct: %+v\n", s)
}

Features

  • Supports comments, both # and ;
  • Define with and without whitespace (foo=bar and foo = bar)
  • Supports common value types (bool, int, float32, float64, string) and all (most) custom types with encoding.TextUnmarshaler and encoding.TextMarshaler interfaces
  • No dependencies (other then builtins)

Limitations

There are some limitations:

  • sub-struct pointers will not work (willfix)
  • cannot unmarshal into a map (wontfix)
  • comments will not be perserved when unmarshaling/marshaling (willfix)

License

This project is licensed under the terms of The Clear BSD License. See the LICENSE file for more details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Marshal

func Marshal(s interface{}) ([]byte, error)

func Unmarshal

func Unmarshal(iniData []byte, s interface{}) error

Types

This section is empty.

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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