toml

package
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2024 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Example
package main

import (
	"context"
	"fmt"
	"os"
	"time"

	"github.com/zpatrick/cfg"
	"github.com/zpatrick/cfg/internal"
	"github.com/zpatrick/cfg/toml"
)

type Config struct {
	Timeout    time.Duration
	ServerPort int64
	ServerAddr string
}

func main() {
	const data = `
timeout = "5s"

[servers]

	[servers.alpha]
	port = 8080
	addr = "localhost"
`

	path, err := internal.WriteTempFile("", data)
	if err != nil {
		panic(err)
	}
	defer os.Remove(path)

	tomlFile, err := toml.New(path)
	if err != nil {
		panic(err)
	}

	var c Config
	if err := cfg.Load(context.Background(), cfg.Schemas{
		"timeout": cfg.Schema[time.Duration]{
			Dest:     &c.Timeout,
			Provider: tomlFile.Duration("timeout"),
		},
		"server port": cfg.Schema[int64]{
			Dest:     &c.ServerPort,
			Provider: tomlFile.Int64("servers", "alpha", "port"),
		},
		"server addr": cfg.Schema[string]{
			Dest:     &c.ServerAddr,
			Provider: tomlFile.String("servers", "alpha", "addr"),
		},
	}); err != nil {
		panic(err)
	}

	fmt.Printf("Timeout: %s ServerPort: %d ServerAddr: %s",
		c.Timeout,
		c.ServerPort,
		c.ServerAddr,
	)

}
Output:

Timeout: 5s ServerPort: 8080 ServerAddr: localhost

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Provide

func Provide[T any](p *Provider, keys ...string) cfg.Provider[T]

Types

type Provider

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

func New

func New(path string) (*Provider, error)

func (*Provider) Bool

func (p *Provider) Bool(keys ...string) cfg.Provider[bool]

func (*Provider) Duration

func (p *Provider) Duration(keys ...string) cfg.Provider[time.Duration]

func (*Provider) Float64

func (p *Provider) Float64(keys ...string) cfg.Provider[float64]

func (*Provider) Int

func (p *Provider) Int(keys ...string) cfg.Provider[int]

func (*Provider) Int64

func (p *Provider) Int64(keys ...string) cfg.Provider[int64]

func (*Provider) String

func (p *Provider) String(keys ...string) cfg.Provider[string]

Jump to

Keyboard shortcuts

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