Giter Club home page Giter Club logo

goini's Introduction

goini

Build Status Coverage Status

This is a Go package to interact with arbitrary INI.

Our applications have a lot of memory data which are key/value pairs in the form of various separators NOT only \n. I have not found any Golang open source code suitable for it. So I write it myself.

goini is designed to be simple, flexible, efficient. It

  1. Supports the standard INI format
  2. Supports section
  3. Supports parsing INI file from local disk
  4. Supports parsing INI configuration data from memory
  5. Supports parsing data which are key/value pairs in the form of various separators NOT only \n
  6. Supports UTF8 encoding
  7. Supports comments which has a leading character ; or #
  8. Supports cascading inheritance
  9. Only depends standard Golang libraries
  10. Has 100% test coverage

Importing

import github.com/zieckey/goini

Usage

Example 1 : Parses an INI file

The simplest example code is :

import github.com/zieckey/goini

ini := goini.New()
err := ini.ParseFile(filename)
if err != nil {
	fmt.Printf("parse INI file %v failed : %v\n", filename, err.Error())
	return
}

v, ok := ini.Get("the-key")
//...

Example 2 : Parses the memory data with similar format of INI

raw := []byte("a:av||b:bv||c:cv||||d:dv||||||")
ini := goini.New()
err := ini.Parse(raw, "||", ":")
if err != nil {
    fmt.Printf("parse INI memory data failed : %v\n", err.Error())
    return
}

key := "a"
v, ok := ini.Get(key)
if ok {
    fmt.Printf("The value of %v is [%v]\n", key, v) // Output : The value of a is [av]
}

key = "c"
v, ok = ini.Get(key)
if ok {
    fmt.Printf("The value of %v is [%v]\n", key, v) // Output : The value of c is [cv]
}

Example 3 : Parses an inherited INI file

Assume we have a large project which has several production environments. Each production environment has its own configuration. But there is tiny difference between these configurations of the standalone production environments. So we use a common INI configuration to store the common configurations. And each production environment inherits from this common INI configuration.

The common.ini is bellow:

product=common
combo=common
debug=0

version=0.0.0.0
encoding=0

[sss]
a = aval
b = bval

The project1.ini is the configuration of project #1 as below:

inherited_from=common.ini

;the following config will override the values inherited from common.ini
product=project1
combo=test
debug=1

local=0
mid=c4ca4238a0b923820dcc509a6f75849b

[sss]
a = project1-aval
c = project1-cval

If we use goini.LoadInheritedINI("project1.ini") is the same as we have the following INI configuration:

product=project1
combo=test
debug=1

local=0
mid=c4ca4238a0b923820dcc509a6f75849b

version=0.0.0.0
encoding=0

[sss]
a = project1-aval
c = project1-cval

The value of the key product has been overwritten by value project1. The value of the key a in section sss has been overwritten by value project1-aval.

goini's People

Contributors

cogspace avatar zieckey avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

goini's Issues

Typo in error message

Saw the following when invalid INI encountered:

Came accross an error : xyz is NOT a valid key/value pair

I believe "accross" should be "across".

consolidate sections during Merge()

The incoming file contains duplicate sections. It would be helpful if they could be consolidated in the merge results (and in the case of duplicates keys in a section, last value being used).

Comments started with '#' are always skipping

Hello, first of all thank you for this package, it's great that it so powerful and tiny!

I just want to share that comments lines started with '#' are skipped always beside of ini.skipCommits.
It looks like because second condition in following if:

if ini.skipCommits && line[0] == ';' || line[0] == '#' {
            // Skip comments
            continue
}

key=() does not work

if i have this: ConfigOverrideItemMaxQuantity=(ItemClassString="PrimalItemAmmo_AdvancedBullet_C",Quantity=(MaxItemQuantity=100,bIgnoreMultiplier=true))
in my ini file and then load it and change another value it just disappears

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.