astconf

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

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

Go to latest
Published: Mar 15, 2023 License: MIT Imports: 9 Imported by: 0

README

astconf Go Reference Go Report Card

Go package for writing asterisk configuration files

Documentation

Overview

Package astconf writes asterisk configuration files.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func AlignLeft

func AlignLeft(e *Encoder)

AlignLeft configures an encoder to align names to the left.

func AlignRight

func AlignRight(e *Encoder)

AlignRight configures an encoder to align names to the right.

func Marshal

func Marshal(v interface{}) ([]byte, error)

Marshal marshals the asterisk configuration of v and returns it as a slice of bytes.

Marshal will return an error if asked to marshal a channel, function, or map.

func MarshalTo

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

MarshalTo marshals the asterisk configuration of v to w.

func Unaligned

func Unaligned(e *Encoder)

Unaligned configures an encoder not to align names.

Types

type Alignment

type Alignment int

Alignment indicates whether names should be aligned in some fashion.

const (
	AlignmentNone Alignment = iota
	AlignmentLeft
	AlignmentRight
)

Name alignments.

type EncOpt

type EncOpt func(*Encoder)

EncOpt is a configuration option for the encoding of values.

type Encoder

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

Encoder encodes Go types as asterisk configuration data.

Example
package main

import (
	"bytes"
	"fmt"
	"strings"

	"github.com/scjalliance/astconf"
)

type Name string

func (n Name) SectionName() string {
	return strings.ToLower(strings.Replace(string(n), " ", "_", -1))
}

type Elephant struct {
	Name Name `astconf:"elephant_name"`
	Age  int  `astconf:"age"`
}

func (elephant *Elephant) SectionName() string {
	return "elephant." + elephant.Name.SectionName()
}

func (elephant *Elephant) MarshalAsteriskPreamble(e *astconf.Encoder) error {
	var kind string
	switch {
	case elephant.Age > 50:
		kind = "old_elephant"
	case elephant.Age < 10:
		kind = "young_elephant"
	default:
		kind = "elephant"
	}
	return e.Printer().Setting("type", kind)
}

type Zookeeper struct {
	Name           Name     `astconf:"zookeeper_name"`
	Experience     int      `astconf:"experience_level"`
	FavoriteColors []string `astconf:"favorite_colors,commaseparated,omitempty"`
}

func (zk Zookeeper) SectionName() string {
	return "zookeeper." + zk.Name.SectionName()
}

func (zk Zookeeper) SectionTemplates() []string {
	return []string{"senior-management"}
}

func (*Zookeeper) MarshalAsteriskPreamble(e *astconf.Encoder) error {
	return e.Printer().Setting("type", "zookeeper")
}

type Zoo struct {
	Name        Name `astconf:"zoo_name"`
	Maintainers []Zookeeper
	//Maintainers []*Zookeeper // FIXME: This use of a slice of references breaks the marshaler and causes the test to fail
	Elephants []Elephant
}

func (zoo *Zoo) SectionName() string {
	return "zoo"
}

func (*Zoo) MarshalAsteriskPreamble(e *astconf.Encoder) error {
	return e.Printer().Setting("type", "zoo")
}

func main() {
	var buf bytes.Buffer
	e := astconf.NewEncoder(&buf, astconf.AlignRight)
	err := e.Encode(&Zoo{
		Name: "Malarky McFee's Mighty Jungle",
		Elephants: []Elephant{
			{Name: "Matilda", Age: 47},
			{Name: "Franklin", Age: 52},
			{Name: "Georgey the Kid", Age: 5},
		},
		Maintainers: []Zookeeper{
			{
				Name:           "Gershwin McFee",
				Experience:     8000,
				FavoriteColors: []string{"blue", "yellow"},
			},
		},
	})
	if err != nil {
		fmt.Println(err)
	} else {
		fmt.Print(buf.String())
	}
}
Output:

[zoo]
    type = zoo
zoo_name = Malarky McFee's Mighty Jungle

[zookeeper.gershwin_mcfee](senior-management)
            type = zookeeper
  zookeeper_name = Gershwin McFee
experience_level = 8000
 favorite_colors = blue,yellow

[elephant.matilda]
         type = elephant
elephant_name = Matilda
          age = 47

[elephant.franklin]
         type = old_elephant
elephant_name = Franklin
          age = 52

[elephant.georgey_the_kid]
         type = young_elephant
elephant_name = Georgey the Kid
          age = 5

func NewEncoder

func NewEncoder(w io.Writer, options ...EncOpt) *Encoder

NewEncoder returns a new Encoder that writes to the given io.Writer.

func (*Encoder) Encode

func (e *Encoder) Encode(v interface{}) error

Encode encodes the given value as an asterisk configuration section.

func (*Encoder) Printer

func (e *Encoder) Printer() *Printer

Printer returns a new Printer that writes to the same io.Writer as e.

type Indenter

type Indenter interface {
	AsteriskIndent() int
}

Indenter is the interface implemented by types that can request a minimum level of indentation when marshaling.

type InvalidContentError

type InvalidContentError struct {
	Component string
	Value     string
	Pos       int
}

InvalidContentError is returned when configuration data contains invalid characters.

func (InvalidContentError) Error

func (err InvalidContentError) Error() string

type InvalidValueError

type InvalidValueError struct {
	Value reflect.Value
}

An InvalidValueError is returned by Marshal when attempting to encode an invalid value.

func (*InvalidValueError) Error

