yaegiconf

package module
v0.0.0-...-cd3bccf Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2021 License: BSD-3-Clause Imports: 6 Imported by: 0

README

yaegiconf

yaegiconf provides a simple interface to the yaegi interpreter for use as a configuration parser

What could possibly go wrong having a Turing complete configuration parser?

Installation

yaegiconf requires a Go installation.

Documentation

http://godoc.org/github.com/kortschak/yaegiconf

License

yaegiconf is distributed under a modified BSD license.

Documentation

Overview

Package yaegiconf provides a simple interface to the yaegi interpreter for use as a configuration parser.

Example (Map)
package main

import (
	"fmt"
	"log"

	"github.com/kortschak/yaegiconf"
)

func main() {
	var cfg map[string]interface{}
	err := yaegiconf.EvalTo(&cfg, `config.Value{"int": 5, "float64": 0.1}`)
	if err != nil {
		log.Fatalf("failed to parse configuration: %v", err)
	}

	fmt.Printf("%#v\n", cfg)

}
Output:


map[string]interface {}{"float64":0.1, "int":5}
Example (Nestedstruct)
package main

import (
	"context"
	"fmt"
	"log"
	"reflect"

	"github.com/traefik/yaegi/interp"

	"github.com/kortschak/yaegiconf"
)

func main() {
	type Sub struct {
		S string
	}
	type Config struct {
		N int
		F float64
		X Sub
	}
	s := interp.Exports{"xcfg/xcfg": map[string]reflect.Value{
		"Value": reflect.Zero(reflect.TypeOf(&Config{})),
		"Sub":   reflect.Zero(reflect.TypeOf(&Sub{})),
	}}
	var cfg Config
	err := yaegiconf.EvalExtWithContextTo(context.Background(), &cfg, `xcfg.Value{
		N: 5, F: 0.1,
		X: xcfg.Sub{S: "set"},
}`,
		interp.Options{}, s)
	if err != nil {
		log.Fatalf("failed to parse configuration: %v", err)
	}

	fmt.Printf("%#v\n", cfg)

}
Output:


yaegiconf_test.Config{N:5, F:0.1, X:yaegiconf_test.Sub{S:"set"}}
Example (String)
package main

import (
	"fmt"
	"log"

	"github.com/kortschak/yaegiconf"
)

func main() {
	var cfg string
	err := yaegiconf.EvalTo(&cfg, `config.Value("Configured")`)
	if err != nil {
		log.Fatalf("failed to parse configuration: %v", err)
	}

	fmt.Printf("%#v\n", cfg)

}
Output:


"Configured"
Example (Struct)
package main

import (
	"fmt"
	"log"

	"github.com/kortschak/yaegiconf"
)

func main() {
	type Config struct {
		N int
		F float64
	}
	var cfg Config
	err := yaegiconf.EvalTo(&cfg, `config.Value{N: 5, F: 0.1}`)
	if err != nil {
		log.Fatalf("failed to parse configuration: %v", err)
	}

	fmt.Printf("%#v\n", cfg)

}
Output:


yaegiconf_test.Config{N:5, F:0.1}
Example (Timeout)
package main

import (
	"fmt"

	"github.com/kortschak/yaegiconf"
)

func main() {
	type Config string
	var cfg Config
	err := yaegiconf.EvalTo(&cfg, `for {}; config.Value("Configured")`)
	if err != nil {
		fmt.Printf("failed to parse configuration: %v", err)
	}

	fmt.Println(cfg)

}
Output:


failed to parse configuration: context deadline exceeded

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func EvalExtWithContextTo

func EvalExtWithContextTo(ctx context.Context, dst interface{}, src string, options interp.Options, symbols ...interp.Exports) error

EvalExtWithContextTo evaluates the configuration source in src and stores the result of the evaluation into dst, which must be a pointer to a value.

EvalExtWithContextTo allows the user to provide a cancellation context and access to additional types and interpreter configuration.

func EvalTo

func EvalTo(dst interface{}, src string) error

EvalTo evaluates the configuration source in src and stores the result of the evaluation into dst, which must be a pointer to a value. The type of the value is accessible within the src via the label config.Value.

EvalTo will timeout if it has not completed evaluating src within ten seconds.

func EvalWithContextTo

func EvalWithContextTo(ctx context.Context, dst interface{}, src string) error

EvalWithContextTo evaluates the configuration source in src and stores the result of the evaluation into dst, which must be a pointer to a value.

EvalWithContextTo allows the user to provide a cancellation context to the evaluation.

Types

This section is empty.

Jump to

Keyboard shortcuts

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