Skip to content

Instantly share code, notes, and snippets.

@jessepeterson
Created November 2, 2016 18:07
Show Gist options
  • Save jessepeterson/3467c5cf761a58848a7278586c667367 to your computer and use it in GitHub Desktop.
Save jessepeterson/3467c5cf761a58848a7278586c667367 to your computer and use it in GitHub Desktop.
convert Apple plist to set of golang structs
import sys
import plistlib
pl = plistlib.readPlist(sys.argv[1])
structs = []
def struct_from_dict(name, vals):
global structs
ststr = "type %s struct {\n" % name
for key in vals.keys():
camel_case_name = ''.join([x.capitalize() for x in key.split('_')])
newname = name + camel_case_name
if isinstance(vals[key], dict):
ststr += " %s %s `plist:\"%s\"`\n" % (camel_case_name, newname, key)
struct_from_dict(newname, vals[key])
# print vals[key]
elif isinstance(vals[key], str):
ststr += " %s string `plist:\"%s\"`\n" % (camel_case_name, key)
elif isinstance(vals[key], list):
if not len(vals[key]):
# can't really determine too much info here
ststr += " %s []interface{} `plist:\"%s\"`\n" % (camel_case_name, key)
continue
fe = vals[key][0]
if isinstance(fe, dict):
ststr += " %s []%s `plist:\"%s\"`\n" % (camel_case_name, newname, key)
struct_from_dict(newname, fe)
if isinstance(fe, str):
ststr += " %s []string `plist:\"%s\"`\n" % (camel_case_name, key)
ststr += "}\n"
structs.append(ststr)
struct_from_dict('plistRoot', pl)
for s in structs:
print s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment