narg

package module
v0.0.0-...-9f3482b Latest Latest
Warning

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

Go to latest
Published: Mar 13, 2017 License: MIT Imports: 11 Imported by: 0

README

narg - Nested arguments as configuration

MIT license GitHub release GoDoc Go Report Card Build Status Coverage Status

Installation

go get github.com/nochso/narg

Documentation

See godoc for API docs and examples.

Decoding configuration

Given a configuration struct consisting of scalar types, structs and slices:

type testConf struct {
	Name   string
	Port   int
	Debug  bool
	Float  float32
	Hosts  []string
	Ports  []int
	PortsU []uint8
	Admin  testUser
	Users  []testUser
}

type testUser struct {
	ID   int    `narg:"0"`
	Name string `narg:"1"`
}

Note that users can be defined using positional arguments instead of named ones. The order of the arguments is defined using the struct tag narg with the zero-based index (see testUser struct above).

The following narg input can be decoded into a pointer to testConf:

in := `name foo
port 80
debug 1
float 3.14
hosts a bee "cee e"
ports 1024 1025
portsu 0xff 255
admin {
	id 1
	name "Phil \"Tandy\" Miller"
}
users {
	id 2
	name "Carol Pilbasian"
}
users 4 Todd`
cfg := &testConf{}
err := Decode(strings.NewReader(in), cfg)

Field names are case-insensitive.

cfg has now been populated with the given narg input:

cfg = &testConf{
    Name:   "foo",
    Port:   80,
    Debug:  true,
    Float:  3.14,
    Hosts:  []string{"a", "bee", "cee e"},
    Ports:  []int{1024, 1025},
    PortsU: []uint8{0xff, 0xff},
    Admin: testUser{
        ID:   1,
        Name: `Phil "Tandy" Miller`,
    },
    Users: []testUser{
        {2, "Carol Pilbasian"},
        {4, "Todd"},
    },
}

Currently only supplied values are overwritten. You can set up your struct with sane defaults and when decoding a sparse config the defaults will be kept.

License

This package is released under the MIT license.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Decode

func Decode(r io.Reader, v interface{}) error

Decode narg input r into a given struct v. v must be a pointer to the struct you want to decode into.

func Encode

func Encode(w io.Writer, v interface{}) error

Encode the given value by writing its narg representation to w.

func EncodeString

func EncodeString(v interface{}) (string, error)

EncodeString encodes the given value as a string.

func Fuzz

func Fuzz(data []byte) int

Fuzz is exposed only for internal use with go-fuzz: https://github.com/dvyukov/go-fuzz

Corpus resides in test-fixtures/fuzz/corpus

go get github.com/dvyukov/go-fuzz/go-fuzz
go get github.com/dvyukov/go-fuzz/go-fuzz-build
go-fuzz-build github.com/nochso/narg
go-fuzz -bin narg-fuzz.zip -workdir test-fixtures/fuzz

Types

type Item

type Item struct {
	Name     string
	Args     []string
	Children ItemSlice
}

Item represents a single block consisting of at least the name.

narg input showing what part of an Item it's parsed into:

Name Args[0] Args[1] {
	Children[0].Name Children[0].Args[0]
}

func (Item) String

func (i Item) String() string

String returns the string representation of an Item and its Children.

type ItemSlice

type ItemSlice []Item

ItemSlice is a list of items.

func Parse

func Parse(r io.Reader) (ItemSlice, error)

Parse narg input into an ItemSlice.

func (ItemSlice) Filter

func (s ItemSlice) Filter(key string) ItemSlice

Filter returns all items filtered by name. The comparison is case-insensitive.

func (ItemSlice) String

func (s ItemSlice) String() string

String returns an indented string representation.

type Lexer

type Lexer struct {
	// Token that was read after the latest successful call to Scan()
	Token token.T
	Err   error
	// contains filtered or unexported fields
}

Lexer reads narg input and parses it into tokens.

func NewLexer

func NewLexer(r io.Reader) *Lexer

NewLexer returns a new Lexer for parsing tokens.

func (*Lexer) Scan

func (l *Lexer) Scan() bool

Scan attempts to read the next Token into Lexer.Token. Returns true when a new Token is ready.

type Parser

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

Parser turns tokens into items.

func (*Parser) Parse

func (p *Parser) Parse() (ItemSlice, error)

Parse all items.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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