diff --git a/ios/nskeyedarchiver/archiverutils.go b/ios/nskeyedarchiver/archiverutils.go index 1efefe93..d2916615 100644 --- a/ios/nskeyedarchiver/archiverutils.go +++ b/ios/nskeyedarchiver/archiverutils.go @@ -4,6 +4,7 @@ import ( "bytes" "encoding/json" "fmt" + "io" plist "howett.net/plist" ) @@ -26,16 +27,13 @@ func toUidList(list []interface{}) []plist.UID { return result } -// plistFromBytes decodes a binary or XML based PLIST using the amazing github.com/DHowett/go-plist library and returns an interface{} or propagates the error raised by the library. -func plistFromBytes(plistBytes []byte) (interface{}, error) { - var test interface{} - decoder := plist.NewDecoder(bytes.NewReader(plistBytes)) +// plistFromReader decodes a binary or XML based PLIST using the amazing github.com/DHowett/go-plist library and returns an interface{} or propagates the error raised by the library. +func plistFromReader(r io.ReadSeeker) (interface{}, error) { + var v interface{} + decoder := plist.NewDecoder(r) - err := decoder.Decode(&test) - if err != nil { - return test, err - } - return test, nil + err := decoder.Decode(&v) + return v, err } // ToPlist converts a given struct to a Plist using the diff --git a/ios/nskeyedarchiver/unarchiver.go b/ios/nskeyedarchiver/unarchiver.go index 4647f0fe..0deca211 100644 --- a/ios/nskeyedarchiver/unarchiver.go +++ b/ios/nskeyedarchiver/unarchiver.go @@ -1,7 +1,9 @@ package nskeyedarchiver import ( + "bytes" "fmt" + "io" "runtime/debug" log "github.com/sirupsen/logrus" @@ -13,6 +15,14 @@ import ( // NSArray, NSMutableArray, NSSet and NSMutableSet will transformed into []interface{} // NSDictionary and NSMutableDictionary will be transformed into map[string] interface{}. I might add non string keys later. func Unarchive(xml []byte) (result []interface{}, err error) { + return UnarchiveReader(bytes.NewReader(xml)) +} + +// UnarchiveReader extracts NSKeyedArchiver Plists from an io.ReadSeeker, either in XML or Binary format, and returns an array of the archived objects converted to usable Go Types. +// Primitives will be extracted just like regular Plist primitives (string, float64, int64, []uint8 etc.). +// NSArray, NSMutableArray, NSSet and NSMutableSet will be transformed into []interface{}. +// NSDictionary and NSMutableDictionary will be transformed into map[string]interface{}. Non-string keys might be added later. +func UnarchiveReader(r io.ReadSeeker) (result []interface{}, err error) { defer func() { if r := recover(); r != nil { stacktrace := string(debug.Stack()) @@ -21,7 +31,7 @@ func Unarchive(xml []byte) (result []interface{}, err error) { }() SetupDecoders() - plist, err := plistFromBytes(xml) + plist, err := plistFromReader(r) if err != nil { return nil, err }