gcfg

package
v0.16.2 Latest Latest
Warning

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

Go to latest
Published: Dec 23, 2024 License: Apache-2.0, BSD-3-Clause Imports: 15 Imported by: 0

README

Gcfg reads INI-style configuration files into Go structs;
supports user-defined types and subsections.

Package docs: https://godoc.org/gopkg.in/gcfg.v1

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func FatalOnly

func FatalOnly(err error) error

FatalOnly returns the fatal error, if any, **in an error returned by a Collector**. It returns nil if and only if err is nil or err is a List with err.Fatal == nil.

func ReadFileInto

func ReadFileInto(config any, filename string) error

ReadFileInto reads gcfg formatted data from the file filename and sets the values into the corresponding fields in config.

func ReadInto

func ReadInto(config any, reader io.Reader) error

ReadInto reads gcfg formatted data from reader and sets the values into the corresponding fields in config.

func ReadStringInto

func ReadStringInto(config any, str string) error

ReadStringInto reads gcfg formatted data from str and sets the values into the corresponding fields in config.

Example
package main

import (
	"fmt"
	"log"

	"github.com/antgroup/hugescm/modules/gcfg"
)

func main() {
	cfgStr := `; Comment line
[section]
name=value # comment`
	cfg := struct {
		Section struct {
			Name string
		}
	}{}
	err := gcfg.ReadStringInto(&cfg, cfgStr)
	if err != nil {
		log.Fatalf("Failed to parse gcfg data: %s", err)
	}
	fmt.Println(cfg.Section.Name)
}
Output:

value
Example (Bool)
package main

import (
	"fmt"
	"log"

	"github.com/antgroup/hugescm/modules/gcfg"
)

func main() {
	cfgStr := `; Comment line
[section]
switch=on`
	cfg := struct {
		Section struct {
			Switch bool
		}
	}{}
	err := gcfg.ReadStringInto(&cfg, cfgStr)
	if err != nil {
		log.Fatalf("Failed to parse gcfg data: %s", err)
	}
	fmt.Println(cfg.Section.Switch)
}
Output:

true
Example (Hyphens)
package main

import (
	"fmt"
	"log"

	"github.com/antgroup/hugescm/modules/gcfg"
)

func main() {
	cfgStr := `; Comment line
[section-name]
variable-name=value # comment`
	cfg := struct {
		Section_Name struct {
			Variable_Name string
		}
	}{}
	err := gcfg.ReadStringInto(&cfg, cfgStr)
	if err != nil {
		log.Fatalf("Failed to parse gcfg data: %s", err)
	}
	fmt.Println(cfg.Section_Name.Variable_Name)
}
Output:

value
Example (Multivalue)
package main

import (
	"fmt"
	"log"

	"github.com/antgroup/hugescm/modules/gcfg"
)

func main() {
	cfgStr := `; Comment line
[section]
multi=value1
multi=value2`
	cfg := struct {
		Section struct {
			Multi []string
		}
	}{}
	err := gcfg.ReadStringInto(&cfg, cfgStr)
	if err != nil {
		log.Fatalf("Failed to parse gcfg data: %s", err)
	}
	fmt.Println(cfg.Section.Multi)
}
Output:

[value1 value2]
Example (Subsections)
package main

import (
	"fmt"
	"log"

	"github.com/antgroup/hugescm/modules/gcfg"
)

func main() {
	cfgStr := `; Comment line
[profile "A"]
color = white

[profile "B"]
color = black
`
	cfg := struct {
		Profile map[string]*struct {
			Color string
		}
	}{}
	err := gcfg.ReadStringInto(&cfg, cfgStr)
	if err != nil {
		log.Fatalf("Failed to parse gcfg data: %s", err)
	}
	fmt.Printf("%s %s\n", cfg.Profile["A"].Color, cfg.Profile["B"].Color)
}
Output:

white black
Example (Tags)
package main

import (
	"fmt"
	"log"

	"github.com/antgroup/hugescm/modules/gcfg"
)

func main() {
	cfgStr := `; Comment line
[section]
var-name=value # comment`
	cfg := struct {
		Section struct {
			FieldName string `gcfg:"var-name"`
		}
	}{}
	err := gcfg.ReadStringInto(&cfg, cfgStr)
	if err != nil {
		log.Fatalf("Failed to parse gcfg data: %s", err)
	}
	fmt.Println(cfg.Section.FieldName)
}
Output:

value
Example (Unicode)
package main

import (
	"fmt"
	"log"

	"github.com/antgroup/hugescm/modules/gcfg"
)

func main() {
	cfgStr := `; Comment line
[甲]
乙=丙 # comment`
	cfg := struct {
		X甲 struct {
			X乙 string
		}
	}{}
	err := gcfg.ReadStringInto(&cfg, cfgStr)
	if err != nil {
		log.Fatalf("Failed to parse gcfg data: %s", err)
	}
	fmt.Println(cfg.X甲.X乙)
}
Output:

func ReadWithCallback

func ReadWithCallback(reader io.Reader, callback func(string, string, string, string, bool) error) error

ReadWithCallback reads gcfg formatted data from reader and calls callback with each section and option found.

Callback is called with section, subsection, option key, option value and blank value flag as arguments.

When a section is found, callback is called with nil subsection, option key and option value.

When a subsection is found, callback is called with nil option key and option value.

If blank value flag is true, it means that the value was not set for an option (as opposed to set to empty string).

If callback returns an error, ReadWithCallback terminates with an error too.

func WarningsOnly

func WarningsOnly(err error) []error

WarningsOnly returns the warnings **in an error returned by a Collector**.

Types

type Collector

type Collector struct {
	// IsFatal distinguishes between warnings and fatal errors.
	IsFatal func(error) bool
	// FatalWithWarnings set to true means that a fatal error is returned as
	// a List together with all warnings so far. The default behavior is to
	// only return the fatal error and discard any warnings that have been
	// collected.
	FatalWithWarnings bool
	// contains filtered or unexported fields
}

A Collector collects errors up to the first fatal error.

func NewCollector

func NewCollector(isFatal func(error) bool) *Collector

NewCollector returns a new Collector; it uses isFatal to distinguish between warnings and fatal errors.

func (*Collector) Collect

func (c *Collector) Collect(err error) error

Collect collects a single error (warning or fatal). It returns nil if collection can continue (only warnings so far), or otherwise the errors collected. Collect mustn't be called after the first fatal error or after Done has been called.

func (*Collector) Done

func (c *Collector) Done() error

Done ends collection and returns the collected error(s).

type List

type List struct {
	Warnings []error
	Fatal    error
}

List holds a collection of warnings and optionally one fatal error.

func (List) Error

func (l List) Error() string

Error implements the error interface.

Directories

Path Synopsis
Package scanner implements a scanner for gcfg configuration text.
Package scanner implements a scanner for gcfg configuration text.
Package token defines constants representing the lexical tokens of the gcfg configuration syntax and basic operations on tokens (printing, predicates).
Package token defines constants representing the lexical tokens of the gcfg configuration syntax and basic operations on tokens (printing, predicates).
Package types defines helpers for type conversions.
Package types defines helpers for type conversions.

Jump to

Keyboard shortcuts

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