Documentation ¶
Overview ¶
Package astconf writes asterisk configuration files.
Index ¶
- func AlignLeft(e *Encoder)
- func AlignRight(e *Encoder)
- func Marshal(v interface{}) ([]byte, error)
- func MarshalTo(v interface{}, w io.Writer) error
- func Unaligned(e *Encoder)
- type Alignment
- type EncOpt
- type Encoder
- type Indenter
- type InvalidContentError
- type InvalidValueError
- type Marshaler
- type MarshalerError
- type ObjectMarshaler
- type PreambleMarshaler
- type Printer
- func (p *Printer) Break()
- func (p *Printer) Comment(comment string) error
- func (p *Printer) Include(path string) error
- func (p *Printer) Object(object, value string) error
- func (p *Printer) Section(section string, templates ...string) error
- func (p *Printer) Setting(setting, value string) error
- func (p *Printer) Start(name string, sep string) error
- func (p *Printer) Write(v []byte) (n int, err error)
- type SectionName
- type SectionNamer
- type SectionTemplater
- type SettingMarshaler
- type UnsupportedTypeError
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 ¶
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.
Types ¶
type Alignment ¶
type Alignment int
Alignment indicates whether names should be aligned in some fashion.
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 ¶
NewEncoder returns a new Encoder that writes to the given io.Writer.
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 ¶
InvalidContentError is returned when configuration data contains invalid characters.
func (InvalidContentError) Error ¶
func (err InvalidContentError) Error() string
type InvalidValueError ¶
An InvalidValueError is returned by Marshal when attempting to encode an invalid value.
func (*InvalidValueError) Error ¶
func (e *InvalidValueError) Error() string
type Marshaler ¶
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 ¶
ObjectMarshaler is the interface implemented by types that can marshal themselves into valid asterisk configuration objects.
type PreambleMarshaler ¶
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 ¶
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) Include ¶
Include will print a file include construct to p.Writer for the given path.
func (*Printer) Setting ¶
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.
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 ¶
SettingMarshaler is the interface implemented by types that can marshal themselves into valid asterisk configuration settings.
type UnsupportedTypeError ¶
An UnsupportedTypeError is returned by Marshal when attempting to encode an unsupported value type.
func (*UnsupportedTypeError) Error ¶
func (e *UnsupportedTypeError) Error() string
Source Files ¶
- addr.go
- alignment.go
- block.go
- bool.go
- condaddr.go
- doc.go
- emptiness.go
- encfunc.go
- encoder.go
- encopt.go
- features.go
- field.go
- fieldenc.go
- fieldwriter.go
- implements.go
- indent.go
- indenter.go
- indirect.go
- int.go
- invalid.go
- marshal.go
- marshaler.go
- marshalerenc.go
- object.go
- omitempty.go
- printer.go
- section.go
- sectionenc.go
- setting.go
- slice.go
- startupwriter.go
- string.go
- struct.go
- tag.go
- type.go
- unsupported.go
- value.go
- writegroup.go
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. |