-
Notifications
You must be signed in to change notification settings - Fork 9
/
plist.go
48 lines (41 loc) · 1.8 KB
/
plist.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package utilities
import (
"bytes"
"os"
"howett.net/plist"
)
// plistCustomType represents a custom data structure for plist encoding and decoding.
type plistCustomType struct {
Name string
Value int
}
// EncodeMapToPlist encodes a map[string]interface{} to a plist format and writes it to standard output.
// The `format` parameter determines the plist format (e.g., XML, Binary).
// Returns an error if encoding fails.
func EncodeMapToPlist(data map[string]interface{}, format int) error {
encoder := plist.NewEncoderForFormat(os.Stdout, format)
return encoder.Encode(data)
}
// DecodePlistToMap decodes plist data from a byte slice into a map[string]interface{}.
// Returns the decoded map, the format of the plist, and an error if decoding fails.
func DecodePlistToMap(data []byte) (map[string]interface{}, int, error) {
var result map[string]interface{}
decoder := plist.NewDecoder(bytes.NewReader(data))
err := decoder.Decode(&result)
return result, decoder.Format, err
}
// EncodeCustomTypeToPlist encodes an instance of plistCustomType to a plist format and writes it to standard output.
// The `format` parameter determines the plist format (e.g., XML, Binary).
// Returns an error if encoding fails.
func EncodeCustomTypeToPlist(customType plistCustomType, format int) error {
encoder := plist.NewEncoderForFormat(os.Stdout, format)
return encoder.Encode(customType)
}
// DecodePlistToCustomType decodes plist data from a byte slice into a provided plistCustomType pointer.
// Updates the value pointed by `customType` with the decoded data.
// Returns the format of the plist and an error if decoding fails.
func DecodePlistToCustomType(data []byte, customType *plistCustomType) (int, error) {
decoder := plist.NewDecoder(bytes.NewReader(data))
err := decoder.Decode(customType)
return decoder.Format, err
}