func (e *InvalidValueError) Error() string

type Marshaler

type Marshaler interface {
	MarshalAsterisk(e *Encoder) error
}

Marshaler is the interface implemented by types that can marshal themselves into valid asterisk configuration blocks.

The provided encoder is only valid for the duration of the call. Marshalers must not retain a reference to e.

type MarshalerError

type MarshalerError struct {
	Type reflect.Type
	Err  error
	// contains filtered or unexported fields
}

MarshalerError is returned when a custom marshaling function encounters an error.

func (*MarshalerError) Error

func (e *MarshalerError) Error() string

type ObjectMarshaler

type ObjectMarshaler interface {
	MarshalAsteriskObject(w io.Writer) error
}

ObjectMarshaler is the interface implemented by types that can marshal themselves into valid asterisk configuration objects.

type PreambleMarshaler

type PreambleMarshaler interface {
	MarshalAsteriskPreamble(e *Encoder) error
}

PreambleMarshaler is the interface implemented by types that marshal a an asterisk configuration preamble in addition to their regular value.

The provided encoder is only valid for the duration of the call. Marshalers must not retain a reference to e.

type Printer

type Printer struct {
	io.Writer
	Indent    int
	Alignment Alignment
	Started   bool // If true a newline will be printed before each section
}

Printer is capable of printing asterisk configuration data to an underlying io.Writer.

Printer performs no buffering on its own. It is recommended that the underlying writer be buffered.

To marshal custom types via reflection, use Encoder or Marshal.

func NewPrinter

func NewPrinter(w io.Writer) *Printer

NewPrinter returns a new printer that writes to w.

func (*Printer) Break

func (p *Printer) Break()

Break causes the printer to insert a newline if the printer has started.

func (*Printer) Comment

func (p *Printer) Comment(comment string) error

Comment writes a single line comment to p.Writer.

func (*Printer) Include

func (p *Printer) Include(path string) error

Include will print a file include construct to p.Writer for the given path.

func (*Printer) Object

func (p *Printer) Object(object, value string) error

Object will print an object to p.Writer.

func (*Printer) Section

func (p *Printer) Section(section string, templates ...string) error

Section writes a header starting a new section.

func (*Printer) Setting

func (p *Printer) Setting(setting, value string) error

Setting will write a setting to p.Writer.

An InvalidContentError will be returned if the setting or its value contain an invalid character.

If the underlying write operation fails an error will be returned.

func (*Printer) Start

func (p *Printer) Start(name string, sep string) error

Start begins a new field by writing its name and optional separator.

func (*Printer) Write

func (p *Printer) Write(v []byte) (n int, err error)

Write will write v to the underlying writer.

type SectionName

type SectionName string

SectionName is a simple implementation of SectionNamer. Its value won't be written as a setting.

func (SectionName) MarshalAsterisk

func (s SectionName) MarshalAsterisk(e *Encoder) error

MarshalAsterisk prevents the section name from being marshaled.

func (SectionName) SectionName

func (s SectionName) SectionName() string

SectionName returns s as the name for the section.

type SectionNamer

type SectionNamer interface {
	SectionName() string
}

SectionNamer provides the section name for a type.

type SectionTemplater

type SectionTemplater interface {
	SectionTemplates() []string
}

SectionTemplater provides section templates for a type. It must be provided by the same type that provides the name for a section.

type SettingMarshaler

type SettingMarshaler interface {
	MarshalAsteriskSetting(w io.Writer) error
}

SettingMarshaler is the interface implemented by types that can marshal themselves into valid asterisk configuration settings.

type UnsupportedTypeError

type UnsupportedTypeError struct {
	Type reflect.Type
}

An UnsupportedTypeError is returned by Marshal when attempting to encode an unsupported value type.

func (*UnsupportedTypeError) Error

func (e *UnsupportedTypeError) Error() string

Directories

Path Synopsis
Package astgen is a highly experimental package that converts astorg datasets into DPMA-based phone configuration files.
Package astgen is a highly experimental package that converts astorg datasets into DPMA-based phone configuration files.
Package astmerge provides helper functions for merging various kinds of asterisk data.
Package astmerge provides helper functions for merging various kinds of asterisk data.
Package astorg provides a set of simplified domain entities for an organization that needs to provide configuration to one or more asterisk phone servers.
Package astorg provides a set of simplified domain entities for an organization that needs to provide configuration to one or more asterisk phone servers.
astorgvm
Package astorgvm defines voicemail configuration settings for an astorg.
Package astorgvm defines voicemail configuration settings for an astorg.
Package astoverlay provides helper functions for overlaying various kinds of asterisk data.
Package astoverlay provides helper functions for overlaying various kinds of asterisk data.
Package astval provides a set of value types used to serialize configuration data for asterisk phone servers.
Package astval provides a set of value types used to serialize configuration data for asterisk phone servers.
Package dialplan facilitates the programmatic creation of asterisk dialplans.
Package dialplan facilitates the programmatic creation of asterisk dialplans.
Package digium facilitates serialization of configuration data for Digium phones.
Package digium facilitates serialization of configuration data for Digium phones.
contactbuilder
Package contactbuilder converts astorg types into digium contacts.
Package contactbuilder converts astorg types into digium contacts.
fieldbuilder
Package fieldbuilder converts astorg types into digium busy lamp fields.
Package fieldbuilder converts astorg types into digium busy lamp fields.

Jump to

Keyboard shortcuts

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