Debian Codegraph @

encode.go

raw symbols SCIP
1 package plist
2
3 import (
4 "bytes"
5 "errors"
6 "io"
7 "reflect"
8 "runtime"
9 )
10
11 type generator interface {
12 generateDocument(cfValue)
13 Indent(string)
14 }
15
16 // An Encoder writes a property list to an output stream.
17 type Encoder struct {
18 writer io.Writer
19 format int
20
21 indent string
22 }
23
24 // Encode writes the property list encoding of v to the stream.
25 func (p *Encoder) Encode(v interface{}) (err error) {
26 defer func() {
27 if r := recover(); r != nil {
28 if _, ok := r.(runtime.Error); ok {
29 panic(r)
30 }
31 err = r.(error)
32 }
33 }()
34
35 pval := p.marshal(reflect.ValueOf(v))
36 if pval == nil {
37 panic(errors.New("plist: no root element to encode"))
38 }
39
40 var g generator
41 switch p.format {
42 case XMLFormat:
43 g = newXMLPlistGenerator(p.writer)
44 case BinaryFormat, AutomaticFormat:
45 g = newBplistGenerator(p.writer)
46 case OpenStepFormat, GNUStepFormat:
47 g = newTextPlistGenerator(p.writer, p.format)
48 }
49 g.Indent(p.indent)
50 g.generateDocument(pval)
51 return
52 }
53
54 // Indent turns on pretty-printing for the XML and Text property list formats.
55 // Each element begins on a new line and is preceded by one or more copies of indent according to its nesting depth.
56 func (p *Encoder) Indent(indent string) {
57 p.indent = indent
58 }
59
60 // NewEncoder returns an Encoder that writes an XML property list to w.
61 func NewEncoder(w io.Writer) *Encoder {
62 return NewEncoderForFormat(w, XMLFormat)
63 }
64
65 // NewEncoderForFormat returns an Encoder that writes a property list to w in the specified format.
66 // Pass AutomaticFormat to allow the library to choose the best encoding (currently BinaryFormat).
67 func NewEncoderForFormat(w io.Writer, format int) *Encoder {
68 return &Encoder{
69 writer: w,
70 format: format,
71 }
72 }
73
74 // NewBinaryEncoder returns an Encoder that writes a binary property list to w.
75 func NewBinaryEncoder(w io.Writer) *Encoder {
76 return NewEncoderForFormat(w, BinaryFormat)
77 }
78
79 // Marshal returns the property list encoding of v in the specified format.
80 //
81 // Pass AutomaticFormat to allow the library to choose the best encoding (currently BinaryFormat).
82 //
83 // Marshal traverses the value v recursively.
84 // Any nil values encountered, other than the root, will be silently discarded as
85 // the property list format bears no representation for nil values.
86 //
87 // Strings, integers of varying size, floats and booleans are encoded unchanged.
88 // Strings bearing non-ASCII runes will be encoded differently depending upon the property list format:
89 // UTF-8 for XML property lists and UTF-16 for binary property lists.
90 //
91 // Slice and Array values are encoded as property list arrays, except for
92 // []byte values, which are encoded as data.
93 //
94 // Map values encode as dictionaries. The map's key type must be string; there is no provision for encoding non-string dictionary keys.
95 //
96 // Struct values are encoded as dictionaries, with only exported fields being serialized. Struct field encoding may be influenced with the use of tags.
97 // The tag format is:
98 //
99 // `plist:"<key>[,flags...]"`
100 //
101 // The following flags are supported:
102 //
103 // omitempty Only include the field if it is not set to the zero value for its type.
104 //
105 // If the key is "-", the field is ignored.
106 //
107 // Anonymous struct fields are encoded as if their exported fields were exposed via the outer struct.
108 //
109 // Pointer values encode as the value pointed to.
110 //
111 // Channel, complex and function values cannot be encoded. Any attempt to do so causes Marshal to return an error.
112 func Marshal(v interface{}, format int) ([]byte, error) {
113 return MarshalIndent(v, format, "")
114 }
115
116 // MarshalIndent works like Marshal, but each property list element
117 // begins on a new line and is preceded by one or more copies of indent according to its nesting depth.
118 func MarshalIndent(v interface{}, format int, indent string) ([]byte, error) {
119 buf := &bytes.Buffer{}
120 enc := NewEncoderForFormat(buf, format)
121 enc.Indent(indent)
122 if err := enc.Encode(v); err != nil {
123 return nil, err
124 }
125 return buf.Bytes(), nil
126 }