uconf

package module
v0.1.22 Latest Latest
Warning

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

Go to latest
Published: May 13, 2023 License: GPL-3.0 Imports: 8 Imported by: 1

README

uconf

Simple linux-style configuration parser. The state of this project is very alpha. Dont expect any stability until version v1

Format / Syntax

// comment
instruction;
/*
	comment
*/
insstruction arg1;
insstruction "arg 1";
insstruction arg1 {
	insstruction arg1;
	insstruction arg2;
}

This will be be parsed into a tree representation like this:

- insstruction
- insstruction arg1
- insstruction "arg 1"
- insstruction arg1
  - insstruction arg1
  - insstruction arg2

Usage example

package main

import (
	"fmt"

	"gitlab.com/uranoxyd/uconf"
)

func main() {
	config := `
		foo 23;

		bar {
			foo 42;
		}
		
		foo 42;
	`

	if scope, err := uconf.Parse(config); err == nil {
		fmt.Println(scope.GetInstructions("foo"))
		fmt.Println(scope.GetInstructions("foo")[1].Argument(0).AsInt())
	} else {
		fmt.Println("error:", err)
	}
}

This will print:

[foo 23; foo 42;]
42

Intended way of use

This is the way uconf should be used. You parse the configuration and load the result into your objects. Have a look at this example:

package main

import (
	"fmt"

	"gitlab.com/uranoxyd/uconf"
)

type MyUser struct {
	Username string
	Name     string
}

func (u *MyUser) String() string {
	return fmt.Sprintf("<%s (%s)>", u.Username, u.Name)
}

func (u *MyUser) loadConfig(scope *uconf.Scope) error {
	for _, instruction := range scope.Values() {
		switch instruction.Name() {
		case "username":
			if instruction.Argument(0).Empty() {
				return fmt.Errorf("missing argument: username")
			}
			u.Username = instruction.Argument(0).String()

		case "name":
			if instruction.Argument(0).Empty() {
				return fmt.Errorf("missing argument: name")
			}
			u.Name = instruction.Argument(0).String()
		}
	}

	return nil
}

type MyConfig struct {
	Users []*MyUser
}

func (c *MyConfig) loadConfig(scope *uconf.Scope) error {
	for _, instruction := range scope.Values() {
		switch instruction.Name() {
		case "user":
			user := &MyUser{}
			if err := user.loadConfig(instruction.SubScope()); err != nil {
				return err
			}

			c.Users = append(c.Users, user)
		}
	}
	return nil
}

func main() {
	config := `
		user {
			username alice;
			name "Alice In Wonderland";
		}
		user {
			username bob;
			name "Bob The Builder";
		}
	`

	if scope, err := uconf.Parse(config); err == nil {
		myConf := MyConfig{}
		myConf.loadConfig(scope)

		fmt.Printf("%+v\n", myConf)
	} else {
		panic(err)
	}
}

Prints:

{Users:[<alice (Alice In Wonderland)> <bob (Bob The Builder)>]}

Licencse

GNU

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Unmarshal added in v0.1.7

func Unmarshal(source []byte, target any) error

Types

type Argument added in v0.1.11

type Argument string

func (Argument) AsBool added in v0.1.19

func (a Argument) AsBool() (bool, error)

func (Argument) AsDuration added in v0.1.19

func (a Argument) AsDuration() (time.Duration, error)

func (Argument) AsFloat32 added in v0.1.11

func (a Argument) AsFloat32() (value float32, err error)

func (Argument) AsFloat64 added in v0.1.11

func (a Argument) AsFloat64() (value float64, err error)

func (Argument) AsInt added in v0.1.11

func (a Argument) AsInt() (value int, err error)

func (Argument) AsInt32 added in v0.1.11

func (a Argument) AsInt32() (value int32, err error)

func (Argument) AsInt64 added in v0.1.11

func (a Argument) AsInt64() (value int64, err error)

func (Argument) AsTime added in v0.1.19

func (a Argument) AsTime() (time.Time, error)

func (Argument) AsUInt added in v0.1.11

func (a Argument) AsUInt() (value uint, err error)

func (Argument) AsUInt32 added in v0.1.11

func (a Argument) AsUInt32() (value uint32, err error)

func (Argument) AsUInt64 added in v0.1.11

func (a Argument) AsUInt64() (value uint64, err error)

func (Argument) Empty added in v0.1.11

func (a Argument) Empty() bool

func (Argument) String added in v0.1.11

func (a Argument) String() string

func (Argument) UnmarshalText added in v0.1.20

func (a Argument) UnmarshalText(target encoding.TextUnmarshaler) error

type ConfigError

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

func NewConfigError

func NewConfigError(line int, char int, message string) *ConfigError

func (*ConfigError) Error

func (e *ConfigError) Error() string

type Instruction added in v0.1.14

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

func NewInstruction added in v0.1.14

func NewInstruction(scope *Scope, index int) *Instruction

func (*Instruction) Argument added in v0.1.14

func (n *Instruction) Argument(index int) Argument

func (*Instruction) Arguments added in v0.1.14

func (v *Instruction) Arguments() []Argument

func (*Instruction) Clone added in v0.1.14

func (n *Instruction) Clone() (clone *Instruction)

func (*Instruction) Name added in v0.1.14

func (v *Instruction) Name() string

func (*Instruction) NumArguments added in v0.1.14

func (v *Instruction) NumArguments() int

func (*Instruction) Path added in v0.1.14

func (v *Instruction) Path() (path string)

func (*Instruction) Root added in v0.1.14

func (n *Instruction) Root() (scope *Scope)

func (*Instruction) Scope added in v0.1.14

func (v *Instruction) Scope() *Scope

func (*Instruction) String added in v0.1.14

func (v *Instruction) String() (out string)

func (*Instruction) SubScope added in v0.1.14

func (v *Instruction) SubScope() *Scope

type Parser

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

func NewParser

func NewParser() *Parser

func (*Parser) EOF

func (p *Parser) EOF() bool

func (*Parser) IsComment

func (p *Parser) IsComment() bool

func (*Parser) IsLiteral

func (p *Parser) IsLiteral() bool

func (*Parser) IsOperator

func (p *Parser) IsOperator(kind ...string) bool

func (*Parser) IsSeperator

func (p *Parser) IsSeperator() bool

func (*Parser) IsString

func (p *Parser) IsString() bool

func (*Parser) Next

func (p *Parser) Next() uparse.Token

func (*Parser) Parse

func (p *Parser) Parse(input string) (scope *Scope, err error)

func (*Parser) SkipWhitespace added in v0.1.14

func (p *Parser) SkipWhitespace() bool

func (*Parser) Token

func (p *Parser) Token() uparse.Token

type Scope

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

func NewScope

func NewScope(parent *Scope, name string) *Scope

func Parse

func Parse(input string) (*Scope, error)

func (*Scope) Clone

func (n *Scope) Clone() (scope *Scope)

func (*Scope) GetInstructions added in v0.1.14

func (n *Scope) GetInstructions(name string) (matches []*Instruction)

func (*Scope) Instructions added in v0.1.15

func (s *Scope) Instructions() []*Instruction

func (*Scope) Parent

func (s *Scope) Parent() *Scope

func (*Scope) Path added in v0.1.11

func (n *Scope) Path() (path string)

func (*Scope) Print added in v0.1.14

func (s *Scope) Print()

func (*Scope) QueryInstructions added in v0.1.14

func (n *Scope) QueryInstructions(pattern string) (matches []*Instruction)

func (*Scope) Root

func (n *Scope) Root() (scope *Scope)

func (*Scope) String

func (s *Scope) String() (out string)

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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