Programming

Google Cloud Function to convert YAML to a plist

TLDR: The GCF at https://github.com/krypted/tinyconverters/blob/main/GCF_YAML_to_PLIST.py will convert basic YAML to PLISTs.

Here’s a Google Cloud Function that converts YAML to a property list. The function takes the YAML data as an event object, uses the yaml and plistlib modules to convert the YAML data to a property list, and returns the property list.

To deploy this function, use the “Create Function” button in the Google Cloud Console. When prompted, select the Python 3.7 runtime and paste in the following script:

import yaml
import plistlib

def main(event, context):
  # Get the YAML data from the event
  yaml_data = event['data']

  # Convert the YAML data to a property list
  plist_data = plistlib.dumps(yaml.safe_load(yaml_data))

  # Return the property list data
  return plist_data

Once the function is deployed, call it with a POST request to the function’s URL and include the YAML data. For example:

curl -X POST -H "Content-Type: application/yaml" -d '
name: Chuck U Farley
age: 17
' https://us-central1-my-project.cloudfunctions.net/yaml-to-property-list

Given the above input, the response body will contain the following property list 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>name</key>
<string>John Doe</string>
<key>age</key>
<integer>30</integer>
</dict>
</plist>

For more information, see the following:

  1. cheeky4n6monkey.blogspot.com/2014/07/squirrelling-away-plists.html 
  2. github.com/wollardj/simple-plist