plist

package module
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Dec 18, 2025 License: BSD-2-Clause-Views, BSD-3-Clause Imports: 16 Imported by: 50

README

Go Plist library

CI/CD Go Reference

This Plist library is used for decoding and encoding Apple Property Lists in both XML and binary forms.

Example using HTTP streams:

func someHTTPHandler(w http.ResponseWriter, r *http.Request) {
	var sparseBundleHeader struct {
		InfoDictionaryVersion *string `plist:"CFBundleInfoDictionaryVersion"`
		BandSize              *uint64 `plist:"band-size"`
		BackingStoreVersion   int     `plist:"bundle-backingstore-version"`
		DiskImageBundleType   string  `plist:"diskimage-bundle-type"`
		Size                  uint64  `plist:"unknownKey"`
	}

    // decode an HTTP request body into the sparseBundleHeader struct
	if err := plist.NewXMLDecoder(r.Body).Decode(&sparseBundleHeader); err != nil {
		log.Println(err)
        return
	}
}

Credit

This library is based on the DHowett go-plist library but has an API that is more like the XML and JSON package in the Go standard library. I.e. the plist.Decoder() accepts an io.Reader instead of an io.ReadSeeker

Documentation

Index

Examples

Constants

View Source
const (
	Invalid plistKind = iota
	Dictionary
	Array
	String
	Integer
	Real
	Boolean
	Data
	Date
)

Variables

This section is empty.

Functions

func Marshal

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

Marshal ...

func MarshalIndent

func MarshalIndent(v interface{}, indent string) ([]byte, error)

MarshalIndent ...

func Unmarshal

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

Unmarshal parses the plist-encoded data and stores the result in the value pointed to by v.

Types

type Decoder

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

A Decoder reads and decodes Apple plist objects from an input stream. The plists can be in XML or binary format.

func NewBinaryDecoder

func NewBinaryDecoder(r io.ReadSeeker) *Decoder

NewBinaryDecoder returns a new decoder that reads a binary plist from r. No error checking is done to make sure that r is actually a binary plist.

func NewDecoder

func NewDecoder(r io.Reader) *Decoder

NewDecoder returns a new XML plist decoder. DEPRECATED: Please use NewXMLDecoder instead.

func NewXMLDecoder

func NewXMLDecoder(r io.Reader) *Decoder

NewXMLDecoder returns a new decoder that reads an XML plist from r.

func (*Decoder) Decode

func (d *Decoder) Decode(v interface{}) error

Decode reads the next plist-encoded value from its input and stores it in the value pointed to by v. Decode uses xml.Decoder to do the heavy lifting for XML plists, and uses binaryParser for binary plists.

type Encoder

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

Encoder ...

func NewEncoder

func NewEncoder(w io.Writer) *Encoder

NewEncoder returns a new encoder that writes to w.

func (*Encoder) Encode

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

Encode ...

func (*Encoder) Indent

func (e *Encoder) Indent(indent string)

Indent ...

type MarshalFunc

type MarshalFunc func(interface{}) error

MarshalFunc is a function used to Unmarshal custom plist types.

type Marshaler

type Marshaler interface {
	MarshalPlist() (interface{}, error)
}

type UnmarshalTypeError

type UnmarshalTypeError struct {
	Value string // description of plist value - "true", "string", "date"
	Type  reflect.Type
}

An UnmarshalTypeError describes a plist value that was not appropriate for a value of a specific Go type.

func (UnmarshalTypeError) Error

func (e UnmarshalTypeError) Error() string

type Unmarshaler

type Unmarshaler interface {
	UnmarshalPlist(f func(interface{}) error) error
}

Unmarshaler is the interface implemented by types that can unmarshal themselves from property list objects. The UnmarshalPlist method receives a function that may be called to unmarshal the original property list value into a field or variable.

It is safe to call the unmarshal function more than once.

Example

ExampleUnmarshaler demonstrates using structs that use the Unmarshaler interface.

package main

import (
	"fmt"

	"github.com/micromdm/plist"
)

const data = `<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>typekey</key>
	<string>A</string>
	<key>typeAkey</key>
	<string>VALUE-A</string>
</dict>
</plist>`

type TypeDecider struct {
	ActualType interface{} `plist:"-"`
}

type TypeA struct {
	TypeAKey string `plist:"typeAkey"`
}

type TypeB struct {
	TypeBKey string `plist:"typeBkey"`
}

func (t *TypeDecider) UnmarshalPlist(f func(interface{}) error) error {
	// stub struct for decoding a single key to tell which
	// specific type we should umarshal into
	typeKey := &struct {
		TypeKey string `plist:"typekey"`
	}{}
	if err := f(typeKey); err != nil {
		return err
	}

	// switch using the decoded value to determine the correct type
	switch typeKey.TypeKey {
	case "A":
		t.ActualType = new(TypeA)
	case "B":
		t.ActualType = new(TypeB)
	case "":
		return fmt.Errorf("empty typekey (or wrong input data)")
	default:
		return fmt.Errorf("unknown typekey: %s", typeKey.TypeKey)
	}

	// decode into the actual type
	return f(t.ActualType)
}

// ExampleUnmarshaler demonstrates using structs that use the Unmarshaler interface.
func main() {
	decider := new(TypeDecider)
	if err := plist.Unmarshal([]byte(data), decider); err != nil {
		fmt.Println(err)
		return
	}

	typeA, ok := decider.ActualType.(*TypeA)
	if !ok {
		fmt.Println("actual type is not TypeA")
		return
	}

	fmt.Println(typeA.TypeAKey)
}
Output:

VALUE-A

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

type UnsupportedValueError

type UnsupportedValueError struct {
	Value reflect.Value
	Str   string
}

UnsupportedValueError ...

func (*UnsupportedValueError) Error

func (e *UnsupportedValueError) Error() string

Jump to

Keyboard shortcuts

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