caddycfg

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Sep 17, 2018 License: MIT Imports: 7 Imported by: 0

README

Reflect based config unmarshaler for Caddy server

Build Status Coverage Status

Installation

I hope it will be included into Caddy installation, but it is not for now. So use

go get github.com/sirkon/caddycfg

Usage

The usage is simple, just like unmarshaling jsons with standard library tools:

var c *caddy.Controller
var cfg ConfigStruct
if err := caddycfg.Unmarshal(c, &cfg); err != nil {
	return err
}
Example 1

Let we have plugin called plugin.

plugin {
    key1 value1
    key2 value2
}

This can be parsed with the following structure:

type pluginConfig struct {
	Key1 string `json:"key1"`
	Key2 string `json:"key2"`
} 
Example 2

A bit harder example

plugin {
    key1 value11 value12
    key2 value2
}

This can be parsed with

type pluginConfig struct {
	Key1 []string `json:"key1"`
	Key2 string   `json:"key2"`
}
Example 3

Arguments appears before block

plugin arg1 arg2 {
    key1 value1
    key2 value2
}

This can be parsed with

type pluginConfig struct {
	caddycfg.Args
	
	Key1 string `json:"key1"`
	Key2 string `json:"key2"`
}

So, you see, this is practically the same as with JSONs.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Unmarshal

func Unmarshal(c *caddy.Controller, dest interface{}) error

Unmarshal unmarshaller into dest, which must not be channel

Types

type Args

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

Args provides access to positional parameters, this is to be used to consume arguments that predate block I mean the following; let we have a config

head arg₁ arg₂ … argₙ {
    …
}

Head is used (in case of structs) to choose a field with its type. In case if there are arguments, i.e. n > 0 followed by block (there can be no block, in this case regular []int, []string, etc is enough) the type must implement argumentAccess – this is equivalent for having type Args embedded or being type Args itself. Although it is possible to use Args itself, it will not work well enough. So, use it for embedding into your own types

func (*Args) Arguments

func (a *Args) Arguments() []string

type Stream

type Stream interface {
	Next() bool
	NextArg() bool
	Token() Token
	Confirm()
}

Stream stream of tokens with so called confirmation. It works in the following way:

  1. Next checks if the previous token read was confirmed. Returns true if it is not – Token will return the

previous token. Looks for the next token and return true if it was found. Otherwise returns false.

  1. NextArg works like Next but only works for the rest of tokens in the current line
  2. Token returns token scanned with Next or NextArg
  3. Confirm confirmes scanned token was consumed

type Token

type Token struct {
	File  string
	Value string
	Lin   int
	Col   int
}

Token config token. Gives token location (Col is not supported currently)

func UnmarshalHeadInfo added in v0.0.2

func UnmarshalHeadInfo(c *caddy.Controller, dest interface{}) (Token, error)

UnmarshalHeadInfo returns token with plugin name and unmarshal c into dest

func (Token) String

func (t Token) String() string

String ...

Jump to

Keyboard shortcuts

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